-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBin.idr
340 lines (279 loc) · 8.45 KB
/
Bin.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
module Data.Bin
import public Data.Bip
%default total
%access public export
-- Basic properties of constructors
Uninhabited (BinO = BinP _) where
uninhabited Refl impossible
Uninhabited (BinP _ = BinO) where
uninhabited Refl impossible
binPInj : BinP p = BinP q -> p = q
binPInj Refl = Refl
-- Following Coq.NArith.BinNatDef
||| Operation x -> 2*x+1
binDPO : (a : Bin) -> Bin
binDPO BinO = BinP U
binDPO (BinP a') = BinP (I a')
||| Operation x -> 2*x
binD : (a : Bin) -> Bin
binD BinO = BinO
binD (BinP a') = BinP (O a')
||| Successor
binSucc : (a : Bin) -> Bin
binSucc BinO = BinP U
binSucc (BinP a') = BinP (bipSucc a')
||| Predecessor
binPred : (a : Bin) -> Bin
binPred BinO = BinO
binPred (BinP a') = bipPredBin a'
||| The successor of Bin seen as a Bip
binSuccBip : (a : Bin) -> Bip
binSuccBip BinO = U
binSuccBip (BinP a') = bipSucc a'
||| Addition
binPlus : (a, b : Bin) -> Bin
-- TODO can't we just have `binPlus BinO b = b` ?
binPlus BinO BinO = BinO
binPlus BinO (BinP b') = BinP b'
binPlus (BinP a') BinO = BinP a'
binPlus (BinP a') (BinP b') = BinP (bipPlus a' b')
bimToBin : (a : Bim) -> Bin
bimToBin (BimP a') = BinP a'
bimToBin _ = BinO
||| Subtraction
binMinus : (a, b : Bin) -> Bin
-- TODO here we could have `binMinus BinO _ = BinO` ?
binMinus BinO BinO = BinO
binMinus BinO (BinP _ ) = BinO
binMinus (BinP a') BinO = BinP a'
binMinus (BinP a') (BinP b') = bimToBin (bimMinus a' b')
||| Multiplication
binMult : (a, b : Bin) -> Bin
-- TODO here we could have `binMult BinO _ = BinO` ?
binMult BinO BinO = BinO
binMult BinO (BinP _ ) = BinO
binMult (BinP _ ) BinO = BinO
binMult (BinP a') (BinP b') = BinP (bipMult a' b')
||| Order
binCompare : (a, b : Bin) -> Ordering
binCompare BinO BinO = EQ
binCompare BinO (BinP _) = LT
binCompare (BinP _) BinO = GT
binCompare (BinP a) (BinP b) = bipCompare a b EQ
-- Boolean equality and comparison
-- Implemented below in Ord
-- Helper for binMin and binMax, to work around #4001
binMinMaxHelp : (a, b : Bin) -> Ordering -> Bin
binMinMaxHelp _ b GT = b
binMinMaxHelp a _ _ = a
||| Min
binMin : (a, b : Bin) -> Bin
binMin a b = binMinMaxHelp a b $ binCompare a b
||| Max
binMax : (a, b : Bin) -> Bin
binMax a b = binMinMaxHelp b a $ binCompare a b
||| Dividing by 2
binDivTwo : (a : Bin) -> Bin
binDivTwo BinO = BinO
binDivTwo (BinP U) = BinO
binDivTwo (BinP (O a')) = BinP a'
binDivTwo (BinP (I a')) = BinP a'
||| Even parity
binEven : (a : Bin) -> Bool
binEven BinO = True
binEven (BinP (O _)) = True
binEven _ = False
||| Odd parity
binOdd : (a : Bin) -> Bool
binOdd = not . binEven
||| Power
binPow : (a, b : Bin) -> Bin
binPow _ BinO = BinP U
binPow BinO _ = BinO
binPow (BinP a') (BinP b') = BinP (bipPow a' b')
||| Square
binSquare : (a : Bin) -> Bin
binSquare BinO = BinO
binSquare (BinP a') = BinP (bipSquare a')
||| Base-2 logarithm
binLogTwo : (a : Bin) -> Bin
binLogTwo BinO = BinO
binLogTwo (BinP U) = BinO
binLogTwo (BinP (O a')) = BinP (bipDigits a')
binLogTwo (BinP (I a')) = BinP (bipDigits a')
||| Digits in number
binDigits : (a : Bin) -> Bin
binDigits BinO = BinO
binDigits (BinP a') = BinP (bipDigits a')
||| Digits in number, as Nat
binDigitsNat : (a : Bin) -> Nat
binDigitsNat BinO = Z
binDigitsNat (BinP a') = bipDigitsNat a'
-- Helper for bipDivEuclid, to work around #4001
bipDivEuclidHelp : (q, r, b : Bin) -> (o : Ordering) -> (Bin, Bin)
bipDivEuclidHelp q r b LT = (binDPO q, binMinus r b)
bipDivEuclidHelp q r b EQ = (binDPO q, binMinus r b)
bipDivEuclidHelp q r _ GT = (binD q, r)
||| Euclidean division on Bip and Bin
bipDivEuclid : (a : Bip) -> (b : Bin) -> (Bin, Bin)
bipDivEuclid U (BinP U) = (BinP U, BinO)
bipDivEuclid U _ = (BinO, BinP U)
bipDivEuclid (O a') b =
let qr = bipDivEuclid a' b
q = fst qr
r = snd qr
r' = binD r in
bipDivEuclidHelp q r' b (binCompare b r')
bipDivEuclid (I a') b =
let qr = bipDivEuclid a' b
q = fst qr
r = snd qr
r' = binDPO r in
bipDivEuclidHelp q r' b (binCompare b r')
||| Euclidean division into remainder and modulo
binDivEuclid : (a, b : Bin) -> (Bin, Bin)
binDivEuclid BinO _ = (BinO, BinO)
binDivEuclid a BinO = (BinO, a)
binDivEuclid (BinP a') b = bipDivEuclid a' b
||| Euclidean division
binDiv : (a, b : Bin) -> Bin
binDiv a b = fst (binDivEuclid a b)
||| Euclidean modulo
binModulo : (a, b : Bin) -> Bin
binModulo a b = snd (binDivEuclid a b)
||| GCD
binGCD : (a, b: Bin) -> Bin
binGCD BinO b = b
binGCD a BinO = a
binGCD (BinP a') (BinP b') = BinP (bipGCD a' b')
||| Generalised GCD
binGGCD : (a, b: Bin) -> (Bin, (Bin, Bin))
binGGCD BinO b = (b, (BinO, BinP U))
binGGCD a BinO = (a, (BinP U, BinO))
binGGCD (BinP a') (BinP b') =
let gaabb = bipGGCD a' b'
g = fst gaabb
aa = fst $ snd gaabb
bb = snd $ snd gaabb
in
(BinP g, (BinP aa, BinP bb))
-- Helper for binSqrtRem, to work around #4001
bipSqrtRemHelp : Bip -> Bim -> (Bin, Bin)
bipSqrtRemHelp s (BimP r) = (BinP s, BinP r)
bipSqrtRemHelp s _ = (BinP s, BinO)
||| Square root with remainder
binSqrtRem : (a : Bin) -> (Bin, Bin)
binSqrtRem BinO = (BinO, BinO)
binSqrtRem (BinP a') = let qr = bipSqrtRem a' in
bipSqrtRemHelp (fst qr) (snd qr)
||| Square root
binSqrt : (a : Bin) -> Bin
binSqrt BinO = BinO
binSqrt (BinP a') = BinP (bipSqrt a')
||| Logical OR
binOr : (a, b : Bin) -> Bin
binOr BinO b = b
binOr a BinO = a
binOr (BinP a') (BinP b') = BinP (bipOr a' b')
||| Logical AND
binAnd : (a, b : Bin) -> Bin
binAnd BinO _ = BinO
binAnd _ BinO = BinO
binAnd (BinP a') (BinP b') = bipAnd a' b'
||| Logical DIFF
binDiff : (a, b : Bin) -> Bin
binDiff BinO _ = BinO
binDiff a BinO = a
binDiff (BinP a') (BinP b') = bipDiff a' b'
||| Logical XOR
binXor : (a, b : Bin) -> Bin
binXor BinO b = b
binXor a BinO = a
binXor (BinP a') (BinP b') = bipXor a' b'
||| Shift left
binShiftL : (a, b : Bin) -> Bin
binShiftL BinO _ = BinO
binShiftL (BinP a') b = BinP (bipShiftL a' b)
||| Shift right
binShiftR : (a, b : Bin) -> Bin
binShiftR a BinO = a
binShiftR a (BinP b') = bipIter binDivTwo a b'
||| Checking whether a bit is set
binTestBitNat : (a : Bin) -> (b : Nat) -> Bool
binTestBitNat BinO _ = False
binTestBitNat (BinP a') b = bipTestBitNat a' b
||| Checking whether a bit is set, with index in Bin
binTestBit : (a, b : Bin) -> Bool
binTestBit BinO _ = False
binTestBit (BinP a') b = bipTestBit a' b
||| Translation from Bin to Nat
toNatBin : (a : Bin) -> Nat
toNatBin BinO = Z
toNatBin (BinP a') = toNatBip a'
||| Nat to Bin
toBinNat : (a : Nat) -> Bin
toBinNat Z = BinO
toBinNat (S a') = BinP (toBipNatSucc a')
-- Seems to be reversed from bipIter for no reason
||| Iteration of a function
binIter : {ty : Type} -> (f : ty -> ty) -> (a : Bin) -> (b : ty) -> ty
binIter _ BinO b = b
binIter f (BinP a') b = bipIter f b a'
-- Idris specific
fromIntegerBin : Integer -> Bin
fromIntegerBin 0 = BinO
fromIntegerBin n =
if n > 1
then BinP (fromIntegerBip n)
else BinP U
Eq Bin where
BinO == BinO = True
BinO == (BinP _) = False
(BinP _) == BinO = False
(BinP a) == (BinP b) = a == b
Cast Bin Nat where
cast = toNatBin
Cast Bin Integer where
cast = (cast {to=Integer}) . toNatBin
-- TODO uncomment and fix proofs
Ord Bin where
compare = binCompare
--min = binMin
--max = binMax
Num Bin where
(+) = binPlus
(*) = binMult
fromInteger = fromIntegerBin
-- negate doesn't make much sense here, but it's syntactically convenient
Neg Bin where
negate = const BinO
(-) = binMinus
Integral Bin where
div = binDiv
mod = binModulo
DecEq Bin where
decEq BinO BinO = Yes Refl
decEq BinO (BinP _) = No absurd
decEq (BinP _) BinO = No absurd
decEq (BinP a) (BinP b) = case decEq a b of
Yes prf => Yes $ cong prf
No contra => No $ contra . binPInj
-- TODO: Where does this come from?
||| Modulo
binMod : (a, b : Bip) -> Bin
binMod U b =
if (O U) <= b
then BinP U
else BinO
binMod (O a') b =
let r = binMod a' b
r' = binD r in
if r' < (BinP b)
then r'
else binMinus r' (BinP b)
binMod (I a') b =
let r = binMod a' b
r' = binSucc (binD r) in
if r' < (BinP b)
then r'
else binMinus r' (BinP b)