This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
instance.cry
386 lines (287 loc) · 10.5 KB
/
instance.cry
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
////////////////////////////////////////////////////////////////////////////////
// Zero
/* instance Zero Bit */
zeroBit : Bit
zeroBit = zero
/* instance Zero Integer */
zeroInteger : Integer
zeroInteger = zero
/* instance Zero Rational */
zeroRational : Rational
zeroRational = zero
/* instance (fin n, n >= 1) => Zero (Z n) */
zeroZ : {n} (fin n, n >= 1) => Z n
zeroZ = zero
/* instance Zero [n] */
zeroWord : {n} [n]
zeroWord = zero
/* instance (Zero a) => Zero [n]a */
zeroSeq : {n, a} (Zero a) => [n]a
zeroSeq = zero
/* instance (ValidFloat e p) => Zero (Float e p) */
zeroFloat : {e, p} (ValidFloat e p) => Float e p
zeroFloat = zero
/* instance (Zero b) => Zero (a -> b) */
zeroFun : {a, b} (Zero b) => a -> b
zeroFun = zero
/* instance Zero () */
zeroUnit : ()
zeroUnit = zero
/* instance (Zero a, Zero b, ...) => Zero (a, b, ...) */
zeroTuple : {a, b} (Zero a, Zero b) => (a, b)
zeroTuple = zero
/* instance Zero {} */
zeroEmpty : {}
zeroEmpty = zero
/* instance (Zero a, Zero b, ...) => Zero { x : a, y : b, ... } */
zeroRecord : {a, b} (Zero a, Zero b) => {x : a, y : b}
zeroRecord = zero
////////////////////////////////////////////////////////////////////////////////
// Logic
/* instance Logic Bit */
logicBit : Bit -> Bit
logicBit = complement
/* instance Logic [n] */
logicWord : {n} [n] -> [n]
logicWord = complement
/* instance (Logic a) => Logic [n]a */
logicSeq : {n, a} (Logic a) => [n]a -> [n]a
logicSeq = complement
/* instance (Logic b) => Logic (a -> b) */
logicFun : {a, b} (Logic b) => (a -> b) -> (a -> b)
logicFun = complement
/* instance Logic () */
logicUnit : () -> ()
logicUnit = complement
/* instance (Logic a, Logic b, ...) => Logic (a, b, ...) */
logicTuple : {a, b} (Logic a, Logic b) => (a, b) -> (a, b)
logicTuple = complement
/* instance Logic {} */
logicEmpty : {} -> {}
logicEmpty = complement
/* instance (Logic a, Logic b, ...) => Logic { x : a, y : b, ... } */
logicRecord : {a, b} (Logic a, Logic b) => {x : a, y : b} -> {x : a, y : b}
logicRecord = complement
////////////////////////////////////////////////////////////////////////////////
// Ring
/* instance Ring Integer */
ringInteger : Integer -> Integer
ringInteger = negate
/* instance Ring Rational */
ringRational : Rational -> Rational
ringRational = negate
/* instance (fin n, n >= 1) => Ring (Z n) */
ringZ : {n} (fin n, n >= 1) => Z n -> Z n
ringZ = negate
/* instance (fin n) => Ring [n] */
ringWord : {n} (fin n) => [n] -> [n]
ringWord = negate
// NOTE: 'instance Ring a => Ring [n]a' holds for any type 'a'
// distinct from 'Bit'.
/* instance Ring [n]Integer */
ringSeqInteger : {n} [n]Integer -> [n]Integer
ringSeqInteger = negate
/* instance Ring [n]Rational */
ringSeqRational : {n} [n]Rational -> [n]Rational
ringSeqRational = negate
/* instance (fin k, k >= 1) => Ring [n](Z k) */
ringSeqZ : {n, k} (fin k, k >= 1) => [n](Z k) -> [n](Z k)
ringSeqZ = negate
/* instance (Ring [k]a) => Ring [n][k]a */
ringSeqSeq : {n, k, a} (Ring ([k]a)) => [n][k]a -> [n][k]a
ringSeqSeq = negate
/* instance (Ring b) => Ring [n](a -> b) */
ringSeqFun : {n, a, b} (Ring b) => [n](a -> b) -> [n](a -> b)
ringSeqFun = negate
/* instance Ring [n]() */
ringSeqUnit : {n} [n]() -> [n]()
ringSeqUnit = negate
/* instance (Ring a, Ring b) => Ring [n](a, b) */
ringSeqTuple : {n, a, b} (Ring a, Ring b) => [n](a, b) -> [n](a, b)
ringSeqTuple = negate
/* instance Ring [n]{} */
ringSeqEmpty : {n} [n]{} -> [n]{}
ringSeqEmpty = negate
/* instance (Ring a, Ring b) => Ring [n]{x : a, y : b} */
ringSeqRecord : {n, a, b} (Ring a, Ring b) => [n]{x : a, y : b} -> [n]{x : a, y : b}
ringSeqRecord = negate
/* instance (ValidFloat e p) => Ring (Float e p) */
ringFloat : {e, p} (ValidFloat e p) => Float e p -> Float e p
ringFloat = negate
/* instance (Ring b) => Ring (a -> b) */
ringFun : {a, b} (Ring b) => (a -> b) -> (a -> b)
ringFun = negate
/* instance Ring () */
ringUnit : () -> ()
ringUnit = negate
/* instance (Ring a, Ring b, ...) => Ring (a, b, ...) */
ringTuple : {a, b} (Ring a, Ring b) => (a, b) -> (a, b)
ringTuple = negate
/* instance Ring {} */
ringEmpty : {} -> {}
ringEmpty = negate
/* instance (Ring a, Ring b, ...) => Ring { x : a, y : b, ... } */
ringRecord : {a, b} (Ring a, Ring b) => {x : a, y : b} -> {x : a, y : b}
ringRecord = negate
////////////////////////////////////////////////////////////////////////////////
// Integral
/* instance Integral Integer */
integralInteger : Integer -> Integer -> Integer
integralInteger = (%)
/* instance (fin n) => Integral [n] */
integralWord : {n} (fin n) => [n] -> [n] -> [n]
integralWord = (%)
////////////////////////////////////////////////////////////////////////////////
// Field
/* instance Field Rational */
fieldRational : Rational -> Rational
fieldRational = recip
/* instance (ValidFloat e p) => Field (Float e p) */
fieldFloat : {e, p} (ValidFloat e p) => Float e p -> Float e p
fieldFloat = recip
/* instance (prime p) => Field (Z p) */
fieldZ : {p} prime p => Z p -> Z p
fieldZ = recip
////////////////////////////////////////////////////////////////////////////////
// Round
/* instance Round Rational */
roundRational : Rational -> Integer
roundRational = floor
/* instance (ValidFloat e p) => Round (Float e p) */
roundFloat : {e, p} (ValidFloat e p) => Float e p -> Integer
roundFloat = floor
////////////////////////////////////////////////////////////////////////////////
// Eq
/* instance Eq Bit */
eqBit : Bit -> Bit -> Bit
eqBit = (==)
/* instance Eq Integer */
eqInteger : Integer -> Integer -> Bit
eqInteger = (==)
/* instance Eq Rational */
eqRational : Rational -> Rational -> Bit
eqRational = (==)
/* instance (fin n, n >= 1) => Eq (Z n) */
eqZ : {n} (fin n, n >= 1) => Z n -> Z n -> Bit
eqZ = (==)
/* instance (fin n) => Eq [n] */
eqWord : {n} (fin n) => [n] -> [n] -> Bit
eqWord = (==)
/* instance (fin n, Eq a) => Eq [n]a */
eqSeq : {n, a} (fin n, Eq a) => [n]a -> [n]a -> Bit
eqSeq = (==)
/* instance (ValidFloat e p) => Eq (Float e p) */
eqFloat : {e, p} (ValidFloat e p) => Float e p -> Float e p -> Bit
eqFloat = (==)
/* instance Eq () */
eqUnit : () -> () -> Bit
eqUnit = (==)
/* instance (Eq a, Eq b, ...) => Eq (a, b, ...) */
eqTuple : {a, b} (Eq a, Eq b) => (a, b) -> (a, b) -> Bit
eqTuple = (==)
/* instance Eq {} */
eqEmpty : {} -> {} -> Bit
eqEmpty = (==)
/* instance (Eq a, Eq b, ...) => Eq { x : a, y : b, ... } */
eqRecord : {a, b} (Eq a, Eq b) => {x : a, y : b} -> {x : a, y : b} -> Bit
eqRecord = (==)
////////////////////////////////////////////////////////////////////////////////
// Cmp
/* instance Cmp Bit */
cmpBit : Bit -> Bit -> Bit
cmpBit = (<)
/* instance Cmp Integer */
cmpInteger : Integer -> Integer -> Bit
cmpInteger = (<)
/* instance Cmp Rational */
cmpRational : Rational -> Rational -> Bit
cmpRational = (<)
/* instance (fin n) => Cmp [n] */
cmpWord : {n} (fin n) => [n] -> [n] -> Bit
cmpWord = (<)
/* instance (fin n, Cmp a) => Cmp [n]a */
cmpSeq : {n, a} (fin n, Cmp a) => [n]a -> [n]a -> Bit
cmpSeq = (<)
/* instance (ValidFloat e p) => Cmp (Float e p) */
cmpFloat : {e, p} (ValidFloat e p) => Float e p -> Float e p -> Bit
cmpFloat = (<)
/* instance Cmp () */
cmpUnit : () -> () -> Bit
cmpUnit = (<)
/* instance (Cmp a, Cmp b, ...) => Cmp (a, b, ...) */
cmpTuple : {a, b} (Cmp a, Cmp b) => (a, b) -> (a, b) -> Bit
cmpTuple = (<)
/* instance Cmp {} */
cmpEmpty : {} -> {} -> Bit
cmpEmpty = (<)
/* instance (Cmp a, Cmp b, ...) => Cmp { x : a, y : b, ... } */
cmpRecord : {a, b} (Cmp a, Cmp b) => {x : a, y : b} -> {x : a, y : b} -> Bit
cmpRecord = (<)
////////////////////////////////////////////////////////////////////////////////
// Cmp
/* instance (fin n, n >= 1) => SignedCmp [n] */
signedCmpWord : {n} (fin n, n >= 1) => [n] -> [n] -> Bit
signedCmpWord = (<$)
// NOTE: 'instance (fin n, SignedCmp a) => SignedCmp ([n]a)' holds for
// any type 'a' distinct from 'Bit'.
/* instance (fin n, SignedCmp [k]a) => SignedCmp [n][k]a */
signedCmpSeqSeq : {n, k, a} (fin n, SignedCmp ([k]a)) => [n][k]a -> [n][k]a -> Bit
signedCmpSeqSeq = (<$)
/* instance (fin n) => SignedCmp [n]() */
signedCmpSeqUnit : {n} (fin n) => [n]() -> [n]() -> Bit
signedCmpSeqUnit = (<$)
/* instance (SignedCmp a, SignedCmp b) => SignedCmp [n](a, b) */
signedCmpSeqTuple : {n, a, b} (fin n, SignedCmp a, SignedCmp b) => [n](a, b) -> [n](a, b) -> Bit
signedCmpSeqTuple = (<$)
/* instance SignedCmp [n]{} */
signedCmpSeqEmpty : {n} (fin n) => [n]{} -> [n]{} -> Bit
signedCmpSeqEmpty = (<$)
/* instance (SignedCmp a, SignedCmp b) => SignedCmp [n]{x : a, y : b} */
signedCmpSeqRecord : {n, a, b} (fin n, SignedCmp a, SignedCmp b) => [n]{x : a, y : b} -> [n]{x : a, y : b} -> Bit
signedCmpSeqRecord = (<$)
/* instance SignedCmp () */
signedCmpUnit : () -> () -> Bit
signedCmpUnit = (<$)
/* instance (SignedCmp a, SignedCmp b, ...) => SignedCmp (a, b, ...) */
signedCmpTuple : {a, b} (SignedCmp a, SignedCmp b) => (a, b) -> (a, b) -> Bit
signedCmpTuple = (<$)
/* instance SignedCmp {} */
signedCmpEmpty : {} -> {} -> Bit
signedCmpEmpty = (<$)
/* instance (SignedCmp a, SignedCmp b, ...) => SignedCmp { x : a, y : b, ... } */
signedCmpRecord : {a, b} (SignedCmp a, SignedCmp b) => {x : a, y : b} -> {x : a, y : b} -> Bit
signedCmpRecord = (<$)
////////////////////////////////////////////////////////////////////////////////
// Literal
/* instance (1 >= val) => Literal val Bit */
literalBit : {val} (1 >= val) => Bit
literalBit = `val
/* instance (fin val) => Literal val Integer */
literalInteger : {val} (fin val) => Integer
literalInteger = `val
/* instance (fin val) => Literal val Rational */
literalRational : {val} (fin val) => Rational
literalRational = `val
/* instance (fin val, fin n, n >= 1, n > val) => Literal val (Z n) */
literalZ : {val, n} (fin val, fin n, n >= 1, n > val) => Z n
literalZ = `val
/* instance (fin val, fin n, n >= width val) => Literal val [n] */
literalWord : {val, n} (fin val, fin n, n >= width val) => [n]
literalWord = `val
////////////////////////////////////////////////////////////////////////////////
// LiteralLessThan
/* instance (2 >= val) => LiteralLessThan val Bit */
literalLessThanBit : {val} (1 >= val) => [val]Bit
literalLessThanBit = [0..<val]
/* instance LiteralLessThan val Integer */
literalLessThanInteger : {val} (fin val) => [val]Integer
literalLessThanInteger = [0..<val]
/* instance LiteralLessThan val Rational */
literalLessThanRational : {val} (fin val) => [val]Rational
literalLessThanRational = [0..<val]
/* instance (fin n, n >= 1, n >= val) => LiteralLessThan val (Z n) */
literalLessThanZ : {val, n} (fin n, n >= 1, n >= val) => [val](Z n)
literalLessThanZ = [0..<val]
/* instance (fin n, n >= width val) => LiteralLessThan val [n] */
literalLessThanWord : {val, n} (fin n, n >= lg2 val) => [val][n]
literalLessThanWord = [0..<val]