-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoq_of_xsd.ml
356 lines (309 loc) · 12.7 KB
/
coq_of_xsd.ml
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
(******************************************************************************
Rainbow, a termination proof certification tool
See the COPYRIGHTS and LICENSE files.
- Frederic Blanqui, 2009-10-20
Generate an Coq type from an xsd value.
******************************************************************************)
(* REMARK: The code below for cpf.xsd version 2.1. *)
open Util;;
open Xsd;;
open Printf;;
open Scc;;
let todo b = string b "todo";;
(******************************************************************************)
(* [2] In the case the leaf is a [Sequence], then it has two cases:
[A]: In the branch has only one element. [type_sequence].
[B]: In the branch has a list of elements. [type_sequences] *)
let rec type_sequences _ b = function
| SimpleType n
| GroupRef (n, 1, Bound 1) -> Util.string b n
| GroupRef (n, 0, Bound 1) -> bprintf b "option %s" n
| GroupRef (n, _, _) -> bprintf b "list %s" n
| Elt (_, None, _, _) -> bprintf b "option boolean"
| Elt (n, Some t, _, Unbounded) -> bprintf b "list %a" (par_of_sequence n) t
| Elt (n, Some t, 0, Bound 1) -> bprintf b "option %a" (par_of_sequence n) t
| Elt (n, Some t, 1, Bound 1) -> bprintf b "%a" (par_of_sequence n) t
| Sequence xs ->
let aux = function
| Choice _ -> false
| _ -> true
in
list " * " (fun b x -> type_sequences (name_of_elt x) b x) b
(List.filter aux xs)
| _ -> todo b
and par_of_sequence tn b = function
| SimpleType n
| GroupRef (n, 1, Bound 1) -> Util. string b n
| x -> bprintf b "(%a)" (type_sequences tn) x;;
(* [type_sequence] is the print function in the case of [Sequence]
where it has one element in the list. This is in the case of
[matrix] and [vector] where I need to print [matrix] and [vector]
and [coefficient] as an [Inductive... with] type. *)
let type_sequence tn b = function
| Elt (_, Some t, 1, Unbounded) ->
(match t with
| Sequence _ as x -> bprintf b "Definition %s := list (%a)."
tn (type_sequences tn) x
(* For debug information: argumentFilter; subsitution type. *)
| SimpleType sn -> (* TODO *)
(* This is the case of [matrix-vector-coefficient]. Other
type also has the same constructor but need to print as a
[Definition] (usableRules; trs; dps). *)
if tn = "matrix"
then bprintf b "Inductive %s :=\n\
| %s : list (list coefficient) -> %s\
"
tn (constructor_name tn tn) tn
else
if tn = "vector"
then bprintf b "with %s :=\n\
| %s : list %s -> %s\
"
tn (constructor_name tn tn) sn tn
else bprintf b "Definition %s := %a. "
tn (type_sequences tn) t
| _ -> todo b)
| x -> bprintf b "Definition %s := %a." tn (type_sequences tn) x;;
(* For debug information: this is in the case of [rules] type. *)
(******************************************************************************)
(* [3] The leaf is a [Choice], then it has two cases:
[a] First one is only one element in this leaf. Corresponding to
the function [type_choice].
[b] Second case is a list of choices. Function [type_choices]. In
this case each branch will become an constructor in COQ type. *)
let rec sequence_in_choice _ b = function
| SimpleType n
| GroupRef (n, 1, Bound 1) -> Util.string b n
| GroupRef (n, 0, Bound 1) -> bprintf b "option %s" n
| GroupRef (n, _, _) -> bprintf b "list %s" n
| Elt (_, None, _, _) -> bprintf b "option boolean"
| Elt (n, Some t, _, Unbounded) -> bprintf b "list %a" (par_of_sequence n) t
| Elt (n, Some t, 0, Bound 1) -> bprintf b "option %a" (par_of_sequence n) t
| Elt (n, Some t, 1, Bound 1) -> bprintf b "%a" (par_of_sequence n) t
| Sequence xs ->
let aux = function
| Choice _ -> false
| _ -> true
in
list " -> "
(fun b x -> sequence_in_choice (name_of_elt x) b x) b (List.filter aux xs)
| _ -> todo b
let type_in_choice tn b = function
| Elt (n, Some (Choice []), _, _)
| Elt (n, None, _, _) -> bprintf b "\n| %s"
(constructor_name tn n)
| GroupRef (n, _, _) -> bprintf b "\n| %s : %s -> %s"
(constructor_name tn n) n tn
| Elt (n, Some t, _, _) -> bprintf b "\n| %s : %a -> %s"
(constructor_name tn n) (sequence_in_choice n) t tn
| Sequence xs -> (* REMARK *)
(* CPF version 2.1, in the case of "Lhs_symbol" *)
let aux = function
| Choice _ -> false
| _ -> true
in
bprintf b "\n| %s: " (constructor_name tn tn);
list "-> " (fun b x -> type_sequences (name_of_elt x) b x) b
(List.filter aux xs);
bprintf b "-> %s" tn
| _ -> todo b;;
(* [b] A list of choices *)
let type_choices tn b = function
| Choice xs -> list "" (type_in_choice tn) b xs
| _ -> todo b;; (* TODO? in type_def *)
(* [A] Only one element *)
let type_choice tn b = function
| Elt (n, Some (Choice []), _, _)
| Elt (n, None, _, _) -> bprintf b "Inductive %s : Set := %s."
tn (constructor_name tn n)
| Elt (n, Some t, _, _) ->
begin match t with
| SimpleType sn ->
(* [function], [complexityClass] *)
bprintf b "Definition %s := %s. " tn sn
| Sequence xs ->
(* REMARK: CPF version 2.1 is the branch [quasiReductionProof]. *)
let aux = function
| Choice _ -> false
| _ -> true
in
bprintf b "Inductive %s := \n \
| %s : " tn (constructor_name tn n);
list " -> " (fun b x -> type_sequences (name_of_elt x) b x) b
(List.filter aux xs);
bprintf b " -> %s." tn
| _ -> todo b
end
| _ -> todo b;;
(* Choice with one constructor where it roots is an Elt. *)
let elt_type_choice tn b = function
| Elt (n, Some (Choice []), _, _)
| Elt (n, None, _, _) -> bprintf b "Inductive %s : Set := %s."
tn (constructor_name tn n)
| Elt (n, Some t, _, _) ->
begin match t with
| SimpleType _ -> bprintf b "Inductive %s := \n | %s : %s -> %s."
tn (constructor_name tn n) n tn
| Sequence xs ->
let aux = function
| Choice _ -> false
| _ -> true
in
bprintf b "Inductive %s := \n \
| %s : " tn (constructor_name tn n);
list " -> "
(fun b x -> type_sequences (name_of_elt x) b x) b
(List.filter aux xs);
bprintf b " -> %s." tn
| _ -> todo b
end
| _ -> todo b;;
(* SimpleType: COQ definition [Definition].
Sequence : COQ definition [Definition].
Choice : COQ definition [Inductive]. *)
let genr_type b = function
| Elt (n, Some t, _, _) ->
begin match t with
| SimpleType sn -> bprintf b "Definition %s := %s." n sn
| Sequence xs ->
(match xs with
| [] -> ()
| [x] ->
(* In the case it is a sequence and it has one element, print
it as a [Definition] call to the function [type_sequence]. *)
type_sequence n b x
| _ :: _ -> (* TODO *)
(* TODO: Version 2.1 patternRule: t5 calls patternRule
and patternRule calls t5 *)
if n = "patternRule"
then bprintf b
"with patternRule :=\n \
| PatternRule_patternRule : (patternTerm * patternTerm * t5) ->\
patternRule."
else
(* In the case it is a list, then print [Definition] and
call to the function to generate type for this
[Definition] type [type_sequences].
Remark: print '.' at the end. *)
bprintf b "Definition %s := %a." n (type_sequences n) t)
| Choice xs ->
(match xs with
| [] -> ()
| [x] ->
(* In the case it is a choice and it has one element, print
it as a [Definition] instead of [Inductive] with one
constructor. Call to the function [type_choice] *)
elt_type_choice n b x
| _ :: _ -> (* TODO *)
(* In the case it is a list, then print [Inductive] and call
to the function to generate type for this [Inductive]
type [type_choices].
Remark: print '.' at the end.*)
(* REMARK: because of [coefficient] type depend on the type
of [matrix - vector] and this is a special case to print. *)
if n = "coefficient"
then bprintf b "with %s := %a." n (type_choices n) t
else bprintf b "Inductive %s := %a." n (type_choices n) t)
| _ -> assert false
end
| Group (n, Some t, _, _) ->
begin match t with
| Choice xs ->
(match xs with
| [] -> ()
| [x] ->
(* In the case it is a choice and it has one element, print
it as a [Definition] instead of [Inductive] with one
constructor. Call to the function [type_choice] *)
type_choice n b x
| _ :: _ -> (* TODO *)
(* REMARK: this is a type of [symbol - label] these two
types are depend to each others. It needs a special
print in COQ type as [Inductive ... with]. (For debug
information: in XSD the top level is [Group...]) *)
if n = "symbol"
then bprintf b "Inductive %s := %a" n (type_choices n) t
else
if n = "label"
then bprintf b "with %s := %a." n (type_choices n) t
else
if n = "t5"
then bprintf b "Inductive %s := %a" n (type_choices n) t
else bprintf b "Inductive %s := %a." n (type_choices n) t)
| _ -> todo b
end
| _ -> todo b;;
let print_types b xsds =
List.iter (function
| [] -> assert false
| h :: t -> bprintf b "%a" genr_type h;
List.iter (fun xsd -> bprintf b "\n\n%a" genr_type xsd) t) xsds;;
(* [genr_types] is a priting function for a whole list [xsds]. *)
let genr_types b xsds = print_types b xsds;;
(**********************************************************************)
let num_of_name defined undefined len_undefined s =
try
let p = position s defined in
Some (p + len_undefined)
with Not_found ->
try
let p = position s undefined in
Some p
with Not_found -> None
(* funtion taking a position of xsds list and undefined list, return
their element *)
let name_of_num xsds undefined len_undefined k =
if k < len_undefined
then List.nth undefined k
else List.nth xsds (k - len_undefined)
let coq_xsd_types =
[ "nonNegativeInteger", "N";
"integer", "Z";
"positiveInteger", "positive";
"long", "Z";
"boolean", "bool" ];;
(* an undefined list is an list that does not has a definition in xsd type *)
let undefined = List.map fst coq_xsd_types;;
let len_undefined = List.length undefined;;
(* build an undefined list into an xsds data types*)
let builtin_undefined_types =
let xsd_of_type (n, d) = Elt (n, Some (SimpleType d), 1, Bound 1) in
List.map xsd_of_type coq_xsd_types;;
let defined xsds = List.map name_of_elt xsds;;
(* transform an xsds to boolean matrix by using an pair of xsds
(string, string list) list, if they have a connection between them
then return true, otherwise return false *)
let matrix xsds =
let pair_xsds = get_list_xsds xsds in
let len = List.length (defined xsds) + len_undefined in
let boolmat = Array.make_matrix len len false in
List.iter (fun (s, strs) ->
match num_of_name (defined xsds) undefined len_undefined s with
| Some pos1 -> List.iter (fun t ->
match num_of_name (defined xsds) undefined len_undefined t with
| Some pos2 -> boolmat.(pos1).(pos2) <- true
| None -> ()) strs
| None -> ()
) pair_xsds;
boolmat
let closure_matrix xsds = transClosure (matrix xsds)
let line_matrix xsds = linearize (closure_matrix xsds)
let equivalence xsds = eq_classes (line_matrix xsds)
let sort_equivalence xsds = sort_eq_classes (line_matrix xsds)
(List.flatten (equivalence xsds))
let xsds_of_int xsds = List.map (List.map
(name_of_num xsds builtin_undefined_types len_undefined))
let xsds_sorted xsds = xsds_of_int xsds [(sort_equivalence xsds)];;
let genr_coq b xsds =
let xsds = flatten_innerchoice xsds in
let xsds_sorted = xsds_sorted xsds in
bprintf b
"(* DO NOT MODIFY THIS FILE. IT HAS BEEN AUTOMATICALLY GENERATED *)\n\n\
Set Implicit Arguments.\n\
Require Import ZArith String Int31.\n\
Open Local Scope type_scope.\n\
(**************************************************************)\n\
(* Describing CPF type in Coq type *)\n\
(**************************************************************)\n\n\
%a\n\n"
genr_types xsds_sorted;;