author | wenzelm |
Tue, 14 Nov 2006 22:16:58 +0100 | |
changeset 21367 | 7a0cc1bb4dcc |
parent 21350 | 6e58289b6685 |
child 21390 | b3a9d8a83dea |
permissions | -rw-r--r-- |
5094 | 1 |
(* Title: HOL/Tools/inductive_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
4 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
5094 | 5 |
|
6424 | 6 |
(Co)Inductive Definition module for HOL. |
5094 | 7 |
|
8 |
Features: |
|
6424 | 9 |
* least or greatest fixedpoints |
10 |
* mutually recursive definitions |
|
11 |
* definitions involving arbitrary monotone operators |
|
12 |
* automatically proves introduction and elimination rules |
|
5094 | 13 |
|
14 |
Introduction rules have the form |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
15 |
[| M Pj ti, ..., Q x, ... |] ==> Pk t |
5094 | 16 |
where M is some monotone operator (usually the identity) |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
17 |
Q x is any side condition on the free variables |
5094 | 18 |
ti, t are any terms |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
19 |
Pj, Pk are two of the predicates being defined in mutual recursion |
5094 | 20 |
*) |
21 |
||
22 |
signature INDUCTIVE_PACKAGE = |
|
23 |
sig |
|
6424 | 24 |
val quiet_mode: bool ref |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
25 |
type inductive_result |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
26 |
type inductive_info |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
27 |
val get_inductive: Proof.context -> string -> inductive_info option |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
28 |
val print_inductives: Proof.context -> unit |
18728 | 29 |
val mono_add: attribute |
30 |
val mono_del: attribute |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
31 |
val get_monos: Proof.context -> thm list |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
32 |
val mk_cases: Proof.context -> term -> thm |
10910
058775a575db
export inductive_forall_name, inductive_forall_def, rulify;
wenzelm
parents:
10804
diff
changeset
|
33 |
val inductive_forall_name: string |
058775a575db
export inductive_forall_name, inductive_forall_def, rulify;
wenzelm
parents:
10804
diff
changeset
|
34 |
val inductive_forall_def: thm |
058775a575db
export inductive_forall_name, inductive_forall_def, rulify;
wenzelm
parents:
10804
diff
changeset
|
35 |
val rulify: thm -> thm |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
36 |
val inductive_cases: ((bstring * Attrib.src list) * string list) list -> |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
37 |
Proof.context -> thm list list * local_theory |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
38 |
val inductive_cases_i: ((bstring * Attrib.src list) * term list) list -> |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
39 |
Proof.context -> thm list list * local_theory |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
40 |
val add_inductive_i: bool -> bstring -> bool -> bool -> bool -> |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
41 |
(string * typ option * mixfix) list -> |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
42 |
(string * typ option) list -> ((bstring * Attrib.src list) * term) list -> thm list -> |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
43 |
local_theory -> inductive_result * local_theory |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
44 |
val add_inductive: bool -> bool -> (string * string option * mixfix) list -> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
45 |
(string * string option * mixfix) list -> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
46 |
((bstring * Attrib.src list) * string) list -> (thmref * Attrib.src list) list -> |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
47 |
local_theory -> inductive_result * local_theory |
18708 | 48 |
val setup: theory -> theory |
5094 | 49 |
end; |
50 |
||
6424 | 51 |
structure InductivePackage: INDUCTIVE_PACKAGE = |
5094 | 52 |
struct |
53 |
||
9598 | 54 |
|
10729 | 55 |
(** theory context references **) |
56 |
||
15525 | 57 |
val mono_name = "Orderings.mono"; |
17010
5abc26872268
changed reference to Lfp.lfp to FixedPint.lfp, ditto for gfp
avigad
parents:
16975
diff
changeset
|
58 |
val gfp_name = "FixedPoint.gfp"; |
5abc26872268
changed reference to Lfp.lfp to FixedPint.lfp, ditto for gfp
avigad
parents:
16975
diff
changeset
|
59 |
val lfp_name = "FixedPoint.lfp"; |
10735 | 60 |
|
11991 | 61 |
val inductive_forall_name = "HOL.induct_forall"; |
62 |
val inductive_forall_def = thm "induct_forall_def"; |
|
63 |
val inductive_conj_name = "HOL.induct_conj"; |
|
64 |
val inductive_conj_def = thm "induct_conj_def"; |
|
65 |
val inductive_conj = thms "induct_conj"; |
|
66 |
val inductive_atomize = thms "induct_atomize"; |
|
18463 | 67 |
val inductive_rulify = thms "induct_rulify"; |
68 |
val inductive_rulify_fallback = thms "induct_rulify_fallback"; |
|
10729 | 69 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
70 |
val notTrueE = TrueI RSN (2, notE); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
71 |
val notFalseI = Seq.hd (atac 1 notI); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
72 |
val simp_thms' = map (fn s => mk_meta_eq (the (find_first |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
73 |
(equal (term_of (read_cterm HOL.thy (s, propT))) o prop_of) simp_thms))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
74 |
["(~True) = False", "(~False) = True", |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
75 |
"(True --> ?P) = ?P", "(False --> ?P) = True", |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
76 |
"(?P & True) = ?P", "(True & ?P) = ?P"]; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
77 |
|
10729 | 78 |
|
79 |
||
10735 | 80 |
(** theory data **) |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
81 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
82 |
type inductive_result = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
83 |
{preds: term list, defs: thm list, elims: thm list, raw_induct: thm, |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
84 |
induct: thm, intrs: thm list, mono: thm, unfold: thm}; |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
85 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
86 |
type inductive_info = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
87 |
{names: string list, coind: bool} * inductive_result; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
88 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
89 |
structure InductiveData = GenericDataFun |
16432 | 90 |
(struct |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
91 |
val name = "HOL/inductive2"; |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
92 |
type T = inductive_info Symtab.table * thm list; |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
93 |
|
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
94 |
val empty = (Symtab.empty, []); |
16432 | 95 |
val extend = I; |
96 |
fun merge _ ((tab1, monos1), (tab2, monos2)) = |
|
11502 | 97 |
(Symtab.merge (K true) (tab1, tab2), Drule.merge_rules (monos1, monos2)); |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
98 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
99 |
fun print generic (tab, monos) = |
16364 | 100 |
[Pretty.strs ("(co)inductives:" :: |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
101 |
map #1 (NameSpace.extern_table |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
102 |
(Sign.const_space (Context.theory_of generic), tab))), (* FIXME? *) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
103 |
Pretty.big_list "monotonicity rules:" |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
104 |
(map (ProofContext.pretty_thm (Context.proof_of generic)) monos)] |
8720 | 105 |
|> Pretty.chunks |> Pretty.writeln; |
16432 | 106 |
end); |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
107 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
108 |
val print_inductives = InductiveData.print o Context.Proof; |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
109 |
|
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
110 |
|
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
111 |
(* get and put data *) |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
112 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
113 |
val get_inductive = Symtab.lookup o #1 o InductiveData.get o Context.Proof; |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
114 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
115 |
fun the_inductive ctxt name = |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
116 |
(case get_inductive ctxt name of |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
117 |
NONE => error ("Unknown (co)inductive predicate " ^ quote name) |
15531 | 118 |
| SOME info => info); |
9598 | 119 |
|
18222 | 120 |
fun put_inductives names info = InductiveData.map (apfst (fn tab => |
121 |
fold (fn name => Symtab.update_new (name, info)) names tab |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
122 |
handle Symtab.DUP dup => error ("Duplicate definition of (co)inductive predicate " ^ quote dup))); |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
123 |
|
8277 | 124 |
|
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
125 |
|
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
126 |
(** monotonicity rules **) |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
127 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
128 |
val get_monos = #2 o InductiveData.get o Context.Proof; |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
129 |
val map_monos = InductiveData.map o apsnd; |
8277 | 130 |
|
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
131 |
fun mk_mono thm = |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
132 |
let |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
133 |
fun eq2mono thm' = [(*standard*) (thm' RS (thm' RS eq_to_mono))] @ |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
134 |
(case concl_of thm of |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
135 |
(_ $ (_ $ (Const ("Not", _) $ _) $ _)) => [] |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
136 |
| _ => [(*standard*) (thm' RS (thm' RS eq_to_mono2))]); |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
137 |
val concl = concl_of thm |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
138 |
in |
20872 | 139 |
if can Logic.dest_equals concl then |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
140 |
eq2mono (thm RS meta_eq_to_obj_eq) |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
141 |
else if can (HOLogic.dest_eq o HOLogic.dest_Trueprop) concl then |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
142 |
eq2mono thm |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
143 |
else [thm] |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
144 |
end; |
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
145 |
|
8634 | 146 |
|
147 |
(* attributes *) |
|
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
148 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
149 |
val mono_add = Thm.declaration_attribute (map_monos o fold Drule.add_rule o mk_mono); |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
150 |
val mono_del = Thm.declaration_attribute (map_monos o fold Drule.del_rule o mk_mono); |
7710
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
151 |
|
bf8cb3fc5d64
Monotonicity rules for inductive definitions can now be added to a theory via
berghofe
parents:
7349
diff
changeset
|
152 |
|
7107 | 153 |
|
10735 | 154 |
(** misc utilities **) |
6424 | 155 |
|
5662 | 156 |
val quiet_mode = ref false; |
10735 | 157 |
fun message s = if ! quiet_mode then () else writeln s; |
158 |
fun clean_message s = if ! quick_and_dirty then () else message s; |
|
5662 | 159 |
|
6424 | 160 |
fun coind_prefix true = "co" |
161 |
| coind_prefix false = ""; |
|
162 |
||
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
163 |
fun log b m n = if m >= n then 0 else 1 + log b (b * m) n; |
6424 | 164 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
165 |
fun make_bool_args f g [] i = [] |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
166 |
| make_bool_args f g (x :: xs) i = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
167 |
(if i mod 2 = 0 then f x else g x) :: make_bool_args f g xs (i div 2); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
168 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
169 |
fun make_bool_args' xs = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
170 |
make_bool_args (K HOLogic.false_const) (K HOLogic.true_const) xs; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
171 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
172 |
fun find_arg T x [] = sys_error "find_arg" |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
173 |
| find_arg T x ((p as (_, (SOME _, _))) :: ps) = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
174 |
apsnd (cons p) (find_arg T x ps) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
175 |
| find_arg T x ((p as (U, (NONE, y))) :: ps) = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
176 |
if T = U then (y, (U, (SOME x, y)) :: ps) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
177 |
else apsnd (cons p) (find_arg T x ps); |
7020
75ff179df7b7
Exported function unify_consts (workaround to avoid inconsistently
berghofe
parents:
6851
diff
changeset
|
178 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
179 |
fun make_args Ts xs = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
180 |
map (fn (T, (NONE, ())) => Const ("arbitrary", T) | (_, (SOME t, ())) => t) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
181 |
(fold (fn (t, T) => snd o find_arg T t) xs (map (rpair (NONE, ())) Ts)); |
7020
75ff179df7b7
Exported function unify_consts (workaround to avoid inconsistently
berghofe
parents:
6851
diff
changeset
|
182 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
183 |
fun make_args' Ts xs Us = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
184 |
fst (fold_map (fn T => find_arg T ()) Us (Ts ~~ map (pair NONE) xs)); |
7020
75ff179df7b7
Exported function unify_consts (workaround to avoid inconsistently
berghofe
parents:
6851
diff
changeset
|
185 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
186 |
fun dest_predicate cs params t = |
5094 | 187 |
let |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
188 |
val k = length params; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
189 |
val (c, ts) = strip_comb t; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
190 |
val (xs, ys) = chop k ts; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
191 |
val i = find_index_eq c cs; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
192 |
in |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
193 |
if xs = params andalso i >= 0 then |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
194 |
SOME (c, i, ys, chop (length ys) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
195 |
(List.drop (binder_types (fastype_of c), k))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
196 |
else NONE |
5094 | 197 |
end; |
198 |
||
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
199 |
fun mk_names a 0 = [] |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
200 |
| mk_names a 1 = [a] |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
201 |
| mk_names a n = map (fn i => a ^ string_of_int i) (1 upto n); |
10988
e0016a009c17
Splitting of arguments of product types in induction rules is now less
berghofe
parents:
10910
diff
changeset
|
202 |
|
6424 | 203 |
|
204 |
||
10729 | 205 |
(** process rules **) |
206 |
||
207 |
local |
|
5094 | 208 |
|
16432 | 209 |
fun err_in_rule thy name t msg = |
210 |
error (cat_lines ["Ill-formed introduction rule " ^ quote name, |
|
211 |
Sign.string_of_term thy t, msg]); |
|
10729 | 212 |
|
16432 | 213 |
fun err_in_prem thy name t p msg = |
214 |
error (cat_lines ["Ill-formed premise", Sign.string_of_term thy p, |
|
215 |
"in introduction rule " ^ quote name, Sign.string_of_term thy t, msg]); |
|
5094 | 216 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
217 |
val bad_concl = "Conclusion of introduction rule must be an inductive predicate"; |
10729 | 218 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
219 |
val bad_ind_occ = "Inductive predicate occurs in argument of inductive predicate"; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
220 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
221 |
val bad_app = "Inductive predicate must be applied to parameter(s) "; |
11358
416ea5c009f5
now checks for leading meta-quantifiers and complains, instead of
paulson
parents:
11036
diff
changeset
|
222 |
|
16432 | 223 |
fun atomize_term thy = MetaSimplifier.rewrite_term thy inductive_atomize []; |
10729 | 224 |
|
225 |
in |
|
5094 | 226 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
227 |
fun check_rule thy cs params ((name, att), rule) = |
10729 | 228 |
let |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
229 |
val params' = Term.variant_frees rule (Logic.strip_params rule); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
230 |
val frees = rev (map Free params'); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
231 |
val concl = subst_bounds (frees, Logic.strip_assums_concl rule); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
232 |
val prems = map (curry subst_bounds frees) (Logic.strip_assums_hyp rule); |
16432 | 233 |
val aprems = map (atomize_term thy) prems; |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
234 |
val arule = list_all_free (params', Logic.list_implies (aprems, concl)); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
235 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
236 |
fun check_ind err t = case dest_predicate cs params t of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
237 |
NONE => err (bad_app ^ |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
238 |
commas (map (Sign.string_of_term thy) params)) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
239 |
| SOME (_, _, ys, _) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
240 |
if exists (fn c => exists (fn t => Logic.occs (c, t)) ys) cs |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
241 |
then err bad_ind_occ else (); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
242 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
243 |
fun check_prem' prem t = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
244 |
if head_of t mem cs then |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
245 |
check_ind (err_in_prem thy name rule prem) t |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
246 |
else (case t of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
247 |
Abs (_, _, t) => check_prem' prem t |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
248 |
| t $ u => (check_prem' prem t; check_prem' prem u) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
249 |
| _ => ()); |
5094 | 250 |
|
10729 | 251 |
fun check_prem (prem, aprem) = |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
252 |
if can HOLogic.dest_Trueprop aprem then check_prem' prem prem |
16432 | 253 |
else err_in_prem thy name rule prem "Non-atomic premise"; |
10729 | 254 |
in |
11358
416ea5c009f5
now checks for leading meta-quantifiers and complains, instead of
paulson
parents:
11036
diff
changeset
|
255 |
(case concl of |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
256 |
Const ("Trueprop", _) $ t => |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
257 |
if head_of t mem cs then |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
258 |
(check_ind (err_in_rule thy name rule) t; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
259 |
List.app check_prem (prems ~~ aprems)) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
260 |
else err_in_rule thy name rule bad_concl |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
261 |
| _ => err_in_rule thy name rule bad_concl); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
262 |
((name, att), arule) |
10729 | 263 |
end; |
5094 | 264 |
|
18222 | 265 |
val rulify = (* FIXME norm_hhf *) |
266 |
hol_simplify inductive_conj |
|
18463 | 267 |
#> hol_simplify inductive_rulify |
268 |
#> hol_simplify inductive_rulify_fallback |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
269 |
(*#> standard*); |
10729 | 270 |
|
271 |
end; |
|
272 |
||
5094 | 273 |
|
6424 | 274 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
275 |
(** proofs for (co)inductive predicates **) |
6424 | 276 |
|
10735 | 277 |
(* prove monotonicity -- NOT subject to quick_and_dirty! *) |
5094 | 278 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
279 |
fun prove_mono predT fp_fun monos ctxt = |
10735 | 280 |
(message " Proving monotonicity ..."; |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
281 |
Goal.prove ctxt [] [] (*NO quick_and_dirty here!*) |
17985 | 282 |
(HOLogic.mk_Trueprop |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
283 |
(Const (mono_name, (predT --> predT) --> HOLogic.boolT) $ fp_fun)) |
17985 | 284 |
(fn _ => EVERY [rtac monoI 1, |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
285 |
REPEAT (resolve_tac [le_funI, le_boolI'] 1), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
286 |
REPEAT (FIRST |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
287 |
[atac 1, |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
288 |
resolve_tac (List.concat (map mk_mono monos) @ get_monos ctxt) 1, |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
289 |
etac le_funE 1, dtac le_boolD 1])])); |
5094 | 290 |
|
6424 | 291 |
|
10735 | 292 |
(* prove introduction rules *) |
5094 | 293 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
294 |
fun prove_intrs coind mono fp_def k intr_ts rec_preds_defs ctxt = |
5094 | 295 |
let |
10735 | 296 |
val _ = clean_message " Proving the introduction rules ..."; |
5094 | 297 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
298 |
val unfold = funpow k (fn th => th RS fun_cong) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
299 |
(mono RS (fp_def RS |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
300 |
(if coind then def_gfp_unfold else def_lfp_unfold))); |
5094 | 301 |
|
302 |
fun select_disj 1 1 = [] |
|
303 |
| select_disj _ 1 = [rtac disjI1] |
|
304 |
| select_disj n i = (rtac disjI2)::(select_disj (n - 1) (i - 1)); |
|
305 |
||
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
306 |
val rules = [refl, TrueI, notFalseI, exI, conjI]; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
307 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
308 |
val intrs = map_index (fn (i, intr) => |
20047 | 309 |
rulify (SkipProof.prove ctxt [] [] intr (fn _ => EVERY |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
310 |
[rewrite_goals_tac rec_preds_defs, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
311 |
rtac (unfold RS iffD2) 1, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
312 |
EVERY1 (select_disj (length intr_ts) (i + 1)), |
17985 | 313 |
(*Not ares_tac, since refl must be tried before any equality assumptions; |
314 |
backtracking may occur if the premises have extra variables!*) |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
315 |
DEPTH_SOLVE_1 (resolve_tac rules 1 APPEND assume_tac 1)]))) intr_ts |
5094 | 316 |
|
317 |
in (intrs, unfold) end; |
|
318 |
||
6424 | 319 |
|
10735 | 320 |
(* prove elimination rules *) |
5094 | 321 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
322 |
fun prove_elims cs params intr_ts intr_names unfold rec_preds_defs ctxt = |
5094 | 323 |
let |
10735 | 324 |
val _ = clean_message " Proving the elimination rules ..."; |
5094 | 325 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
326 |
val ([pname], ctxt') = Variable.variant_fixes ["P"] ctxt; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
327 |
val P = HOLogic.mk_Trueprop (Free (pname, HOLogic.boolT)); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
328 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
329 |
fun dest_intr r = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
330 |
(the (dest_predicate cs params (HOLogic.dest_Trueprop (Logic.strip_assums_concl r))), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
331 |
Logic.strip_assums_hyp r, Logic.strip_params r); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
332 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
333 |
val intrs = map dest_intr intr_ts ~~ intr_names; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
334 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
335 |
val rules1 = [disjE, exE, FalseE]; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
336 |
val rules2 = [conjE, FalseE, notTrueE]; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
337 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
338 |
fun prove_elim c = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
339 |
let |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
340 |
val Ts = List.drop (binder_types (fastype_of c), length params); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
341 |
val (anames, ctxt'') = Variable.variant_fixes (mk_names "a" (length Ts)) ctxt'; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
342 |
val frees = map Free (anames ~~ Ts); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
343 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
344 |
fun mk_elim_prem ((_, _, us, _), ts, params') = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
345 |
list_all (params', |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
346 |
Logic.list_implies (map (HOLogic.mk_Trueprop o HOLogic.mk_eq) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
347 |
(frees ~~ us) @ ts, P)); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
348 |
val c_intrs = (List.filter (equal c o #1 o #1 o #1) intrs); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
349 |
val prems = HOLogic.mk_Trueprop (list_comb (c, params @ frees)) :: |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
350 |
map mk_elim_prem (map #1 c_intrs) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
351 |
in |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
352 |
(SkipProof.prove ctxt'' [] prems P |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
353 |
(fn {prems, ...} => EVERY |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
354 |
[cut_facts_tac [hd prems] 1, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
355 |
rewrite_goals_tac rec_preds_defs, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
356 |
dtac (unfold RS iffD1) 1, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
357 |
REPEAT (FIRSTGOAL (eresolve_tac rules1)), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
358 |
REPEAT (FIRSTGOAL (eresolve_tac rules2)), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
359 |
EVERY (map (fn prem => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
360 |
DEPTH_SOLVE_1 (ares_tac [rewrite_rule rec_preds_defs prem, conjI] 1)) (tl prems))]) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
361 |
|> rulify |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
362 |
|> singleton (ProofContext.export ctxt'' ctxt), |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
363 |
map #2 c_intrs) |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
364 |
end |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
365 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
366 |
in map prove_elim cs end; |
5094 | 367 |
|
6424 | 368 |
|
10735 | 369 |
(* derivation of simplified elimination rules *) |
5094 | 370 |
|
11682
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
371 |
local |
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
372 |
|
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
373 |
(*delete needless equality assumptions*) |
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
374 |
val refl_thin = prove_goal HOL.thy "!!P. a = a ==> P ==> P" (fn _ => [assume_tac 1]); |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
375 |
val elim_rls = [asm_rl, FalseE, refl_thin, conjE, exE]; |
11682
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
376 |
val elim_tac = REPEAT o Tactic.eresolve_tac elim_rls; |
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
377 |
|
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
378 |
fun simp_case_tac solved ss i = |
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
379 |
EVERY' [elim_tac, asm_full_simp_tac ss, elim_tac, REPEAT o bound_hyp_subst_tac] i |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
380 |
THEN_MAYBE (if solved then no_tac else all_tac); (* FIXME !? *) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
381 |
|
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
382 |
(*prop should have the form "P t" where P is an inductive predicate*) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
383 |
val mk_cases_err = "mk_cases: proposition not an inductive predicate"; |
11682
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
384 |
|
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
385 |
in |
9598 | 386 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
387 |
fun mk_cases ctxt prop = |
7107 | 388 |
let |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
389 |
val thy = ProofContext.theory_of ctxt; |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
390 |
val ss = Simplifier.local_simpset_of ctxt; |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
391 |
|
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
392 |
val c = #1 (Term.dest_Const (Term.head_of (HOLogic.dest_Trueprop |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
393 |
(Logic.strip_imp_concl prop)))) handle TERM _ => error mk_cases_err; |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
394 |
val (_, {elims, ...}) = the_inductive ctxt c; |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
395 |
|
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
396 |
val cprop = Thm.cterm_of thy prop; |
11682
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
397 |
val tac = ALLGOALS (simp_case_tac false ss) THEN prune_params_tac; |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
398 |
fun mk_elim rl = |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
399 |
Thm.implies_intr cprop (Tactic.rule_by_tactic tac (Thm.assume cprop RS rl)) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
400 |
|> singleton (Variable.export (Variable.auto_fixes prop ctxt) ctxt); |
7107 | 401 |
in |
402 |
(case get_first (try mk_elim) elims of |
|
15531 | 403 |
SOME r => r |
404 |
| NONE => error (Pretty.string_of (Pretty.block |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
405 |
[Pretty.str mk_cases_err, Pretty.fbrk, ProofContext.pretty_term ctxt prop]))) |
7107 | 406 |
end; |
407 |
||
11682
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
408 |
end; |
d9063229b4a1
simp_case_tac is back again from induct_method.ML;
wenzelm
parents:
11628
diff
changeset
|
409 |
|
7107 | 410 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
411 |
(* inductive_cases *) |
7107 | 412 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
413 |
fun gen_inductive_cases prep_att prep_prop args lthy = |
9598 | 414 |
let |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
415 |
val thy = ProofContext.theory_of lthy; |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12798
diff
changeset
|
416 |
val facts = args |> map (fn ((a, atts), props) => |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
417 |
((a, map (prep_att thy) atts), |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
418 |
map (Thm.no_attributes o single o mk_cases lthy o prep_prop lthy) props)); |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
419 |
in lthy |> LocalTheory.notes facts |>> map snd end; |
5094 | 420 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
421 |
val inductive_cases = gen_inductive_cases Attrib.intern_src ProofContext.read_prop; |
12172 | 422 |
val inductive_cases_i = gen_inductive_cases (K I) ProofContext.cert_prop; |
7107 | 423 |
|
6424 | 424 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
425 |
fun ind_cases src = |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
426 |
Method.syntax (Scan.repeat1 Args.prop) src |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
427 |
#> (fn (ctxt, props) => Method.erule 0 (map (mk_cases ctxt) props)); |
9598 | 428 |
|
429 |
||
430 |
||
10735 | 431 |
(* prove induction rule *) |
5094 | 432 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
433 |
fun prove_indrule cs argTs bs xs rec_const params intr_ts mono |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
434 |
fp_def rec_preds_defs ctxt = |
5094 | 435 |
let |
10735 | 436 |
val _ = clean_message " Proving the induction rule ..."; |
20047 | 437 |
val thy = ProofContext.theory_of ctxt; |
5094 | 438 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
439 |
(* predicates for induction rule *) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
440 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
441 |
val (pnames, ctxt') = Variable.variant_fixes (mk_names "P" (length cs)) ctxt; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
442 |
val preds = map Free (pnames ~~ |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
443 |
map (fn c => List.drop (binder_types (fastype_of c), length params) ---> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
444 |
HOLogic.boolT) cs); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
445 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
446 |
(* transform an introduction rule into a premise for induction rule *) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
447 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
448 |
fun mk_ind_prem r = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
449 |
let |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
450 |
fun subst s = (case dest_predicate cs params s of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
451 |
SOME (_, i, ys, (_, Ts)) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
452 |
let |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
453 |
val k = length Ts; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
454 |
val bs = map Bound (k - 1 downto 0); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
455 |
val P = list_comb (List.nth (preds, i), ys @ bs); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
456 |
val Q = list_abs (mk_names "x" k ~~ Ts, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
457 |
HOLogic.mk_binop inductive_conj_name (list_comb (s, bs), P)) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
458 |
in (Q, case Ts of [] => SOME (s, P) | _ => NONE) end |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
459 |
| NONE => (case s of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
460 |
(t $ u) => (fst (subst t) $ fst (subst u), NONE) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
461 |
| (Abs (a, T, t)) => (Abs (a, T, fst (subst t)), NONE) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
462 |
| _ => (s, NONE))); |
7293 | 463 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
464 |
fun mk_prem (s, prems) = (case subst s of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
465 |
(_, SOME (t, u)) => t :: u :: prems |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
466 |
| (t, _) => t :: prems); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
467 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
468 |
val SOME (_, i, ys, _) = dest_predicate cs params |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
469 |
(HOLogic.dest_Trueprop (Logic.strip_assums_concl r)) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
470 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
471 |
in list_all_free (Logic.strip_params r, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
472 |
Logic.list_implies (map HOLogic.mk_Trueprop (foldr mk_prem |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
473 |
[] (map HOLogic.dest_Trueprop (Logic.strip_assums_hyp r))), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
474 |
HOLogic.mk_Trueprop (list_comb (List.nth (preds, i), ys)))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
475 |
end; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
476 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
477 |
val ind_prems = map mk_ind_prem intr_ts; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
478 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
479 |
(* make conclusions for induction rules *) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
480 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
481 |
val Tss = map (binder_types o fastype_of) preds; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
482 |
val (xnames, ctxt'') = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
483 |
Variable.variant_fixes (mk_names "x" (length (flat Tss))) ctxt'; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
484 |
val mutual_ind_concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
485 |
(map (fn (((xnames, Ts), c), P) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
486 |
let val frees = map Free (xnames ~~ Ts) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
487 |
in HOLogic.mk_imp |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
488 |
(list_comb (c, params @ frees), list_comb (P, frees)) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
489 |
end) (unflat Tss xnames ~~ Tss ~~ cs ~~ preds))); |
5094 | 490 |
|
13626
282fbabec862
Fixed bug involving inductive definitions having equalities in the premises,
paulson
parents:
13197
diff
changeset
|
491 |
|
5094 | 492 |
(* make predicate for instantiation of abstract induction rule *) |
493 |
||
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
494 |
val ind_pred = fold_rev lambda (bs @ xs) (foldr1 HOLogic.mk_conj |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
495 |
(map_index (fn (i, P) => foldr HOLogic.mk_imp |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
496 |
(list_comb (P, make_args' argTs xs (binder_types (fastype_of P)))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
497 |
(make_bool_args HOLogic.mk_not I bs i)) preds)); |
5094 | 498 |
|
499 |
val ind_concl = HOLogic.mk_Trueprop |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
500 |
(HOLogic.mk_binrel "Orderings.less_eq" (rec_const, ind_pred)); |
5094 | 501 |
|
13626
282fbabec862
Fixed bug involving inductive definitions having equalities in the premises,
paulson
parents:
13197
diff
changeset
|
502 |
val raw_fp_induct = (mono RS (fp_def RS def_lfp_induct)); |
282fbabec862
Fixed bug involving inductive definitions having equalities in the premises,
paulson
parents:
13197
diff
changeset
|
503 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
504 |
val induct = SkipProof.prove ctxt'' [] ind_prems ind_concl |
20248 | 505 |
(fn {prems, ...} => EVERY |
17985 | 506 |
[rewrite_goals_tac [inductive_conj_def], |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
507 |
DETERM (rtac raw_fp_induct 1), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
508 |
REPEAT (resolve_tac [le_funI, le_boolI] 1), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
509 |
rewrite_goals_tac (map mk_meta_eq [meet_fun_eq, meet_bool_eq] @ simp_thms'), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
510 |
(*This disjE separates out the introduction rules*) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
511 |
REPEAT (FIRSTGOAL (eresolve_tac [disjE, exE, FalseE])), |
5094 | 512 |
(*Now break down the individual cases. No disjE here in case |
513 |
some premise involves disjunction.*) |
|
13747
bf308fcfd08e
Better treatment of equality in premises of inductive definitions. Less
paulson
parents:
13709
diff
changeset
|
514 |
REPEAT (FIRSTGOAL (etac conjE ORELSE' bound_hyp_subst_tac)), |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
515 |
REPEAT (FIRSTGOAL |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
516 |
(resolve_tac [conjI, impI] ORELSE' (etac notE THEN' atac))), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
517 |
EVERY (map (fn prem => DEPTH_SOLVE_1 (ares_tac [rewrite_rule |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
518 |
(inductive_conj_def :: rec_preds_defs) prem, conjI, refl] 1)) prems)]); |
5094 | 519 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
520 |
val lemma = SkipProof.prove ctxt'' [] [] |
17985 | 521 |
(Logic.mk_implies (ind_concl, mutual_ind_concl)) (fn _ => EVERY |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
522 |
[rewrite_goals_tac rec_preds_defs, |
5094 | 523 |
REPEAT (EVERY |
524 |
[REPEAT (resolve_tac [conjI, impI] 1), |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
525 |
REPEAT (eresolve_tac [le_funE, le_boolE] 1), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
526 |
atac 1, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
527 |
rewrite_goals_tac simp_thms', |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
528 |
atac 1])]) |
5094 | 529 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
530 |
in singleton (ProofContext.export ctxt'' ctxt) (induct RS lemma) end; |
5094 | 531 |
|
6424 | 532 |
|
533 |
||
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
534 |
(** specification of (co)inductive predicates **) |
10729 | 535 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
536 |
fun mk_ind_def alt_name coind cs intr_ts monos |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
537 |
params cnames_syn ctxt = |
5094 | 538 |
let |
10735 | 539 |
val fp_name = if coind then gfp_name else lfp_name; |
5094 | 540 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
541 |
val argTs = fold (fn c => fn Ts => Ts @ |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
542 |
(List.drop (binder_types (fastype_of c), length params) \\ Ts)) cs []; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
543 |
val k = log 2 1 (length cs); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
544 |
val predT = replicate k HOLogic.boolT ---> argTs ---> HOLogic.boolT; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
545 |
val p :: xs = map Free (Variable.variant_frees ctxt intr_ts |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
546 |
(("p", predT) :: (mk_names "x" (length argTs) ~~ argTs))); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
547 |
val bs = map Free (Variable.variant_frees ctxt (p :: xs @ intr_ts) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
548 |
(map (rpair HOLogic.boolT) (mk_names "b" k))); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
549 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
550 |
fun subst t = (case dest_predicate cs params t of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
551 |
SOME (_, i, ts, (Ts, Us)) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
552 |
let val zs = map Bound (length Us - 1 downto 0) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
553 |
in |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
554 |
list_abs (map (pair "z") Us, list_comb (p, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
555 |
make_bool_args' bs i @ make_args argTs ((ts ~~ Ts) @ (zs ~~ Us)))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
556 |
end |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
557 |
| NONE => (case t of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
558 |
t1 $ t2 => subst t1 $ subst t2 |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
559 |
| Abs (x, T, u) => Abs (x, T, subst u) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
560 |
| _ => t)); |
5149 | 561 |
|
5094 | 562 |
(* transform an introduction rule into a conjunction *) |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
563 |
(* [| p_i t; ... |] ==> p_j u *) |
5094 | 564 |
(* is transformed into *) |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
565 |
(* b_j & x_j = u & p b_j t & ... *) |
5094 | 566 |
|
567 |
fun transform_rule r = |
|
568 |
let |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
569 |
val SOME (_, i, ts, (Ts, _)) = dest_predicate cs params |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
570 |
(HOLogic.dest_Trueprop (Logic.strip_assums_concl r)); |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
571 |
val ps = make_bool_args HOLogic.mk_not I bs i @ |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
572 |
map HOLogic.mk_eq (make_args' argTs xs Ts ~~ ts) @ |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
573 |
map (subst o HOLogic.dest_Trueprop) |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
574 |
(Logic.strip_assums_hyp r) |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
575 |
in foldr (fn ((x, T), P) => HOLogic.exists_const T $ (Abs (x, T, P))) |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
576 |
(if null ps then HOLogic.true_const else foldr1 HOLogic.mk_conj ps) |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
577 |
(Logic.strip_params r) |
5094 | 578 |
end |
579 |
||
580 |
(* make a disjunction of all introduction rules *) |
|
581 |
||
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
582 |
val fp_fun = fold_rev lambda (p :: bs @ xs) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
583 |
(if null intr_ts then HOLogic.false_const |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
584 |
else foldr1 HOLogic.mk_disj (map transform_rule intr_ts)); |
5094 | 585 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
586 |
(* add definiton of recursive predicates to theory *) |
5094 | 587 |
|
14235
281295a1bbaa
Fixed bug in mk_ind_def that caused the inductive definition package to
berghofe
parents:
13747
diff
changeset
|
588 |
val rec_name = if alt_name = "" then |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
589 |
space_implode "_" (map fst cnames_syn) else alt_name; |
5094 | 590 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
591 |
val ((rec_const, (_, fp_def)), ctxt') = ctxt |> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
592 |
Variable.add_fixes (map (fst o dest_Free) params) |> snd |> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
593 |
fold Variable.declare_term intr_ts |> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
594 |
LocalTheory.def |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
595 |
((rec_name, case cnames_syn of [(_, syn)] => syn | _ => NoSyn), |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
596 |
(("", []), fold_rev lambda params |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
597 |
(Const (fp_name, (predT --> predT) --> predT) $ fp_fun))); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
598 |
val fp_def' = Simplifier.rewrite (HOL_basic_ss addsimps [fp_def]) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
599 |
(cterm_of (ProofContext.theory_of ctxt') (list_comb (rec_const, params))); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
600 |
val specs = if length cs < 2 then [] else |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
601 |
map_index (fn (i, (name_mx, c)) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
602 |
let |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
603 |
val Ts = List.drop (binder_types (fastype_of c), length params); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
604 |
val xs = map Free (Variable.variant_frees ctxt intr_ts |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
605 |
(mk_names "x" (length Ts) ~~ Ts)) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
606 |
in |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
607 |
(name_mx, (("", []), fold_rev lambda (params @ xs) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
608 |
(list_comb (rec_const, params @ make_bool_args' bs i @ |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
609 |
make_args argTs (xs ~~ Ts))))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
610 |
end) (cnames_syn ~~ cs); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
611 |
val (consts_defs, ctxt'') = fold_map LocalTheory.def specs ctxt'; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
612 |
val preds = (case cs of [_] => [rec_const] | _ => map #1 consts_defs); |
5094 | 613 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
614 |
val mono = prove_mono predT fp_fun monos ctxt'' |
5094 | 615 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
616 |
in (ctxt'', rec_name, mono, fp_def', map (#2 o #2) consts_defs, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
617 |
list_comb (rec_const, params), preds, argTs, bs, xs) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
618 |
end; |
5094 | 619 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
620 |
fun add_ind_def verbose alt_name coind no_elim no_ind cs |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
621 |
intros monos params cnames_syn ctxt = |
9072
a4896cf23638
Now also proves monotonicity when in quick_and_dirty mode.
berghofe
parents:
8720
diff
changeset
|
622 |
let |
10735 | 623 |
val _ = |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
624 |
if verbose then message ("Proofs for " ^ coind_prefix coind ^ "inductive predicate(s) " ^ |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
625 |
commas_quote (map fst cnames_syn)) else (); |
9072
a4896cf23638
Now also proves monotonicity when in quick_and_dirty mode.
berghofe
parents:
8720
diff
changeset
|
626 |
|
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
627 |
val cnames = map (Sign.full_name (ProofContext.theory_of ctxt) o #1) cnames_syn; |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
628 |
val ((intr_names, intr_atts), intr_ts) = apfst split_list (split_list intros); |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
629 |
|
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
630 |
val (ctxt1, rec_name, mono, fp_def, rec_preds_defs, rec_const, preds, |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
631 |
argTs, bs, xs) = mk_ind_def alt_name coind cs intr_ts |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
632 |
monos params cnames_syn ctxt; |
9072
a4896cf23638
Now also proves monotonicity when in quick_and_dirty mode.
berghofe
parents:
8720
diff
changeset
|
633 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
634 |
val (intrs, unfold) = prove_intrs coind mono fp_def (length bs + length xs) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
635 |
intr_ts rec_preds_defs ctxt1; |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
636 |
val elims = if no_elim then [] else |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
637 |
cnames ~~ map (apfst (singleton (ProofContext.export ctxt1 ctxt))) |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
638 |
(prove_elims cs params intr_ts intr_names unfold rec_preds_defs ctxt1); |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
639 |
val raw_induct = singleton (ProofContext.export ctxt1 ctxt) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
640 |
(if no_ind then Drule.asm_rl else |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
641 |
if coind then ObjectLogic.rulify (rule_by_tactic |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
642 |
(rewrite_tac [le_fun_def, le_bool_def] THEN |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
643 |
fold_tac rec_preds_defs) (mono RS (fp_def RS def_coinduct))) |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
644 |
else |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
645 |
prove_indrule cs argTs bs xs rec_const params intr_ts mono fp_def |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
646 |
rec_preds_defs ctxt1); |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
647 |
val induct_cases = map (#1 o #1) intros; |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
648 |
val ind_case_names = RuleCases.case_names induct_cases; |
12165 | 649 |
val induct = |
18222 | 650 |
if coind then |
651 |
(raw_induct, [RuleCases.case_names [rec_name], |
|
18234 | 652 |
RuleCases.case_conclusion (rec_name, induct_cases), |
18222 | 653 |
RuleCases.consumes 1]) |
654 |
else if no_ind orelse length cs > 1 then |
|
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
655 |
(raw_induct, [ind_case_names, RuleCases.consumes 0]) |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
656 |
else (raw_induct RSN (2, rev_mp), [ind_case_names, RuleCases.consumes 1]); |
5094 | 657 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
658 |
val (intrs', ctxt2) = |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
659 |
ctxt1 |> |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
660 |
LocalTheory.notes |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
661 |
(map (NameSpace.append rec_name) intr_names ~~ |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
662 |
intr_atts ~~ |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
663 |
map (fn th => [([th], [])]) (ProofContext.export ctxt1 ctxt intrs)) |>> |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
664 |
map (hd o snd); (* FIXME? *) |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
665 |
val (((_, elims'), (_, [induct'])), ctxt3) = |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
666 |
ctxt2 |> |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
667 |
LocalTheory.note ((NameSpace.append rec_name "intros", []), intrs') ||>> |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
668 |
fold_map (fn (name, (elim, cases)) => |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
669 |
LocalTheory.note ((NameSpace.append (Sign.base_name name) "cases", |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
670 |
[Attrib.internal (RuleCases.case_names cases), |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
671 |
Attrib.internal (RuleCases.consumes 1), |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
672 |
Attrib.internal (InductAttrib.cases_set name)]), [elim]) #> |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
673 |
apfst (hd o snd)) elims ||>> |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
674 |
LocalTheory.note ((NameSpace.append rec_name (coind_prefix coind ^ "induct"), |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
675 |
map Attrib.internal (#2 induct)), [rulify (#1 induct)]); |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
676 |
|
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
677 |
val induct_att = if coind then InductAttrib.coinduct_set else InductAttrib.induct_set; |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
678 |
val ctxt4 = if no_ind then ctxt3 else |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
679 |
let val inducts = cnames ~~ ProjectRule.projects ctxt (1 upto length cnames) induct' |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
680 |
in |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
681 |
ctxt3 |> |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
682 |
LocalTheory.notes (inducts |> map (fn (name, th) => (("", |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
683 |
[Attrib.internal ind_case_names, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
684 |
Attrib.internal (RuleCases.consumes 1), |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
685 |
Attrib.internal (induct_att name)]), [([th], [])]))) |> snd |> |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
686 |
LocalTheory.note ((NameSpace.append rec_name (coind_prefix coind ^ "inducts"), |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
687 |
[Attrib.internal ind_case_names, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
688 |
Attrib.internal (RuleCases.consumes 1)]), map snd inducts) |> snd |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
689 |
end; |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
690 |
|
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
691 |
val result = |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
692 |
{preds = preds, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
693 |
defs = fp_def :: rec_preds_defs, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
694 |
mono = singleton (ProofContext.export ctxt1 ctxt) mono, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
695 |
unfold = singleton (ProofContext.export ctxt1 ctxt) unfold, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
696 |
intrs = intrs', |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
697 |
elims = elims', |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
698 |
raw_induct = rulify raw_induct, |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
699 |
induct = induct'} |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
700 |
|
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
701 |
in |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
702 |
(result, LocalTheory.declaration |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
703 |
(put_inductives cnames ({names = cnames, coind = coind}, result)) ctxt4) |
5094 | 704 |
end; |
705 |
||
6424 | 706 |
|
10735 | 707 |
(* external interfaces *) |
5094 | 708 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
709 |
fun add_inductive_i verbose alt_name coind no_elim no_ind cnames_syn pnames pre_intros monos ctxt = |
5094 | 710 |
let |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
711 |
val thy = ProofContext.theory_of ctxt; |
6424 | 712 |
val _ = Theory.requires thy "Inductive" (coind_prefix coind ^ "inductive definitions"); |
5094 | 713 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
714 |
val frees = fold (Term.add_frees o snd) pre_intros []; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
715 |
fun type_of s = (case AList.lookup op = frees s of |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
716 |
NONE => error ("No such variable: " ^ s) | SOME T => T); |
5094 | 717 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
718 |
val params = map |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
719 |
(fn (s, SOME T) => Free (s, T) | (s, NONE) => Free (s, type_of s)) pnames; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
720 |
val cs = map |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
721 |
(fn (s, SOME T, _) => Free (s, T) | (s, NONE, _) => Free (s, type_of s)) cnames_syn; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
722 |
val cnames_syn' = map (fn (s, _, mx) => (s, mx)) cnames_syn; |
5094 | 723 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
724 |
fun close_rule (x, r) = (x, list_all_free (rev (fold_aterms |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
725 |
(fn t as Free (v as (s, _)) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
726 |
if Variable.is_fixed ctxt s orelse member op = cs t orelse |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
727 |
member op = params t then I else insert op = v |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
728 |
| _ => I) r []), r)); |
5094 | 729 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
730 |
val intros = map (close_rule o check_rule thy cs params) pre_intros; |
21048
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
731 |
in |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
732 |
add_ind_def verbose alt_name coind no_elim no_ind cs intros monos |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
733 |
params cnames_syn' ctxt |
e57e91f72831
Restructured and repaired code dealing with case names
berghofe
parents:
21024
diff
changeset
|
734 |
end; |
5094 | 735 |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
736 |
fun add_inductive verbose coind cnames_syn pnames_syn intro_srcs raw_monos ctxt = |
5094 | 737 |
let |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
738 |
val (_, ctxt') = Specification.read_specification (cnames_syn @ pnames_syn) [] ctxt; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
739 |
val intrs = map (fn spec => apsnd hd (hd (snd (fst |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
740 |
(Specification.read_specification [] [apsnd single spec] ctxt'))))) intro_srcs; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
741 |
val pnames = map (fn (s, _, _) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
742 |
(s, SOME (ProofContext.infer_type ctxt' s))) pnames_syn; |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
743 |
val cnames_syn' = map (fn (s, _, mx) => |
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
744 |
(s, SOME (ProofContext.infer_type ctxt' s), mx)) cnames_syn; |
21350 | 745 |
val (monos, ctxt'') = LocalTheory.theory_result (IsarCmd.apply_theorems raw_monos) ctxt; |
6424 | 746 |
in |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
747 |
add_inductive_i verbose "" coind false false cnames_syn' pnames intrs monos ctxt'' |
5094 | 748 |
end; |
749 |
||
6424 | 750 |
|
751 |
||
6437 | 752 |
(** package setup **) |
753 |
||
754 |
(* setup theory *) |
|
755 |
||
8634 | 756 |
val setup = |
18708 | 757 |
InductiveData.init #> |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
758 |
Method.add_methods [("ind_cases2", ind_cases, (* FIXME "ind_cases" (?) *) |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
759 |
"dynamic case analysis on predicates")] #> |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
760 |
Attrib.add_attributes [("mono2", Attrib.add_del_args mono_add mono_del, (* FIXME "mono" *) |
18728 | 761 |
"declaration of monotonicity rule")]; |
6437 | 762 |
|
763 |
||
764 |
(* outer syntax *) |
|
6424 | 765 |
|
17057 | 766 |
local structure P = OuterParse and K = OuterKeyword in |
6424 | 767 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
768 |
(* FIXME tmp *) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
769 |
fun flatten_specification specs = specs |> maps |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
770 |
(fn (a, (concl, [])) => concl |> map |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
771 |
(fn ((b, atts), [B]) => |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
772 |
if a = "" then ((b, atts), B) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
773 |
else if b = "" then ((a, atts), B) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
774 |
else error ("Illegal nested case names " ^ quote (NameSpace.append a b)) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
775 |
| ((b, _), _) => error ("Illegal simultaneous specification " ^ quote b)) |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
776 |
| (a, _) => error ("Illegal local specification parameters for " ^ quote a)); |
6424 | 777 |
|
778 |
fun ind_decl coind = |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
779 |
P.opt_locale_target -- |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
780 |
P.fixes -- P.for_fixes -- |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
781 |
Scan.optional (P.$$$ "where" |-- P.!!! P.specification) [] -- |
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12798
diff
changeset
|
782 |
Scan.optional (P.$$$ "monos" |-- P.!!! P.xthms1) [] |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
783 |
>> (fn ((((loc, preds), params), specs), monos) => |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
784 |
Toplevel.local_theory loc |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
785 |
(fn lthy => lthy |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
786 |
|> add_inductive true coind preds params (flatten_specification specs) monos |> snd)); |
6424 | 787 |
|
6723 | 788 |
val inductiveP = |
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
789 |
OuterSyntax.command "inductive2" "define inductive predicates" K.thy_decl (ind_decl false); |
6723 | 790 |
|
791 |
val coinductiveP = |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
792 |
OuterSyntax.command "coinductive2" "define coinductive predicates" K.thy_decl (ind_decl true); |
6424 | 793 |
|
7107 | 794 |
|
795 |
val inductive_casesP = |
|
21024
63ab84bb64d1
Completely rewrote inductive definition package. Now allows to
berghofe
parents:
20901
diff
changeset
|
796 |
OuterSyntax.command "inductive_cases2" |
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
797 |
"create simplified instances of elimination rules (improper)" K.thy_script |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
798 |
(P.opt_locale_target -- P.and_list1 P.spec |
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
799 |
>> (fn (loc, specs) => Toplevel.local_theory loc (snd o inductive_cases specs))); |
7107 | 800 |
|
21367
7a0cc1bb4dcc
inductive: canonical specification syntax (flattened result only);
wenzelm
parents:
21350
diff
changeset
|
801 |
val _ = OuterSyntax.add_keywords ["monos"]; |
7107 | 802 |
val _ = OuterSyntax.add_parsers [inductiveP, coinductiveP, inductive_casesP]; |
6424 | 803 |
|
5094 | 804 |
end; |
6424 | 805 |
|
806 |
end; |