on master help sign in
Got jets working
commitbe02e585796ac5d81b721ac1a4f1b63b888f10b5 authoriko <mail@iko.soy> dateSun 20 Aug 2023, 18:13 (3 years ago) parentc7b5568
8 files changed+100−79
src/Nock.hs+12−62 src/Nock/Jets.hs+10 src/Nock/Parser.hs+1−1 src/Nock/Types.hs+67 tests/Main.hs+1 tests/golden/add-nock.nock tests/golden/add.hoon+8−13 tests/golden/dec.hoon+1−3
src/Nock.hs+12−62
@@ -1,6 +1,6 @@
11{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
22
3−module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..), pretty) where
3+module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) where
44
55import Control.DeepSeq (NFData, force)
66import Data.Bits
@@ -13,24 +13,8 @@ import GHC.Base
1313import GHC.Conc (par)
1414import GHC.Generics (Generic)
1515import GHC.Num
16−
17−newtype Annotation = Annotation
18− { hash :: Int
19− }
20− deriving stock (Show, Generic)
21− deriving anyclass (NFData)
22−
23−atom :: Natural -> Noun
24−atom nat = Atom nat Annotation {hash = hash nat}
25−
26−cell :: Noun -> Noun -> Noun
27−cell lhs rhs =
28− Cell
29− lhs
30− rhs
31− Annotation
32− { hash = hash ((annotation lhs).hash, (annotation rhs).hash)
33− }
16+import Nock.Jets
17+import Nock.Types
3418
3519sig :: Noun
3620sig = atom 0
@@ -53,37 +37,6 @@ twoThree = cell two three
5337sigOne :: Noun
5438sigOne = cell sig one
5539
56−annotation :: RawNoun ann -> ann
57−annotation (Atom _ ann) = ann
58−annotation (Cell _ _ ann) = ann
59−
60−data RawNoun ann
61− = Atom !Natural ann
62− | Cell (RawNoun ann) (RawNoun ann) ann
63− deriving stock (Generic)
64− deriving anyclass (NFData)
65−
66−instance Show (RawNoun ann) where
67− show = TL.unpack . pretty
68−
69−instance Eq (RawNoun ann) where
70− (Atom lhs _) == (Atom rhs _) = lhs == rhs
71− (Cell lhslhs lhsrhs _) == (Cell rhslhs rhsrhs _) =
72− let rhsRes = lhsrhs == rhsrhs
73− in (force rhsRes `par` lhslhs == rhslhs) && rhsRes
74− _ == _ = False
75−
76−hashEq :: Noun -> Noun -> Bool
77−hashEq (Atom lhsNat lhsAnn) (Atom rhsNat rhsAnn) =
78− let normalEq = lhsNat == rhsNat
79− in (force normalEq `par` lhsAnn.hash == rhsAnn.hash) && normalEq
80−hashEq (Cell lhslhs lhsrhs lhsAnn) (Cell rhslhs rhsrhs rhsAnn) =
81− let normalEq = hashEq lhslhs rhslhs && hashEq lhsrhs rhsrhs
82− in (force normalEq `par` lhsAnn.hash == rhsAnn.hash) && normalEq
83−hashEq _ _ = False
84−
85−type Noun = RawNoun Annotation
86−
8740wut :: Noun -> Noun
8841wut Cell {} = sig
8942wut Atom {} = one
@@ -149,9 +102,14 @@ tar' b c subject = case b of
149102 4## -> case c of
150103 ~(Cell l k _) -> lus $ tar' l k subject
151104 _ -> case c of
152− ~(Cell x ~formula@(Cell h j _) _) -> case n of
153− 2## -> case (tar' h j subject, x) of
154− ~(~(Cell u v _), ~(Cell l k _)) -> tar' u v (tar' l k subject)
105+ ~(Cell x ~(Cell h j _) _) -> case n of
106+ 2## -> case x of
107+ ~(Cell l k _) -> case tar' l k subject of
108+ ~(formula@(Cell battery ~(Cell sample _ _) _)) ->
109+ if battery == Nock.Jets.dec
110+ then error "Found it!"
111+ else case (tar' h j subject) of
112+ ~(Cell u v _) -> tar' u v formula
155113 5## -> case x of
156114 ~(Cell l k _) -> tis (tar' h j subject) (tar' l k subject)
157115 7## -> case x of
@@ -164,14 +122,6 @@ tar' b c subject = case b of
164122 10## -> case x of
165123 ~(Cell ~(Atom b' _) ~(Cell u v _) _) -> hax b' (tar' u v subject) (tar' h j subject)
166124 11## -> case x of
167− Cell _ ~(Cell u v _) _ -> traceShow (formula, subject) $ tar' h j subject
125+ Cell _ ~(Cell u v _) _ -> tar' h j subject
168126 Atom _ _ -> tar' h j subject
169127 _ -> undefined
170−
171−pretty :: RawNoun ann -> TL.Text
172−pretty noun = TLB.toLazyText $ pretty' noun False
173−
174−pretty' :: RawNoun ann -> Bool -> TLB.Builder
175−pretty' (Atom a _) _ = TLB.decimal a
176−pretty' (Cell lhs rhs _) True = pretty' lhs False <> " " <> pretty' rhs True
177−pretty' (Cell lhs rhs _) False = "[" <> pretty' lhs False <> " " <> pretty' rhs True <> "]"
src/Nock/Jets.hsnew file+10
@@ -0,0 +1,10 @@
1+module Nock.Jets (add, dec) where
2+
3+import Nock.Parser qualified
4+import Nock.Types
5+
6+dec :: Noun
7+dec = Nock.Parser.noun "[8 [1 0] 8 [1 6 [5 [0 30] 4 0 6] [0 6] 9 2 10 [6 4 0 6] 0 1] 9 2 0 1]"
8+
9+add :: Noun
10+add = Nock.Parser.noun "[ [ 6 [5 [1 0] 0 12] [0 13] 9 2 10 [6 [8 [9 5 0 7] 9 2 10 [6 0 28] 0 2] 4 0 13] 0 1 ] [0 0] [ [ 8 [1 0 0] [ 1 6 [5 [1 0] 0 12] [0 13] 9 2 10 [6 [8 [9 5 0 7] 9 2 10 [6 0 28] 0 2] 4 0 13] 0 1 ] 0 1 ] 7 [ 8 [1 69] [ 1 6 [5 [1 0] 0 6] [0 0] 8 [1 0] 8 [1 6 [5 [0 30] 4 0 6] [0 6] 9 2 10 [6 4 0 6] 0 1] 9 2 0 1 ] 0 1 ] 11 [1.953.718.630 1 6.514.020 [0 7] 0] 0 1 ] 0 ]"
src/Nock/Parser.hs+1−1
@@ -5,7 +5,7 @@ module Nock.Parser (noun) where
55import Control.Applicative
66import Data.Text.Lazy as TL
77import Data.Void (Void)
8−import Nock
8+import Nock.Types
99import Text.Megaparsec
1010import Text.Megaparsec.Char
1111import Text.Megaparsec.Char.Lexer (decimal)
src/Nock/Types.hsnew file+67
@@ -0,0 +1,67 @@
1+module Nock.Types (pretty, hashEq, Noun, RawNoun (..), Annotation (..), atom, cell) where
2+
3+import Control.DeepSeq
4+import Data.Hashable
5+import Data.Text.Lazy qualified as TL
6+import Data.Text.Lazy.Builder qualified as TLB
7+import Data.Text.Lazy.Builder.Int qualified as TLB
8+import GHC.Conc
9+import GHC.Generics
10+import GHC.Natural
11+
12+newtype Annotation = Annotation
13+ { hash :: Int
14+ }
15+ deriving stock (Show, Generic)
16+ deriving anyclass (NFData)
17+
18+atom :: Natural -> Noun
19+atom nat = Atom nat Annotation {hash = hash nat}
20+
21+cell :: Noun -> Noun -> Noun
22+cell lhs rhs =
23+ Cell
24+ lhs
25+ rhs
26+ Annotation
27+ { hash = hash ((annotation lhs).hash, (annotation rhs).hash)
28+ }
29+
30+annotation :: RawNoun ann -> ann
31+annotation (Atom _ ann) = ann
32+annotation (Cell _ _ ann) = ann
33+
34+data RawNoun ann
35+ = Atom !Natural ann
36+ | Cell !(RawNoun ann) !(RawNoun ann) ann
37+ deriving stock (Generic)
38+ deriving anyclass (NFData)
39+
40+instance Show (RawNoun ann) where
41+ show = TL.unpack . pretty
42+
43+instance Eq (RawNoun ann) where
44+ (Atom lhs _) == (Atom rhs _) = lhs == rhs
45+ (Cell lhslhs lhsrhs _) == (Cell rhslhs rhsrhs _) =
46+ let rhsRes = lhsrhs == rhsrhs
47+ in (force rhsRes `par` lhslhs == rhslhs) && rhsRes
48+ _ == _ = False
49+
50+hashEq :: Noun -> Noun -> Bool
51+hashEq (Atom lhsNat lhsAnn) (Atom rhsNat rhsAnn) =
52+ let normalEq = lhsNat == rhsNat
53+ in (force normalEq `par` lhsAnn.hash == rhsAnn.hash) && normalEq
54+hashEq (Cell lhslhs lhsrhs lhsAnn) (Cell rhslhs rhsrhs rhsAnn) =
55+ let normalEq = hashEq lhslhs rhslhs && hashEq lhsrhs rhsrhs
56+ in (force normalEq `par` lhsAnn.hash == rhsAnn.hash) && normalEq
57+hashEq _ _ = False
58+
59+type Noun = RawNoun Annotation
60+
61+pretty :: RawNoun ann -> TL.Text
62+pretty noun = TLB.toLazyText $ pretty' noun False
63+
64+pretty' :: RawNoun ann -> Bool -> TLB.Builder
65+pretty' (Atom a _) _ = TLB.decimal a
66+pretty' (Cell lhs rhs _) True = pretty' lhs False <> " " <> pretty' rhs True
67+pretty' (Cell lhs rhs _) False = "[" <> pretty' lhs False <> " " <> pretty' rhs True <> "]"
tests/Main.hs+1
@@ -7,6 +7,7 @@ import Data.Text.Lazy.Encoding as TL
77import Nock
88import Nock.Parser qualified
99import Nock.Printer
10+import Nock.Types
1011import System.FilePath.Posix
1112import System.Process.Typed
1213import Test.Tasty
tests/golden/add-nock.nocknew file

No changes to the content.

tests/golden/add.hoon+8−13
@@ -1,19 +1,15 @@
11!=
22=>
33|%
4+++ foo 1
45++ dec
5− ~/ %dec
6− :: unsigned decrement by one.
7− |= a=@
8− ~_ leaf+"decrement-underflow"
9− ?< =(0 a)
10− =+ b=0
11− :: decremented integer
12− |- ^- @
13− ?: =(a +(b)) b
14− $(b +(b))
6+|= a=@
7+=/ b 0
8+|- ^- @
9+?. =(a +(b)) $(b +(b)) b
10+++ bar 1
1511++ add
16− ~/ %add
12+ :: ~/ %add
1713 :: unsigned addition
1814 ::
1915 :: a: augend
@@ -24,5 +20,4 @@
2420 ?: =(0 a) b
2521 $(a (dec a), b +(b))
2622--
27−%+ add
28−1.000 1.000
23+%+ add 1 2
tests/golden/dec.hoon+1−3
@@ -6,7 +6,5 @@
66=/ b 0
77|- ^- @
88?. =(a +(b)) $(b +(b)) b
9−
109--
11−
12−%- dec 1.000.000
10+%- dec 8