on master help sign in
MemoTrie
0c59d55iko
package.yaml+1
@@ -18,6 +18,7 @@ dependencies:
18 - gauge
18 - gauge
19 - filepath
19 - filepath
20 - deepseq
20 - deepseq
21+ - MemoTrie
21library:
22library:
22 source-dirs: src
23 source-dirs: src
23
24
src/Nock.hs+26−18
@@ -4,7 +4,10 @@ module Nock (tar, hax, fas, cell, atom, Annotation (..), Noun, RawNoun (..)) whe
4
4
5import Control.DeepSeq (NFData)
5import Control.DeepSeq (NFData)
6import Data.Hashable
6import Data.Hashable
7+import Data.MemoTrie (memo)
8+import Debug.Trace (traceShowId)
7import GHC.Generics (Generic)
9import GHC.Generics (Generic)
10+import GHC.Word (Word64)
8import Numeric.Natural
11import Numeric.Natural
9
12
10data Annotation = Annotation
13data Annotation = Annotation
@@ -80,29 +83,34 @@ lus ~(Atom nat _) = atom (nat + 1)
80tis :: Noun -> Noun -> Noun
83tis :: Noun -> Noun -> Noun
81tis lhs rhs = if hashEq lhs rhs then sig else one
84tis lhs rhs = if hashEq lhs rhs then sig else one
82
85
83−fas :: Natural -> Noun -> Noun
86+fas' :: Word64 -> Noun -> Noun
84−fas lhs rhs =
87+fas' n = case (traceShowId n) of
85− case lhs of
88+ 1 -> id
86− 1 -> rhs
87− 2 -> case rhs of
88− ~(Cell lhs' _ _) -> lhs'
89+ 2 -> \ ~(Cell x _ _) -> x
89− 3 -> case rhs of
90− ~(Cell _ rhs' _) -> rhs'
90+ 3 -> \ ~(Cell _ x _) -> x
91− n
91+ _ ->
92− | n > 0 ->
93− let (d, m) = divMod n 2
92+ let (d, m) = divMod n 2
93+ next = fas d
94− in if m == 0
94+ in if m == 0
95− then fas 2 (fas d rhs)
95+ then \x ->
96+ ( case next x of
97+ ~(Cell y _ _) -> y
98+ )
96− else fas 3 (fas d rhs)
99+ else \x ->
97− _ -> undefined
100+ ( case next x of
101+ ~(Cell _ y _) -> y
102+ )
103+
104+fas :: Word64 -> Noun -> Noun
105+fas = memo fas'
98
106
99hax :: Natural -> Noun -> Noun -> Noun
107hax :: Natural -> Noun -> Noun -> Noun
100hax n b c =
108hax n b c =
101 let (a, m) = divMod n 2
109 let (a, m) = divMod n 2
102 in case n of
110 in case n of
103 1 -> b
111 1 -> b
104− _ | m == 0 -> hax a (cell b (fas (a + a + 1) c)) c
112+ _ | m == 0 -> hax a (cell b (fas (fromIntegral $ a + a + 1) c)) c
105− _ -> hax a (cell (fas (a + a) c) b) c
113+ _ -> hax a (cell (fas (fromIntegral $ a + a) c) b) c
106
114
107tar :: Noun -> Noun
115tar :: Noun -> Noun
108tar ~(Cell subject ~(Cell a b _) _) = tar' a b subject
116tar ~(Cell subject ~(Cell a b _) _) = tar' a b subject
@@ -113,7 +121,7 @@ tar' b c subject = case b of
113 ~(Cell l k _) -> cell (tar' x y subject) (tar' l k subject)
121 ~(Cell l k _) -> cell (tar' x y subject) (tar' l k subject)
114 Atom n _ -> case n of
122 Atom n _ -> case n of
115 0 -> case c of
123 0 -> case c of
116− ~(Atom nat _) -> fas nat subject
124+ ~(Atom nat _) -> fas (fromIntegral nat) subject
117 1 -> c
125 1 -> c
118 3 -> case c of
126 3 -> case c of
119 ~(Cell l k _) -> wut $ tar' l k subject
127 ~(Cell l k _) -> wut $ tar' l k subject
stack.yaml+2
@@ -53,6 +53,8 @@ extra-deps:
53# commit: 8aa66ff52166ff283b66c7c322108fcb4aa55192
53# commit: 8aa66ff52166ff283b66c7c322108fcb4aa55192
54- git: https://github.com/ilyakooo0/binary-bits
54- git: https://github.com/ilyakooo0/binary-bits
55 commit: f58ae476ff50dfc277f8dd603142b7ce864b8e03
55 commit: f58ae476ff50dfc277f8dd603142b7ce864b8e03
56+- git: https://github.com/jlwoodwa/MemoTrie
57+ commit: 2c8c1a5da4cde71c04ca431371ab4b47c48a06b1
56# Override default flag values for local packages and extra-deps
58# Override default flag values for local packages and extra-deps
57# flags: {}
59# flags: {}
58
60
stack.yaml.lock+11
@@ -15,6 +15,17 @@ packages:
15 original:
15 original:
16 commit: f58ae476ff50dfc277f8dd603142b7ce864b8e03
16 commit: f58ae476ff50dfc277f8dd603142b7ce864b8e03
17 git: https://github.com/ilyakooo0/binary-bits
17 git: https://github.com/ilyakooo0/binary-bits
18+- completed:
19+ commit: 2c8c1a5da4cde71c04ca431371ab4b47c48a06b1
20+ git: https://github.com/jlwoodwa/MemoTrie
21+ name: MemoTrie
22+ pantry-tree:
23+ sha256: 1820c56b5265d010f46283da208006cf2a8520690b6948d4e9a54953c8e607e6
24+ size: 517
25+ version: 0.6.10
26+ original:
27+ commit: 2c8c1a5da4cde71c04ca431371ab4b47c48a06b1
28+ git: https://github.com/jlwoodwa/MemoTrie
18snapshots:
29snapshots:
19- completed:
30- completed:
20 sha256: 2e7d4a730d8eb5373b2d383fac84efcf7c81e3b7a5fce71b4c2e19a1768f25a6
31 sha256: 2e7d4a730d8eb5373b2d383fac84efcf7c81e3b7a5fce71b4c2e19a1768f25a6