@@ -0,0 +1,114 @@
1+{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
3+module Nock (tar, cell, atom, Annotation (..), Noun, RawNoun (..)) where
6+import GHC.Generics (Generic)
9+data Annotation = Annotation
12+ deriving stock (Show)
17+atom :: Natural -> Noun
18+atom nat = Atom nat Annotation {hash = hash nat}
20+cell :: Noun -> Noun -> Noun
26+ { hash = hash ((annotation lhs).hash, (annotation rhs).hash)
35+annotation :: RawNoun ann -> ann
36+annotation (Atom _ ann) = ann
37+annotation (Cell _ _ ann) = ann
41+ | Cell (RawNoun ann) (RawNoun ann) ann
42+ deriving stock (Generic, Show)
44+instance Eq (RawNoun ann) where
45+ (Atom lhs _) == (Atom rhs _) = lhs == rhs
46+ (Cell lhslhs lhsrhs _) == (Cell rhslhs rhsrhs _) = lhslhs == rhslhs && lhsrhs == rhsrhs
49+hashEq :: Noun -> Noun -> Bool
50+hashEq (Atom lhsNat lhsAnn) (Atom rhsNat rhsAnn) =
51+ lhsAnn.hash == rhsAnn.hash && lhsNat == rhsNat
52+hashEq (Cell lhslhs lhsrhs lhsAnn) (Cell rhslhs rhsrhs rhsAnn) =
53+ lhsAnn.hash == rhsAnn.hash && hashEq lhslhs rhslhs && hashEq lhsrhs rhsrhs
56+type Noun = RawNoun Annotation
63+lus ~(Atom nat _) = atom (nat + 1)
66+tis ~(Cell lhs rhs _) = if hashEq lhs rhs then sig else one
69+fas ~(Cell ~(Atom lhs _) rhs _) =
73+ ~(Cell lhs' _ _) -> lhs'
75+ ~(Cell _ rhs' _) -> rhs'
78+ let (d, m) = divMod n 2
80+ then fas (cell (atom 2) (fas (cell (atom d) rhs)))
81+ else fas (cell (atom 3) (fas (cell (atom d) rhs)))
85+hax ~(Cell ~(Atom n _) ~(Cell a b _) _) =
86+ let (d, m) = divMod n 2
89+ _ | m == 0 -> hax (cell (atom d) (cell (cell a (fas (cell (atom $ n + 1) b))) b))
90+ _ -> hax (cell (atom d) (cell (cell (fas (cell (atom $ n + n) b)) a) b))
93+tar ~(Cell a ~(Cell b c _) _) = case b of
94+ Cell x y _ -> cell (tar (cell a (cell x y))) (tar (cell a c))
95+ Atom n _ -> case n of
98+ 3 -> wut $ tar $ cell a c
99+ 4 -> lus $ tar $ cell a c
101+ ~(Cell x y _) -> case n of
102+ 2 -> tar $ cell (tar $ cell a x) (tar $ cell a y)
103+ 5 -> tis $ cell (tar $ cell a x) (tar $ cell a y)
104+ 7 -> tar $ cell (tar $ cell a x) y
105+ 8 -> tar $ cell (cell (tar $ cell a x) a) y
106+ 9 -> tar $ cell (tar $ cell a y) $ cell (atom 2) (cell (cell sig one) (cell sig x))
108+ ~(Cell l k _) -> tar $ cell a $ tar $ cell (cell l k) $ cell sig $ tar $ cell (cell (atom 2) (atom 3)) $ cell sig $ tar $ cell a (cell (atom 4) (cell (atom 4) x))
110+ ~(Cell l k _) -> hax $ cell l $ cell (tar (cell a k)) $ tar $ cell a y
112+ Cell l k _ -> tar $ cell (cell (tar $ cell a l) (tar $ cell a k)) $ cell sig (atom 3)
113+ Atom l _ -> tar $ cell a y