-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathanoma.hoon
260 lines (260 loc) · 4.59 KB
/
anoma.hoon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
:: Simple demo Nock standard library.
!.
=~ %909
~% %k.909 ~ ~
:: layer 0: version stub (+3)
|%
++ anoma +
--
:: layer 1: basic arithmetic (+1)
~% %one + ~
|%
++ dec :: +342
~/ %dec
|= a=@
?< =(0 a)
=| b=@
|- ^- @
?: =(a +(b)) b
$(b +(b))
++ add :: +20
~/ %add
|= [a=@ b=@]
^- @
?: =(0 a) b
$(a (dec a), b +(b))
++ sub :: +47
~/ %sub
|= [a=@ b=@]
^- @
?: =(0 b) a
$(a (dec a), b (dec b))
++ lth :: +343
~/ %lth
|= [a=@ b=@]
^- ?
?& !=(a b)
|-
?| =(0 a)
?& !=(0 b)
$(a (dec a), b (dec b))
== == ==
++ lte :: +84
~/ %lte
|= [a=@ b=@]
^- ?
|(=(a b) (lth a b))
++ gth :: +43
~/ %gth
|= [a=@ b=@]
^- ?
!(lte a b)
++ gte :: +22
~/ %gte
|= [a=@ b=@]
^- ?
!(lth a b)
++ mul :: +4
~/ %mul
|: [a=`@`1 b=`@`1]
=| c=@
|- ^- @
?: =(0 a) c
$(a (dec a), c (add b c))
++ div :: +170
~/ %div
|: [a=`@`1 b=`@`1]
?< =(0 b)
=| c=@
|- ^- @
?: (lth a b) c
$(a (sub a b), c +(c))
++ mod :: +46
~/ %mod
|: [a=`@`1 b=`@`1]
^- @
?< =(0 b)
(sub a (mul b (div a b)))
--
:: layer 2: data structures, HOFs
~% %two + ~
|%
:: some types
+$ mold
$~(* $-(* *))
+$ gate
$-(* *)
++ trap
|$ [product]
_|?($:product)
++ unit
|$ [item]
$@(~ [~ u=item])
++ each
|$ [left right]
$% [%| p=left]
[%& p=right]
==
++ list
|$ [item]
$@(~ [i=item t=(list item)])
++ nonempty-list
|$ [item]
[i=item t=(list item)]
++ tree
|$ [node]
$@(~ [n=node l=(tree node) r=(tree node)])
:: utilities on some of these types
++ length
|* l=(list)
?~ l
0
+($(l t.l))
:: HOFs on some of these types
++ curry
|* [a=$-(^ *) b=*]
=+ c=+<+.a
|@
++ $
(a b c)
--
++ turn :: map over list
|* [a=(list) b=gate]
^- (list _?>(?=(^ a) (b i.a)))
|-
?~ a ~
[i=(b i.a) t=$(a t.a)]
++ foldr
|* [a=(list) b=_=>(~ |=([* *] +<+))]
|- ^+ ,.+<+.b
?~ a
+<+.b
(b i.a $(a t.a))
--
:: layer 3: fancy arithmetic
~% %three + ~
|%
++ pow
~/ %pow
|= [a=@ b=@]
^- @
?: =(0 b)
1
(mul a $(b (dec b)))
++ modulo :: name this 'mod' and rename 'mod' to 'rem'?
|_ modulus=@
++ reduce
|= a=@
^- @
(mod a modulus)
++ congruent
|= [a=@ b=@]
=((reduce a) (reduce b))
++ add
|= [a=@ b=@]
^- @
(reduce (^add a b))
++ sub
|= [a=@ b=@]
^- @
(reduce (^sub (^add modulus a) (reduce b)))
++ mul
|: [a=`@`1 b=`@`1]
^- @
(reduce (^mul a b))
++ pow
|= [a=@ b=@]
^- @
(reduce (^pow a b))
++ neg
|= a=@
^- @
(^sub modulus (reduce a))
++ inv :: only works in prime fields
|= a=@
^- @
!!
++ div :: only works in prime fields
|: [a=`@`1 b=`@`1]
^- @
(mul a (inv b))
--
--
:: layer 4: bits and bytes
~% %four + ~
|%
++ bex :: 2^a
|= a=@
^- @
?: =(0 a) 1
(mul 2 $(a (dec a)))
++ block :: better name?
|_ block-size=@ :: exponent of 2, i.e. size 3 = 8 bits
++ bits
(bex block-size)
++ modulus
(bex (bex block-size))
++ lsh
|= [count=@ value=@]
^- @
:: lsh(n, a) = a * 2^(bits to shift)
(mul value (bex (mul bits count)))
++ rsh
|= [count=@ value=@]
^- @
:: rsh(n, a) = a / 2^(bits to shift)
(div value (bex (mul bits count)))
++ end :: least significant blocks
|= [count=@ value=@]
^- @
(mod value (bex (mul bits count)))
++ cut :: slice an array
|= [[offset=@ length=@] value=@]
^- @
(end length (rsh offset value))
++ cat :: lengthless concatenate, lsb-first
|= [fst=@ snd=@]
^- @
(add (lsh (met fst) snd) fst)
++ fil :: fill with repeating
|= [count=@ value=@]
^- @
=| n=@
=. value (reduce value)
=/ result value
|-
?: =(n count)
(rsh 1 result)
$(result (add value (lsh 1 result)), n +(n))
++ reduce :: shortcut to reduce modulo block
|= a=@
^- @
(end 1 a)
++ met :: measure in current block size
|= a=@
^- @
=| result=@
|-
?: =(0 a) result
$(a (rsh 1 a), result +(result))
++ inv :: invert all bits mod block size
|= a=@
^- @
(sub (dec modulus) (reduce a))
++ w-add :: wrapping addition mod block size
|= [a=@ b=@]
^- @
(reduce (add a b))
++ twos-complement :: two's complement mod block size
|= a=@
^- @
(w-add (inv a) 1)
--
++ xeb :: log_2(a) + 1
:: NB: not an inverse to bex
:: this is "number of bits required"
|= a=@
^- @
(~(met block 0) a)
--
==