-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBits.fm
176 lines (157 loc) · 3.74 KB
/
Bits.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
// Bits.fm
// =======
//
// Defines a bitstream, or binary sequence.
import Equal
import Nat
import Pair
// Definition
// ----------
T Bits
| be
| b0(pred : Bits)
| b1(pred : Bits)
// Functions
// ----------
// Bitwise negation
not_bits(x: Bits) : Bits
case x
| be => be
| b0 => b1(not_bits(x.pred))
| b1 => b0(not_bits(x.pred))
: Bits
// Bitwise conjunction
and_bits(x: Bits, y: Bits) : Bits
case x
with y : Bits
| be => be
| b0 => case y
with x.pred : Bits
| be => be
| b0 => b0(and_bits(x.pred, y.pred))
| b1 => b0(and_bits(x.pred, y.pred))
| b1 => case y
with x.pred : Bits
| be => be
| b0 => b0(and_bits(x.pred, y.pred))
| b1 => b1(and_bits(x.pred, y.pred))
// Bitwise disjunction
or_bits(x: Bits, y: Bits) : Bits
case x
with y : Bits
| be => be
| b0 => case y
with x.pred : Bits
| be => be
| b0 => b0(or_bits(x.pred, y.pred))
| b1 => b1(or_bits(x.pred, y.pred))
| b1 => case y
with x.pred : Bits
| be => be
| b0 => b1(or_bits(x.pred, y.pred))
| b1 => b1(or_bits(x.pred, y.pred))
// Bitwise exclusive disjunction
xor_bits(x: Bits, y: Bits) : Bits
case x
with y : Bits
| be => be
| b0 => case y
with x.pred : Bits
| be => be
| b0 => b0(xor_bits(x.pred, y.pred))
| b1 => b1(xor_bits(x.pred, y.pred))
| b1 => case y
with x.pred : Bits
| be => be
| b0 => b1(xor_bits(x.pred, y.pred))
| b1 => b0(xor_bits(x.pred, y.pred))
// Bitwise right shift
right_shift_bits(n: Nat, x: Bits) : Bits
case n
with x : Bits
| zero => x
| succ => case x
with n.pred : Nat
| be => be
| b0 => right_shift_bits(n.pred, x.pred)
| b1 => right_shift_bits(n.pred, x.pred)
// Bitwise left shift
left_shift_bits(n: Nat, x: Bits) : Bits
case n
with x : Bits
| zero => x
| succ => case x
with n.pred : Nat
| be => be
| b0 => left_shift_bits(n.pred, b0(b0(x.pred)))
| b1 => left_shift_bits(n.pred, b0(b1(x.pred)))
// Increment
inc_bits(x: Bits) : Bits
case x
| be => b1(be)
| b0 => b1(x.pred)
| b1 => b0(inc_bits(x.pred))
// Addition
add_bits(x: Bits, y: Bits) : Bits
case x
with y : Bits
| be => be
| b0 => case y
with x.pred : Bits
| be => be
| b0 => b0(add_bits(x.pred, y.pred))
| b1 => b1(add_bits(x.pred, y.pred))
| b1 => case y
with x.pred : Bits
| be => be
| b0 => b1(add_bits(x.pred, y.pred))
| b1 => b0(inc_bits(add_bits(x.pred, y.pred)))
bits_to_nat(x: Bits) : Nat
case x
| be => zero
| b0 => double(bits_to_nat(x.pred))
| b1 => succ(double(bits_to_nat(x.pred)))
nat_to_bits(n: Nat) : Bits
case n
| zero => be
| succ => inc_bits(nat_to_bits(n.pred))
copy_bits(bs: Bits) : And(Bits, Bits)
case bs
with copy_bits : Bits -> And(Bits, Bits)
| be =>
pair(__ be, be)
| b0 => case copy_bits(bs.pred) as pred
| pair => pair(__ b0(pred.fst), b0(pred.snd))
| b1 => case copy_bits(bs.pred) as pred
| pair => pair(__ b1(pred.fst), b1(pred.snd))
equal_bits(as: Bits, bs: Bits) : Bool
case as
with bs : Bits
with equal_bits : Bits -> Bits -> Bool
| be => case bs
| be => true
| b0 => false
| b1 => false
| b0 => case bs
| be => false
| b0 => equal_bits(as.pred, bs.pred)
| b1 => false
| b1 => case bs
| be => false
| b0 => false
| b1 => equal_bits(as.pred, bs.pred)
concat_bits(as: Bits, bs: Bits) : Bits
case as
with bs : Bits
with concat_bits : Bits -> Bits -> Bits
| be => bs
| b0 => b0(concat_bits(as.pred, bs))
| b1 => b1(concat_bits(as.pred, bs))
// Theorems
// --------
double_negation_bits(x: Bits) : not_bits(not_bits(x)) == x
case x
| be => equal(__)
| b0 => apply(_____ double_negation_bits(x.pred))
| b1 => apply(_____ double_negation_bits(x.pred))
: not_bits(not_bits(x)) == x