-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBiz.idr
539 lines (445 loc) · 14.8 KB
/
Biz.idr
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
module Data.Biz
import public Data.Bin
%default total
%access public export
-- Basic properties of constructors
Uninhabited (BizO = BizP _) where
uninhabited Refl impossible
Uninhabited (BizP _ = BizO) where
uninhabited Refl impossible
Uninhabited (BizO = BizM _) where
uninhabited Refl impossible
Uninhabited (BizM _ = BizO) where
uninhabited Refl impossible
Uninhabited (BizM _ = BizP _) where
uninhabited Refl impossible
Uninhabited (BizP _ = BizM _) where
uninhabited Refl impossible
bizPInj : BizP p = BizP q -> p = q
bizPInj Refl = Refl
bizMInj : BizM p = BizM q -> p = q
bizMInj Refl = Refl
-- Following Coq.ZArith.BinIntDef
||| Double
bizD : (a : Biz) -> Biz
bizD BizO = BizO
bizD (BizP a') = BizP (O a')
bizD (BizM a') = BizM (O a')
||| Succ double
bizDPO : (a : Biz) -> Biz
bizDPO BizO = BizP U
bizDPO (BizP a') = BizP (I a')
bizDPO (BizM a') = BizM (bipDMO a')
||| Pred double
bizDMO : (a : Biz) -> Biz
bizDMO BizO = BizM U
bizDMO (BizM a') = BizM (I a')
bizDMO (BizP a') = BizP (bipDMO a')
-- TODO: Does "bipMinuzBiz" sound too much like Bip -> Biz -> Biz?
||| Subtraction of Bip into Biz
bipMinusBiz : (a, b : Bip) -> Biz
bipMinusBiz (I a') (I b') = bizD (bipMinusBiz a' b')
bipMinusBiz (I a') (O b') = bizDPO (bipMinusBiz a' b')
bipMinusBiz (I a') U = BizP (O a')
bipMinusBiz (O a') (I b') = bizDMO (bipMinusBiz a' b')
bipMinusBiz (O a') (O b') = bizD (bipMinusBiz a' b')
bipMinusBiz (O a') U = BizP (bipDMO a')
bipMinusBiz U (I b') = BizM (O b')
bipMinusBiz U (O b') = BizM (bipDMO b')
bipMinusBiz U U = BizO
||| Addition
bizPlus : (a, b : Biz) -> Biz
bizPlus BizO b = b
bizPlus a BizO = a
bizPlus (BizP a') (BizP b') = BizP (bipPlus a' b')
bizPlus (BizP a') (BizM b') = bipMinusBiz a' b'
bizPlus (BizM a') (BizP b') = bipMinusBiz b' a'
bizPlus (BizM a') (BizM b') = BizM (bipPlus a' b')
||| Opposite
bizOpp : (a : Biz) -> Biz
bizOpp BizO = BizO
bizOpp (BizP a') = BizM a'
bizOpp (BizM a') = BizP a'
||| Successor
bizSucc : (a : Biz) -> Biz
bizSucc a = bizPlus a (BizP U)
||| Predecessor
bizPred : (a : Biz) -> Biz
bizPred a = bizPlus a (BizM U)
||| Subtraction
bizMinus : (a, b : Biz) -> Biz
bizMinus a b = bizPlus a (bizOpp b)
||| Multiplication
bizMult : (a, b : Biz) -> Biz
bizMult BizO _ = BizO
bizMult _ BizO = BizO
bizMult (BizP a') (BizP b') = BizP (bipMult a' b')
bizMult (BizP a') (BizM b') = BizM (bipMult a' b')
bizMult (BizM a') (BizP b') = BizM (bipMult a' b')
bizMult (BizM a') (BizM b') = BizP (bipMult a' b')
||| Power
bizPowBip : (a : Biz) -> (b : Bip) -> Biz
bizPowBip a b = bipIter (bizMult a) (BizP U) b
bizPow : (a, b : Biz) -> Biz
bizPow a (BizP b') = bizPowBip a b'
bizPow _ BizO = BizP U
bizPow _ (BizM _) = BizO
||| 2^n
bizPow2 : (x : Biz) -> Biz
bizPow2 BizO = BizP U
bizPow2 (BizP y) = BizP $ bipIter O U y
bizPow2 (BizM _) = BizO
||| Square
bizSquare : (a : Biz) -> Biz
bizSquare BizO = BizO
bizSquare (BizP a') = BizP (bipSquare a')
bizSquare (BizM a') = BizP (bipSquare a')
||| Comparison
bizCompare : (a, b : Biz) -> Ordering
bizCompare BizO BizO = EQ
bizCompare BizO (BizP _) = LT
bizCompare BizO (BizM _) = GT
bizCompare (BizP _) BizO = GT
bizCompare (BizP a') (BizP b') = bipCompare a' b' EQ
bizCompare (BizP _) (BizM _) = GT
bizCompare (BizM _) BizO = LT
bizCompare (BizM _) (BizP _) = LT
bizCompare (BizM a') (BizM b') = bipCompare b' a' EQ
||| Sign
bizSign : (a : Biz) -> Biz
bizSign BizO = BizO
bizSign (BizP _) = BizP U
bizSign (BizM _) = BizM U
-- Boolean comparisons are implemented in Ord
-- Helper for bizMin and bizMax, to work around #4001
bizMinMaxHelp : (a, b : Biz) -> Ordering -> Biz
bizMinMaxHelp _ b LT = b
bizMinMaxHelp a _ _ = a
||| Max
bizMax : (a, b : Biz) -> Biz
bizMax a b = bizMinMaxHelp a b (bizCompare a b)
||| Min
bizMin : (a, b : Biz) -> Biz
bizMin a b = bizMinMaxHelp b a (bizCompare a b)
||| Absolute value
bizAbs : (a : Biz) -> Biz
bizAbs BizO = BizO
bizAbs (BizP a') = BizP a'
bizAbs (BizM a') = BizP a'
||| Biz to Nat via absolute
bizAbsNat : (a : Biz) -> Nat
bizAbsNat BizO = Z
bizAbsNat (BizP a') = toNatBip a'
bizAbsNat (BizM a') = toNatBip a'
||| Biz to Bin via absolute
bizAbsBin : (a : Biz) -> Bin
bizAbsBin BizO = BinO
bizAbsBin (BizP a') = BinP a'
bizAbsBin (BizM a') = BinP a'
||| Biz to Nat, rounding negative numbers to zero
toNatBiz : (a : Biz) -> Nat
toNatBiz (BizP a') = toNatBip a'
toNatBiz _ = Z
||| Biz to Bin, rounding negative numbers to zero
toBinBiz : (a : Biz) -> Bin
toBinBiz (BizP a') = BinP a'
toBinBiz _ = BinO
||| Nat to Biz
toBizNat : (n : Nat) -> Biz
toBizNat Z = BizO
toBizNat (S n') = BizP (toBipNatSucc n')
||| Bin to Biz
toBizBin : (a : Bin) -> Biz
toBizBin BinO = BizO
toBizBin (BinP a') = BizP a'
||| Biz to Bip, rounding non-positive numbers to one
toBipBiz : (a: Biz) -> Bip
toBipBiz (BizP a') = a'
toBipBiz _ = U
||| Iteration
bizIter : (f : ty -> ty) -> (a : Biz) -> (b : ty) -> ty
bizIter f (BizP a') b = bipIter f b a'
bizIter _ _ b = b
||| Euclidean division on Biz and Bin
-- Helper for bipzDivEuclid, to work around #4001
bipzDivEuclidHelp : (q,r,s : Biz) -> (o : Ordering) -> (Biz, Biz)
bipzDivEuclidHelp q r _ LT = (bizD q, r)
bipzDivEuclidHelp q r s EQ = (bizDPO q, bizMinus r s)
bipzDivEuclidHelp q r s GT = (bizDPO q, bizMinus r s)
bipzDivEuclid : (a : Bip) -> (b : Biz) -> (Biz, Biz)
bipzDivEuclid U b =
case bizCompare (BizP (O U)) b of
LT => (BizO, BizP U)
EQ => (BizO, BizP U)
GT => (BizP U, BizO)
bipzDivEuclid (O a') b =
let qr = bipzDivEuclid a' b
r' = bizD $ snd qr in
bipzDivEuclidHelp (fst qr) r' b (bizCompare r' b)
bipzDivEuclid (I a') b =
let qr = bipzDivEuclid a' b
r' = bizDPO $ snd qr in
bipzDivEuclidHelp (fst qr) r' b (bizCompare r' b)
||| Euclidean division into remainder and modulo
-- Helpers for bizDivEuclid, to work around #4001
bizDivEuclidHelp1 : (q, r, s : Biz) -> (Biz, Biz)
bizDivEuclidHelp1 q BizO _ = (bizOpp q, BizO)
bizDivEuclidHelp1 q r s = (bizOpp (bizSucc q), bizMinus s r)
bizDivEuclidHelp2 : (q, r, s : Biz) -> (Biz, Biz)
bizDivEuclidHelp2 q BizO _ = (bizOpp q, BizO)
bizDivEuclidHelp2 q r s = (bizOpp (bizSucc q), bizMinus r s)
bizDivEuclid : (a, b : Biz) -> (Biz, Biz)
bizDivEuclid BizO _ = (BizO, BizO)
bizDivEuclid _ BizO = (BizO, BizO)
bizDivEuclid (BizP a') (BizP b') = bipzDivEuclid a' (BizP b')
bizDivEuclid (BizM a') (BizP b') =
let qr = bipzDivEuclid a' (BizP b') in
bizDivEuclidHelp1 (fst qr) (snd qr) (BizP b')
bizDivEuclid (BizM a') (BizM b') =
let qr = bipzDivEuclid a' (BizP b') in
(fst qr, bizOpp $ snd qr)
bizDivEuclid (BizP a') (BizM b') =
let qr = bipzDivEuclid a' (BizP b') in
bizDivEuclidHelp2 (fst qr) (snd qr) (BizP b')
||| Division
bizDiv : (a, b : Biz) -> Biz
bizDiv a b = fst $ bizDivEuclid a b
||| Modulo
bizMod : (a, b : Biz) -> Biz
bizMod a b = snd $ bizDivEuclid a b
||| Truncated towards zero Euclidean division
bizQuotRem : (a, b : Biz) -> (Biz, Biz)
bizQuotRem BizO _ = (BizO, BizO)
bizQuotRem a BizO = (BizO, a)
bizQuotRem (BizP a') (BizP b') =
let qr = bipDivEuclid a' (BinP b') in
(toBizBin $ fst qr, toBizBin $ snd qr)
bizQuotRem (BizM a') (BizP b') =
let qr = bipDivEuclid a' (BinP b') in
(bizOpp $ toBizBin $ fst qr, bizOpp $ toBizBin $ snd qr)
bizQuotRem (BizP a') (BizM b') =
let qr = bipDivEuclid a' (BinP b') in
(bizOpp $ toBizBin $ fst qr, toBizBin $ snd qr)
bizQuotRem (BizM a') (BizM b') =
let qr = bipDivEuclid a' (BinP b') in
(toBizBin $ fst qr, bizOpp $ toBizBin $ snd qr)
||| TTZ Euclidean division
bizQuot : (a, b : Biz) -> Biz
bizQuot a b = fst $ bizQuotRem a b
||| TTZ Euclidean remainder
bizRem : (a, b : Biz) -> Biz
bizRem a b = snd $ bizQuotRem a b
||| Even parity
bizEven : (a : Biz) -> Bool
bizEven BizO = True
bizEven (BizP (O _)) = True
bizEven (BizM (O _)) = True
bizEven _ = False
||| Odd parity
bizOdd : (a : Biz) -> Bool
bizOdd BizO = False
bizOdd (BizP (O _)) = False
bizOdd (BizM (O _)) = False
bizOdd _ = True
||| Division by two
bizDivTwo : (a : Biz) -> Biz
bizDivTwo BizO = BizO
bizDivTwo (BizP U) = BizO
bizDivTwo (BizP a') = BizP (bipDivTwo a')
bizDivTwo (BizM a') = BizM (bipDivTwoCeil a')
||| Quot by two
bizQuotTwo : (a : Biz) -> Biz
bizQuotTwo BizO = BizO
bizQuotTwo (BizP U) = BizO
bizQuotTwo (BizP a') = BizP (bipDivTwo a')
bizQuotTwo (BizM U) = BizO
bizQuotTwo (BizM a') = BizM (bipDivTwo a')
-- TODO: Call the others "log2" too?
-- TODO: Rename -Two functions -2?
||| Log2
bizLog2 : (a : Biz) -> Biz
bizLog2 (BizP (I a')) = BizP (bipDigits a')
bizLog2 (BizP (O a')) = BizP (bipDigits a')
bizLog2 _ = BizO
||| Square root with remainder
-- Helper for bizSqrtRem, to work around #4001
bizSqrtRemHelp : Bip -> Bim -> (Biz, Biz)
bizSqrtRemHelp s (BimP r) = (BizP s, BizP r)
bizSqrtRemHelp s _ = (BizP s, BizO)
bizSqrtRem : (a : Biz) -> (Biz, Biz)
bizSqrtRem BizO = (BizO, BizO)
bizSqrtRem (BizP a') = let sr = bipSqrtRem a' in bizSqrtRemHelp (fst sr) (snd sr)
bizSqrtRem (BizM _) = (BizO, BizO)
||| Square root
bizSqrt : (a : Biz) -> Biz
bizSqrt (BizP a') = BizP (bipSqrt a')
bizSqrt _ = BizO
||| GCD
bizGCD : (a, b : Biz) -> Biz
bizGCD BizO b = bizAbs b
bizGCD a BizO = bizAbs a
bizGCD (BizP a') (BizP b') = BizP (bipGCD a' b')
bizGCD (BizP a') (BizM b') = BizP (bipGCD a' b')
bizGCD (BizM a') (BizP b') = BizP (bipGCD a' b')
bizGCD (BizM a') (BizM b') = BizP (bipGCD a' b')
||| Generalised GCD
bizGGCD : (a, b : Biz) -> (Biz, (Biz, Biz))
bizGGCD BizO b = (bizAbs b, (BizO, bizSign b))
bizGGCD a BizO = (bizAbs a, (bizSign a, BizO))
bizGGCD (BizP a') (BizP b') =
let gaabb = bipGGCD a' b'
g = fst gaabb
aa = fst $ snd gaabb
bb = snd $ snd gaabb in
(BizP g, (BizP aa, BizP bb))
bizGGCD (BizP a') (BizM b') =
let gaabb = bipGGCD a' b'
g = fst gaabb
aa = fst $ snd gaabb
bb = snd $ snd gaabb in
(BizP g, (BizP aa, BizM bb))
bizGGCD (BizM a') (BizP b') =
let gaabb = bipGGCD a' b'
g = fst gaabb
aa = fst $ snd gaabb
bb = snd $ snd gaabb in
(BizP g, (BizM aa, BizP bb))
bizGGCD (BizM a') (BizM b') =
let gaabb = bipGGCD a' b'
g = fst gaabb
aa = fst $ snd gaabb
bb = snd $ snd gaabb in
(BizP g, (BizM aa, BizM bb))
-- TODO: Should be a Biz -> Bin -> Biz version of this
||| Test bit
bizTestBit : (a, b : Biz) -> Bool
bizTestBit a BizO = bizOdd a
bizTestBit BizO (BizP _ ) = False
bizTestBit (BizP a') (BizP b') = bipTestBit a' (BinP b')
bizTestBit (BizM a') (BizP b') = not (binTestBit (bipPredBin a') (BinP b'))
bizTestBit _ (BizM _ ) = False
||| Shift left
bizShiftL : (a, b : Biz) -> Biz
bizShiftL a BizO = a
bizShiftL a (BizP b') = bipIter (bizMult (BizP (O U))) a b' -- TODO should this be just `bizD`?
bizShiftL a (BizM b') = bipIter bizDivTwo a b'
||| Shift right
bizShiftR : (a, b : Biz) -> Biz
bizShiftR a b = bizShiftL a (bizOpp b)
bizShiftin : (b : Bool) -> (x : Biz) -> Biz
bizShiftin True x = bizDPO x
bizShiftin False x = bizD x
bizZeroExt : (n, x : Biz) -> Biz
bizZeroExt n = bizIter (\rec, x => bizShiftin (bizOdd x) (rec (bizDivTwo x))) n (\_ => BizO)
bizSignExt : (n, x : Biz) -> Biz
bizSignExt n = bizIter (\rec, x => bizShiftin (bizOdd x) (rec (bizDivTwo x))) (bizPred n) (\x => if bizOdd x then BizM U else BizO)
||| Logical OR
bizOr : (a, b : Biz) -> Biz
bizOr BizO b = b
bizOr a BizO = a
bizOr (BizP a') (BizP b') = BizP (bipOr a' b')
bizOr (BizM a') (BizP b') =
BizM (binSuccBip (binDiff (bipPredBin a') (BinP b')))
bizOr (BizP a') (BizM b') =
BizM (binSuccBip (binDiff (bipPredBin b') (BinP a')))
bizOr (BizM a') (BizM b') =
BizM (binSuccBip (binAnd (bipPredBin a') (bipPredBin b')))
||| Logical AND
bizAnd : (a, b : Biz) -> Biz
bizAnd BizO _ = BizO
bizAnd _ BizO = BizO
bizAnd (BizP a') (BizP b') = toBizBin (bipAnd a' b')
bizAnd (BizM a') (BizP b') = toBizBin (binDiff (BinP b') (bipPredBin a'))
bizAnd (BizP a') (BizM b') = toBizBin (binDiff (BinP a') (bipPredBin b'))
bizAnd (BizM a') (BizM b') =
BizM (binSuccBip (binOr (bipPredBin a') (bipPredBin b')))
||| Logical DIFF
bizDiff : (a, b : Biz) -> Biz
bizDiff BizO _ = BizO
bizDiff a BizO = a
bizDiff (BizP a') (BizP b') = toBizBin (bipDiff a' b')
bizDiff (BizM a') (BizP b') =
BizM (binSuccBip (binOr (bipPredBin a') (BinP b')))
bizDiff (BizP a') (BizM b') = toBizBin (binAnd (BinP a') (bipPredBin b'))
bizDiff (BizM a') (BizM b') = toBizBin (binDiff (bipPredBin b') (bipPredBin a'))
||| Logical OR
bizXor : (a, b : Biz) -> Biz
bizXor BizO b = b
bizXor a BizO = a
bizXor (BizP a') (BizP b') = toBizBin (bipXor a' b')
bizXor (BizM a') (BizP b') =
BizM (binSuccBip (binXor (bipPredBin a') (BinP b')))
bizXor (BizP a') (BizM b') =
BizM (binSuccBip (binXor (BinP a') (bipPredBin b')))
bizXor (BizM a') (BizM b') =
toBizBin (binXor (bipPredBin a') (bipPredBin b'))
bizDigits : (x : Biz) -> Biz
bizDigits (BizP p) = BizP (bipDigits p)
bizDigits _ = BizO
-- Idris specific
fromIntegerBiz : Integer -> Biz
fromIntegerBiz 0 = BizO
fromIntegerBiz n =
if n > 0
then BizP (fromIntegerBip n)
else BizM (fromIntegerBip (-n))
-- TODO something faster
toIntegerBiz : Biz -> Integer
toIntegerBiz BizO = 0
toIntegerBiz (BizP a) = cast {to=Integer} $ toNatBip a
toIntegerBiz (BizM a) = -(cast {to=Integer} $ toNatBip a)
Eq Biz where
BizO == BizO = True
BizO == (BizP _) = False
BizO == (BizM _) = False
(BizP _) == BizO = False
(BizM _) == BizO = False
(BizM _) == (BizP _) = False
(BizP _) == (BizM _) = False
(BizP a) == (BizP b) = a == b
(BizM a) == (BizM b) = a == b
Cast Nat Biz where
cast = toBizNat
Cast Biz Nat where
cast = toNatBiz
-- TODO something better
Cast Biz Int where
cast = (cast {to=Int}) . (cast {to=String}) . toIntegerBiz
Cast Biz Integer where
cast = toIntegerBiz
Cast Biz Bip where
cast = toBipBiz
Cast Biz Bin where
cast = toBinBiz
-- Cast Bip Biz where
-- cast = ?
Cast Bin Biz where
cast = toBizBin
Ord Biz where
compare = bizCompare
max = bizMax
min = bizMin
Num Biz where
(+) = bizPlus
(*) = bizMult
fromInteger = fromIntegerBiz
Neg Biz where
negate = bizOpp
(-) = bizMinus
Abs Biz where
abs = bizAbs
-- TODO Integral instance? which div+mod to use?
DecEq Biz where
decEq BizO BizO = Yes Refl
decEq BizO (BizP _) = No uninhabited
decEq BizO (BizM _) = No uninhabited
decEq (BizP _) BizO = No uninhabited
decEq (BizP a) (BizP b) = case decEq a b of
Yes prf => Yes $ cong prf
No contra => No $ contra . bizPInj
decEq (BizP _) (BizM _) = No uninhabited
decEq (BizM _) BizO = No uninhabited
decEq (BizM _) (BizP _) = No uninhabited
decEq (BizM a) (BizM b) = case decEq a b of
Yes prf => Yes $ cong prf
No contra => No $ contra . bizMInj