| author | wenzelm | 
| Wed, 27 Mar 2024 13:23:15 +0100 | |
| changeset 80026 | a03a7d4b82f8 | 
| parent 78812 | d769a183d51d | 
| child 80701 | 39cd50407f79 | 
| permissions | -rw-r--r-- | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
30860diff
changeset | 1 | (* Title: HOL/Tools/inductive_set.ML | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 2 | Author: Stefan Berghofer, TU Muenchen | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 3 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 4 | Wrapper for defining inductive sets using package for inductive predicates, | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 5 | including infrastructure for converting between predicates and sets. | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 6 | *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 7 | |
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
30860diff
changeset | 8 | signature INDUCTIVE_SET = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 9 | sig | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 10 | val to_set_att: thm list -> attribute | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 11 | val to_pred_att: thm list -> attribute | 
| 32306 
19f55947d4d5
removed debug messages; exported to_pred in InductiveSet; added further display function; adjusted mode analysis
 bulwahn parents: 
32287diff
changeset | 12 | val to_pred : thm list -> Context.generic -> thm -> thm | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 13 | val pred_set_conv_att: attribute | 
| 69709 | 14 | val add_inductive: | 
| 15 | Inductive.flags -> | |
| 29581 | 16 | ((binding * typ) * mixfix) list -> | 
| 28084 
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
 wenzelm parents: 
28083diff
changeset | 17 | (string * typ) list -> | 
| 
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
 wenzelm parents: 
28083diff
changeset | 18 | (Attrib.binding * term) list -> thm list -> | 
| 69709 | 19 | local_theory -> Inductive.result * local_theory | 
| 20 | val add_inductive_cmd: bool -> bool -> | |
| 29581 | 21 | (binding * string option * mixfix) list -> | 
| 22 | (binding * string option * mixfix) list -> | |
| 63064 
2f18172214c8
support 'assumes' in specifications, e.g. 'definition', 'inductive';
 wenzelm parents: 
63041diff
changeset | 23 | Specification.multi_specs_cmd -> (Facts.ref * Token.src list) list -> | 
| 69709 | 24 | local_theory -> Inductive.result * local_theory | 
| 45384 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 25 | val mono_add: attribute | 
| 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 26 | val mono_del: attribute | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 27 | end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 28 | |
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
30860diff
changeset | 29 | structure Inductive_Set: INDUCTIVE_SET = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 30 | struct | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 31 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 32 | (***********************************************************************************) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 33 | (* simplifies (%x y. (x, y) : S & P x y) to (%x y. (x, y) : S Int {(x, y). P x y}) *)
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 34 | (* and        (%x y. (x, y) : S | P x y) to (%x y. (x, y) : S Un {(x, y). P x y})  *)
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 35 | (* used for converting "strong" (co)induction rules *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 36 | (***********************************************************************************) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 37 | |
| 23849 
2a0e24c74593
strong_ind_simproc now only rewrites arguments of inductive predicates.
 berghofe parents: 
23764diff
changeset | 38 | val anyt = Free ("t", TFree ("'t", []));
 | 
| 
2a0e24c74593
strong_ind_simproc now only rewrites arguments of inductive predicates.
 berghofe parents: 
23764diff
changeset | 39 | |
| 
2a0e24c74593
strong_ind_simproc now only rewrites arguments of inductive predicates.
 berghofe parents: 
23764diff
changeset | 40 | fun strong_ind_simproc tab = | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 41 | Simplifier.make_simproc \<^context> | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 42 |    {name = "strong_ind",
 | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 43 |     lhss = [\<^term>\<open>x::'a::{}\<close>],
 | 
| 61144 | 44 | proc = fn _ => fn ctxt => fn ct => | 
| 45 | let | |
| 46 | fun close p t f = | |
| 47 | let val vs = Term.add_vars t [] | |
| 48 | in Thm.instantiate' [] (rev (map (SOME o Thm.cterm_of ctxt o Var) vs)) | |
| 49 | (p (fold (Logic.all o Var) vs t) f) | |
| 50 | end; | |
| 67149 | 51 | fun mkop \<^const_name>\<open>HOL.conj\<close> T x = | 
| 52 | SOME (Const (\<^const_name>\<open>Lattices.inf\<close>, T --> T --> T), x) | |
| 53 | | mkop \<^const_name>\<open>HOL.disj\<close> T x = | |
| 54 | SOME (Const (\<^const_name>\<open>Lattices.sup\<close>, T --> T --> T), x) | |
| 61144 | 55 | | mkop _ _ _ = NONE; | 
| 56 | fun mk_collect p T t = | |
| 57 | let val U = HOLogic.dest_setT T | |
| 58 | in HOLogic.Collect_const U $ | |
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61268diff
changeset | 59 | HOLogic.mk_ptupleabs (HOLogic.flat_tuple_paths p) U HOLogic.boolT t | 
| 61144 | 60 | end; | 
| 67149 | 61 | fun decomp (Const (s, _) $ ((m as Const (\<^const_name>\<open>Set.member\<close>, | 
| 61144 | 62 | Type (_, [_, Type (_, [T, _])]))) $ p $ S) $ u) = | 
| 63 | mkop s T (m, p, S, mk_collect p T (head_of u)) | |
| 67149 | 64 | | decomp (Const (s, _) $ u $ ((m as Const (\<^const_name>\<open>Set.member\<close>, | 
| 61144 | 65 | Type (_, [_, Type (_, [T, _])]))) $ p $ S)) = | 
| 66 | mkop s T (m, p, mk_collect p T (head_of u), S) | |
| 67 | | decomp _ = NONE; | |
| 68 | val simp = | |
| 69 | full_simp_tac | |
| 63399 | 70 |             (put_simpset HOL_basic_ss ctxt addsimps @{thms mem_Collect_eq case_prod_conv}) 1;
 | 
| 61144 | 71 | fun mk_rew t = (case strip_abs_vars t of | 
| 72 | [] => NONE | |
| 73 | | xs => (case decomp (strip_abs_body t) of | |
| 74 | NONE => NONE | |
| 75 | | SOME (bop, (m, p, S, S')) => | |
| 76 | SOME (close (Goal.prove ctxt [] []) | |
| 77 | (Logic.mk_equals (t, fold_rev Term.abs xs (m $ p $ (bop $ S $ S')))) | |
| 78 | (K (EVERY | |
| 79 | [resolve_tac ctxt [eq_reflection] 1, | |
| 80 |                      REPEAT (resolve_tac ctxt @{thms ext} 1),
 | |
| 63399 | 81 |                      resolve_tac ctxt @{thms iffI} 1,
 | 
| 82 |                      EVERY [eresolve_tac ctxt @{thms conjE} 1,
 | |
| 83 |                        resolve_tac ctxt @{thms IntI} 1, simp, simp,
 | |
| 84 |                        eresolve_tac ctxt @{thms IntE} 1,
 | |
| 85 |                        resolve_tac ctxt @{thms conjI} 1, simp, simp] ORELSE
 | |
| 86 |                      EVERY [eresolve_tac ctxt @{thms disjE} 1,
 | |
| 87 |                        resolve_tac ctxt @{thms UnI1} 1, simp,
 | |
| 88 |                        resolve_tac ctxt @{thms UnI2} 1, simp,
 | |
| 89 |                        eresolve_tac ctxt @{thms UnE} 1,
 | |
| 90 |                        resolve_tac ctxt @{thms disjI1} 1, simp,
 | |
| 91 |                        resolve_tac ctxt @{thms disjI2} 1, simp]])))
 | |
| 61144 | 92 | handle ERROR _ => NONE)) | 
| 93 | in | |
| 94 | (case strip_comb (Thm.term_of ct) of | |
| 95 | (h as Const (name, _), ts) => | |
| 96 | if Symtab.defined tab name then | |
| 97 | let val rews = map mk_rew ts | |
| 98 | in | |
| 99 | if forall is_none rews then NONE | |
| 100 | else SOME (fold (fn th1 => fn th2 => Thm.combination th2 th1) | |
| 101 | (map2 (fn SOME r => K r | NONE => Thm.reflexive o Thm.cterm_of ctxt) | |
| 102 | rews ts) (Thm.reflexive (Thm.cterm_of ctxt h))) | |
| 103 | end | |
| 104 | else NONE | |
| 105 | | _ => NONE) | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 106 | end, | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 107 | identifier = []}; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 108 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 109 | (* only eta contract terms occurring as arguments of functions satisfying p *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 110 | fun eta_contract p = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 111 | let | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 112 | fun eta b (Abs (a, T, body)) = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 113 | (case eta b body of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 114 | body' as (f $ Bound 0) => | 
| 42083 
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
 wenzelm parents: 
41489diff
changeset | 115 | if Term.is_dependent f orelse not b then Abs (a, T, body') | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 116 | else incr_boundvars ~1 f | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 117 | | body' => Abs (a, T, body')) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 118 | | eta b (t $ u) = eta b t $ eta (p (head_of t)) u | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 119 | | eta b t = t | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 120 | in eta false end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 121 | |
| 60328 | 122 | fun eta_contract_thm ctxt p = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 123 | Conv.fconv_rule (Conv.then_conv (Thm.beta_conversion true, fn ct => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 124 | Thm.transitive (Thm.eta_conversion ct) | 
| 60328 | 125 | (Thm.symmetric (Thm.eta_conversion (Thm.cterm_of ctxt (eta_contract p (Thm.term_of ct))))))); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 126 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 127 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 128 | (***********************************************************) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 129 | (* rules for converting between predicate and set notation *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 130 | (* *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 131 | (* rules for converting predicates to sets have the form *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 132 | (* P (%x y. (x, y) : s) = (%x y. (x, y) : S s) *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 133 | (* *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 134 | (* rules for converting sets to predicates have the form *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 135 | (* S {(x, y). p x y} = {(x, y). P p x y}                   *)
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 136 | (* *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 137 | (* where s and p are parameters *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 138 | (***********************************************************) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 139 | |
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 140 | structure Data = Generic_Data | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 141 | ( | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 142 | type T = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 143 |     {(* rules for converting predicates to sets *)
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 144 | to_set_simps: thm list, | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 145 | (* rules for converting sets to predicates *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 146 | to_pred_simps: thm list, | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 147 | (* arities of functions of type t set => ... => u set *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 148 | set_arities: (typ * (int list list option list * int list list option)) list Symtab.table, | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 149 | (* arities of functions of type (t => ... => bool) => u => ... => bool *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 150 | pred_arities: (typ * (int list list option list * int list list option)) list Symtab.table}; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 151 |   val empty = {to_set_simps = [], to_pred_simps = [],
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 152 | set_arities = Symtab.empty, pred_arities = Symtab.empty}; | 
| 33519 | 153 | fun merge | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 154 |     ({to_set_simps = to_set_simps1, to_pred_simps = to_pred_simps1,
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 155 | set_arities = set_arities1, pred_arities = pred_arities1}, | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 156 |      {to_set_simps = to_set_simps2, to_pred_simps = to_pred_simps2,
 | 
| 29288 | 157 | set_arities = set_arities2, pred_arities = pred_arities2}) : T = | 
| 24039 
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
 wenzelm parents: 
23849diff
changeset | 158 |     {to_set_simps = Thm.merge_thms (to_set_simps1, to_set_simps2),
 | 
| 
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
 wenzelm parents: 
23849diff
changeset | 159 | to_pred_simps = Thm.merge_thms (to_pred_simps1, to_pred_simps2), | 
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
38864diff
changeset | 160 | set_arities = Symtab.merge_list (op =) (set_arities1, set_arities2), | 
| 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
38864diff
changeset | 161 | pred_arities = Symtab.merge_list (op =) (pred_arities1, pred_arities2)}; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 162 | ); | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 163 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 164 | fun name_type_of (Free p) = SOME p | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 165 | | name_type_of (Const p) = SOME p | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 166 | | name_type_of _ = NONE; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 167 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 168 | fun map_type f (Free (s, T)) = Free (s, f T) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 169 | | map_type f (Var (ixn, T)) = Var (ixn, f T) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 170 | | map_type f _ = error "map_type"; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 171 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 172 | fun find_most_specific is_inst f eq xs T = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 173 | find_first (fn U => is_inst (T, f U) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 174 | andalso forall (fn U' => eq (f U, f U') orelse not | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 175 | (is_inst (T, f U') andalso is_inst (f U', f U))) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 176 | xs) xs; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 177 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 178 | fun lookup_arity thy arities (s, T) = case Symtab.lookup arities s of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 179 | NONE => NONE | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 180 | | SOME xs => find_most_specific (Sign.typ_instance thy) fst (op =) xs T; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 181 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 182 | fun lookup_rule thy f rules = find_most_specific | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 183 | (swap #> Pattern.matches thy) (f #> fst) (op aconv) rules; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 184 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 185 | fun infer_arities thy arities (optf, t) fs = case strip_comb t of | 
| 56512 | 186 | (Abs (_, _, u), []) => infer_arities thy arities (NONE, u) fs | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 187 | | (Abs _, _) => infer_arities thy arities (NONE, Envir.beta_norm t) fs | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 188 | | (u, ts) => (case Option.map (lookup_arity thy arities) (name_type_of u) of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 189 | SOME (SOME (_, (arity, _))) => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 190 | (fold (infer_arities thy arities) (arity ~~ List.take (ts, length arity)) fs | 
| 43278 | 191 | handle General.Subscript => error "infer_arities: bad term") | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 192 | | _ => fold (infer_arities thy arities) (map (pair NONE) ts) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 193 | (case optf of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 194 | NONE => fs | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 195 | | SOME f => AList.update op = (u, the_default f | 
| 33049 
c38f02fdf35d
curried inter as canonical list operation (beware of argument order)
 haftmann parents: 
33038diff
changeset | 196 | (Option.map (fn g => inter (op =) g f) (AList.lookup op = fs u))) fs)); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 197 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 198 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 199 | (**************************************************************) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 200 | (* derive the to_pred equation from the to_set equation *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 201 | (* *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 202 | (* 1. instantiate each set parameter with {(x, y). p x y}     *)
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 203 | (* 2. apply %P. {(x, y). P x y} to both sides of the equation *)
 | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 204 | (* 3. simplify *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 205 | (**************************************************************) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 206 | |
| 59642 | 207 | fun mk_to_pred_inst ctxt fs = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 208 | map (fn (x, ps) => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 209 | let | 
| 46828 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 210 | val (Ts, T) = strip_type (fastype_of x); | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 211 | val U = HOLogic.dest_setT T; | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 212 | val x' = map_type | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 213 | (K (Ts @ HOLogic.strip_ptupleT ps U ---> HOLogic.boolT)) x; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 214 | in | 
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
60330diff
changeset | 215 | (dest_Var x, | 
| 59642 | 216 | Thm.cterm_of ctxt (fold_rev (Term.abs o pair "x") Ts | 
| 46828 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 217 | (HOLogic.Collect_const U $ | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61268diff
changeset | 218 | HOLogic.mk_ptupleabs ps U HOLogic.boolT | 
| 46828 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 219 | (list_comb (x', map Bound (length Ts - 1 downto 0)))))) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 220 | end) fs; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 221 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 222 | fun mk_to_pred_eq ctxt p fs optfs' T thm = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 223 | let | 
| 59642 | 224 | val insts = mk_to_pred_inst ctxt fs; | 
| 74282 | 225 | val thm' = Thm.instantiate (TVars.empty, Vars.make insts) thm; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 226 | val thm'' = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 227 | (case optfs' of | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 228 | NONE => thm' RS sym | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 229 | | SOME fs' => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 230 | let | 
| 45979 | 231 | val U = HOLogic.dest_setT (body_type T); | 
| 32342 
3fabf5b5fc83
path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
 haftmann parents: 
32287diff
changeset | 232 | val Ts = HOLogic.strip_ptupleT fs' U; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 233 | val arg_cong' = Thm.incr_indexes (Thm.maxidx_of thm + 1) arg_cong; | 
| 60781 | 234 | val (Var (arg_cong_f, _), _) = arg_cong' |> Thm.concl_of |> | 
| 235 | dest_comb |> snd |> strip_comb |> snd |> hd |> dest_comb; | |
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 236 | in | 
| 60781 | 237 | thm' RS (infer_instantiate ctxt [(arg_cong_f, | 
| 59642 | 238 |               Thm.cterm_of ctxt (Abs ("P", Ts ---> HOLogic.boolT,
 | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61268diff
changeset | 239 | HOLogic.Collect_const U $ HOLogic.mk_ptupleabs fs' U | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 240 | HOLogic.boolT (Bound 0))))] arg_cong' RS sym) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 241 | end) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 242 | in | 
| 63399 | 243 | Simplifier.simplify | 
| 244 |       (put_simpset HOL_basic_ss ctxt addsimps @{thms mem_Collect_eq case_prod_conv}
 | |
| 67149 | 245 | addsimprocs [\<^simproc>\<open>Collect_mem\<close>]) thm'' | 
| 63399 | 246 | |> zero_var_indexes |> eta_contract_thm ctxt (equal p) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 247 | end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 248 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 249 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 250 | (**** declare rules for converting predicates to sets ****) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 251 | |
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 252 | exception Malformed of string; | 
| 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 253 | |
| 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 254 | fun add context thm (tab as {to_set_simps, to_pred_simps, set_arities, pred_arities}) =
 | 
| 59582 | 255 | (case Thm.prop_of thm of | 
| 67149 | 256 | Const (\<^const_name>\<open>Trueprop\<close>, _) $ (Const (\<^const_name>\<open>HOL.eq\<close>, Type (_, [T, _])) $ lhs $ rhs) => | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 257 | (case body_type T of | 
| 67149 | 258 | \<^typ>\<open>bool\<close> => | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 259 | let | 
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 260 | val thy = Context.theory_of context; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 261 | val ctxt = Context.proof_of context; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 262 | fun factors_of t fs = case strip_abs_body t of | 
| 67149 | 263 | Const (\<^const_name>\<open>Set.member\<close>, _) $ u $ S => | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 264 | if is_Free S orelse is_Var S then | 
| 32287 
65d5c5b30747
cleaned up abstract tuple operations and named them consistently
 haftmann parents: 
32135diff
changeset | 265 | let val ps = HOLogic.flat_tuple_paths u | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 266 | in (SOME ps, (S, ps) :: fs) end | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 267 | else (NONE, fs) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 268 | | _ => (NONE, fs); | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 269 | val (h, ts) = strip_comb lhs | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 270 | val (pfs, fs) = fold_map factors_of ts []; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 271 | val ((h', ts'), fs') = (case rhs of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 272 | Abs _ => (case strip_abs_body rhs of | 
| 67149 | 273 | Const (\<^const_name>\<open>Set.member\<close>, _) $ u $ S => | 
| 32287 
65d5c5b30747
cleaned up abstract tuple operations and named them consistently
 haftmann parents: 
32135diff
changeset | 274 | (strip_comb S, SOME (HOLogic.flat_tuple_paths u)) | 
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 275 | | _ => raise Malformed "member symbol on right-hand side expected") | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 276 | | _ => (strip_comb rhs, NONE)) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 277 | in | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 278 | case (name_type_of h, name_type_of h') of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 279 | (SOME (s, T), SOME (s', T')) => | 
| 26047 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 280 | if exists (fn (U, _) => | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 281 | Sign.typ_instance thy (T', U) andalso | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 282 | Sign.typ_instance thy (U, T')) | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 283 | (Symtab.lookup_list set_arities s') | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 284 | then | 
| 57870 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 285 | (if Context_Position.is_really_visible ctxt then | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 286 |                      warning ("Ignoring conversion rule for operator " ^ s')
 | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 287 | else (); tab) | 
| 26047 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 288 | else | 
| 67637 | 289 |                    {to_set_simps = Thm.trim_context thm :: to_set_simps,
 | 
| 26047 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 290 | to_pred_simps = | 
| 67637 | 291 | Thm.trim_context (mk_to_pred_eq ctxt h fs fs' T' thm) :: to_pred_simps, | 
| 26047 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 292 | set_arities = Symtab.insert_list op = (s', | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 293 | (T', (map (AList.lookup op = fs) ts', fs'))) set_arities, | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 294 | pred_arities = Symtab.insert_list op = (s, | 
| 
d27b89c95b29
Instead of raising an exception, pred_set_conv now ignores conversion
 berghofe parents: 
25978diff
changeset | 295 | (T, (pfs, fs'))) pred_arities} | 
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 296 | | _ => raise Malformed "set / predicate constant expected" | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 297 | end | 
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 298 | | _ => raise Malformed "equation between predicates expected") | 
| 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 299 | | _ => raise Malformed "equation expected") | 
| 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 300 | handle Malformed msg => | 
| 57870 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 301 | let | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 302 | val ctxt = Context.proof_of context | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 303 | val _ = | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 304 | if Context_Position.is_really_visible ctxt then | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 305 |           warning ("Ignoring malformed set / predicate conversion rule: " ^ msg ^
 | 
| 61268 | 306 | "\n" ^ Thm.string_of_thm ctxt thm) | 
| 57870 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 307 | else (); | 
| 
561680651364
observe context visibility -- less redundant warnings;
 wenzelm parents: 
56512diff
changeset | 308 | in tab end; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 309 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 310 | val pred_set_conv_att = Thm.declaration_attribute | 
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 311 | (fn thm => fn ctxt => Data.map (add ctxt thm) ctxt); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 312 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 313 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 314 | (**** convert theorem in set notation to predicate notation ****) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 315 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 316 | fun is_pred tab t = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 317 | case Option.map (Symtab.lookup tab o fst) (name_type_of t) of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 318 | SOME (SOME _) => true | _ => false; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 319 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 320 | fun to_pred_simproc rules = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 321 | let val rules' = map mk_meta_eq rules | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 322 | in | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 323 | Simplifier.make_simproc \<^context> | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 324 |       {name = "to_pred",
 | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 325 | lhss = [anyt], | 
| 61144 | 326 | proc = fn _ => fn ctxt => fn ct => | 
| 327 | lookup_rule (Proof_Context.theory_of ctxt) | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 328 | (Thm.prop_of #> Logic.dest_equals) rules' (Thm.term_of ct), | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78095diff
changeset | 329 | identifier = []} | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 330 | end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 331 | |
| 59642 | 332 | fun to_pred_proc thy rules t = | 
| 333 | case lookup_rule thy I rules t of | |
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 334 | NONE => NONE | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 335 | | SOME (lhs, rhs) => | 
| 32035 | 336 | SOME (Envir.subst_term | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 337 | (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rhs); | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 338 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 339 | fun to_pred thms context thm = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 340 | let | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 341 | val thy = Context.theory_of context; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 342 | val ctxt = Context.proof_of context; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 343 |     val {to_pred_simps, set_arities, pred_arities, ...} =
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 344 | fold (add context) thms (Data.get context); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 345 | val fs = filter (is_Var o fst) | 
| 59582 | 346 | (infer_arities thy set_arities (NONE, Thm.prop_of thm) []); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 347 |     (* instantiate each set parameter with {(x, y). p x y} *)
 | 
| 59642 | 348 | val insts = mk_to_pred_inst ctxt fs | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 349 | in | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 350 | thm |> | 
| 74282 | 351 | Thm.instantiate (TVars.empty, Vars.make insts) |> | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 352 | Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimprocs | 
| 67637 | 353 | [to_pred_simproc | 
| 354 |         (@{thm mem_Collect_eq} :: @{thm case_prod_conv} :: map (Thm.transfer thy) to_pred_simps)]) |>
 | |
| 60328 | 355 | eta_contract_thm ctxt (is_pred pred_arities) |> | 
| 33368 | 356 | Rule_Cases.save thm | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 357 | end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 358 | |
| 61853 
fb7756087101
rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
 wenzelm parents: 
61424diff
changeset | 359 | val to_pred_att = Thm.rule_attribute [] o to_pred; | 
| 45979 | 360 | |
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 361 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 362 | (**** convert theorem in predicate notation to set notation ****) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 363 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 364 | fun to_set thms context thm = | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 365 | let | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 366 | val thy = Context.theory_of context; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 367 | val ctxt = Context.proof_of context; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 368 |     val {to_set_simps, pred_arities, ...} =
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 369 | fold (add context) thms (Data.get context); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 370 | val fs = filter (is_Var o fst) | 
| 59582 | 371 | (infer_arities thy pred_arities (NONE, Thm.prop_of thm) []); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 372 | (* instantiate each predicate parameter with %x y. (x, y) : s *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 373 | val insts = map (fn (x, ps) => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 374 | let | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 375 | val Ts = binder_types (fastype_of x); | 
| 46828 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 376 | val l = length Ts; | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 377 | val k = length ps; | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 378 | val (Rs, Us) = chop (l - k - 1) Ts; | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 379 | val T = HOLogic.mk_ptupleT ps Us; | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 380 | val x' = map_type (K (Rs ---> HOLogic.mk_setT T)) x | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 381 | in | 
| 60642 
48dd1cefb4ae
simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
 wenzelm parents: 
60330diff
changeset | 382 | (dest_Var x, | 
| 59642 | 383 | Thm.cterm_of ctxt (fold_rev (Term.abs o pair "x") Ts | 
| 46828 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 384 | (HOLogic.mk_mem (HOLogic.mk_ptuple ps T (map Bound (k downto 0)), | 
| 
b1d15637381a
to_pred/set attributes now properly handle variables of type "... => T set"
 berghofe parents: 
46219diff
changeset | 385 | list_comb (x', map Bound (l - 1 downto k + 1)))))) | 
| 46219 
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
 wenzelm parents: 
45979diff
changeset | 386 | end) fs; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 387 | in | 
| 25416 
1d8ebaf5f211
to_pred and to_set now save induction and case rule tags.
 berghofe parents: 
25016diff
changeset | 388 | thm |> | 
| 74282 | 389 | Thm.instantiate (TVars.empty, Vars.make insts) |> | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50774diff
changeset | 390 | Simplifier.full_simplify (put_simpset HOL_basic_ss ctxt addsimps to_set_simps | 
| 67149 | 391 | addsimprocs [strong_ind_simproc pred_arities, \<^simproc>\<open>Collect_mem\<close>]) |> | 
| 33368 | 392 | Rule_Cases.save thm | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 393 | end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 394 | |
| 61853 
fb7756087101
rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
 wenzelm parents: 
61424diff
changeset | 395 | val to_set_att = Thm.rule_attribute [] o to_set; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 396 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 397 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 398 | (**** definition of inductive sets ****) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 399 | |
| 29389 | 400 | fun add_ind_set_def | 
| 49170 
03bee3a6a1b7
discontinued obsolete fork_mono to loosen some brakes -- NB: TTY interaction has Goal.future_proofs disabled due to missing Future.worker_task;
 wenzelm parents: 
46961diff
changeset | 401 |     {quiet_mode, verbose, alt_name, coind, no_elim, no_ind, skip_mono}
 | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 402 | cs intros monos params cnames_syn lthy = | 
| 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 403 | let | 
| 42361 | 404 | val thy = Proof_Context.theory_of lthy; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 405 |     val {set_arities, pred_arities, to_pred_simps, ...} =
 | 
| 50774 
ac53370dfae1
more tolerant set/pred rule declaration to improve "tool compliance", notably for "context assumes";
 wenzelm parents: 
49324diff
changeset | 406 | Data.get (Context.Proof lthy); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 407 | fun infer (Abs (_, _, t)) = infer t | 
| 67149 | 408 | | infer (Const (\<^const_name>\<open>Set.member\<close>, _) $ t $ u) = | 
| 32287 
65d5c5b30747
cleaned up abstract tuple operations and named them consistently
 haftmann parents: 
32135diff
changeset | 409 | infer_arities thy set_arities (SOME (HOLogic.flat_tuple_paths t), u) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 410 | | infer (t $ u) = infer t #> infer u | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 411 | | infer _ = I; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 412 | val new_arities = filter_out | 
| 45979 | 413 | (fn (x as Free (_, T), _) => member (op =) params x andalso length (binder_types T) > 0 | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 414 | | _ => false) (fold (snd #> infer) intros []); | 
| 33278 | 415 | val params' = map (fn x => | 
| 416 | (case AList.lookup op = new_arities x of | |
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 417 | SOME fs => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 418 | let | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 419 | val T = HOLogic.dest_setT (fastype_of x); | 
| 32342 
3fabf5b5fc83
path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
 haftmann parents: 
32287diff
changeset | 420 | val Ts = HOLogic.strip_ptupleT fs T; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 421 | val x' = map_type (K (Ts ---> HOLogic.boolT)) x | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 422 | in | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 423 | (x, (x', | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 424 | (HOLogic.Collect_const T $ | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61268diff
changeset | 425 | HOLogic.mk_ptupleabs fs T HOLogic.boolT x', | 
| 46219 
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
 wenzelm parents: 
45979diff
changeset | 426 | fold_rev (Term.abs o pair "x") Ts | 
| 
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
 wenzelm parents: 
45979diff
changeset | 427 | (HOLogic.mk_mem | 
| 
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
 wenzelm parents: 
45979diff
changeset | 428 | (HOLogic.mk_ptuple fs T (map Bound (length fs downto 0)), x))))) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 429 | end | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 430 | | NONE => (x, (x, (x, x))))) params; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 431 | val (params1, (params2, params3)) = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 432 | params' |> map snd |> split_list ||> split_list; | 
| 30860 
e5f9477aed50
Added check whether argument types of inductive set agree with types of declared
 berghofe parents: 
30528diff
changeset | 433 | val paramTs = map fastype_of params; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 434 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 435 | (* equations for converting sets to predicates *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 436 | val ((cs', cs_info), eqns) = cs |> map (fn c as Free (s, T) => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 437 | let | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 438 | val fs = the_default [] (AList.lookup op = new_arities c); | 
| 45979 | 439 | val (Us, U) = strip_type T |> apsnd HOLogic.dest_setT; | 
| 30860 
e5f9477aed50
Added check whether argument types of inductive set agree with types of declared
 berghofe parents: 
30528diff
changeset | 440 | val _ = Us = paramTs orelse error (Pretty.string_of (Pretty.chunks | 
| 
e5f9477aed50
Added check whether argument types of inductive set agree with types of declared
 berghofe parents: 
30528diff
changeset | 441 | [Pretty.str "Argument types", | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 442 | Pretty.block (Pretty.commas (map (Syntax.pretty_typ lthy) Us)), | 
| 30860 
e5f9477aed50
Added check whether argument types of inductive set agree with types of declared
 berghofe parents: 
30528diff
changeset | 443 |            Pretty.str ("of " ^ s ^ " do not agree with types"),
 | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 444 | Pretty.block (Pretty.commas (map (Syntax.pretty_typ lthy) paramTs)), | 
| 30860 
e5f9477aed50
Added check whether argument types of inductive set agree with types of declared
 berghofe parents: 
30528diff
changeset | 445 | Pretty.str "of declared parameters"])); | 
| 32342 
3fabf5b5fc83
path-sensitive tuple combinators carry a "p"(ath) prefix; combinators for standard right-fold tuples
 haftmann parents: 
32287diff
changeset | 446 | val Ts = HOLogic.strip_ptupleT fs U; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 447 | val c' = Free (s ^ "p", | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 448 | map fastype_of params1 @ Ts ---> HOLogic.boolT) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 449 | in | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 450 | ((c', (fs, U, Ts)), | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 451 | (list_comb (c, params2), | 
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61268diff
changeset | 452 | HOLogic.Collect_const U $ HOLogic.mk_ptupleabs fs U HOLogic.boolT | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 453 | (list_comb (c', params1)))) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 454 | end) |> split_list |>> split_list; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 455 | val eqns' = eqns @ | 
| 59582 | 456 | map (Thm.prop_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq) | 
| 63399 | 457 |         (@{thm mem_Collect_eq} :: @{thm case_prod_conv} :: to_pred_simps);
 | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 458 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 459 | (* predicate version of the introduction rules *) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 460 | val intros' = | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 461 | map (fn (name_atts, t) => (name_atts, | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 462 | t |> | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 463 | map_aterms (fn u => | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 464 | (case AList.lookup op = params' u of | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 465 | SOME (_, (u', _)) => u' | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 466 | | NONE => u)) |> | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 467 | Pattern.rewrite_term thy [] [to_pred_proc thy eqns'] |> | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 468 | eta_contract (member op = cs' orf is_pred pred_arities))) intros; | 
| 30345 | 469 | val cnames_syn' = map (fn (b, _) => (Binding.suffix_name "p" b, NoSyn)) cnames_syn; | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 470 | val monos' = map (to_pred [] (Context.Proof lthy)) monos; | 
| 38665 
e92223c886f8
introducing simplification equations for inductive sets; added data structure for storing equations; rewriting retrieval of simplification equation for inductive predicates and sets
 bulwahn parents: 
37863diff
changeset | 471 |     val ({preds, intrs, elims, raw_induct, eqs, ...}, lthy1) =
 | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
30860diff
changeset | 472 | Inductive.add_ind_def | 
| 33669 | 473 |         {quiet_mode = quiet_mode, verbose = verbose, alt_name = Binding.empty,
 | 
| 49170 
03bee3a6a1b7
discontinued obsolete fork_mono to loosen some brakes -- NB: TTY interaction has Goal.future_proofs disabled due to missing Future.worker_task;
 wenzelm parents: 
46961diff
changeset | 474 | coind = coind, no_elim = no_elim, no_ind = no_ind, skip_mono = skip_mono} | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 475 | cs' intros' monos' params1 cnames_syn' lthy; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 476 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 477 | (* define inductive sets using previously defined predicates *) | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 478 | val (defs, lthy2) = lthy1 | 
| 33766 
c679f05600cd
adapted Local_Theory.define -- eliminated odd thm kind;
 wenzelm parents: 
33671diff
changeset | 479 | |> fold_map Local_Theory.define | 
| 61951 | 480 | (map (fn (((b, mx), (fs, U, _)), p) => | 
| 63041 
cb495c4807b3
clarified def binding position: reset for implicit/derived binding, keep for explicit binding;
 wenzelm parents: 
63006diff
changeset | 481 | ((b, mx), ((Thm.def_binding b, []), | 
| 61951 | 482 | fold_rev lambda params (HOLogic.Collect_const U $ | 
| 483 | HOLogic.mk_ptupleabs fs U HOLogic.boolT (list_comb (p, params3)))))) | |
| 484 | (cnames_syn ~~ cs_info ~~ preds)); | |
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 485 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 486 | (* prove theorems for converting predicate to set notation *) | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 487 | val lthy3 = fold | 
| 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 488 | (fn (((p, c as Free (s, _)), (fs, U, Ts)), (_, (_, def))) => fn lthy => | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 489 | let val conv_thm = | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 490 | Goal.prove lthy (map (fst o dest_Free) params) [] | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 491 | (HOLogic.mk_Trueprop (HOLogic.mk_eq | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 492 | (list_comb (p, params3), | 
| 46219 
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
 wenzelm parents: 
45979diff
changeset | 493 | fold_rev (Term.abs o pair "x") Ts | 
| 
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
 wenzelm parents: 
45979diff
changeset | 494 | (HOLogic.mk_mem (HOLogic.mk_ptuple fs U (map Bound (length fs downto 0)), | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 495 | list_comb (c, params)))))) | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
58839diff
changeset | 496 |             (K (REPEAT (resolve_tac lthy @{thms ext} 1) THEN
 | 
| 58839 | 497 | simp_tac (put_simpset HOL_basic_ss lthy addsimps | 
| 63399 | 498 |                 [def, @{thm mem_Collect_eq}, @{thm case_prod_conv}]) 1))
 | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 499 | in | 
| 33671 | 500 | lthy |> Local_Theory.note ((Binding.name (s ^ "p_" ^ s ^ "_eq"), | 
| 78095 | 501 | [Attrib.internal \<^here> (K pred_set_conv_att)]), | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 502 | [conv_thm]) |> snd | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 503 | end) (preds ~~ cs ~~ cs_info ~~ defs) lthy2; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 504 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 505 | (* convert theorems to set notation *) | 
| 28083 
103d9282a946
explicit type Name.binding for higher-specification elements;
 wenzelm parents: 
27330diff
changeset | 506 | val rec_name = | 
| 63006 | 507 | if Binding.is_empty alt_name then Binding.conglomerate (map #1 cnames_syn) else alt_name; | 
| 33671 | 508 | val cnames = map (Local_Theory.full_name lthy3 o #1) cnames_syn; (* FIXME *) | 
| 71214 
5727bcc3c47c
proper spec_rule name via naming/binding/Morphism.binding;
 wenzelm parents: 
71179diff
changeset | 509 | val spec_name = Binding.conglomerate (map #1 cnames_syn); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 510 | val (intr_names, intr_atts) = split_list (map fst intros); | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 511 | val raw_induct' = to_set [] (Context.Proof lthy3) raw_induct; | 
| 37734 
489ac1ecb9f1
added the new command inductive_cases to derive simplification equations for inductive predicates; added binding simps for general simplification equation
 bulwahn parents: 
37677diff
changeset | 512 | val (intrs', elims', eqs', induct, inducts, lthy4) = | 
| 71179 | 513 | Inductive.declare_rules rec_name coind no_ind spec_name cnames (map fst defs) | 
| 33459 | 514 | (map (to_set [] (Context.Proof lthy3)) intrs) intr_names intr_atts | 
| 515 | (map (fn th => (to_set [] (Context.Proof lthy3) th, | |
| 44045 
2814ff2a6e3e
infrastructure for attaching names to hypothesis in cases; realised via the same tag mechanism as case names
 nipkow parents: 
43278diff
changeset | 516 | map (fst o fst) (fst (Rule_Cases.get th)), | 
| 34986 
7f7939c9370f
Added "constraints" tag / attribute for specifying the number of equality
 berghofe parents: 
34903diff
changeset | 517 | Rule_Cases.get_constraints th)) elims) | 
| 38665 
e92223c886f8
introducing simplification equations for inductive sets; added data structure for storing equations; rewriting retrieval of simplification equation for inductive predicates and sets
 bulwahn parents: 
37863diff
changeset | 518 | (map (to_set [] (Context.Proof lthy3)) eqs) raw_induct' lthy3; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 519 | in | 
| 35646 | 520 |     ({intrs = intrs', elims = elims', induct = induct, inducts = inducts,
 | 
| 37734 
489ac1ecb9f1
added the new command inductive_cases to derive simplification equations for inductive predicates; added binding simps for general simplification equation
 bulwahn parents: 
37677diff
changeset | 521 | raw_induct = raw_induct', preds = map fst defs, eqs = eqs'}, | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 522 | lthy4) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 523 | end; | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 524 | |
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
30860diff
changeset | 525 | val add_inductive = Inductive.gen_add_inductive add_ind_set_def; | 
| 69709 | 526 | val add_inductive_cmd = Inductive.gen_add_inductive_cmd add_ind_set_def; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 527 | |
| 45384 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 528 | fun mono_att att = | 
| 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 529 | Thm.declaration_attribute (fn thm => fn context => | 
| 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 530 | Thm.attribute_declaration att (to_pred [] context thm) context); | 
| 45375 
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
 wenzelm parents: 
45177diff
changeset | 531 | |
| 45384 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 532 | val mono_add = mono_att Inductive.mono_add; | 
| 
dffa657f0aa2
clarified attribute "mono_set": pure declaration, proper export in ML;
 wenzelm parents: 
45375diff
changeset | 533 | val mono_del = mono_att Inductive.mono_del; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 534 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 535 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 536 | (** package setup **) | 
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 537 | |
| 56512 | 538 | (* attributes *) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 539 | |
| 56512 | 540 | val _ = | 
| 541 | Theory.setup | |
| 67149 | 542 | (Attrib.setup \<^binding>\<open>pred_set_conv\<close> (Scan.succeed pred_set_conv_att) | 
| 56512 | 543 | "declare rules for converting between predicate and set notation" #> | 
| 67149 | 544 | Attrib.setup \<^binding>\<open>to_set\<close> (Attrib.thms >> to_set_att) | 
| 56512 | 545 | "convert rule to set notation" #> | 
| 67149 | 546 | Attrib.setup \<^binding>\<open>to_pred\<close> (Attrib.thms >> to_pred_att) | 
| 56512 | 547 | "convert rule to predicate notation" #> | 
| 67149 | 548 | Attrib.setup \<^binding>\<open>mono_set\<close> (Attrib.add_del mono_add mono_del) | 
| 56512 | 549 | "declare of monotonicity rule for set operators"); | 
| 30528 | 550 | |
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 551 | |
| 61424 
c3658c18b7bc
prod_case as canonical name for product type eliminator
 haftmann parents: 
61268diff
changeset | 552 | (* commands *) | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 553 | |
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
30860diff
changeset | 554 | val ind_set_decl = Inductive.gen_ind_decl add_ind_set_def; | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 555 | |
| 24867 | 556 | val _ = | 
| 67149 | 557 | Outer_Syntax.local_theory \<^command_keyword>\<open>inductive_set\<close> "define inductive sets" | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 558 | (ind_set_decl false); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 559 | |
| 24867 | 560 | val _ = | 
| 67149 | 561 | Outer_Syntax.local_theory \<^command_keyword>\<open>coinductive_set\<close> "define coinductive sets" | 
| 33458 
ae1f5d89b082
proper naming convention lthy: local_theory, but ctxt: Proof.context for arbitrary context;
 wenzelm parents: 
33368diff
changeset | 562 | (ind_set_decl true); | 
| 23764 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 563 | |
| 
15f81c5d5330
New wrapper for defining inductive sets with new inductive
 berghofe parents: diff
changeset | 564 | end; |