2020-01-23 07:16:09 +03:00
|
|
|
{-# LANGUAGE DisambiguateRecordFields #-}
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2019-09-26 23:29:19 +03:00
|
|
|
|
2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
|
|
|
Hoon's `map` and `set` types and conversions to/from Nouns.
|
|
|
|
-}
|
2020-01-24 08:28:38 +03:00
|
|
|
module Urbit.Noun.Tree
|
2019-09-26 23:29:19 +03:00
|
|
|
( HoonSet, setToHoonSet, setFromHoonSet
|
|
|
|
, HoonMap, mapToHoonMap, mapFromHoonMap
|
|
|
|
) where
|
|
|
|
|
|
|
|
import ClassyPrelude
|
|
|
|
import Control.Lens hiding (non)
|
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Noun.Conversions ()
|
|
|
|
import Urbit.Noun.Convert
|
|
|
|
import Urbit.Noun.Core
|
|
|
|
import Urbit.Noun.TH
|
2019-09-26 23:29:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Types -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
data NounVal a = NounVal
|
2020-10-05 20:26:46 +03:00
|
|
|
{ non :: !Noun
|
2020-09-25 02:43:03 +03:00
|
|
|
, val :: !a
|
2019-09-26 23:29:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data HoonTreeNode a = NTN
|
2020-10-05 20:26:46 +03:00
|
|
|
{ n :: !(NounVal a)
|
|
|
|
, l :: !(HoonTree a)
|
|
|
|
, r :: !(HoonTree a)
|
2019-09-26 23:29:19 +03:00
|
|
|
}
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
data HoonTree a = E | Node (HoonTreeNode a)
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
2020-11-03 22:03:58 +03:00
|
|
|
pattern N :: NounVal a -> HoonTree a -> HoonTree a -> HoonTree a
|
2019-09-26 23:29:19 +03:00
|
|
|
pattern N n l r = Node (NTN n l r)
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
newtype HoonSet a = HoonSet { unHoonSet :: HoonTree a }
|
2019-09-26 23:29:19 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, FromNoun, ToNoun)
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
newtype HoonMap k v = HoonMap { unHoonMap :: HoonTree (k, v) }
|
2019-09-26 23:29:19 +03:00
|
|
|
deriving newtype (Eq, Ord, Show, FromNoun, ToNoun)
|
|
|
|
|
|
|
|
|
|
|
|
-- Instances -------------------------------------------------------------------
|
|
|
|
|
|
|
|
instance Eq (NounVal a) where
|
|
|
|
(==) = on (==) non
|
|
|
|
|
|
|
|
instance Ord (NounVal a) where
|
|
|
|
compare = comparing non
|
|
|
|
|
|
|
|
instance ToNoun (NounVal a) where
|
|
|
|
toNoun = non
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
instance Show a => Show (NounVal a) where
|
2019-09-26 23:29:19 +03:00
|
|
|
show = show . val
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
instance FromNoun a => FromNoun (NounVal a) where
|
2019-09-26 23:29:19 +03:00
|
|
|
parseNoun x = NounVal x <$> parseNoun x
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
instance ToNoun a => ToNoun (HoonTree a) where
|
2019-09-26 23:29:19 +03:00
|
|
|
toNoun E = A 0
|
|
|
|
toNoun (Node n) = toNoun n
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
instance FromNoun a => FromNoun (HoonTree a) where
|
2019-09-26 23:29:19 +03:00
|
|
|
parseNoun (A 0) = pure E
|
|
|
|
parseNoun n = Node <$> parseNoun n
|
|
|
|
|
|
|
|
deriveNoun ''HoonTreeNode
|
|
|
|
|
|
|
|
|
|
|
|
-- Order -----------------------------------------------------------------------
|
|
|
|
|
|
|
|
{-
|
|
|
|
Orders in ascending double mug hash order, collisions fall back to dor.
|
|
|
|
-}
|
2020-09-25 02:43:03 +03:00
|
|
|
mor :: Noun -> Noun -> Bool
|
2019-09-26 23:29:19 +03:00
|
|
|
mor a b = if c == d then dor a b else c < d
|
|
|
|
where
|
2020-11-12 03:28:04 +03:00
|
|
|
c = mug $ A $ fromIntegral $ mug a
|
|
|
|
d = mug $ A $ fromIntegral $ mug b
|
2019-09-26 23:29:19 +03:00
|
|
|
|
|
|
|
{-
|
|
|
|
Orders in ascending tree depth.
|
|
|
|
-}
|
2020-09-25 02:43:03 +03:00
|
|
|
dor :: Noun -> Noun -> Bool
|
2019-09-26 23:29:19 +03:00
|
|
|
dor a b | a == b = True
|
|
|
|
dor (A a) (C _ _) = True
|
|
|
|
dor (C x y) (A b) = False
|
|
|
|
dor (A a) (A b) = a < b
|
|
|
|
dor (C x y) (C p q) | x == p = dor y q
|
|
|
|
dor (C x y) (C p q) = dor x p
|
|
|
|
|
|
|
|
{-
|
|
|
|
Orders in ascending +mug hash order.
|
|
|
|
|
|
|
|
Collisions fall back to dor.
|
|
|
|
-}
|
2020-09-25 02:43:03 +03:00
|
|
|
gor :: Noun -> Noun -> Bool
|
2019-09-26 23:29:19 +03:00
|
|
|
gor a b = if c==d then dor a b else c<d
|
|
|
|
where (c, d) = (mug a, mug b)
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
morVal, gorVal :: NounVal a -> NounVal a -> Bool
|
2019-09-26 23:29:19 +03:00
|
|
|
morVal = on mor non
|
|
|
|
gorVal = on gor non
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
nounVal :: ToNoun a => Iso' a (NounVal a)
|
2019-09-26 23:29:19 +03:00
|
|
|
nounVal = iso to val
|
|
|
|
where
|
|
|
|
to x = NounVal (toNoun x) x
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
treeToList :: forall a. HoonTree a -> [a]
|
2019-09-26 23:29:19 +03:00
|
|
|
treeToList = go []
|
|
|
|
where
|
2020-09-25 02:43:03 +03:00
|
|
|
go :: [a] -> HoonTree a -> [a]
|
2019-09-26 23:29:19 +03:00
|
|
|
go acc = \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> acc
|
|
|
|
Node (NTN v l r) -> go (go (val v : acc) l) r
|
2019-09-26 23:29:19 +03:00
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
setFromHoonSet :: Ord a => HoonSet a -> Set a
|
2019-09-26 23:29:19 +03:00
|
|
|
setFromHoonSet = setFromList . treeToList . unHoonSet
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
mapFromHoonMap :: Ord k => HoonMap k v -> Map k v
|
2019-09-26 23:29:19 +03:00
|
|
|
mapFromHoonMap = mapFromList . treeToList . unHoonMap
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
setToHoonSet :: forall a. (Ord a, ToNoun a) => Set a -> HoonSet a
|
2019-09-26 23:29:19 +03:00
|
|
|
setToHoonSet = HoonSet . foldr put E . fmap (view nounVal) . setToList
|
|
|
|
where
|
|
|
|
put x = \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> N x E E
|
|
|
|
Node a | x == n a -> Node a
|
|
|
|
Node a | gorVal x (n a) -> lef x a
|
|
|
|
Node a -> rit x a
|
2019-09-26 23:29:19 +03:00
|
|
|
|
2019-09-27 00:28:11 +03:00
|
|
|
rit x a = put x (r a) & \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> error "bad-put-set"
|
|
|
|
Node c | morVal (n a) (n c) -> N (n a) (l a) (Node c)
|
|
|
|
Node c -> N (n c) (N (n a) (l a) (l c)) (r c)
|
2019-09-26 23:29:19 +03:00
|
|
|
|
2019-09-27 00:28:11 +03:00
|
|
|
lef x a = put x (l a) & \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> error "bad-put-set"
|
|
|
|
Node c | morVal (n a) (n c) -> N (n a) (Node c) (r a)
|
|
|
|
Node c -> N (n c) (l c) (N (n a) (r c) (r a))
|
2019-09-26 23:29:19 +03:00
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
p :: (ToNoun a, ToNoun b) => NounVal (a,b) -> NounVal a
|
2019-09-27 00:28:11 +03:00
|
|
|
p = view (from nounVal . to fst . nounVal)
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
pq :: (ToNoun a, ToNoun b) => NounVal (a,b) -> (NounVal a, NounVal b)
|
2019-09-27 00:28:11 +03:00
|
|
|
pq = boof . view (from nounVal)
|
|
|
|
where
|
|
|
|
boof (x, y) = (x ^. nounVal, y ^. nounVal)
|
|
|
|
|
2020-09-25 02:43:03 +03:00
|
|
|
mapToHoonMap :: forall k v. (ToNoun k, ToNoun v, Ord k, Ord v) => Map k v -> HoonMap k v
|
2019-09-27 00:28:11 +03:00
|
|
|
mapToHoonMap = HoonMap . foldr put E . fmap (view nounVal) . mapToList
|
2019-09-26 23:29:19 +03:00
|
|
|
where
|
2020-09-25 02:43:03 +03:00
|
|
|
put :: NounVal (k, v) -> HoonTree (k, v) -> HoonTree (k, v)
|
2019-09-27 00:28:11 +03:00
|
|
|
put kv@(pq -> (b, c)) = \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> N kv E E
|
|
|
|
Node a | kv == n a -> Node a
|
|
|
|
Node a | b == p (n a) -> N kv (l a) (r a)
|
|
|
|
Node a | gorVal b (p $ n a) -> lef kv a
|
|
|
|
Node a -> rit kv a
|
2019-09-27 00:28:11 +03:00
|
|
|
|
|
|
|
lef kv@(pq -> (b, c)) a = put kv (l a) & \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> error "bad-put-map"
|
|
|
|
Node d | morVal (p $ n a) (p $ n d) -> N (n a) (Node d) (r a)
|
|
|
|
Node d -> N (n d) (l d) (N (n a) (r d) (r a))
|
2019-09-27 00:28:11 +03:00
|
|
|
|
|
|
|
rit kv@(pq -> (b, c)) a = put kv (r a) & \case
|
2020-09-25 02:43:03 +03:00
|
|
|
E -> error "bad-put-map"
|
|
|
|
Node d | morVal (p $ n a) (p $ n d) -> N (n a) (l a) (Node d)
|
|
|
|
Node d -> N (n d) (N (n a) (l a) (l d)) (r d)
|