author | wenzelm |
Fri, 13 Nov 2009 21:11:15 +0100 | |
changeset 33671 | 4b0f2599ed48 |
parent 33643 | b275f26a638b |
child 33766 | c679f05600cd |
permissions | -rw-r--r-- |
31775 | 1 |
(* Title: HOL/Tools/Function/mutual.ML |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
2 |
Author: Alexander Krauss, TU Muenchen |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
3 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
4 |
A package for general recursive function definitions. |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
5 |
Tools for mutual recursive definitions. |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
6 |
*) |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
7 |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
8 |
signature FUNCTION_MUTUAL = |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
9 |
sig |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
10 |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
11 |
val prepare_function_mutual : Function_Common.function_config |
22166 | 12 |
-> string (* defname *) |
13 |
-> ((string * typ) * mixfix) list |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
14 |
-> term list |
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
15 |
-> local_theory |
22166 | 16 |
-> ((thm (* goalstate *) |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
17 |
* (thm -> Function_Common.function_result) (* proof continuation *) |
22166 | 18 |
) * local_theory) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
19 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
20 |
end |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
21 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
22 |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
23 |
structure Function_Mutual: FUNCTION_MUTUAL = |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
24 |
struct |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
25 |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
26 |
open Function_Lib |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
27 |
open Function_Common |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
28 |
|
22166 | 29 |
type qgar = string * (string * typ) list * term list * term list * term |
30 |
||
23217 | 31 |
fun name_of_fqgar ((f, _, _, _, _): qgar) = f |
22166 | 32 |
|
33 |
datatype mutual_part = |
|
34 |
MutualPart of |
|
35 |
{ |
|
23494 | 36 |
i : int, |
37 |
i' : int, |
|
22166 | 38 |
fvar : string * typ, |
39 |
cargTs: typ list, |
|
40 |
f_def: term, |
|
41 |
||
42 |
f: term option, |
|
43 |
f_defthm : thm option |
|
44 |
} |
|
45 |
||
46 |
||
47 |
datatype mutual_info = |
|
48 |
Mutual of |
|
49 |
{ |
|
23494 | 50 |
n : int, |
51 |
n' : int, |
|
22166 | 52 |
fsum_var : string * typ, |
53 |
||
54 |
ST: typ, |
|
55 |
RST: typ, |
|
56 |
||
57 |
parts: mutual_part list, |
|
58 |
fqgars: qgar list, |
|
59 |
qglrs: ((string * typ) list * term list * term * term) list, |
|
60 |
||
61 |
fsum : term option |
|
62 |
} |
|
63 |
||
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
64 |
fun mutual_induct_Pnames n = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
65 |
if n < 5 then fst (chop n ["P","Q","R","S"]) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
66 |
else map (fn i => "P" ^ string_of_int i) (1 upto n) |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
67 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
68 |
fun get_part fname = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
69 |
the o find_first (fn (MutualPart {fvar=(n,_), ...}) => n = fname) |
21237 | 70 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
71 |
(* FIXME *) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
72 |
fun mk_prod_abs e (t1, t2) = |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
73 |
let |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
74 |
val bTs = rev (map snd e) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
75 |
val T1 = fastype_of1 (bTs, t1) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
76 |
val T2 = fastype_of1 (bTs, t2) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
77 |
in |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
78 |
HOLogic.pair_const T1 T2 $ t1 $ t2 |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
79 |
end; |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
80 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
81 |
|
22166 | 82 |
fun analyze_eqs ctxt defname fs eqs = |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
83 |
let |
23494 | 84 |
val num = length fs |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
85 |
val fnames = map fst fs |
24170 | 86 |
val fqgars = map (split_def ctxt) eqs |
30906
3c7a76e79898
simplify computation and consistency checks of argument counts in the input
krauss
parents:
30607
diff
changeset
|
87 |
val arity_of = map (fn (fname,_,_,args,_) => (fname, length args)) fqgars |
3c7a76e79898
simplify computation and consistency checks of argument counts in the input
krauss
parents:
30607
diff
changeset
|
88 |
|> AList.lookup (op =) #> the |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
89 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
90 |
fun curried_types (fname, fT) = |
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
91 |
let |
30906
3c7a76e79898
simplify computation and consistency checks of argument counts in the input
krauss
parents:
30607
diff
changeset
|
92 |
val (caTs, uaTs) = chop (arity_of fname) (binder_types fT) |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
93 |
in |
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
94 |
(caTs, uaTs ---> body_type fT) |
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
95 |
end |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
96 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
97 |
val (caTss, resultTs) = split_list (map curried_types fs) |
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
98 |
val argTs = map (foldr1 HOLogic.mk_prodT) caTss |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
99 |
|
32029
94b4a921c88d
compare types directly -- no need to invoke Type.eq_type with empty environment;
wenzelm
parents:
31775
diff
changeset
|
100 |
val dresultTs = distinct (op =) resultTs |
23494 | 101 |
val n' = length dresultTs |
102 |
||
32765 | 103 |
val RST = Balanced_Tree.make (uncurry SumTree.mk_sumT) dresultTs |
104 |
val ST = Balanced_Tree.make (uncurry SumTree.mk_sumT) argTs |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
105 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
106 |
val fsum_type = ST --> RST |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
107 |
|
22166 | 108 |
val ([fsum_var_name], _) = Variable.add_fixes [ defname ^ "_sum" ] ctxt |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
109 |
val fsum_var = (fsum_var_name, fsum_type) |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
110 |
|
23494 | 111 |
fun define (fvar as (n, T)) caTs resultT i = |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
112 |
let |
23494 | 113 |
val vars = map_index (fn (j,T) => Free ("x" ^ string_of_int j, T)) caTs (* FIXME: Bind xs properly *) |
32029
94b4a921c88d
compare types directly -- no need to invoke Type.eq_type with empty environment;
wenzelm
parents:
31775
diff
changeset
|
114 |
val i' = find_index (fn Ta => Ta = resultT) dresultTs + 1 |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
115 |
|
25555 | 116 |
val f_exp = SumTree.mk_proj RST n' i' (Free fsum_var $ SumTree.mk_inj ST num i (foldr1 HOLogic.mk_prod vars)) |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
117 |
val def = Term.abstract_over (Free fsum_var, fold_rev lambda vars f_exp) |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
118 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
119 |
val rew = (n, fold_rev lambda vars f_exp) |
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
120 |
in |
23494 | 121 |
(MutualPart {i=i, i'=i', fvar=fvar,cargTs=caTs,f_def=def,f=NONE,f_defthm=NONE}, rew) |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
122 |
end |
21237 | 123 |
|
23494 | 124 |
val (parts, rews) = split_list (map4 define fs caTss resultTs (1 upto num)) |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
125 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
126 |
fun convert_eqs (f, qs, gs, args, rhs) = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
127 |
let |
23494 | 128 |
val MutualPart {i, i', ...} = get_part f parts |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
129 |
in |
25555 | 130 |
(qs, gs, SumTree.mk_inj ST num i (foldr1 (mk_prod_abs qs) args), |
131 |
SumTree.mk_inj RST n' i' (replace_frees rews rhs) |
|
23494 | 132 |
|> Envir.beta_norm) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
133 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
134 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
135 |
val qglrs = map convert_eqs fqgars |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
136 |
in |
23494 | 137 |
Mutual {n=num, n'=n', fsum_var=fsum_var, ST=ST, RST=RST, |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
138 |
parts=parts, fqgars=fqgars, qglrs=qglrs, fsum=NONE} |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
139 |
end |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
140 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
141 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
142 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
143 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
144 |
fun define_projections fixes mutual fsum lthy = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
145 |
let |
23494 | 146 |
fun def ((MutualPart {i=i, i'=i', fvar=(fname, fT), cargTs, f_def, ...}), (_, mixfix)) lthy = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
147 |
let |
21436 | 148 |
val ((f, (_, f_defthm)), lthy') = |
33671 | 149 |
Local_Theory.define "" |
33279 | 150 |
((Binding.name fname, mixfix), |
33393
2240b0e7fa10
do not use Binding.empty: conceal flag gets lost in Thm.def_binding_optional
krauss
parents:
33279
diff
changeset
|
151 |
((Binding.conceal (Binding.name (fname ^ "_def")), []), |
2240b0e7fa10
do not use Binding.empty: conceal flag gets lost in Thm.def_binding_optional
krauss
parents:
33279
diff
changeset
|
152 |
Term.subst_bound (fsum, f_def))) lthy |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
153 |
in |
23494 | 154 |
(MutualPart {i=i, i'=i', fvar=(fname, fT), cargTs=cargTs, f_def=f_def, |
21237 | 155 |
f=SOME f, f_defthm=SOME f_defthm }, |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
156 |
lthy') |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
157 |
end |
21237 | 158 |
|
23494 | 159 |
val Mutual { n, n', fsum_var, ST, RST, parts, fqgars, qglrs, ... } = mutual |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
160 |
val (parts', lthy') = fold_map def (parts ~~ fixes) lthy |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
161 |
in |
23494 | 162 |
(Mutual { n=n, n'=n', fsum_var=fsum_var, ST=ST, RST=RST, parts=parts', |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
163 |
fqgars=fqgars, qglrs=qglrs, fsum=SOME fsum }, |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
164 |
lthy') |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
165 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
166 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
167 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
168 |
fun in_context ctxt (f, pre_qs, pre_gs, pre_args, pre_rhs) F = |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
169 |
let |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
170 |
val thy = ProofContext.theory_of ctxt |
21237 | 171 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
172 |
val oqnames = map fst pre_qs |
20797
c1f0bc7e7d80
renamed Variable.invent_fixes to Variable.variant_fixes;
wenzelm
parents:
20654
diff
changeset
|
173 |
val (qs, ctxt') = Variable.variant_fixes oqnames ctxt |
21237 | 174 |
|>> map2 (fn (_, T) => fn n => Free (n, T)) pre_qs |
175 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
176 |
fun inst t = subst_bounds (rev qs, t) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
177 |
val gs = map inst pre_gs |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
178 |
val args = map inst pre_args |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
179 |
val rhs = inst pre_rhs |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
180 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
181 |
val cqs = map (cterm_of thy) qs |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
182 |
val ags = map (assume o cterm_of thy) gs |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
183 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
184 |
val import = fold forall_elim cqs |
24977
9f98751c9628
replaced (flip Thm.implies_elim) by Thm.elim_implies;
wenzelm
parents:
24171
diff
changeset
|
185 |
#> fold Thm.elim_implies ags |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
186 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
187 |
val export = fold_rev (implies_intr o cprop_of) ags |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
188 |
#> fold_rev forall_intr_rename (oqnames ~~ cqs) |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
189 |
in |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
190 |
F ctxt (f, qs, gs, args, rhs) import export |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
191 |
end |
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
192 |
|
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
193 |
fun recover_mutual_psimp all_orig_fdefs parts ctxt (fname, _, _, args, rhs) import (export : thm -> thm) sum_psimp_eq = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
194 |
let |
23494 | 195 |
val (MutualPart {f=SOME f, f_defthm=SOME f_def, ...}) = get_part fname parts |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
196 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
197 |
val psimp = import sum_psimp_eq |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
198 |
val (simp, restore_cond) = case cprems_of psimp of |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
199 |
[] => (psimp, I) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
200 |
| [cond] => (implies_elim psimp (assume cond), implies_intr cond) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
201 |
| _ => sys_error "Too many conditions" |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
202 |
in |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
203 |
Goal.prove ctxt [] [] |
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
204 |
(HOLogic.Trueprop $ HOLogic.mk_eq (list_comb (f, args), rhs)) |
24171
25381ce95316
Issue a warning, when "function" encounters variables occuring in function position,
krauss
parents:
24170
diff
changeset
|
205 |
(fn _ => (LocalDefs.unfold_tac ctxt all_orig_fdefs) |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
206 |
THEN EqSubst.eqsubst_tac ctxt [0] [simp] 1 |
32149
ef59550a55d3
renamed simpset_of to global_simpset_of, and local_simpset_of to simpset_of -- same for claset and clasimpset;
wenzelm
parents:
32029
diff
changeset
|
207 |
THEN (simp_tac (simpset_of ctxt addsimps SumTree.proj_in_rules)) 1) |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
208 |
|> restore_cond |
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
209 |
|> export |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
210 |
end |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
211 |
|
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
212 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
213 |
(* FIXME HACK *) |
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
214 |
fun mk_applied_form ctxt caTs thm = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
215 |
let |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
216 |
val thy = ProofContext.theory_of ctxt |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
217 |
val xs = map_index (fn (i,T) => cterm_of thy (Free ("x" ^ string_of_int i, T))) caTs (* FIXME: Bind xs properly *) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
218 |
in |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
219 |
fold (fn x => fn thm => combination thm (reflexive x)) xs thm |
26196 | 220 |
|> Conv.fconv_rule (Thm.beta_conversion true) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
221 |
|> fold_rev forall_intr xs |
26653 | 222 |
|> Thm.forall_elim_vars 0 |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
223 |
end |
23494 | 224 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
225 |
|
23494 | 226 |
fun mutual_induct_rules lthy induct all_f_defs (Mutual {n, ST, RST, parts, ...}) = |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
227 |
let |
22623 | 228 |
val cert = cterm_of (ProofContext.theory_of lthy) |
20949 | 229 |
val newPs = map2 (fn Pname => fn MutualPart {cargTs, ...} => |
21237 | 230 |
Free (Pname, cargTs ---> HOLogic.boolT)) |
20949 | 231 |
(mutual_induct_Pnames (length parts)) |
232 |
parts |
|
21237 | 233 |
|
234 |
fun mk_P (MutualPart {cargTs, ...}) P = |
|
235 |
let |
|
236 |
val avars = map_index (fn (i,T) => Var (("a", i), T)) cargTs |
|
237 |
val atup = foldr1 HOLogic.mk_prod avars |
|
238 |
in |
|
239 |
tupled_lambda atup (list_comb (P, avars)) |
|
240 |
end |
|
241 |
||
242 |
val Ps = map2 mk_P parts newPs |
|
25555 | 243 |
val case_exp = SumTree.mk_sumcases HOLogic.boolT Ps |
21237 | 244 |
|
245 |
val induct_inst = |
|
22623 | 246 |
forall_elim (cert case_exp) induct |
25555 | 247 |
|> full_simplify SumTree.sumcase_split_ss |
21237 | 248 |
|> full_simplify (HOL_basic_ss addsimps all_f_defs) |
249 |
||
25046 | 250 |
fun project rule (MutualPart {cargTs, i, ...}) k = |
21237 | 251 |
let |
25046 | 252 |
val afs = map_index (fn (j,T) => Free ("a" ^ string_of_int (j + k), T)) cargTs (* FIXME! *) |
25555 | 253 |
val inj = SumTree.mk_inj ST n i (foldr1 HOLogic.mk_prod afs) |
21237 | 254 |
in |
25046 | 255 |
(rule |
22623 | 256 |
|> forall_elim (cert inj) |
25555 | 257 |
|> full_simplify SumTree.sumcase_split_ss |
25046 | 258 |
|> fold_rev (forall_intr o cert) (afs @ newPs), |
259 |
k + length cargTs) |
|
21237 | 260 |
end |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
261 |
in |
25046 | 262 |
fst (fold_map (project induct_inst) parts 0) |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
263 |
end |
21237 | 264 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
265 |
|
22623 | 266 |
fun mk_partial_rules_mutual lthy inner_cont (m as Mutual {parts, fqgars, ...}) proof = |
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
267 |
let |
22166 | 268 |
val result = inner_cont proof |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
269 |
val FunctionResult {fs=[f], G, R, cases, psimps, trsimps, simple_pinducts=[simple_pinduct], |
22166 | 270 |
termination,domintros} = result |
21237 | 271 |
|
22733
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22623
diff
changeset
|
272 |
val (all_f_defs, fs) = map (fn MutualPart {f_defthm = SOME f_def, f = SOME f, cargTs, ...} => |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22623
diff
changeset
|
273 |
(mk_applied_form lthy cargTs (symmetric f_def), f)) |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22623
diff
changeset
|
274 |
parts |
0b14bb35be90
definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents:
22623
diff
changeset
|
275 |
|> split_list |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
276 |
|
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
277 |
val all_orig_fdefs = map (fn MutualPart {f_defthm = SOME f_def, ...} => f_def) parts |
21237 | 278 |
|
20878
384c5bb713b2
mk_partial_rules_mutual: expand result terms/thms;
wenzelm
parents:
20851
diff
changeset
|
279 |
fun mk_mpsimp fqgar sum_psimp = |
22497
1fe951654cee
fixed problem with the construction of mutual simp rules
krauss
parents:
22323
diff
changeset
|
280 |
in_context lthy fqgar (recover_mutual_psimp all_orig_fdefs parts) sum_psimp |
21237 | 281 |
|
23189 | 282 |
val rew_ss = HOL_basic_ss addsimps all_f_defs |
22166 | 283 |
val mpsimps = map2 mk_mpsimp fqgars psimps |
284 |
val mtrsimps = map_option (map2 mk_mpsimp fqgars) trsimps |
|
22623 | 285 |
val minducts = mutual_induct_rules lthy simple_pinduct all_f_defs m |
23189 | 286 |
val mtermination = full_simplify rew_ss termination |
287 |
val mdomintros = map_option (map (full_simplify rew_ss)) domintros |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
288 |
in |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
289 |
FunctionResult { fs=fs, G=G, R=R, |
26529 | 290 |
psimps=mpsimps, simple_pinducts=minducts, |
22623 | 291 |
cases=cases, termination=mtermination, |
23189 | 292 |
domintros=mdomintros, |
22623 | 293 |
trsimps=mtrsimps} |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
19922
diff
changeset
|
294 |
end |
21237 | 295 |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
296 |
fun prepare_function_mutual config defname fixes eqss lthy = |
22166 | 297 |
let |
22323
b8c227d8ca91
beta_eta_contract specification befor processing. These normal forms avoid unpleasant surprises later on.
krauss
parents:
22244
diff
changeset
|
298 |
val mutual = analyze_eqs lthy defname (map fst fixes) (map Envir.beta_eta_contract eqss) |
22166 | 299 |
val Mutual {fsum_var=(n, T), qglrs, ...} = mutual |
300 |
||
301 |
val ((fsum, goalstate, cont), lthy') = |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
32765
diff
changeset
|
302 |
Function_Core.prepare_function config defname [((n, T), NoSyn)] qglrs lthy |
22166 | 303 |
|
304 |
val (mutual', lthy'') = define_projections fixes mutual fsum lthy' |
|
305 |
||
306 |
val mutual_cont = mk_partial_rules_mutual lthy'' cont mutual' |
|
307 |
in |
|
23819 | 308 |
((goalstate, mutual_cont), lthy'') |
22166 | 309 |
end |
310 |
||
21237 | 311 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
diff
changeset
|
312 |
end |