| author | paulson | 
| Mon, 17 Aug 1998 13:09:08 +0200 | |
| changeset 5325 | f7a5e06adea1 | 
| parent 5222 | 3e40c6bffb87 | 
| child 5658 | 082debccf486 | 
| permissions | -rw-r--r-- | 
| 923 | 1  | 
(* Title: HOL/thy_syntax.ML  | 
2  | 
ID: $Id$  | 
|
3  | 
Author: Markus Wenzel and Lawrence C Paulson and Carsten Clasohm  | 
|
4  | 
||
5  | 
Additional theory file sections for HOL.  | 
|
6  | 
*)  | 
|
7  | 
||
| 3622 | 8  | 
local  | 
| 923 | 9  | 
|
10  | 
open ThyParse;  | 
|
11  | 
||
12  | 
||
| 1475 | 13  | 
(** typedef **)  | 
| 923 | 14  | 
|
| 1475 | 15  | 
fun mk_typedef_decl (((((opt_name, vs), t), mx), rhs), wt) =  | 
| 923 | 16  | 
let  | 
17  | 
val name' = if_none opt_name t;  | 
|
18  | 
val name = strip_quotes name';  | 
|
19  | 
in  | 
|
20  | 
(cat_lines [name', mk_triple (t, mk_list vs, mx), rhs, wt],  | 
|
21  | 
[name ^ "_def", "Rep_" ^ name, "Rep_" ^ name ^ "_inverse",  | 
|
22  | 
"Abs_" ^ name ^ "_inverse"])  | 
|
23  | 
end;  | 
|
24  | 
||
| 1475 | 25  | 
val typedef_decl =  | 
| 923 | 26  | 
  optional ("(" $$-- name --$$ ")" >> Some) None --
 | 
27  | 
type_args -- name -- opt_infix --$$ "=" -- string -- opt_witness  | 
|
| 1475 | 28  | 
>> mk_typedef_decl;  | 
| 923 | 29  | 
|
30  | 
||
31  | 
||
| 3980 | 32  | 
(** record **)  | 
33  | 
||
34  | 
val record_decl =  | 
|
| 4225 | 35  | 
(type_args -- name >> (mk_pair o apfst mk_list)) --$$ "="  | 
| 4226 | 36  | 
-- optional (typ --$$ "+" >> (parens o cat "Some")) "None"  | 
| 4225 | 37  | 
-- repeat1 ((name --$$ "::" -- typ) >> mk_pair)  | 
| 4001 | 38  | 
>> (fn ((x, y), zs) => cat_lines [x, y, mk_big_list zs]);  | 
| 3980 | 39  | 
|
40  | 
||
| 923 | 41  | 
(** (co)inductive **)  | 
42  | 
||
| 5097 | 43  | 
fun inductive_decl coind =  | 
| 923 | 44  | 
let  | 
45  | 
fun mk_intr_name (s, _) = (*the "op" cancels any infix status*)  | 
|
46  | 
if Syntax.is_identifier s then "op " ^ s else "_";  | 
|
47  | 
fun mk_params (((recs, ipairs), monos), con_defs) =  | 
|
48  | 
let val big_rec_name = space_implode "_" (map (scan_to_id o trim) recs)  | 
|
49  | 
and srec_tms = mk_list recs  | 
|
50  | 
and sintrs = mk_big_list (map snd ipairs)  | 
|
51  | 
in  | 
|
| 5097 | 52  | 
";\n\n\  | 
53  | 
\local\n\  | 
|
54  | 
        \val (thy, {defs, mono, unfold, intrs, elims, mk_cases, induct, ...}) =\n\
 | 
|
55  | 
\ InductivePackage.add_inductive true " ^  | 
|
56  | 
(if coind then "true " else "false ") ^ srec_tms ^ " " ^  | 
|
57  | 
sintrs ^ " " ^ monos ^ " " ^ con_defs ^ " thy;\n\  | 
|
58  | 
\in\n\  | 
|
59  | 
\structure " ^ big_rec_name ^ " =\n\  | 
|
60  | 
\struct\n\  | 
|
61  | 
\ val defs = defs;\n\  | 
|
62  | 
\ val mono = mono;\n\  | 
|
63  | 
\ val unfold = unfold;\n\  | 
|
64  | 
\ val intrs = intrs;\n\  | 
|
65  | 
\ val elims = elims;\n\  | 
|
66  | 
\ val elim = hd elims;\n\  | 
|
67  | 
\ val " ^ (if coind then "co" else "") ^ "induct = induct;\n\  | 
|
68  | 
\ val mk_cases = mk_cases;\n\  | 
|
69  | 
\ val " ^ mk_list (map mk_intr_name ipairs) ^ " = intrs;\n\  | 
|
70  | 
\end;\n\  | 
|
71  | 
\val thy = thy;\nend;\n\  | 
|
72  | 
\val thy = thy\n"  | 
|
| 923 | 73  | 
end  | 
74  | 
val ipairs = "intrs" $$-- repeat1 (ident -- !! string)  | 
|
| 3403 | 75  | 
fun optstring s = optional (s $$-- string >> trim) "[]"  | 
| 923 | 76  | 
in  | 
| 1788 | 77  | 
repeat1 name -- ipairs -- optstring "monos" -- optstring "con_defs"  | 
| 923 | 78  | 
>> mk_params  | 
79  | 
end;  | 
|
80  | 
||
81  | 
||
82  | 
(** datatype **)  | 
|
83  | 
||
84  | 
local  | 
|
| 5183 | 85  | 
(*** generate string for calling add_datatype ***)  | 
86  | 
(*** and bindig theorems to ML identifiers ***)  | 
|
| 923 | 87  | 
|
| 5183 | 88  | 
fun mk_bind_thms_string names =  | 
89  | 
(case find_first (not o Syntax.is_identifier) names of  | 
|
90  | 
Some id => (warning (id ^ " is not a valid identifier"); "")  | 
|
91  | 
| None =>  | 
|
92  | 
let  | 
|
93  | 
fun mk_dt_struct (s, (id, i)) =  | 
|
94  | 
s ^ "structure " ^ id ^ " =\n\  | 
|
95  | 
\struct\n\  | 
|
96  | 
            \  val distinct = nth_elem (" ^ i ^ ", distinct);\n\
 | 
|
97  | 
            \  val inject = nth_elem (" ^ i ^ ", inject);\n\
 | 
|
98  | 
            \  val exhaust = nth_elem (" ^ i ^ ", exhaustion);\n\
 | 
|
99  | 
            \  val cases = nth_elem (" ^ i ^ ", case_thms);\n\
 | 
|
100  | 
            \  val (split, split_asm) = nth_elem (" ^ i ^ ", split_thms);\n" ^
 | 
|
101  | 
(if length names = 1 then  | 
|
102  | 
" val induct = induction;\n\  | 
|
103  | 
\ val recs = rec_thms;\n\  | 
|
104  | 
\ val simps = simps;\n\  | 
|
105  | 
\ val size = size;\n"  | 
|
106  | 
else "") ^  | 
|
107  | 
"end;\n";  | 
|
108  | 
||
109  | 
val structs = foldl mk_dt_struct  | 
|
110  | 
            ("", (names ~~ (map string_of_int (0 upto length names - 1))));
 | 
|
111  | 
||
112  | 
in  | 
|
113  | 
(if length names > 1 then  | 
|
114  | 
"structure " ^ (space_implode "_" names) ^ " =\n\  | 
|
115  | 
\struct\n\  | 
|
116  | 
\val induct = induction;\n\  | 
|
117  | 
\val recs = rec_thms;\n\  | 
|
118  | 
\val simps = simps;\n\  | 
|
119  | 
\val size = size;\n"  | 
|
120  | 
else "") ^ structs ^  | 
|
121  | 
(if length names > 1 then "end;\n" else "")  | 
|
122  | 
end);  | 
|
123  | 
||
124  | 
fun mk_dt_string dts =  | 
|
| 923 | 125  | 
let  | 
| 5183 | 126  | 
val names = map (fn ((((alt_name, _), name), _), _) =>  | 
127  | 
strip_quotes (if_none alt_name name)) dts;  | 
|
128  | 
||
129  | 
val add_datatype_args = brackets (commas (map quote names)) ^ " " ^  | 
|
130  | 
brackets (commas (map (fn ((((_, vs), name), mx), cs) =>  | 
|
131  | 
parens (brackets (commas vs) ^ ", " ^ name ^ ", " ^ mx ^ ", " ^  | 
|
132  | 
brackets (commas cs))) dts));  | 
|
133  | 
||
| 923 | 134  | 
in  | 
| 5183 | 135  | 
";\nlocal\n\  | 
136  | 
      \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
 | 
|
137  | 
\ case_thms, split_thms, induction, size, simps}) =\n\  | 
|
138  | 
\ DatatypePackage.add_datatype " ^ add_datatype_args ^ " thy;\n\  | 
|
139  | 
\in\n" ^ mk_bind_thms_string names ^  | 
|
140  | 
"val thy = thy;\nend;\nval thy = thy\n"  | 
|
| 923 | 141  | 
end;  | 
142  | 
||
| 5183 | 143  | 
fun mk_rep_dt_string (((names, distinct), inject), induct) =  | 
144  | 
";\nlocal\n\  | 
|
145  | 
    \val (thy, {distinct, inject, exhaustion, rec_thms,\n\
 | 
|
146  | 
\ case_thms, split_thms, induction, size, simps}) =\n\  | 
|
147  | 
\ DatatypePackage.add_rep_datatype " ^  | 
|
148  | 
(case names of  | 
|
149  | 
        Some names => "(Some [" ^ commas (map quote names) ^ "]) (" ^
 | 
|
150  | 
          distinct ^ ") (" ^ inject ^ ") (" ^ induct ^ ") thy;\nin\n" ^
 | 
|
151  | 
mk_bind_thms_string names  | 
|
152  | 
      | None => "None (" ^ distinct ^ ") (" ^ inject ^ ") (" ^ induct ^
 | 
|
153  | 
") thy;\nin\n") ^  | 
|
154  | 
"val thy = thy;\nend;\nval thy = thy\n";  | 
|
| 923 | 155  | 
|
| 5183 | 156  | 
(*** parsers ***)  | 
| 4106 | 157  | 
|
| 5183 | 158  | 
val simple_typ = ident || (type_var >> strip_quotes);  | 
| 1316 | 159  | 
|
| 
1251
 
