-
Notifications
You must be signed in to change notification settings - Fork 5
/
Word.fm
277 lines (239 loc) · 8.99 KB
/
Word.fm
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// TODO: update style
T Word ~ (size: Nat)
| Word.nil ~ (Nat.zero);
| Word.0<size: Nat>(pred: Word(size)) ~ (Nat.succ(size));
| Word.1<size: Nat>(pred: Word(size)) ~ (Nat.succ(size));
// TODO: improve syntax
Word.add.aux: <size: Nat> -> Word(size) -> Word(size) -> Bool -> Word(size)
<size> (a) (b) (c)
( a<(a.size) () Word(a.size) -> Word(a.size)>
| (b) Word.nil;
| <a.size> (a.pred) (b)
( b<(b.size) () Word(Nat.pred(b.size)) -> Word(b.size)>
| (a.pred)
Word.nil;
| <b.size> (b.pred) (a.pred)
c<() Unit -> Word(Nat.succ(b.size))>
| () Word.1<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.false)); // a=0 b=0 c=0
| () Word.0<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.false));
| Unit.new;; // a=0 b=0 c=1
| <b.size> (b.pred) (a.pred)
c<() Unit -> Word(Nat.succ(b.size))>
| () Word.0<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.true)); // a=0 b=1 c=0
| () Word.1<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.false)); // a=0 b=1 c=1
| Unit.new;
;)(a.pred);
| <a.size> (a.pred) (b)
( b<(b.size) () Word(Nat.pred(b.size)) -> Word(b.size)>
| (a.pred) Word.nil;
| <b.size> (b.pred) (a.pred)
c<() Unit -> Word(Nat.succ(b.size))>
| () Word.0<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.true)); // a=1 b=0 c=0
| () Word.1<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.false));
| Unit.new;; // a=1 b=0 c=1
| <b.size> (b.pred) (a.pred)
c<() Unit -> Word(Nat.succ(b.size))>
| () Word.1<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.true)); // a=1 b=1 c=0
| () Word.0<b.size>(Word.add.aux<b.size>(a.pred)(b.pred)(Bool.true)); // a=1 b=1 c=1
| Unit.new;
;)(a.pred)
;)(b)
Word.add: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
<size> (a) (b) Word.add.aux<size>(a)(b)(Bool.false)
Word.and: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.and
Word.cmp.aux: <size: Nat> -> Word(size) -> Word(size) -> Cmp -> Cmp
<size> (a) (b) (c)
( a<(a.size) () Word(a.size) -> Cmp>
| (b) c;
| <a.size> (a.pred) (b)
( b<(b.size) () Word(Nat.pred(b.size)) -> Cmp>
| (a.pred) c;
| <b.size> (b.pred) (a.pred) Word.cmp.aux<b.size>(a.pred)(b.pred)(c);
| <b.size> (b.pred) (a.pred) Word.cmp.aux<b.size>(a.pred)(b.pred)(Cmp.ltn)
; )(a.pred);
| <a.size> (a.pred) (b)
( b<(b.size) () Word(Nat.pred(b.size)) -> Cmp>
| (a.pred) c;
| <b.size> (b.pred) (a.pred) Word.cmp.aux<b.size>(a.pred)(b.pred)(Cmp.gtn);
| <b.size> (b.pred) (a.pred) Word.cmp.aux<b.size>(a.pred)(b.pred)(c)
; )(a.pred)
; )(b)
Word.cmp: <size: Nat> -> Word(size) -> Word(size) -> Cmp
<size> (a) (b)
Word.cmp.aux<size>(a)(b)(Cmp.eql)
Word.concat
: <a_size: Nat> ->
<b_size: Nat> ->
Word(a_size) ->
Word(b_size) ->
Word(Nat.add(a_size)(b_size))
<a_size> <b_size> (a) (b)
a<(a_size) () Word(Nat.add(a_size)(b_size))>
| b;
| <a.size> (a.pred)
let rest = Word.concat<a.size><b_size>(a.pred)(b)
Word.0<Nat.add(a.size)(b_size)>(rest);
| <a.size> (a.pred)
let rest = Word.concat<a.size><b_size>(a.pred)(b)
Word.1<Nat.add(a.size)(b_size)>(rest);
// TODO word division
Word.div: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.div
Word.drop: <size: Nat> -> (n: Nat) -> Word(Nat.add(n)(size)) -> Word(size)
<size> (n) (word)
n<(n) Word(Nat.add(n)(size)) -> Word(size)>
| (word) word;
| (n.pred) (word)
let word.pred = Word.pred<Nat.add(n.pred)(size)>(word)
Word.drop<size>(n.pred)(word.pred);
| word;
// a == b
Word.eql: <size:Nat> -> Word(size) -> Word(size) -> Bool
<size> (a) (b)
Word.cmp<size>(a)(b)<() Bool>(Bool.false)(Bool.true)(Bool.false)
Word.zero(size: Nat): Word(size)
case size:
| Word.nil;
| Word.0<size.pred>(Word.zero(size.pred));
: Word(size.self);
Word.from_bits: (size: Nat) -> Bits -> Word(size)
(size) (bits)
size<(size) Word(size)>
| Word.nil;
| (size.pred)
bits<() Word(Nat.succ(size.pred))>
| Word.0<size.pred>(Word.from_bits(size.pred)(Bits.nil));
| (bits.pred) Word.0<size.pred>(Word.from_bits(size.pred)(bits.pred));
| (bits.pred) Word.1<size.pred>(Word.from_bits(size.pred)(bits.pred));;
// a >= b
Word.gte: <size:Nat> -> Word(size) -> Word(size) -> Bool
<size> (a) (b)
Word.cmp<size>(a)(b)<() Bool>(Bool.false)(Bool.true)(Bool.true)
// a > b
Word.gtn: <size:Nat> -> Word(size) -> Word(size) -> Bool
<size> (a) (b)
Word.cmp<size>(a)(b)<() Bool>(Bool.false)(Bool.false)(Bool.true)
Word.inc<size: Nat>(word: Word(size)): Word(size)
case word:
| Word.nil;
| Word.1<word.size>(word.pred);
| Word.0<word.size>(Word.inc<word.size>(word.pred));
: Word(word.size);
// a <= b
Word.lte: <size:Nat> -> Word(size) -> Word(size) -> Bool
<size> (a) (b)
Word.cmp<size>(a)(b)<() Bool>(Bool.true)(Bool.true)(Bool.false)
// a < b
Word.ltn: <size:Nat> -> Word(size) -> Word(size) -> Bool
<size> (a) (b)
Word.cmp<size>(a)(b)<() Bool>(Bool.true)(Bool.false)(Bool.false)
// TODO word modulus
Word.mod: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.mod
// TODO Multiplies two words
Word.mul: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.mul
// TODO bitwise OR between two words
Word.or: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.or
Word.parse_hex: (size: Nat) -> String -> Word(size)
(size) (str)
Word.from_bits(size)(Bits.parse_hex(str))
// TODO word exponentiation
Word.pow: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.pow
Word.pred: <size: Nat> -> Word(Nat.succ(size)) -> Word(size)
<size> (word)
word<(size) () Word(Nat.pred(size))>
| Word.nil;
| <word.size> (word.pred) word.pred;
| <word.size> (word.pred) word.pred;
// TODO Bitwise-shifts a word left
Word.shift_left: <size: Nat> -> Nat -> Word(size) -> Word(size)
Word.shift_left
// TODO Bitwise-shifts a word right
Word.shift_right: <size: Nat> -> Nat -> Word(size) -> Word(size)
Word.shift_right
Word.sub: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
<size> (a) (b) Word.add<size>(a, Word.neg<size>(b))
Word.neg: <size: Nat> -> Word(size) -> Word(size)
<size> (word)
word<(size) () Word(size)>
| Word.nil;
| <size> (pred) Word.0<size>(Word.neg.aux<size>(pred, Bool.true));
| <size> (pred) Word.1<size>(Word.neg.aux<size>(pred, Bool.false));
Word.neg.aux: <size: Nat> -> Word(size) -> Bool -> Word(size)
<size> (word) (inc)
word<(size) () Word(size)>
| Word.nil;
| <size> (pred)
inc<() Word(Nat.succ(size))>
| Word.0<size>(Word.neg.aux<size>(pred, Bool.true));
| Word.1<size>(Word.neg.aux<size>(pred, Bool.false));;
| <size> (pred)
inc<() Word(Nat.succ(size))>
| Word.1<size>(Word.neg.aux<size>(pred, Bool.false));
| Word.0<size>(Word.neg.aux<size>(pred, Bool.false));;
Word.to_bits: <size: Nat> -> Word(size) -> Bits
<size> (a)
a<() () Bits>
| Bits.nil;
| <size> (pred) Bits.0(Word.to_bits<size>(pred));
| <size> (pred) Bits.1(Word.to_bits<size>(pred));
Word.to_nat: <size: Nat> -> Word(size) -> Nat
<size> (word)
Word.to_nat.go<size>(word)(1)
Word.to_nat.go: <size: Nat> -> Word(size) -> Nat -> Nat
<size> (word) (add)
word<() () Nat>
| Nat.zero;
| <size> (word.pred)
Word.to_nat.go<size>(word.pred)(Nat.mul(add)(2));
| <size> (word.pred)
Nat.add(add)(Word.to_nat.go<size>(word.pred)(Nat.mul(add)(2)));
// TODO bitwise XOR between two words
Word.xor: <size: Nat> -> Word(size) -> Word(size) -> Word(size)
Word.xor
// Converts a word to another size, removing bits or adding zeroes as needed.
Word.trim<size: Nat>(new_size: Nat, word: Word(size)): Word(new_size)
case new_size:
| Word.nil;
| case word:
| Word.0<new_size.pred>(Word.trim<Nat.zero>(new_size.pred, Word.nil));
| Word.0<new_size.pred>(Word.trim<word.size>(new_size.pred, word.pred));
| Word.1<new_size.pred>(Word.trim<word.size>(new_size.pred, word.pred));;
: Word(new_size.self);
Word.from_nat(size: Nat, n: Nat): Word(size)
Nat.apply<Word(size)>(n, Word.inc<size>, Word.zero(size))
Word.nat_log2.go<size: Nat>(word: Word(size), c: Nat, n: Nat): Nat
case word:
| n;
| Word.nat_log2.go<word.size>(word.pred, Nat.succ(c), n);
| Word.nat_log2.go<word.size>(word.pred, Nat.succ(c), c);
Word.nat_log2<size: Nat>(word: Word(size)): Nat
Word.nat_log2.go<size>(word, 0, 0)
Word.indl<P: Nat -> Type, m: Nat>
(nil : P(Nat.zero))
(w0 : <n: Nat> -> P(n) -> P(Nat.succ(n)))
(w1 : <n: Nat> -> P(n) -> P(Nat.succ(n)))
(word : Word(m))
: P(m)
case word:
| nil;
| def P = (n) P(Nat.succ(n))
def nil = w0<Nat.zero>(nil)
def w0 = <x> w0<Nat.succ(x)>
def w1 = <x> w1<Nat.succ(x)>
Word.indl<P, word.size>(nil, w0, w1, word.pred);
| def P = (n) P(Nat.succ(n))
def nil = w1<Nat.zero>(nil)
def w0 = <x> w0<Nat.succ(x)>
def w1 = <x> w1<Nat.succ(x)>
Word.indl<P, word.size>(nil, w0, w1, word.pred);
: P(word.size);
Word.reverse<size: Nat>(word: Word(size)): Word(size)
def nil = Word.nil
def w0 = <size> (rev) Word.0<size>(rev)
def w1 = <size> (rev) Word.1<size>(rev)
Word.indl<Word, size>(nil, w0, w1, word)