@@ -1,30 +1,15 @@
11{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
22
3−module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) where
4−
5−import Data.Hashable
6−import Debug.Trace (trace, traceShowId)
7−import GHC.Generics (Generic)
8−import GHC.Stack
9−import Numeric.Natural
10−import Test.QuickCheck.Arbitrary.Generic
11−
12−data Annotation = Annotation
13− { hash :: Int
14− }
15− deriving stock (Show)
16−
17−atom :: Natural -> Noun
18−atom nat = Atom nat Annotation {hash = hash nat}
19−
20−cell :: Noun -> Noun -> Noun
21−cell lhs rhs =
22− Cell
23− lhs
24− rhs
25− Annotation
26− { hash = hash ((annotation lhs).hash, (annotation rhs).hash)
27− }
3+module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun) where
4+
5+import Data.Bits
6+import Data.Functor ((<&>))
7+import Effectful
8+import Effectful.State.Static.Local qualified as SEL
9+import GHC.Base
10+import GHC.Num
11+import GHC.Num.BigNat (bigNatAddWord#, bigNatFromWord2#)
12+import Nock.Types
2813
2914sig :: Noun
3015sig = atom 0
@@ -32,84 +17,119 @@ sig = atom 0
3217one :: Noun
3318one = atom 1
3419
35−annotation :: RawNoun ann -> ann
36−annotation (Atom _ ann) = ann
37−annotation (Cell _ _ ann) = ann
20+two :: Noun
21+two = atom 2
3822
39−data RawNoun ann
40− = Atom !Natural ann
41− | Cell (RawNoun ann) (RawNoun ann) ann
42− deriving stock (Generic, Show)
23+three :: Noun
24+three = atom 3
4325
44−instance Eq (RawNoun ann) where
45− (Atom lhs _) == (Atom rhs _) = lhs == rhs
46− (Cell lhslhs lhsrhs _) == (Cell rhslhs rhsrhs _) = lhslhs == rhslhs && lhsrhs == rhsrhs
47− _ == _ = False
26+four :: Noun
27+four = atom 4
4828
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
54−hashEq _ _ = False
29+twoThree :: Noun
30+twoThree = cell two three
5531
56−type Noun = RawNoun Annotation
32+sigOne :: Noun
33+sigOne = cell sig one
5734
5835wut :: Noun -> Noun
5936wut Cell {} = sig
6037wut Atom {} = one
6138
6239lus :: Noun -> Noun
63−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##)
6445
65−tis :: Noun -> Noun
66−tis ~(Cell 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
6748
68−fas :: Noun -> Noun
69−fas ~(Cell ~(Atom lhs _) rhs _) =
49+fas# :: Word# -> Noun -> Noun
50+fas# lhs rhs =
7051 case lhs of
71− 1 -> rhs
72− 2 -> case rhs of
73− (Cell lhs' _ _) -> lhs'
74− _ -> undefined
75− 3 -> case rhs of
52+ 1## -> rhs
53+ 2## -> case rhs of
54+ ~(Cell lhs' _ _) -> lhs'
55+ 3## -> case rhs of
7656 ~(Cell _ rhs' _) -> rhs'
77− n
78− | n > 0 ->
79− let (d, m) = divMod n 2
80− in if m == 0
81− then fas (cell (atom 2) (fas (cell (atom d) rhs)))
82− else fas (cell (atom 3) (fas (cell (atom d) rhs)))
83− _ -> undefined
84−
85−hax :: Noun -> Noun
86−hax ~(Cell ~(Atom n _) ~(Cell b c _) _) =
87− let (a, m) = divMod n 2
88− in case n of
89− 1 -> b
90− _ | m == 0 -> hax (cell (atom a) (cell (cell b (fas (cell (atom $ a + a + 1) c))) c))
91− _ -> hax (cell (atom a) (cell (cell (fas (cell (atom $ a + a) c)) b) c))
92−
93−tar :: Noun -> Noun
94−tar ~(Cell a ~(Cell b c _) _) = case b of
95− Cell x y _ -> cell (tar (cell a (cell x y))) (tar (cell a c))
96− Atom n _ -> case n of
97− 0 -> fas $ cell c a
98− 1 -> c
99− 3 -> wut $ tar $ cell a c
100− 4 -> lus $ tar $ cell a c
57+ n ->
58+ let rest = fas# (uncheckedShiftRL# n 1#) rhs
59+ in case and# 1## n of
60+ 1## -> fas# 3## rest
61+ _ -> fas# 2## rest
62+
63+fas :: Natural -> Noun -> Noun
64+fas (NS w) rhs = fas# w rhs
65+fas n rhs =
66+ let rest = fas (unsafeShiftR n 1) rhs
67+ in if testBit n 0
68+ then fas 3 rest
69+ else fas 2 rest
70+
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
78+
79+hax :: 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
88+tar ~(Cell subject ~(Cell a b _) _) = tar' a b subject
89+
90+tar' :: Noun -> Noun -> Noun -> Eff es Noun
91+tar' b c subject = case b of
92+ Cell x y _ -> case c of
93+ ~(Cell l k _) -> cell <$> tar' x y subject <*> tar' l k subject
94+ Atom (NB _) _ -> undefined
95+ Atom (NS n) _ -> case n of
96+ 0## -> case c of
97+ ~(Atom nat _) -> pure $ fas nat subject
98+ 1## -> pure c
99+ 3## -> case c of
100+ ~(Cell l k _) -> wut <$> tar' l k subject
101+ 4## -> case c of
102+ ~(Cell l k _) -> lus <$> tar' l k subject
101103 _ -> case c of
102− ~(Cell x y _) -> case n of
103− 2 -> tar $ cell (tar $ cell a x) (tar $ cell a y)
104− 5 -> tis $ cell (tar $ cell a x) (tar $ cell a y)
105− 7 -> tar $ cell (tar $ cell a x) y
106− 8 -> tar $ cell (cell (tar $ cell a x) a) y
107− 9 -> tar $ cell (tar $ cell a y) $ cell (atom 2) (cell (cell sig one) (cell sig x))
108− 6 -> case y of
109− ~(Cell c' d' _) -> tar $ cell a $ tar $ cell (cell c' d') $ cell sig $ tar $ cell (cell (atom 2) (atom 3)) $ cell sig $ tar $ cell a (cell (atom 4) (cell (atom 4) x))
110− 10 -> case x of
111− ~(Cell b' c' _) -> hax $ cell b' $ cell (tar (cell a c')) $ tar $ cell a y
112− 11 -> case x of
113− Cell _ c' _ -> tar $ cell (cell (tar $ cell a c') (tar $ cell a y)) $ cell sig (atom 3)
114− Atom _ _ -> tar $ cell a y
115− u -> error $ show u
104+ ~(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
113+ 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
118+ 7## -> case x of
119+ ~(Cell l k _) -> tar' l k subject >>= tar' h j
120+ 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
130+ 10## -> case x of
131+ ~(Cell ~(Atom b' _) ~(Cell u v _) _) -> hax b' <$> tar' u v subject <*> tar' h j subject
132+ 11## -> case x of
133+ Cell _ ~(Cell u v _) _ -> tar' h j subject
134+ Atom _ _ -> tar' h j subject
135+ _ -> undefined