| author | wenzelm |
| Wed, 30 Mar 2016 23:46:44 +0200 | |
| changeset 62775 | b486f512a471 |
| parent 62326 | 3cf7a067599c |
| child 63859 | dca6fabd8060 |
| permissions | -rw-r--r-- |
| 55571 | 1 |
(* Title: HOL/Tools/BNF/bnf_lfp_rec_sugar_more.ML |
2 |
Author: Lorenz Panny, TU Muenchen |
|
3 |
Author: Jasmin Blanchette, TU Muenchen |
|
4 |
Copyright 2013 |
|
5 |
||
| 58315 | 6 |
More recursor sugar. |
| 55571 | 7 |
*) |
8 |
||
| 60000 | 9 |
signature BNF_LFP_REC_SUGAR_MORE = |
10 |
sig |
|
11 |
val massage_nested_rec_call: Proof.context -> (term -> bool) -> (typ -> typ -> term -> term) -> |
|
| 60002 | 12 |
(typ * typ -> term) -> typ list -> term -> term -> term -> term |
| 60000 | 13 |
end; |
14 |
||
15 |
structure BNF_LFP_Rec_Sugar_More : BNF_LFP_REC_SUGAR_MORE = |
|
| 55571 | 16 |
struct |
17 |
||
18 |
open BNF_Util |
|
19 |
open BNF_Def |
|
20 |
open BNF_FP_Util |
|
21 |
open BNF_FP_Def_Sugar |
|
22 |
open BNF_FP_N2M_Sugar |
|
23 |
open BNF_LFP_Rec_Sugar |
|
24 |
||
| 58389 | 25 |
(* FIXME: remove "nat" cases throughout once it is registered as a datatype *) |
26 |
||
| 57993 | 27 |
val nested_simps = @{thms o_def[abs_def] id_def split fst_conv snd_conv};
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
28 |
|
| 58389 | 29 |
fun is_new_datatype _ @{type_name nat} = true
|
30 |
| is_new_datatype ctxt s = |
|
31 |
(case fp_sugar_of ctxt s of SOME {fp = Least_FP, ...} => true | _ => false);
|
|
| 55571 | 32 |
|
| 58461 | 33 |
fun basic_lfp_sugar_of C fun_arg_Tsss ({T, fp_res_index, fp_ctr_sugar = {ctr_sugar, ...},
|
34 |
fp_co_induct_sugar = {co_rec = recx, co_rec_thms = rec_thms, ...}, ...} : fp_sugar) =
|
|
| 58388 | 35 |
{T = T, fp_res_index = fp_res_index, C = C, fun_arg_Tsss = fun_arg_Tsss, ctr_sugar = ctr_sugar,
|
36 |
recx = recx, rec_thms = rec_thms}; |
|
| 55571 | 37 |
|
| 58389 | 38 |
fun basic_lfp_sugars_of _ [@{typ nat}] _ _ lthy =
|
|
62326
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
39 |
([], [0], [nat_basic_lfp_sugar], [], [], [], TrueI (*dummy*), [], false, lthy) |
| 58389 | 40 |
| basic_lfp_sugars_of bs arg_Ts callers callssss0 lthy0 = |
41 |
let |
|
42 |
val ((missing_arg_Ts, perm0_kks, |
|
| 61760 | 43 |
fp_sugars as {fp_nesting_bnfs,
|
44 |
fp_co_induct_sugar = {common_co_inducts = [common_induct], ...}, ...} :: _,
|
|
| 58389 | 45 |
(lfp_sugar_thms, _)), lthy) = |
46 |
nested_to_mutual_fps (K true) Least_FP bs arg_Ts callers callssss0 lthy0; |
|
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55571
diff
changeset
|
47 |
|
| 58389 | 48 |
val induct_attrs = (case lfp_sugar_thms of SOME ((_, _, attrs), _) => attrs | NONE => []); |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58131
diff
changeset
|
49 |
|
| 58389 | 50 |
val Ts = map #T fp_sugars; |
51 |
val Xs = map #X fp_sugars; |
|
| 58461 | 52 |
val Cs = map (body_type o fastype_of o #co_rec o #fp_co_induct_sugar) fp_sugars; |
| 58389 | 53 |
val Xs_TCs = Xs ~~ (Ts ~~ Cs); |
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55571
diff
changeset
|
54 |
|
| 58389 | 55 |
fun zip_XrecT (Type (s, Us)) = [Type (s, map (HOLogic.mk_tupleT o zip_XrecT) Us)] |
56 |
| zip_XrecT U = |
|
57 |
(case AList.lookup (op =) Xs_TCs U of |
|
58 |
SOME (T, C) => [T, C] |
|
59 |
| NONE => [U]); |
|
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55571
diff
changeset
|
60 |
|
| 58460 | 61 |
val ctrXs_Tsss = map (#ctrXs_Tss o #fp_ctr_sugar) fp_sugars; |
| 58389 | 62 |
val fun_arg_Tssss = map (map (map zip_XrecT)) ctrXs_Tsss; |
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55571
diff
changeset
|
63 |
|
| 58389 | 64 |
val fp_nesting_map_ident0s = map map_ident0_of_bnf fp_nesting_bnfs; |
65 |
val fp_nesting_map_comps = map map_comp_of_bnf fp_nesting_bnfs; |
|
|
62326
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
66 |
val fp_nesting_pred_maps = map pred_map_of_bnf fp_nesting_bnfs; |
| 58389 | 67 |
in |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58461
diff
changeset
|
68 |
(missing_arg_Ts, perm0_kks, @{map 3} basic_lfp_sugar_of Cs fun_arg_Tssss fp_sugars,
|
|
62326
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
69 |
fp_nesting_map_ident0s, fp_nesting_map_comps, fp_nesting_pred_maps, common_induct, |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
70 |
induct_attrs, is_some lfp_sugar_thms, lthy) |
| 58389 | 71 |
end; |
| 55571 | 72 |
|
| 58387 | 73 |
exception NO_MAP of term; |
| 55571 | 74 |
|
75 |
fun ill_formed_rec_call ctxt t = |
|
76 |
error ("Ill-formed recursive call: " ^ quote (Syntax.string_of_term ctxt t));
|
|
77 |
fun invalid_map ctxt t = |
|
78 |
error ("Invalid map function in " ^ quote (Syntax.string_of_term ctxt t));
|
|
| 60001 | 79 |
fun unexpected_rec_call ctxt eqns t = |
80 |
error_at ctxt eqns ("Unexpected recursive call in " ^ quote (Syntax.string_of_term ctxt t));
|
|
| 55571 | 81 |
|
| 60002 | 82 |
fun massage_nested_rec_call ctxt has_call massage_fun massage_nonfun bound_Ts y y' t0 = |
| 55571 | 83 |
let |
| 60001 | 84 |
fun check_no_call t = if has_call t then unexpected_rec_call ctxt [t0] t else (); |
| 55571 | 85 |
|
86 |
val typof = curry fastype_of1 bound_Ts; |
|
| 60002 | 87 |
val massage_no_call = build_map ctxt [] massage_nonfun; |
| 55571 | 88 |
|
89 |
val yT = typof y; |
|
90 |
val yU = typof y'; |
|
91 |
||
| 60002 | 92 |
fun y_of_y' () = massage_no_call (yU, yT) $ y'; |
| 55571 | 93 |
val elim_y = Term.map_aterms (fn t => if t = y then y_of_y' () else t); |
94 |
||
95 |
fun massage_mutual_fun U T t = |
|
96 |
(case t of |
|
97 |
Const (@{const_name comp}, _) $ t1 $ t2 =>
|
|
98 |
mk_comp bound_Ts (tap check_no_call t1, massage_mutual_fun U T t2) |
|
99 |
| _ => |
|
| 60002 | 100 |
if has_call t then massage_fun U T t else mk_comp bound_Ts (t, massage_no_call (U, T))); |
| 55571 | 101 |
|
102 |
fun massage_map (Type (_, Us)) (Type (s, Ts)) t = |
|
103 |
(case try (dest_map ctxt s) t of |
|
104 |
SOME (map0, fs) => |
|
105 |
let |
|
106 |
val Type (_, ran_Ts) = range_type (typof t); |
|
107 |
val map' = mk_map (length fs) Us ran_Ts map0; |
|
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58461
diff
changeset
|
108 |
val fs' = map_flattened_map_args ctxt s (@{map 3} massage_map_or_map_arg Us Ts) fs;
|
| 55571 | 109 |
in |
110 |
Term.list_comb (map', fs') |
|
111 |
end |
|
|
62326
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
112 |
| NONE => |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
113 |
(case try (dest_pred ctxt s) t of |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
114 |
SOME (pred0, fs) => |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
115 |
let |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
116 |
val pred' = mk_pred Us pred0; |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
117 |
val fs' = map_flattened_map_args ctxt s (@{map 3} massage_map_or_map_arg Us Ts) fs;
|
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
118 |
in |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
119 |
Term.list_comb (pred', fs') |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
120 |
end |
|
3cf7a067599c
allow predicator instead of map function in 'primrec'
blanchet
parents:
61760
diff
changeset
|
121 |
| NONE => raise NO_MAP t)) |
| 58387 | 122 |
| massage_map _ _ t = raise NO_MAP t |
| 55571 | 123 |
and massage_map_or_map_arg U T t = |
124 |
if T = U then |
|
125 |
tap check_no_call t |
|
126 |
else |
|
127 |
massage_map U T t |
|
| 58387 | 128 |
handle NO_MAP _ => massage_mutual_fun U T t; |
| 55571 | 129 |
|
| 60001 | 130 |
fun massage_outer_call (t as t1 $ t2) = |
| 55571 | 131 |
if has_call t then |
132 |
if t2 = y then |
|
133 |
massage_map yU yT (elim_y t1) $ y' |
|
| 58387 | 134 |
handle NO_MAP t' => invalid_map ctxt t' |
| 55571 | 135 |
else |
136 |
let val (g, xs) = Term.strip_comb t2 in |
|
137 |
if g = y then |
|
| 60001 | 138 |
if exists has_call xs then unexpected_rec_call ctxt [t0] t2 |
139 |
else Term.list_comb (massage_outer_call (mk_compN (length xs) bound_Ts (t1, y)), xs) |
|
| 55571 | 140 |
else |
141 |
ill_formed_rec_call ctxt t |
|
142 |
end |
|
143 |
else |
|
144 |
elim_y t |
|
| 60001 | 145 |
| massage_outer_call t = if t = y then y_of_y' () else ill_formed_rec_call ctxt t; |
| 55571 | 146 |
in |
| 60001 | 147 |
massage_outer_call t0 |
| 55571 | 148 |
end; |
149 |
||
| 60001 | 150 |
fun rewrite_map_fun ctxt get_ctr_pos U T t = |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
151 |
let |
| 60001 | 152 |
val _ = |
153 |
(case try HOLogic.dest_prodT U of |
|
154 |
SOME (U1, _) => U1 = T orelse invalid_map ctxt t |
|
155 |
| NONE => invalid_map ctxt t); |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
156 |
|
| 60001 | 157 |
fun subst d (t as Bound d') = t |> d = SOME d' ? curry (op $) (fst_const U) |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
158 |
| subst d (Abs (v, T, b)) = |
| 60001 | 159 |
Abs (v, if d = SOME ~1 then U else T, subst (Option.map (Integer.add 1) d) b) |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
160 |
| subst d t = |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
161 |
let |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
162 |
val (u, vs) = strip_comb t; |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
163 |
val ctr_pos = try (get_ctr_pos o fst o dest_Free) u |> the_default ~1; |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
164 |
in |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
165 |
if ctr_pos >= 0 then |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
166 |
if d = SOME ~1 andalso length vs = ctr_pos then |
| 60001 | 167 |
Term.list_comb (permute_args ctr_pos (snd_const U), vs) |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
168 |
else if length vs > ctr_pos andalso is_some d andalso |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
169 |
d = try (fn Bound n => n) (nth vs ctr_pos) then |
| 60001 | 170 |
Term.list_comb (snd_const U $ nth vs ctr_pos, map (subst d) (nth_drop ctr_pos vs)) |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
171 |
else |
| 59595 | 172 |
error ("Recursive call not directly applied to constructor argument in " ^
|
173 |
quote (Syntax.string_of_term ctxt t)) |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
174 |
else |
| 60001 | 175 |
Term.list_comb (u, map (subst (if d = SOME ~1 then NONE else d)) vs) |
176 |
end; |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
177 |
in |
| 60001 | 178 |
subst (SOME ~1) t |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
179 |
end; |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
180 |
|
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
181 |
fun rewrite_nested_rec_call ctxt has_call get_ctr_pos = |
| 60002 | 182 |
massage_nested_rec_call ctxt has_call (rewrite_map_fun ctxt get_ctr_pos) (fst_const o fst); |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
183 |
|
| 55571 | 184 |
val _ = Theory.setup (register_lfp_rec_extension |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
185 |
{nested_simps = nested_simps, is_new_datatype = is_new_datatype,
|
| 58389 | 186 |
basic_lfp_sugars_of = basic_lfp_sugars_of, |
187 |
rewrite_nested_rec_call = SOME rewrite_nested_rec_call}); |
|
| 55571 | 188 |
|
189 |
end; |