author | lcp |
Thu, 24 Nov 1994 10:23:41 +0100 | |
changeset 737 | 436019ca97d7 |
parent 733 | 5207fca25b6b |
child 802 | 2f2fbf0a7e4f |
permissions | -rw-r--r-- |
516 | 1 |
(* Title: ZF/Datatype.ML |
0 | 2 |
ID: $Id$ |
516 | 3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
0 | 4 |
Copyright 1993 University of Cambridge |
5 |
||
120 | 6 |
(Co)Datatype Definitions for Zermelo-Fraenkel Set Theory |
0 | 7 |
*) |
8 |
||
9 |
||
10 |
(*For most datatypes involving univ*) |
|
70
8a29f8b4aca1
ZF/ind-syntax/fold_con_tac: deleted, since fold_tac now works
lcp
parents:
55
diff
changeset
|
11 |
val datatype_intrs = |
0 | 12 |
[SigmaI, InlI, InrI, |
13 |
Pair_in_univ, Inl_in_univ, Inr_in_univ, |
|
55 | 14 |
zero_in_univ, A_into_univ, nat_into_univ, UnCI]; |
0 | 15 |
|
55 | 16 |
(*Needed for mutual recursion*) |
70
8a29f8b4aca1
ZF/ind-syntax/fold_con_tac: deleted, since fold_tac now works
lcp
parents:
55
diff
changeset
|
17 |
val datatype_elims = [make_elim InlD, make_elim InrD]; |
0 | 18 |
|
120 | 19 |
(*For most codatatypes involving quniv*) |
20 |
val codatatype_intrs = |
|
0 | 21 |
[QSigmaI, QInlI, QInrI, |
22 |
QPair_in_quniv, QInl_in_quniv, QInr_in_quniv, |
|
55 | 23 |
zero_in_quniv, A_into_quniv, nat_into_quniv, UnCI]; |
0 | 24 |
|
120 | 25 |
val codatatype_elims = [make_elim QInlD, make_elim QInrD]; |
55 | 26 |
|
516 | 27 |
signature INDUCTIVE_THMS = |
28 |
sig |
|
29 |
val monos : thm list (*monotonicity of each M operator*) |
|
30 |
val type_intrs : thm list (*type-checking intro rules*) |
|
31 |
val type_elims : thm list (*type-checking elim rules*) |
|
32 |
end; |
|
33 |
||
34 |
functor Data_section_Fun |
|
35 |
(Arg: sig include CONSTRUCTOR_ARG INDUCTIVE_I INDUCTIVE_THMS end) |
|
36 |
: sig include CONSTRUCTOR_RESULT INTR_ELIM INDRULE end = |
|
37 |
struct |
|
38 |
||
39 |
structure Con = Constructor_Fun |
|
40 |
(structure Const = Arg and Pr=Standard_Prod and Su=Standard_Sum); |
|
41 |
||
42 |
structure Inductive = Ind_section_Fun |
|
43 |
(open Arg; |
|
44 |
val con_defs = Con.con_defs |
|
45 |
val type_intrs = Arg.type_intrs @ datatype_intrs |
|
46 |
val type_elims = Arg.type_elims @ datatype_elims); |
|
47 |
||
48 |
open Inductive Con |
|
49 |
end; |
|
50 |
||
51 |
||
52 |
functor CoData_section_Fun |
|
53 |
(Arg: sig include CONSTRUCTOR_ARG INDUCTIVE_I INDUCTIVE_THMS end) |
|
54 |
: sig include CONSTRUCTOR_RESULT INTR_ELIM COINDRULE end = |
|
55 |
struct |
|
56 |
||
57 |
structure Con = Constructor_Fun |
|
58 |
(structure Const = Arg and Pr=Quine_Prod and Su=Quine_Sum); |
|
59 |
||
60 |
structure CoInductive = CoInd_section_Fun |
|
61 |
(open Arg; |
|
62 |
val con_defs = Con.con_defs |
|
63 |
val type_intrs = Arg.type_intrs @ codatatype_intrs |
|
64 |
val type_elims = Arg.type_elims @ codatatype_elims); |
|
65 |
||
66 |
open CoInductive Con |
|
67 |
end; |
|
68 |
||
69 |
||
70 |
(*For installing the theory section. co is either "" or "Co"*) |
|
71 |
fun datatype_decl co = |
|
72 |
let open ThyParse Ind_Syntax |
|
73 |
(*generate strings*) |
|
74 |
fun mk_const ((x, y), z) = mk_triple (x, mk_list y, z); |
|
75 |
val mk_data = mk_list o map mk_const o snd |
|
76 |
val mk_scons = mk_big_list o map mk_data |
|
77 |
fun mk_intr_name s = (*the "op" cancels any infix status*) |
|
78 |
if Syntax.is_identifier s then "op " ^ s ^ "_I" else "_" |
|
79 |
fun mk_params ((((dom, rec_pairs), monos), type_intrs), type_elims) = |
|
80 |
let val rec_names = map (scan_to_id o trim o fst) rec_pairs |
|
81 |
val big_rec_name = space_implode "_" rec_names |
|
82 |
and srec_tms = mk_list (map fst rec_pairs) |
|
83 |
and scons = mk_scons rec_pairs |
|
733
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
84 |
and sdom_sum = |
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
85 |
if dom = "" then co ^ "data_domain rec_tms" (*default*) |
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
86 |
else "readtm (sign_of thy) iT " ^ dom |
516 | 87 |
val stri_name = big_rec_name ^ "_Intrnl" |
88 |
val con_names = flat (map (map (trim o #1 o #1) o snd) rec_pairs) |
|
89 |
in |
|
90 |
(";\n\n\ |
|
91 |
\structure " ^ stri_name ^ " =\n\ |
|
92 |
\ let open Ind_Syntax in\n\ |
|
93 |
\ struct\n\ |
|
733
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
94 |
\ val _ = writeln \"" ^ co ^ |
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
95 |
"Datatype definition " ^ big_rec_name ^ "\"\n\ |
516 | 96 |
\ val rec_tms\t= map (readtm (sign_of thy) iT) " ^ srec_tms ^ "\n\ |
733
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
97 |
\ val dom_sum\t= " ^ sdom_sum ^ "\n\ |
516 | 98 |
\ and con_ty_lists\t= read_constructs (sign_of thy)\n" |
99 |
^ scons ^ "\n\ |
|
100 |
\ val intr_tms\t= mk_all_intr_tms (rec_tms, con_ty_lists)\n\ |
|
101 |
\ end\n\ |
|
102 |
\ end;\n\n\ |
|
103 |
\val thy = thy |> " ^ co ^ "Ind.add_constructs_def(" ^ |
|
104 |
mk_list (map quote rec_names) ^ ", " ^ |
|
105 |
stri_name ^ ".con_ty_lists) \n\ |
|
106 |
\ |> " ^ co ^ "Ind.add_fp_def_i \n (" ^ |
|
107 |
stri_name ^ ".rec_tms, " ^ |
|
733
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
108 |
stri_name ^ ".dom_sum, " ^ |
516 | 109 |
stri_name ^ ".intr_tms)" |
110 |
, |
|
111 |
"structure " ^ big_rec_name ^ " =\n\ |
|
112 |
\ struct\n\ |
|
733
5207fca25b6b
ZF/Datatype/datatype_decl: supplies a SINGLE domain for the mutually
lcp
parents:
516
diff
changeset
|
113 |
\ val _ = writeln \"Proofs for " ^ co ^ |
516 | 114 |
"Datatype definition " ^ big_rec_name ^ "\"\n\ |
115 |
\ structure Result = " ^ co ^ "Data_section_Fun\n\ |
|
116 |
\ (open " ^ stri_name ^ "\n\ |
|
117 |
\ val thy\t\t= thy\n\ |
|
118 |
\ val big_rec_name\t= \"" ^ big_rec_name ^ "\"\n\ |
|
119 |
\ val monos\t\t= " ^ monos ^ "\n\ |
|
120 |
\ val type_intrs\t= " ^ type_intrs ^ "\n\ |
|
121 |
\ val type_elims\t= " ^ type_elims ^ ");\n\n\ |
|
122 |
\ val " ^ mk_list (map mk_intr_name con_names) ^ " = Result.intrs;\n\ |
|
123 |
\ open Result\n\ |
|
124 |
\ end\n" |
|
125 |
) |
|
126 |
end |
|
127 |
fun optstring s = optional (s $$-- string) "\"[]\"" >> trim |
|
128 |
val string_list = "(" $$-- list1 string --$$ ")" || ThyParse.empty; |
|
129 |
val construct = name -- string_list -- opt_mixfix; |
|
130 |
in optional ("<=" $$-- string) "" -- |
|
131 |
enum1 "and" (string --$$ "=" -- enum1 "|" construct) -- |
|
132 |
optstring "monos" -- optstring "type_intrs" -- optstring "type_elims" |
|
133 |
>> mk_params |
|
134 |
end; |