101 let |
101 let |
102 val _ = writeln "Constructing primrec combinators..."; |
102 val _ = writeln "Constructing primrec combinators..."; |
103 |
103 |
104 val descr' = flat descr; |
104 val descr' = flat descr; |
105 val recTs = get_rec_types descr' sorts; |
105 val recTs = get_rec_types descr' sorts; |
|
106 val used = foldr add_typ_tfree_names (recTs, []); |
106 val newTs = take (length (hd descr), recTs); |
107 val newTs = take (length (hd descr), recTs); |
107 |
108 |
108 val induct_Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of induct))); |
109 val induct_Ps = map head_of (dest_conj (HOLogic.dest_Trueprop (concl_of induct))); |
109 |
110 |
110 val big_rec_name' = (space_implode "_" new_type_names) ^ "_rec_set"; |
111 val big_rec_name' = (space_implode "_" new_type_names) ^ "_rec_set"; |
111 val rec_set_names = map (Sign.full_name (sign_of thy)) |
112 val rec_set_names = map (Sign.full_name (sign_of thy)) |
112 (if length descr' = 1 then [big_rec_name'] else |
113 (if length descr' = 1 then [big_rec_name'] else |
113 (map ((curry (op ^) (big_rec_name' ^ "_")) o string_of_int) |
114 (map ((curry (op ^) (big_rec_name' ^ "_")) o string_of_int) |
114 (1 upto (length descr')))); |
115 (1 upto (length descr')))); |
115 |
116 |
116 val rec_result_Ts = map (fn (i, _) => |
117 val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~ |
117 TFree ("'t" ^ (string_of_int (i + 1)), HOLogic.termS)) descr'; |
118 replicate (length descr') HOLogic.termS); |
118 |
119 |
119 val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) => |
120 val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) => |
120 map (fn (_, cargs) => |
121 map (fn (_, cargs) => |
121 let |
122 let |
122 val recs = filter is_rec_type cargs; |
123 val recs = filter is_rec_type cargs; |
278 let |
279 let |
279 val _ = writeln "Proving characteristic theorems for case combinators..."; |
280 val _ = writeln "Proving characteristic theorems for case combinators..."; |
280 |
281 |
281 val descr' = flat descr; |
282 val descr' = flat descr; |
282 val recTs = get_rec_types descr' sorts; |
283 val recTs = get_rec_types descr' sorts; |
|
284 val used = foldr add_typ_tfree_names (recTs, []); |
283 val newTs = take (length (hd descr), recTs); |
285 val newTs = take (length (hd descr), recTs); |
|
286 val T' = TFree (variant used "'t", HOLogic.termS); |
284 |
287 |
285 val case_dummy_fns = map (fn (_, (_, _, constrs)) => map (fn (_, cargs) => |
288 val case_dummy_fns = map (fn (_, (_, _, constrs)) => map (fn (_, cargs) => |
286 let |
289 let |
287 val Ts = map (typ_of_dtyp descr' sorts) cargs; |
290 val Ts = map (typ_of_dtyp descr' sorts) cargs; |
288 val free = TFree ("'t", HOLogic.termS); |
291 val Ts' = replicate (length (filter is_rec_type cargs)) T' |
289 val Ts' = replicate (length (filter is_rec_type cargs)) free |
292 in Const ("arbitrary", Ts @ Ts' ---> T') |
290 in Const ("arbitrary", Ts @ Ts' ---> free) |
|
291 end) constrs) descr'; |
293 end) constrs) descr'; |
292 |
294 |
293 val case_names = map (fn s => |
295 val case_names = map (fn s => |
294 Sign.full_name (sign_of thy) (s ^ "_case")) new_type_names; |
296 Sign.full_name (sign_of thy) (s ^ "_case")) new_type_names; |
295 |
297 |
296 (* define case combinators via primrec combinators *) |
298 (* define case combinators via primrec combinators *) |
297 |
299 |
298 val (case_defs, thy2) = foldl (fn ((defs, thy), |
300 val (case_defs, thy2) = foldl (fn ((defs, thy), |
299 ((((i, (_, _, constrs)), T), name), recname)) => |
301 ((((i, (_, _, constrs)), T), name), recname)) => |
300 let |
302 let |
301 val T' = TFree ("'t", HOLogic.termS); |
|
302 |
|
303 val (fns1, fns2) = ListPair.unzip (map (fn ((_, cargs), j) => |
303 val (fns1, fns2) = ListPair.unzip (map (fn ((_, cargs), j) => |
304 let |
304 let |
305 val Ts = map (typ_of_dtyp descr' sorts) cargs; |
305 val Ts = map (typ_of_dtyp descr' sorts) cargs; |
306 val Ts' = Ts @ (replicate (length (filter is_rec_type cargs)) T'); |
306 val Ts' = Ts @ (replicate (length (filter is_rec_type cargs)) T'); |
307 val frees' = map (uncurry (mk_Free "x")) (Ts' ~~ (1 upto length Ts')); |
307 val frees' = map (uncurry (mk_Free "x")) (Ts' ~~ (1 upto length Ts')); |