81fc4d8e3eda
added nested types on right hand side of datatype definitions
 
clasohm 
parents: 
977 
diff
changeset
 | 
160  | 
fun complex_typ toks =  | 
| 1316 | 161  | 
    let val typ = simple_typ || "(" $$-- complex_typ --$$ ")";
 | 
162  | 
        val typ2 = complex_typ || "(" $$-- complex_typ --$$ ")";
 | 
|
163  | 
in  | 
|
| 5183 | 164  | 
(typ ^^ (repeat ident >> (cat "" o space_implode " ")) ||  | 
165  | 
      "(" $$-- !! (list1 typ2 >> (parens o commas)) --$$ ")" ^^ !!
 | 
|
166  | 
(repeat1 ident >> (cat "" o space_implode " "))) toks  | 
|
| 1316 | 167  | 
end;  | 
168  | 
||
| 5183 | 169  | 
val opt_typs = repeat ((string >> strip_quotes) ||  | 
170  | 
    simple_typ || ("(" $$-- complex_typ --$$ ")"));
 | 
|
171  | 
val constructor = name -- opt_typs -- opt_mixfix >> (fn ((n, Ts), mx) =>  | 
|
172  | 
parens (n ^ ", " ^ mx ^ ", " ^ brackets (commas (map quote Ts))));  | 
|
173  | 
  val opt_name = optional ("(" $$-- name --$$ ")" >> Some) None
 | 
