@@ -1,15 +1,32 @@
11{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
22
3−module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun) where
3+module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) where
44
5+import Control.DeepSeq (NFData, force)
56import Data.Bits
6−import Data.Functor ((<&>))
7−import Effectful
8−import Effectful.State.Static.Local qualified as SEL
7+import Data.Hashable
98import GHC.Base
9+import GHC.Conc (par)
10+import GHC.Generics (Generic)
1011import GHC.Num
11−import GHC.Num.BigNat (bigNatAddWord#, bigNatFromWord2#)
12−import Nock.Types
12+
13+newtype Annotation = Annotation
14+ { hash :: Int
15+ }
16+ deriving stock (Show, Generic)
17+ deriving anyclass (NFData)
18+
19+atom :: Natural -> Noun
20+atom nat = Atom nat Annotation {hash = hash nat}
21+
22+cell :: Noun -> Noun -> Noun
23+cell lhs rhs =
24+ Cell
25+ lhs
26+ rhs
27+ Annotation
28+ { hash = hash ((annotation lhs).hash, (annotation rhs).hash)
29+ }
1330
1431sig :: Noun
1532sig = atom 0
@@ -32,22 +49,46 @@ twoThree = cell two three
3249sigOne :: Noun
3350sigOne = cell sig one
3451
52+annotation :: RawNoun ann -> ann
53+annotation (Atom _ ann) = ann
54+annotation (Cell _ _ ann) = ann
55+
56+data RawNoun ann
57+ = Atom !Natural ann
58+ | Cell (RawNoun ann) (RawNoun ann) ann
59+ deriving stock (Generic, Show)
60+ deriving anyclass (NFData)
61+
62+instance Eq (RawNoun ann) where
63+ (Atom lhs _) == (Atom rhs _) = lhs == rhs
64+ (Cell lhslhs lhsrhs _) == (Cell rhslhs rhsrhs _) =
65+ let rhsRes = lhsrhs == rhsrhs
66+ in (force rhsRes `par` lhslhs == rhslhs) && rhsRes
67+ _ == _ = False
68+
69+hashEq :: Noun -> Noun -> Bool
70+hashEq (Atom lhsNat lhsAnn) (Atom rhsNat rhsAnn) =
71+ let normalEq = lhsNat == rhsNat
72+ in (force normalEq `par` lhsAnn.hash == rhsAnn.hash) && normalEq
73+hashEq (Cell lhslhs lhsrhs lhsAnn) (Cell rhslhs rhsrhs rhsAnn) =
74+ let normalEq = hashEq lhslhs rhslhs && hashEq lhsrhs rhsrhs
75+ in (force normalEq `par` lhsAnn.hash == rhsAnn.hash) && normalEq
76+hashEq _ _ = False
77+
78+type Noun = RawNoun Annotation
79+
3580wut :: Noun -> Noun
3681wut Cell {} = sig
3782wut Atom {} = one
3883
3984lus :: Noun -> Noun
40−lus ~(Atom nat _) = atom $ case nat of
41− (NS n) -> case addWordC# n 1## of
42− (# l, 0# #) -> NS l
43− (# l, c #) -> NB (bigNatFromWord2# (int2Word# c) l)
44− (NB n) -> NB (bigNatAddWord# n 1##)
85+lus ~(Atom nat _) = atom (nat + 1)
4586
46−tis :: Noun -> Noun -> Eff es Noun
47−tis lhs rhs = pure $ if lhs == rhs then sig else one
87+tis :: Noun -> Noun -> Noun
88+tis lhs rhs = if hashEq lhs rhs then sig else one
4889
49−fas# :: Word# -> Noun -> Noun
50−fas# lhs rhs =
90+fasWord :: Word# -> Noun -> Noun
91+fasWord lhs rhs =
5192 case lhs of
5293 1## -> rhs
5394 2## -> case rhs of
@@ -55,81 +96,67 @@ fas# lhs rhs =
5596 3## -> case rhs of
5697 ~(Cell _ rhs' _) -> rhs'
5798 n ->
58− let rest = fas# (uncheckedShiftRL# n 1#) rhs
99+ let rest = fasWord (uncheckedShiftRL# n 1#) rhs
59100 in case and# 1## n of
60− 1## -> fas# 3## rest
61− _ -> fas# 2## rest
101+ 1## -> fasWord 3## rest
102+ _ -> fasWord 2## rest
62103
63104fas :: Natural -> Noun -> Noun
64−fas (NS w) rhs = fas# w rhs
105+fas (NS w) rhs = fasWord w rhs
65106fas n rhs =
66107 let rest = fas (unsafeShiftR n 1) rhs
67108 in if testBit n 0
68109 then fas 3 rest
69110 else fas 2 rest
70111
71−hax# :: Word# -> Noun -> Noun -> Noun
72−hax# 1## b _ = b
73−hax# n b c =
74− let a = uncheckedShiftRL# n 1#
75− in case and# n 1## of
76− 1## -> hax# a (cell (fas# (n `xor#` 1##) c) b) c
77− _ -> hax# a (cell b (fas# (n `or#` 1##) c)) c
112+haxWord :: Word# -> Noun -> Noun -> Noun
113+haxWord 1## newValue _ = newValue
114+haxWord place newValue ~(Cell lhs rhs _) =
115+ let a = uncheckedShiftRL# place 1#
116+ in case and# place 1## of
117+ 1## -> cell (haxWord a newValue lhs) rhs
118+ _ -> cell lhs (haxWord a newValue rhs)
78119
79120hax :: Natural -> Noun -> Noun -> Noun
80−hax (NS n) b c = hax# n b c
81−hax n b c =
82− let a = unsafeShiftR n 1
83− in if testBit n 0
84− then hax a (cell (fas (n - 1) c) b) c
85− else hax a (cell b (fas (n + 1) c)) c
86−
87−tar :: Noun -> Eff es Noun
121+hax (NS w) newValue rhs = haxWord w newValue rhs
122+hax place newValue ~(Cell lhs rhs _) =
123+ let a = unsafeShiftR place 1
124+ in if testBit place 0
125+ then cell (hax a newValue lhs) rhs
126+ else cell lhs (hax a newValue rhs)
127+
128+tar :: Noun -> Noun
88129tar ~(Cell subject ~(Cell a b _) _) = tar' a b subject
89130
90−tar' :: Noun -> Noun -> Noun -> Eff es Noun
131+tar' :: Noun -> Noun -> Noun -> Noun
91132tar' b c subject = case b of
92133 Cell x y _ -> case c of
93− ~(Cell l k _) -> cell <$> tar' x y subject <*> tar' l k subject
134+ ~(Cell l k _) -> cell (tar' x y subject) (tar' l k subject)
94135 Atom (NB _) _ -> undefined
95136 Atom (NS n) _ -> case n of
96137 0## -> case c of
97− ~(Atom nat _) -> pure $ fas nat subject
98− 1## -> pure c
138+ ~(Atom nat _) -> fas nat subject
139+ 1## -> c
99140 3## -> case c of
100− ~(Cell l k _) -> wut <$> tar' l k subject
141+ ~(Cell l k _) -> wut $ tar' l k subject
101142 4## -> case c of
102− ~(Cell l k _) -> lus <$> tar' l k subject
143+ ~(Cell l k _) -> lus $ tar' l k subject
103144 _ -> case c of
104145 ~(Cell x ~(Cell h j _) _) -> case n of
105− 2## -> case x of
106− ~(Cell l k _) ->
107− tar' l k subject >>= \case
108− ~formula@(Cell battery ~(Cell sample _ _) _) ->
109− -- if traceShowId battery == Nock.Jets.add
110− -- then error "Found it!"
111− tar' h j subject >>= \case
112− ~(Cell u v _) -> tar' u v formula
146+ 2## -> case (tar' h j subject, x) of
147+ ~(~(Cell u v _), ~(Cell l k _)) -> tar' u v (tar' l k subject)
113148 5## -> case x of
114− ~(Cell l k _) -> do
115− p <- tar' h j subject
116− q <- tar' l k subject
117− tis p q
149+ ~(Cell l k _) -> tis (tar' h j subject) (tar' l k subject)
118150 7## -> case x of
119− ~(Cell l k _) -> tar' l k subject >>= tar' h j
151+ ~(Cell l k _) -> tar' h j (tar' l k subject)
120152 8## -> case x of
121− ~(Cell l k _) -> do
122− p <- tar' l k subject
123− tar' h j (cell p subject)
124− 9## -> tar' h j subject >>= tar' two (cell sigOne (cell sig x))
125− 6## -> do
126− p <- tar' four (cell four x) subject
127− q <- tar' sig p twoThree
128− tar' sig q (cell h j) >>= \case
129− ~(Cell u v _) -> tar' u v subject
153+ ~(Cell l k _) -> tar' h j (cell (tar' l k subject) subject)
154+ 9## -> tar' two (cell sigOne (cell sig x)) (tar' h j subject)
155+ 6## -> case tar' sig (tar' sig (tar' four (cell four x) subject) twoThree) (cell h j) of
156+ ~(Cell u v _) -> tar' u v subject
130157 10## -> case x of
131− ~(Cell ~(Atom b' _) ~(Cell u v _) _) -> hax b' <$> tar' u v subject <*> tar' h j subject
158+ ~(Cell ~(Atom b' _) ~(Cell u v _) _) -> hax b' (tar' u v subject) (tar' h j subject)
132159 11## -> case x of
133− Cell _ ~(Cell u v _) _ -> tar' h j subject
160+ Cell _ ~(Cell u v _) _ -> tar' sig three (cell (tar' u v subject) (tar' h j subject))
134161 Atom _ _ -> tar' h j subject
135162 _ -> undefined