@@ -1,32 +1,15 @@
11{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
22
3−module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) where
3+module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun) where
44
5−import Control.DeepSeq (NFData, force)
65import Data.Bits
7−import Data.Hashable
6+import Data.Functor ((<&>))
7+import Effectful
8+import Effectful.State.Static.Local qualified as SEL
89import GHC.Base
9−import GHC.Conc (par)
10−import GHC.Generics (Generic)
1110import GHC.Num
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− }
11+import GHC.Num.BigNat (bigNatAddWord#, bigNatFromWord2#)
12+import Nock.Types
3013
3114sig :: Noun
3215sig = atom 0
@@ -49,46 +32,22 @@ twoThree = cell two three
4932sigOne :: Noun
5033sigOne = cell sig one
5134
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−
8035wut :: Noun -> Noun
8136wut Cell {} = sig
8237wut Atom {} = one
8338
8439lus :: Noun -> Noun
85−lus ~(Atom nat _) = atom (nat + 1)
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##)
8645
87−tis :: Noun -> Noun -> Noun
88−tis lhs rhs = if hashEq lhs rhs then sig else one
46+tis :: Noun -> Noun -> Eff es Noun
47+tis lhs rhs = pure $ if lhs == rhs then sig else one
8948
90−fasWord :: Word# -> Noun -> Noun
91−fasWord lhs rhs =
49+fas# :: Word# -> Noun -> Noun
50+fas# lhs rhs =
9251 case lhs of
9352 1## -> rhs
9453 2## -> case rhs of
@@ -96,67 +55,81 @@ fasWord lhs rhs =
9655 3## -> case rhs of
9756 ~(Cell _ rhs' _) -> rhs'
9857 n ->
99− let rest = fasWord (uncheckedShiftRL# n 1#) rhs
58+ let rest = fas# (uncheckedShiftRL# n 1#) rhs
10059 in case and# 1## n of
101− 1## -> fasWord 3## rest
102− _ -> fasWord 2## rest
60+ 1## -> fas# 3## rest
61+ _ -> fas# 2## rest
10362
10463fas :: Natural -> Noun -> Noun
105−fas (NS w) rhs = fasWord w rhs
64+fas (NS w) rhs = fas# w rhs
10665fas n rhs =
10766 let rest = fas (unsafeShiftR n 1) rhs
10867 in if testBit n 0
10968 then fas 3 rest
11069 else fas 2 rest
11170
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)
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
11978
12079hax :: Natural -> Noun -> Noun -> 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
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
12988tar ~(Cell subject ~(Cell a b _) _) = tar' a b subject
13089
131−tar' :: Noun -> Noun -> Noun -> Noun
90+tar' :: Noun -> Noun -> Noun -> Eff es Noun
13291tar' b c subject = case b of
13392 Cell x y _ -> case c of
134− ~(Cell l k _) -> cell (tar' x y subject) (tar' l k subject)
93+ ~(Cell l k _) -> cell <$> tar' x y subject <*> tar' l k subject
13594 Atom (NB _) _ -> undefined
13695 Atom (NS n) _ -> case n of
13796 0## -> case c of
138− ~(Atom nat _) -> fas nat subject
139− 1## -> c
97+ ~(Atom nat _) -> pure $ fas nat subject
98+ 1## -> pure c
14099 3## -> case c of
141− ~(Cell l k _) -> wut $ tar' l k subject
100+ ~(Cell l k _) -> wut <$> tar' l k subject
142101 4## -> case c of
143− ~(Cell l k _) -> lus $ tar' l k subject
102+ ~(Cell l k _) -> lus <$> tar' l k subject
144103 _ -> case c of
145104 ~(Cell x ~(Cell h j _) _) -> case n of
146− 2## -> case (tar' h j subject, x) of
147− ~(~(Cell u v _), ~(Cell l k _)) -> tar' u v (tar' l k subject)
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
148113 5## -> case x of
149− ~(Cell l k _) -> tis (tar' h j subject) (tar' l k subject)
114+ ~(Cell l k _) -> do
115+ p <- tar' h j subject
116+ q <- tar' l k subject
117+ tis p q
150118 7## -> case x of
151− ~(Cell l k _) -> tar' h j (tar' l k subject)
119+ ~(Cell l k _) -> tar' l k subject >>= tar' h j
152120 8## -> case x of
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
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
157130 10## -> case x of
158− ~(Cell ~(Atom b' _) ~(Cell u v _) _) -> hax b' (tar' u v subject) (tar' h j subject)
131+ ~(Cell ~(Atom b' _) ~(Cell u v _) _) -> hax b' <$> tar' u v subject <*> tar' h j subject
159132 11## -> case x of
160− Cell _ ~(Cell u v _) _ -> tar' sig three (cell (tar' u v subject) (tar' h j subject))
133+ Cell _ ~(Cell u v _) _ -> tar' h j subject
161134 Atom _ _ -> tar' h j subject
162135 _ -> undefined