|
174  | 
||
| 923 | 175  | 
in  | 
176  | 
val datatype_decl =  | 
|
| 5183 | 177  | 
enum1 "and" (opt_name -- type_args -- name -- opt_infix --$$ "=" --  | 
178  | 
enum1 "|" constructor) >> mk_dt_string;  | 
|
179  | 
val rep_datatype_decl =  | 
|
180  | 
((optional ((repeat1 (name >> strip_quotes)) >> Some) None) --  | 
|
181  | 
      ("distinct" $$-- (name >> strip_quotes)) -- ("inject" $$--
 | 
|
182  | 
        (name >> strip_quotes)) -- ("induct" $$-- (name >> strip_quotes))) >>
 | 
|
183  | 
mk_rep_dt_string;  | 
|
| 923 | 184  | 
end;  | 
185  | 
||
186  | 
||
187  | 
(** primrec **)  | 
|
188  | 
||
| 5221 | 189  | 
fun mk_primrec_decl (alt_name, (names, eqns)) =  | 
| 5183 | 190  | 
";\nval (thy, " ^ (if null names then "_" else brackets (commas names)) ^  | 
| 5222 | 191  | 
  ") = PrimrecPackage.add_primrec (" ^ alt_name ^ ") " ^
 | 
| 5221 | 192  | 
brackets (commas_quote names) ^ " " ^ brackets (commas eqns) ^ " thy;\n\  | 
| 5183 | 193  | 
\val thy = thy\n";  | 
| 923 | 194  | 
|
| 5183 | 195  | 
(* either names and axioms or just axioms *)  | 
| 5221 | 196  | 
val primrec_decl = (optional ("(" $$-- name --$$ ")" >> (cat "Some")) "None" --
 | 
197  | 
((repeat1 (ident -- string) >> ListPair.unzip) ||  | 
|
198  | 
(repeat1 string >> (pair [])))) >> mk_primrec_decl;  | 
|
| 923 | 199  | 
|
| 2922 | 200  | 
|
201  | 
(** rec: interface to Slind's TFL **)  | 
|
202  | 
||
203  | 
||
| 3194 | 204  | 
(*fname: name of function being defined; rel: well-founded relation*)  | 
| 3456 | 205  | 
fun mk_rec_decl ((((fname, rel), congs), ss), axms) =  | 
| 2922 | 206  | 
let val fid = trim fname  | 
| 3194 | 207  | 
val intrnl_name = fid ^ "_Intrnl"  | 
| 2922 | 208  | 
in  | 
209  | 
	 (";\n\n\
 | 
|
| 3194 | 210  | 
\val _ = writeln \"Recursive function " ^ fid ^ "\"\n\  | 
211  | 
\val (thy, pats_" ^ intrnl_name ^ ") = Tfl.define thy " ^  | 
|
| 
3345
 
4d888ddd6284
Now recdef checks the name of the function being defined.
 
paulson 
parents: 
3308 
diff
changeset
 | 
212  | 
quote fid ^ " " ^  | 
| 3194 | 213  | 
rel ^ "\n" ^ mk_big_list axms ^ ";\n\  | 
| 2922 | 214  | 
\val thy = thy"  | 
215  | 
,  | 
|
| 3194 | 216  | 
"structure " ^ fid ^ " =\n\  | 
217  | 
\ struct\n\  | 
|
218  | 
\ val _ = writeln \"Proofs for recursive function " ^ fid ^ "\"\n\  | 
|
219  | 
          \  val {rules, induct, tcs} = \n\
 | 
|
| 3456 | 220  | 
          \    \t Tfl.simplify_defn (" ^ ss ^ ", " ^ congs ^ ")\n\
 | 
221  | 
          \    \t\t  (thy, (" ^ quote fid ^ ", pats_" ^ intrnl_name ^ "))\n\
 | 
|
| 3194 | 222  | 
\ end;\n\  | 
223  | 
\val pats_" ^ intrnl_name ^ " = ();\n")  | 
|
| 2922 | 224  | 
end;  | 
225  | 
||
| 3403 | 226  | 
val rec_decl = (name -- string --  | 
| 3456 | 227  | 
		optional ("congs" $$-- string >> trim) "[]" -- 
 | 
| 4091 | 228  | 
		optional ("simpset" $$-- string >> trim) "simpset()" -- 
 | 
| 3403 | 229  | 
repeat1 string >> mk_rec_decl) ;  | 
| 2922 | 230  | 
|
231  | 
||
232  | 
||
| 3622 | 233  | 
(** augment thy syntax **)  | 
| 923 | 234  | 
|
| 3622 | 235  | 
in  | 
| 923 | 236  | 
|
| 3622 | 237  | 
val _ = ThySyn.add_syntax  | 
| 5183 | 238  | 
["intrs", "monos", "con_defs", "congs", "simpset", "|",  | 
239  | 
"and", "distinct", "inject", "induct"]  | 
|
| 4873 | 240  | 
[axm_section "typedef" "|> TypedefPackage.add_typedef" typedef_decl,  | 
| 5097 | 241  | 
section "record" "|> RecordPackage.add_record" record_decl,  | 
242  | 
section "inductive" "" (inductive_decl false),  | 
|
243  | 
section "coinductive" "" (inductive_decl true),  | 
|
244  | 
section "datatype" "" datatype_decl,  | 
|
| 5183 | 245  | 
section "rep_datatype" "" rep_datatype_decl,  | 
246  | 
section "primrec" "" primrec_decl,  | 
|
| 2922 | 247  | 
  ("recdef", rec_decl)];
 | 
| 923 | 248  | 
|
249  | 
end;  |