-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathTypeclassesAlt2.fst
More file actions
239 lines (202 loc) · 6.03 KB
/
TypeclassesAlt2.fst
File metadata and controls
239 lines (202 loc) · 6.03 KB
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
module TypeclassesAlt2
open FStar.Tactics.Typeclasses
class bounded_unsigned_int (a:Type) = {
bound : a;
as_nat : a -> nat;
from_nat : (x:nat { x <= as_nat bound }) -> a;
[@@@no_method]
properties : squash (
(forall (x:a). as_nat x <= as_nat bound) /\
(forall (x:a). from_nat (as_nat x) == x) /\
(forall (x:nat{ x <= as_nat bound}). as_nat (from_nat x) == x)
)
}
let fits #a {| bounded_unsigned_int a |}
(op: int -> int -> int)
(x y:a)
: prop
= 0 <= op (as_nat x) (as_nat y) /\
op (as_nat x) (as_nat y) <= as_nat #a bound
unfold
let related_ops #a {| bounded_unsigned_int a |}
(iop: int -> int -> int)
(bop: (x:a -> y:a { fits iop x y } -> a))
= forall (x y:a). fits iop x y ==> as_nat (bop x y) = as_nat x `iop` as_nat y
//SNIPPET_START: addable$
class addable_bounded_unsigned_int (a:Type) = {
[@@@no_method]
base : bounded_unsigned_int a;
add : (x:a -> y:a { fits ( + ) x y } -> a);
[@@@no_method]
properties : squash (
related_ops ( + ) add
)
}
instance addable_base {| d : addable_bounded_unsigned_int 'a |}
: bounded_unsigned_int 'a
= d.base
//SNIPPET_END: addable$
//SNIPPET_START: subtractable$
class subtractable_bounded_unsigned_int (a:Type) = {
[@@@no_method]
base : bounded_unsigned_int a;
sub : (x:a -> y:a { fits op_Subtraction x y } -> a);
[@@@no_method]
properties : squash (
related_ops op_Subtraction sub /\
(forall (x:a). fits op_Subtraction bound x)
)
}
instance subtractable_base {| d : subtractable_bounded_unsigned_int 'a |}
: bounded_unsigned_int 'a
= d.base
//SNIPPET_END: subtractable$
//SNIPPET_START: comparable$
class comparable_bounded_unsigned_int (a:Type) = {
[@@@no_method]
base : bounded_unsigned_int a;
comp : a -> a -> bool;
[@@@no_method]
properties : squash (
(forall (x y:a).{:pattern comp x y} comp x y <==> as_nat x < as_nat y)
)
}
instance comparable_base {| d : comparable_bounded_unsigned_int 'a |}
: bounded_unsigned_int 'a
= d.base
//SNIPPET_END: comparable$
//SNIPPET_START: try_sub_fail$
[@@expect_failure [19]]
let try_sub #a {| s: subtractable_bounded_unsigned_int a|}
{| c: comparable_bounded_unsigned_int a |}
(acc:a)
= bound `sub` acc
//SNIPPET_END: try_sub_fail$
//SNIPPET_START: try_sub$
let try_sub {| s: subtractable_bounded_unsigned_int 'a |}
{| c: comparable_bounded_unsigned_int 'a |}
(acc:'a { s.base == c.base } )
= bound `sub` acc
//SNIPPET_END: try_sub$
instance u32_instance_base : bounded_unsigned_int FStar.UInt32.t =
let open FStar.UInt32 in
{
bound = 0xfffffffful;
as_nat = v;
from_nat = uint_to_t;
properties = ()
}
instance u32_instance_add : addable_bounded_unsigned_int FStar.UInt32.t =
let open FStar.UInt32 in
{
base = u32_instance_base;
add = (fun x y -> add x y);
properties = ()
}
instance u32_instance_sub : subtractable_bounded_unsigned_int FStar.UInt32.t =
let open FStar.UInt32 in
{
base = u32_instance_base;
sub = (fun x y -> sub x y);
properties = ()
}
instance u32_instance_cmp : comparable_bounded_unsigned_int FStar.UInt32.t =
let open FStar.UInt32 in
{
base = u32_instance_base;
comp = (fun x y -> x <^ y);
properties = ()
}
instance u64_instance_base : bounded_unsigned_int FStar.UInt64.t =
let open FStar.UInt64 in
{
bound = 0xffffffffffffffffuL;
as_nat = v;
from_nat = uint_to_t;
properties = ()
}
instance u64_instance_add : addable_bounded_unsigned_int FStar.UInt64.t =
let open FStar.UInt64 in
{
base = u64_instance_base;
add = (fun x y -> add x y);
properties = ()
}
instance u64_instance_sub : subtractable_bounded_unsigned_int FStar.UInt64.t =
let open FStar.UInt64 in
{
base = u64_instance_base;
sub = (fun x y -> sub x y);
properties = ()
}
instance u64_instance_cmp : comparable_bounded_unsigned_int FStar.UInt64.t =
let open FStar.UInt64 in
{
base = u64_instance_base;
comp = (fun x y -> x <^ y);
properties = ()
}
module U32 = FStar.UInt32
module U64 = FStar.UInt64
// let test32 (x:U32.t)
// (y:U32.t)
// = if x <= 0xffffffful &&
// y <= 0xffffffful
// then Some (x +^ y)
// else None
// let test64 (x y:U64.t)
// = if x <= 0xfffffffuL &&
// y <= 0xfffffffuL
// then Some (x +^ y)
// else None
// module L = FStar.List.Tot
// let try_add (x:U32.t)
// (y:U32.t)
// = if x <= (bound -^ y)
// then x +^ y
// else y
#push-options "--query_stats --fuel 0 --ifuel 1"
module L = FStar.List.Tot
let sum {| c : comparable_bounded_unsigned_int 'a |}
{| a : addable_bounded_unsigned_int 'a |}
{| s : subtractable_bounded_unsigned_int 'a |}
(l:list 'a) (acc:'a { c.base == a.base /\ a.base == s.base })
=
L.fold_right
(fun (x:'a) (acc:option 'a) ->
match acc with
| None -> None
| Some y ->
if x `comp` (bound `sub` y)
then Some (x `add` y)
else None)
l
(Some acc)
let testsum32 = sum [0x01ul; 0x02ul; 0x03ul] 0x00ul
let testsum64 = sum [0x01uL; 0x02uL; 0x03uL] 0x00uL
class bounded_unsigned_int_ops (a:Type) = {
_base_add: addable_bounded_unsigned_int a;
_base_sub: subtractable_bounded_unsigned_int a;
_base_comp: comparable_bounded_unsigned_int a;
[@@@no_method]
properties : squash (
_base_add.base == _base_sub.base /\
_base_sub.base == _base_comp.base
);
}
instance _base #a {| d : bounded_unsigned_int_ops a |}
: bounded_unsigned_int a
= d._base_sub.base
instance __base_add #a {| d : bounded_unsigned_int_ops a |}
: addable_bounded_unsigned_int a
= d._base_add
instance __base_sub #a {| d : bounded_unsigned_int_ops a |}
: subtractable_bounded_unsigned_int a
= d._base_sub
instance __base_comp #a {| d : bounded_unsigned_int_ops a |}
: comparable_bounded_unsigned_int a
= d._base_comp
let try_sub3 {| bounded_unsigned_int_ops 'a |}
(acc:'a)
= bound `sub` acc
#pop-options