-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplemSubClass.v
338 lines (265 loc) · 7.53 KB
/
ImplemSubClass.v
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
(** * Bicalano: Implementation of a subclass test *)
Require Import List.
Set Implicit Arguments.
Section FOLD.
Variable A : Type.
Variable f : A -> option A.
Fixpoint fold_opt (l:list A) : option (list A) :=
match l with
| nil => None
| a::l =>
match f a with
| None =>
match fold_opt l with
| None => None
| Some l' => Some (a::l')
end
| Some a' => Some (a' :: l)
end
end.
End FOLD.
Section SUBCLASS.
Variable name : Set.
Variable class : Type.
Variable get_name : class -> name.
Variable eq_name : name -> name -> bool.
Variable superclass : class -> option name.
Inductive tree : Set :=
node : name -> list tree -> tree.
Fixpoint get_tree (n:name) (lt: list tree) {struct lt} :
option (tree * list tree) :=
match lt with
| nil => None
| t :: lt =>
let (x,_) := t in
if eq_name x n then Some (t, lt)
else match get_tree n lt with
| None => None
| Some (nt,lt) => Some (nt, t::lt)
end
end.
Definition init_tree n lt :=
match get_tree n lt with
| None => (node n nil, lt)
| Some(tc, lt) => (tc, lt)
end.
Fixpoint add_subclass (s:name) (c:tree) (t:tree) {struct t} : option tree :=
match t with
| node x l =>
if eq_name x s then Some (node x (c::l))
else match fold_opt (add_subclass s c) l with
| None => None
| Some l => Some (node x l)
end
end.
Section BUILD_TREE.
Variable obj : name.
Fixpoint build_tree (lt:list tree) (l:list class) {struct l} :
list tree :=
match l with
| nil => lt
| c::l =>
let (tc,lt) := init_tree (get_name c) lt in
let super := match superclass c with Some s => s | None => obj end in
let lt :=
match fold_opt (add_subclass super tc) lt with
| Some lt => lt
| None => (node super (cons tc nil)) :: lt
end in
build_tree lt l
end.
End BUILD_TREE.
Variable map : Type -> Type.
Variable empty : forall C:Set, map C.
Variable update : forall C:Set, map C -> name -> C -> map C.
Variable get : forall C:Set, map C -> name -> option C.
Fixpoint build_supers (super:list name) (t:tree) (m:map(list name)){struct t} :
map (list name) :=
match t with
| node a lt =>
update (fold_right (build_supers (a::super)) m lt) a super
end.
Fixpoint test_In (a:name) (l:list name) {struct l} : bool :=
match l with
nil => false
| x::q => if eq_name a x then true else test_In a q
end.
Fixpoint get_obj (l:list class) : option (name * list class) :=
match l with
| nil => None
| c::l =>
match superclass c with
| None => Some (get_name c, l)
| Some _ =>
match get_obj l with
| None => None
| Some (obj, l) => Some (obj, c::l)
end
end
end.
Definition subclass_test (l:list class) : option (name -> name -> bool) :=
match get_obj l with
| None => None
| Some (obj, l) =>
match build_tree obj nil l with
| t::nil =>
let m := build_supers nil t (empty (list name)) in
Some (fun x y =>
match get m x with
| None => false
| Some supers => if eq_name x y then true else test_In y supers
end)
| _ => None
end
end.
End SUBCLASS.
(*
Section __A__.
Variable A : Set.
Variable test : A -> A -> bool.
Inductive tree (A:Set) : Set :=
node : A -> list (tree A) -> tree A.
Section fold_opt.
Variable B:Set.
Variable f:B->option B.
Fixpoint fold_opt (l:list B) {struct l} : option (list B) :=
match l with
nil => None
| a::q =>
match f a with
| Some a' => Some (a'::q)
| None =>
match fold_opt q with
None => None
| Some q' => Some (a::q')
end
end
end.
End fold_opt.
Fixpoint add (n s:A) (t:tree A) {struct t} : option (tree A) :=
match t with
node x l =>
if test x s then Some (node x (node n nil::l))
else
match fold_opt (add n s) l with
None => None
| Some l' => Some (node x l')
end
end.
Variable class : Set.
Variable name : class -> A.
Variable superclass : class -> option A.
Fixpoint build_tree_rec (t:tree A) (l:list class) {struct l} : option (tree A) :=
match l with
nil => Some t
| c::q =>
match superclass c with
Some s =>
match (add (name c) s t) with
None => None
| Some t' => build_tree_rec t' q
end
| _ => None
end
end.
Definition build_tree (l:list class) : option (tree A) :=
match l with
| nil => None
| c::l =>
match superclass c with
None => build_tree_rec (node (name c) nil) l
| _ => None
end
end.
Fixpoint test_In (a:A) (l:list A) {struct l} : bool :=
match l with
nil => false
| x::q => if test a x then true else test_In a q
end.
Fixpoint find (C:Set) (a:A) (l:list (A*C)) {struct l} : option C :=
match l with
nil => None
| (x,c)::q => if test a x then Some c else find a q
end.
Variable B:Set -> Set.
Variable empty : forall C:Set, B C.
Variable update : forall C:Set, B C -> A -> C -> B C.
Variable get : forall C:Set, B C -> A -> option C.
Fixpoint build_supers (l:list A) (t:tree A) (b:B (list A)) {struct t} : B (list A) :=
match t with
node a ltt =>
update (fold_right (build_supers (a::l)) b ltt) a l
end.
Definition subclass_test (l:list class) : option (A -> A -> bool) :=
match build_tree l with
None => None
| Some t =>
let m := build_supers nil t (empty (list A)) in
Some (fun x y =>
match get m x with
None => false
| Some supers => test_In y supers
end)
end.
End __A__.
(*
Definition l : list (class nat) :=
(mc 0 None)::(mc 1 (Some 0))::(mc 2 (Some 0))::(mc 3 (Some 0))::
(mc 4 (Some 1))::
(mc 5 (Some 1))::
(mc 6 (Some 4))::
(mc 7 (Some 2))::
(mc 8 (Some 2))::nil.
Fixpoint nat_eq (n1 n2:nat) {struct n1} : bool :=
match n1,n2 with
O,O => true
| S p1,S p2 => nat_eq p1 p2
| _,_ => false
end.
Eval compute in build_tree nat_eq l.
Definition t :=
(node 0
(node 3 nil::
node 2 (node 8 nil ::
node 7 nil :: nil) ::
node 1 (node 5 nil ::
node 4 (node 6 nil :: nil) ::
nil) ::
nil)).
Definition subclass_nat_test :=
@subclass_test nat nat_eq
(fun C => list (nat*C))
(fun C => @nil (nat*C))
(fun C a c b =>cons (a,c) b)
(find nat_eq).
Eval compute in
@build_supers nat
(fun C => list (nat*C))
(fun C a c b =>cons (a,c) b)
nil t nil.
Definition test (A:Set) (f_op:option (A->A->bool)) :=
match f_op with
None => fun _ _ => false
| Some f => f
end.
Eval compute in test (subclass_nat_test l) 3 0.
Eval compute in test (subclass_nat_test l) 2 0.
Eval compute in test (subclass_nat_test l) 8 0.
Eval compute in test (subclass_nat_test l) 8 2.
Eval compute in test (subclass_nat_test l) 7 0.
Eval compute in test (subclass_nat_test l) 7 2.
Eval compute in test (subclass_nat_test l) 1 0.
Eval compute in test (subclass_nat_test l) 5 0.
Eval compute in test (subclass_nat_test l) 5 1.
Eval compute in test (subclass_nat_test l) 4 0.
Eval compute in test (subclass_nat_test l) 4 1.
Eval compute in test (subclass_nat_test l) 6 0.
Eval compute in test (subclass_nat_test l) 6 1.
Eval compute in test (subclass_nat_test l) 6 4.
Eval compute in test (subclass_nat_test l) 6 2.
Eval compute in test (subclass_nat_test l) 6 3.
Eval compute in test (subclass_nat_test l) 6 5.
Eval compute in test (subclass_nat_test l) 6 7.
Eval compute in test (subclass_nat_test l) 6 8.
*)
*)