urbit/pkg/hs/proto/lib/Nock.hs

115 lines
3.7 KiB
Haskell
Raw Normal View History

2019-09-11 01:57:00 +03:00
module Nock where
import ClassyPrelude
2019-09-23 06:19:17 +03:00
import Dashboard
import SimpleNoun
2019-09-11 01:57:00 +03:00
data Nock
= NC Nock Nock -- ^ ^: autocons
| N0 Axis -- ^ 0, axis: tree addressing
| N1 Noun -- ^ 1, const
| N2 Nock Nock -- ^ 2, compose: compute subject, formula; apply
| N3 Nock -- ^ 3, is cell
| N4 Nock -- ^ 4, succ
| N5 Nock Nock -- ^ 5, eq
| N6 Nock Nock Nock -- ^ 6, if
| N7 Nock Nock -- ^ 7, then: =>
| N8 Nock Nock -- ^ 8, push: =+
| N9 Axis Nock -- ^ 9, invoke
| N10 (Axis, Nock) Nock -- ^ 10, edit
| N11 Hint Nock -- ^ 11, hint
| N12 Nock Nock -- ^ 12, scry
2019-09-23 06:19:17 +03:00
deriving (Eq, Ord, Read, Generic)
2019-09-11 01:57:00 +03:00
2019-10-02 01:04:11 +03:00
data Hint
= Tag Atom
| Assoc Atom Nock
2019-09-23 06:19:17 +03:00
deriving (Eq, Ord, Read, Show, Generic)
instance Hashable Nock
instance Hashable Hint
2019-09-11 01:57:00 +03:00
2019-09-13 06:42:07 +03:00
instance Show Nock where
show = show . nockToNoun
2019-09-11 01:57:00 +03:00
nockToNoun :: Nock -> Noun
nockToNoun = go
where
go = \case
2019-09-29 08:44:29 +03:00
NC f g -> C (go f) (go g)
N0 a -> C (A 0) (A a)
N1 n -> C (A 1) n
N2 f g -> C (A 2) (C (go f) (go g))
N3 f -> C (A 3) (go f)
N4 f -> C (A 4) (go f)
N5 f g -> C (A 5) (C (go f) (go g))
N6 f g h -> C (A 6) (C (go f) (C (go g) (go h)))
N7 f g -> C (A 7) (C (go f) (go g))
N8 f g -> C (A 8) (C (go f) (go g))
N9 a f -> C (A 9) (C (A a) (go f))
N10 (a, f) g -> C (A 10) (C (C (A a) (go f)) (go g))
N11 (Tag a) f -> C (A 11) (C (A a) (go f))
2019-09-13 06:42:07 +03:00
N11 (Assoc a f) g -> C (A 11) (C (C (A a) (go f)) (go g))
2019-09-29 08:44:29 +03:00
N12 f g -> C (A 12) (C (go f) (go g))
2019-09-11 01:57:00 +03:00
nounToNock :: Noun -> Nock
nounToNock = go
where
go = \case
2019-09-13 06:42:07 +03:00
A{} -> error "nounToNock: atom"
C n@C{} m -> NC (go n) (go m)
C (A op) n -> case op of
0 | (A a) <- n -> N0 a
1 -> N1 n
2 | (C m o) <- n -> N2 (go m) (go o)
3 -> N3 (go n)
4 -> N4 (go n)
5 | (C m o) <- n -> N5 (go m) (go o)
6 | (C m (C o p)) <- n -> N6 (go m) (go o) (go p)
7 | (C m o) <- n -> N7 (go m) (go o)
8 | (C m o) <- n -> N8 (go m) (go o)
9 | (C (A a) m) <- n -> N9 a (go m)
10 | (C (C (A a) m) o) <- n -> N10 (a, (go m)) (go o)
11 | (C (C (A a) m) o) <- n -> N11 (Assoc a (go m)) (go o)
| (C (A a) m) <- n -> N11 (Tag a) (go m)
12 | (C m o) <- n -> N12 (go m) (go o)
2019-09-11 01:57:00 +03:00
_ -> error ("nockToNoun: invalid " <> show op <> " " <> show n)
-- | Nock interpreter
2019-09-23 06:19:17 +03:00
nock :: (Dashboard d) => Noun -> Nock -> d Noun
2019-09-11 01:57:00 +03:00
nock n = \case
2019-09-23 06:19:17 +03:00
NC f g -> C <$> nock n f <*> nock n g
N0 a -> pure $ axis a n
N1 n' -> pure n'
N2 sf ff -> do
s <- nock n sf
f <- nock n ff
2019-09-24 02:10:24 +03:00
match f >>= \case
Just jet -> pure (jet s)
Nothing -> nock s (nounToNock f)
2019-09-23 06:19:17 +03:00
N3 f -> nock n f <&> \case
2019-09-13 06:42:07 +03:00
C{} -> yes
A{} -> no
2019-09-23 06:19:17 +03:00
N4 f -> nock n f <&> \case
2019-09-13 06:42:07 +03:00
C{} -> error "nock: cannot increment cell"
A a -> A (a + 1)
2019-09-23 06:19:17 +03:00
N5 f g -> loob <$> ((==) <$> nock n f <*> nock n g)
N6 f g h -> nock n f >>= \case
2019-09-13 06:42:07 +03:00
(A 0) -> nock n g
(A 1) -> nock n h
2019-09-11 01:57:00 +03:00
_ -> error "nock: invalid test value"
2019-09-23 06:19:17 +03:00
N7 f g -> do
n' <- nock n f
nock n' g
N8 f g -> do
n' <- nock n f
nock (C n' n) g
N9 a f -> do
c <- nock n f
nock c (nounToNock (axis a c))
N10 (a, f) g -> edit a <$> nock n f <*> nock n g
2019-09-11 01:57:00 +03:00
N11 _ f -> nock n f
N12{} -> error "nock: scrying is not allowed"