-
Notifications
You must be signed in to change notification settings - Fork 7
/
n.m
255 lines (197 loc) · 4.88 KB
/
n.m
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
# nat, non-negative natural number, unsigned integer
http://en.wikipedia.org/wiki/Natural_number
https://en.wikipedia.org/wiki/Integer_(computer_science)#Long_long
http://danluu.com/integer-overflow/# The Performance Cost of Integer Overflow Checking
add x:N y:N : N = Asm
mov a sp 16
mov c sp 8
add a c
mov sp 24 a
ret
sub x:N y:N : N = Asm
mov a sp 16
mov c sp 8
sub a c
mov sp 24 a
ret
mul x:N y:N : N = Asm
mov a sp 16
mul sp 8
mov sp 24 a
ret
div x:N y:N : N = Asm
mov a sp 16
mov d 0
div sp 8
mov sp 24 a
ret
mod x:N y:N : N = Asm
mov a sp 16
mov d 0
div sp 8
mov sp 24 d
ret
or x:N y:N : N = Asm
mov a sp 16
mov c sp 8
or a c
mov sp 24 a
ret
and x:N y:N : N = Asm
mov a sp 16
mov c sp 8
and a c
mov sp 24 a
ret
shl x:N y:N : N = Asm
mov a sp 16
mov c sp 8
shl a
mov sp 24 a
ret
shl32 x:N : N = Asm
mov a sp 8
shl a 32
mov sp 16 a
ret
shr x:N y:N : N = Asm
mov a sp 16
mov c sp 8
shr a
mov sp 24 a
ret
eq x:N y:N : B = Asm
mov a sp 16
cmp a sp 8
mov c 0
set e c
mov sp 24 c
ret
ne x:N y:N : B = Asm
mov a sp 16
cmp a sp 8
mov c 0
set ne c
mov sp 24 c
ret
gt x:N y:N : B = Asm
mov a sp 16
cmp a sp 8
mov c 0
set g c
mov sp 24 c
ret
ge x:N y:N : B = Asm
mov a sp 16
cmp a sp 8
mov c 0
set ge c
mov sp 24 c
ret
lt x:N y:N : B = Asm
mov a sp 16
cmp a sp 8
mov c 0
set l c
mov sp 24 c
ret
le x:N y:N : B = Asm
mov a sp 16
cmp a sp 8
mov c 0
set le c
mov sp 24 c
ret
neg x:N : N = Asm
mov a sp 8
neg a
mov sp 16 a
ret
char x:N : C = Cast.nat_char x
bit x:N : B = Cast.bit x
str_rev x:N y:S = x & (S.set y (x % 10 + \0).char; str_rev x/10 y+1)
str x:N : S = (y = S.new 20; x & (str_rev x y; S.rev y) | (S.set y \0; y))
Fact (str 42 == '42')
Fact (str \a == 'a')
Fact (N.str \a == '97')
Fact (str 3141592653589793238 == '3141592653589793238')
str_pure x:N : S = (y = S.new 20; x & (str_rev x y; S.rev_pure y) | (S.set y \0; y))
Fact (str_pure 42 == '42')
Fact (str_pure \a == '97')
Fact (str_pure 3141592653589793238 == '3141592653589793238')
str3 x:N : S = str x . S.div3
Fact (str3 3141592653589793238 == '3_141_592_653_589_793_238')
of1 a:N b:N : N = or (shl a 8) b
Fact (of1 0ca 0fe == 0cafe)
of2 a:N b:N c:N d:N : N = or (shl (of1 a b) 16) (of1 c d)
Fact (of2 0ca 0fe 0ba 0be == 0cafe_babe)
of3 a:N b:N c:N d:N e:N f:N g:N h:N : N = or (shl (of2 a b c d) 32) (of2 e f g h)
Fact (of3 0ca 0fe 0ba 0be 0de 0ad 0be 0ef == 0cafe_babe_dead_beef)
str5 x:N : S = (y = x.str; S.heads y+' ' 5)
put x:N = x.str.Put
err x:N = x.str.Err
log x:N = x.str.Log
write file:File x:N = File.write file x.str
linef file:File x:N = File.line file x.str
between a:N x:N b:N : B = a <= x & x <= b
Fact (between 13 29 42)
Fact !(between 42 13 29)
min x:N y:N : N = x < y & x | y
Fact (min 0 42 == 0)
Fact (min 13 42 == 13)
max x:N y:N : N = x < y & y | x
Fact (max 0 42 == 42)
Fact (max 42 13 == 42)
rank x:N : N =
| x <= 0ff & 0
| x <= 0ffff & 1
| x <= 0ffff_ffff & 2
| 3
Fact (rank 42 == 0)
Fact (rank 0cafe == 1)
Fact (rank 0cafe_babe == 2)
Fact (rank 0cafe_babe_dead_beef == 3)
at x:N shift:N : N = and (shr x shift) 0ff
Fact (at 0cafe_babe_dead_beef 16 == 0ad)
for n:N f:N?0 i=0:N = i < n & (f i; for n f i+1)
_fact_sum = %0 : %N
Fact (for 10 (_ x:N = Ref.add _fact_sum x); !_fact_sum == 45)
for2 n:N f:0?N?Z x:0 i=0:N = i < n & (f x i; for2 n f x i+1)
_fact_sum2 = %0 : %N
Fact (for2 10 (_ a:N x:N = Ref.add _fact_sum2 a+x) 1; !_fact_sum2 == 55)
any f:N?B n:N i=0:N : !N = i < n & (f i & opt i | any f n i+1)
Fact (any (_ x:N : B = 5*x > 42) 10 == opt 9)
do n:N f:Z?Z = n > 0 & (f 0; do n-1 f)
_fact_do_sum = %0 : %N
Fact (do 10 (? Ref.tickz _fact_do_sum); !_fact_do_sum == 10)
add3 x:N y:N z:N : N = x + y + z
even x:N : B = x % 2 == 0
odd x:N : B = x % 2 == 1
mod2 x:N y:N : B = x % 2 == y % 2
mod2_ne x:N y:N : B = x % 2 != y % 2 # !mod2
tick x:N : N = x + 1
div0 x:N y:N : N = y & x / y
sub2 y:N x:N : N = x - y
map n:N f:N?0 : *0 = n & f n-1, map n-1 f
Fact (map 3 Fun.id == [2, 1, 0])
# https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/rand.3.html int rand(void);
lib_rand = Dl'rand' : Z? N
rand _ : N = Fun.call0 lib_rand
pick n:N : N = 0.rand % n # RAND_MAX = 2147483647 = 0x7fffffff, 31-bit
Fact (pick 13 <= 13)
hex1 x:C : N = between \a x \f & x - \a + 10 | x - \0
Fact (hex1 \d == 13)
or3 x:N y:N z:N : N = or (or x y) z
or4 x:N y:N z:N w:N : N = or (or (or x y) z) w
bytes : N ? N =
0? 1
1? 2
2? 4
3? 8
n? Fail.main2 $Fun n.N.str
max3 = 0ffff_ffff_ffff_ffff
ip x:N : S = S.fill '$n.$n.$n.$n' [N.at x 0, N.at x 8, N.at x 16, N.at x 24]
real x:N : R = Cast.bits_real x
str_size = 20 # 2^64 = 18446744073709551616
opt x:N : !N = Cast.any (x + 1)
must x:!N : N = Cast.nat x - 1