-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathword8.lem
289 lines (212 loc) · 8.3 KB
/
word8.lem
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
(*
Copyright 2016 Sami Mäkelä
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
open import Pervasives
open import Word
type word8 = W8 of integer
declare isabelle target_rep type word8 = `word` 8
(* perhaps should truncate here? *)
val bs_to_w8 : bitSequence -> word8
let bs_to_w8 seq = W8 (integerFromBitSeq seq)
declare isabelle target_rep function bs_to_w8 = (fun w -> `word_of_int` (integerFromBitSeq w))
val w8_to_bs : word8 -> bitSequence
let w8_to_bs (W8 i) = bitSeqFromInteger (Just 8) i
val base : integer
let base = 2**8
val word8BinTest : forall 'a. (integer -> integer -> 'a) -> word8 -> word8 -> 'a
let word8BinTest binop (W8 w1) (W8 w2) = binop (w1 mod base) (w2 mod base)
val word8BBinTest : forall 'a. (bitSequence -> bitSequence -> 'a) -> word8 -> word8 -> 'a
let word8BBinTest binop w1 w2 = binop (w8_to_bs w1) (w8_to_bs w2)
val word8BinOp : (integer -> integer -> integer) -> word8 -> word8 -> word8
let word8BinOp binop (W8 w1) (W8 w2) = W8 (binop (w1 mod base) (w2 mod base) mod base)
val word8BBinOp : (bitSequence -> bitSequence -> bitSequence) -> word8 -> word8 -> word8
let word8BBinOp binop w1 w2 = bs_to_w8 (binop (w8_to_bs w1) (w8_to_bs w2))
val word8NatOp : (integer -> nat -> integer) -> word8 -> nat -> word8
let word8NatOp binop (W8 w1) n = W8 (binop (w1 mod base) n mod base)
val word8BNatOp : (bitSequence -> nat -> bitSequence) -> word8 -> nat -> word8
let word8BNatOp binop w1 n = bs_to_w8 (binop (w8_to_bs w1) n)
val word8BUnaryOp : (bitSequence -> bitSequence) -> word8 -> word8
let word8BUnaryOp op w = bs_to_w8 (op (w8_to_bs w))
val word8UnaryOp : (integer -> integer) -> word8 -> word8
let word8UnaryOp op (W8 w) = W8 (op w mod base)
val word8ToNat : word8 -> nat
let word8ToNat w = natFromInteger (integerFromBitSeq (w8_to_bs w))
declare isabelle target_rep function word8ToNat = `unat`
val word8ToInt : word8 -> int
let word8ToInt w = intFromInteger (integerFromBitSeq (w8_to_bs w))
declare isabelle target_rep function word8ToInt = `sint`
declare isabelle target_rep function w8_to_bs = (fun w -> bitSeqFromInteger (Just 8) (integerFromInt (word8ToInt w)))
val word8FromInteger : integer -> word8
let word8FromInteger i = W8 (i mod base)
declare isabelle target_rep function word8FromInteger = (fun i -> `word_of_int` (intFromInteger i))
val word8FromInt : int -> word8
let word8FromInt i = W8 (integerFromInt i mod base)
declare isabelle target_rep function word8FromInt = `word_of_int`
val word8FromNat : nat -> word8
let word8FromNat i = word8FromInteger (integerFromNat i)
val word8FromBoollist : list bool -> word8
let word8FromBoollist lst = match bitSeqFromBoolList lst with
| Nothing -> bs_to_w8 0
| Just a -> bs_to_w8 a
end
declare isabelle target_rep function word8FromBoollist = `of_bl`
val boolListFromWord8 : word8 -> list bool
let boolListFromWord8 w = boolListFrombitSeq 8 (w8_to_bs w)
declare isabelle target_rep function boolListFromWord8 = `to_bl`
val word8FromNumeral : numeral -> word8
let word8FromNumeral w = W8 (integerFromNumeral w mod base)
instance (Numeral word8)
let fromNumeral = word8FromNumeral
end
val w8Eq : word8 -> word8 -> bool
let w8Eq = unsafe_structural_equality
val w8Less : word8 -> word8 -> bool
let w8Less bs1 bs2 = word8BinTest (<) bs1 bs2
val w8LessEqual : word8 -> word8 -> bool
let w8LessEqual bs1 bs2 = word8BinTest (<=) bs1 bs2
val w8Greater : word8 -> word8 -> bool
let w8Greater bs1 bs2 = word8BinTest (>) bs1 bs2
val w8GreaterEqual : word8 -> word8 -> bool
let w8GreaterEqual bs1 bs2 = word8BinTest (>=) bs1 bs2
val w8Compare : word8 -> word8 -> ordering
let w8Compare bs1 bs2 = word8BinTest compare bs1 bs2
instance (Ord word8)
let compare = w8Compare
let (<) = w8Less
let (<=) = w8LessEqual
let (>) = w8Greater
let (>=) = w8GreaterEqual
end
instance (SetType word8)
let setElemCompare = w8Compare
end
val word8Negate : word8 -> word8
let word8Negate = word8UnaryOp ~
val word8Succ : word8 -> word8
let word8Succ = word8UnaryOp succ
val word8Pred : word8 -> word8
let word8Pred = word8UnaryOp pred
val word8Lnot : word8 -> word8
let word8Lnot = word8UnaryOp lnot
val word8Add : word8 -> word8 -> word8
let word8Add = word8BinOp (+)
val word8Minus : word8 -> word8 -> word8
let word8Minus = word8BinOp (-)
val word8Mult : word8 -> word8 -> word8
let word8Mult = word8BinOp ( * )
val word8IntegerDivision : word8 -> word8 -> word8
let word8IntegerDivision = word8BinOp (/)
val word8Division : word8 -> word8 -> word8
let word8Division = word8BinOp (div)
val word8Remainder : word8 -> word8 -> word8
let word8Remainder = word8BinOp (mod)
val word8Land : word8 -> word8 -> word8
let word8Land = word8BinOp (land)
val word8Lor : word8 -> word8 -> word8
let word8Lor = word8BinOp (lor)
val word8Lxor : word8 -> word8 -> word8
let word8Lxor = word8BinOp (lxor)
val word8Min : word8 -> word8 -> word8
let word8Min = word8BinOp (min)
val word8Max : word8 -> word8 -> word8
let word8Max = word8BinOp (max)
val word8Power : word8 -> nat -> word8
let word8Power = word8NatOp ( ** )
val word8Asr : word8 -> nat -> word8
let word8Asr = word8NatOp (asr)
val word8Lsr : word8 -> nat -> word8
let word8Lsr = word8NatOp (lsr)
val word8Lsl : word8 -> nat -> word8
let word8Lsl = word8NatOp (lsl)
declare isabelle target_rep function word8Lnot w = (`NOT` w)
declare isabelle target_rep function word8Lor = infix `OR`
declare isabelle target_rep function word8Lxor = infix `XOR`
declare isabelle target_rep function word8Land = infix `AND`
declare isabelle target_rep function word8Lsl = infix `<<`
declare isabelle target_rep function word8Lsr = infix `>>`
declare isabelle target_rep function word8Asr = infix `>>>`
instance (NumNegate word8)
let ~ = word8Negate
end
instance (NumAdd word8)
let (+) = word8Add
end
instance (NumMinus word8)
let (-) = word8Minus
end
instance (NumSucc word8)
let succ = word8Succ
end
instance (NumPred word8)
let pred = word8Pred
end
instance (NumMult word8)
let ( * ) = word8Mult
end
instance (NumPow word8)
let ( ** ) = word8Power
end
instance ( NumIntegerDivision word8)
let (div) = word8IntegerDivision
end
instance ( NumDivision word8 )
let (/) = word8Division
end
instance (NumRemainder word8)
let (mod) = word8Remainder
end
instance (OrdMaxMin word8)
let max = word8Max
let min = word8Min
end
instance (WordNot word8)
let lnot = word8Lnot
end
instance (WordAnd word8)
let (land) = word8Land
end
instance (WordOr word8)
let (lor) = word8Lor
end
instance (WordXor word8)
let (lxor) = word8Lxor
end
instance (WordLsl word8)
let (lsl) = word8Lsl
end
instance (WordLsr word8)
let (lsr) = word8Lsr
end
instance (WordAsr word8)
let (asr) = word8Asr
end
val word8UGT : word8 -> word8 -> bool
let word8UGT a b = word8ToNat a > word8ToNat b
declare isabelle target_rep function word8UGT = infix `>`
declare isabelle target_rep function word8FromNumeral n = ((`word_of_int` n) : word8)
declare isabelle target_rep function w8Less = `word_sless`
declare isabelle target_rep function w8LessEqual = `word_sle`
declare isabelle target_rep function w8Greater x y = w8Less y x
declare isabelle target_rep function w8GreaterEqual x y = w8LessEqual y x
declare isabelle target_rep function w8Compare = genericCompare w8Less w8Eq
declare isabelle target_rep function word8Negate i = `-` i
declare isabelle target_rep function word8Add = infix `+`
declare isabelle target_rep function word8Minus = infix `-`
declare isabelle target_rep function word8Succ n = n + 1
declare isabelle target_rep function word8Pred n = n - 1
declare isabelle target_rep function word8Mult = infix `*`
declare isabelle target_rep function word8Power = infix `^`
declare isabelle target_rep function word8Division = infix `div`
declare isabelle target_rep function word8IntegerDivision = infix `div`
declare isabelle target_rep function word8Remainder = infix `mod`
declare isabelle target_rep function word8Min = `min`
declare isabelle target_rep function word8Max = `max`