| author | wenzelm | 
| Wed, 27 Mar 2024 15:38:41 +0100 | |
| changeset 80033 | 71d005ffa9fe | 
| parent 79738 | 8ae4fc4692e8 | 
| child 80064 | 0d94dd2fd2d0 | 
| permissions | -rw-r--r-- | 
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 1 | (* Title: Pure/raw_simplifier.ML | 
| 29269 
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
 wenzelm parents: 
28839diff
changeset | 2 | Author: Tobias Nipkow and Stefan Berghofer, TU Muenchen | 
| 10413 | 3 | |
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 4 | Higher-order Simplification. | 
| 10413 | 5 | *) | 
| 6 | ||
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 7 | infix 4 | 
| 45620 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45405diff
changeset | 8 | addsimps delsimps addsimprocs delsimprocs | 
| 52037 | 9 | setloop addloop delloop | 
| 45625 
750c5a47400b
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45621diff
changeset | 10 | setSSolver addSSolver setSolver addSolver; | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 11 | |
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 12 | signature BASIC_RAW_SIMPLIFIER = | 
| 11672 | 13 | sig | 
| 41227 | 14 | val simp_depth_limit: int Config.T | 
| 15 | val simp_trace_depth_limit: int Config.T | |
| 40878 
7695e4de4d86
renamed trace_simp to simp_trace, and debug_simp to simp_debug;
 wenzelm parents: 
39163diff
changeset | 16 | val simp_debug: bool Config.T | 
| 
7695e4de4d86
renamed trace_simp to simp_trace, and debug_simp to simp_debug;
 wenzelm parents: 
39163diff
changeset | 17 | val simp_trace: bool Config.T | 
| 51590 | 18 | type cong_name = bool * string | 
| 15023 | 19 | type rrule | 
| 55316 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 20 | val mk_rrules: Proof.context -> thm list -> rrule list | 
| 16807 | 21 | val eq_rrule: rrule * rrule -> bool | 
| 78800 | 22 | type proc = Proof.context -> cterm -> thm option | 
| 23 | type simproc0 | |
| 17614 | 24 | type solver | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 25 | val mk_solver: string -> (Proof.context -> int -> tactic) -> solver | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 26 | type simpset | 
| 15023 | 27 | val empty_ss: simpset | 
| 28 | val merge_ss: simpset * simpset -> simpset | |
| 30356 | 29 | val dest_ss: simpset -> | 
| 30 |    {simps: (string * thm) list,
 | |
| 61098 | 31 | procs: (string * term list) list, | 
| 51590 | 32 | congs: (cong_name * thm) list, | 
| 33 | weak_congs: cong_name list, | |
| 30356 | 34 | loopers: string list, | 
| 35 | unsafe_solvers: string list, | |
| 36 | safe_solvers: string list} | |
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 37 | val dest_simps: simpset -> thm list | 
| 15023 | 38 | type simproc | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 39 | val cert_simproc: theory -> | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 40 |     {name: string, lhss: term list, proc: proc Morphism.entity, identifier: thm list} -> simproc
 | 
| 45290 | 41 | val transform_simproc: morphism -> simproc -> simproc | 
| 78114 | 42 | val trim_context_simproc: simproc -> simproc | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 43 | val simpset_of: Proof.context -> simpset | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 44 | val put_simpset: simpset -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 45 | val simpset_map: Proof.context -> (Proof.context -> Proof.context) -> simpset -> simpset | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 46 | val map_theory_simpset: (Proof.context -> Proof.context) -> theory -> theory | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 47 | val empty_simpset: Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 48 | val clear_simpset: Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 49 | val addsimps: Proof.context * thm list -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 50 | val delsimps: Proof.context * thm list -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 51 | val addsimprocs: Proof.context * simproc list -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 52 | val delsimprocs: Proof.context * simproc list -> Proof.context | 
| 52037 | 53 | val setloop: Proof.context * (Proof.context -> int -> tactic) -> Proof.context | 
| 54 | val addloop: Proof.context * (string * (Proof.context -> int -> tactic)) -> Proof.context | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 55 | val delloop: Proof.context * string -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 56 | val setSSolver: Proof.context * solver -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 57 | val addSSolver: Proof.context * solver -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 58 | val setSolver: Proof.context * solver -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 59 | val addSolver: Proof.context * solver -> Proof.context | 
| 21708 | 60 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 61 | val rewrite_rule: Proof.context -> thm list -> thm -> thm | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 62 | val rewrite_goals_rule: Proof.context -> thm list -> thm -> thm | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 63 | val rewrite_goals_tac: Proof.context -> thm list -> tactic | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 64 | val rewrite_goal_tac: Proof.context -> thm list -> int -> tactic | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 65 | val prune_params_tac: Proof.context -> tactic | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 66 | val fold_rule: Proof.context -> thm list -> thm -> thm | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 67 | val fold_goals_tac: Proof.context -> thm list -> tactic | 
| 54883 
dd04a8b654fc
proper context for norm_hhf and derived operations;
 wenzelm parents: 
54742diff
changeset | 68 | val norm_hhf: Proof.context -> thm -> thm | 
| 
dd04a8b654fc
proper context for norm_hhf and derived operations;
 wenzelm parents: 
54742diff
changeset | 69 | val norm_hhf_protect: Proof.context -> thm -> thm | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 70 | end; | 
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 71 | |
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 72 | signature RAW_SIMPLIFIER = | 
| 10413 | 73 | sig | 
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 74 | include BASIC_RAW_SIMPLIFIER | 
| 54997 | 75 | exception SIMPLIFIER of string * thm list | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 76 | type trace_ops | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 77 | val set_trace_ops: trace_ops -> theory -> theory | 
| 71235 | 78 | val subgoal_tac: Proof.context -> int -> tactic | 
| 79 | val loop_tac: Proof.context -> int -> tactic | |
| 80 | val solvers: Proof.context -> solver list * solver list | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 81 | val map_ss: (Proof.context -> Proof.context) -> Context.generic -> Context.generic | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 82 | val prems_of: Proof.context -> thm list | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 83 | val add_simp: thm -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 84 | val del_simp: thm -> Proof.context -> Proof.context | 
| 68403 | 85 | val flip_simp: thm -> Proof.context -> Proof.context | 
| 63221 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 86 | val init_simpset: thm list -> Proof.context -> Proof.context | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 87 | val add_eqcong: thm -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 88 | val del_eqcong: thm -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 89 | val add_cong: thm -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 90 | val del_cong: thm -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 91 | val mksimps: Proof.context -> thm -> thm list | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 92 | val set_mksimps: (Proof.context -> thm -> thm list) -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 93 | val set_mkcong: (Proof.context -> thm -> thm) -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 94 | val set_mksym: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 95 | val set_mkeqTrue: (Proof.context -> thm -> thm option) -> Proof.context -> Proof.context | 
| 70586 | 96 | val set_term_ord: term ord -> Proof.context -> Proof.context | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 97 | val set_subgoaler: (Proof.context -> int -> tactic) -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 98 | val solver: Proof.context -> solver -> int -> tactic | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 99 | val default_mk_sym: Proof.context -> thm -> thm option | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 100 | val add_prems: thm list -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 101 | val set_reorient: (Proof.context -> term list -> term -> term -> bool) -> | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 102 | Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 103 | val set_solvers: solver list -> Proof.context -> Proof.context | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 104 | val rewrite_cterm: bool * bool * bool -> | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 105 | (Proof.context -> thm -> thm option) -> Proof.context -> conv | 
| 16458 | 106 | val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term | 
| 15023 | 107 | val rewrite_thm: bool * bool * bool -> | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 108 | (Proof.context -> thm -> thm option) -> Proof.context -> thm -> thm | 
| 46465 | 109 | val generic_rewrite_goal_tac: bool * bool * bool -> | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 110 | (Proof.context -> tactic) -> Proof.context -> int -> tactic | 
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 111 | val rewrite0: Proof.context -> bool -> conv | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 112 | val rewrite: Proof.context -> bool -> thm list -> conv | 
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 113 | val rewrite0_rule: Proof.context -> thm -> thm | 
| 10413 | 114 | end; | 
| 115 | ||
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 116 | structure Raw_Simplifier: RAW_SIMPLIFIER = | 
| 10413 | 117 | struct | 
| 118 | ||
| 15023 | 119 | (** datatype simpset **) | 
| 120 | ||
| 51590 | 121 | (* congruence rules *) | 
| 122 | ||
| 123 | type cong_name = bool * string; | |
| 124 | ||
| 125 | fun cong_name (Const (a, _)) = SOME (true, a) | |
| 126 | | cong_name (Free (a, _)) = SOME (false, a) | |
| 127 | | cong_name _ = NONE; | |
| 128 | ||
| 71239 | 129 | structure Congtab = Table(type key = cong_name val ord = prod_ord bool_ord fast_string_ord); | 
| 130 | ||
| 51590 | 131 | |
| 15023 | 132 | (* rewrite rules *) | 
| 10413 | 133 | |
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 134 | type rrule = | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 135 |  {thm: thm,         (*the rewrite rule*)
 | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 136 | name: string, (*name of theorem from which rewrite rule was extracted*) | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 137 | lhs: term, (*the left-hand side*) | 
| 58836 | 138 | elhs: cterm, (*the eta-contracted lhs*) | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 139 | extra: bool, (*extra variables outside of elhs*) | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 140 | fo: bool, (*use first-order matching*) | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 141 | perm: bool}; (*the rewrite rule is permutative*) | 
| 15023 | 142 | |
| 61057 | 143 | fun trim_context_rrule ({thm, name, lhs, elhs, extra, fo, perm}: rrule) =
 | 
| 144 |   {thm = Thm.trim_context thm, name = name, lhs = lhs, elhs = Thm.trim_context_cterm elhs,
 | |
| 145 | extra = extra, fo = fo, perm = perm}; | |
| 146 | ||
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 147 | (* | 
| 12603 | 148 | Remarks: | 
| 10413 | 149 | - elhs is used for matching, | 
| 15023 | 150 | lhs only for preservation of bound variable names; | 
| 10413 | 151 | - fo is set iff | 
| 152 | either elhs is first-order (no Var is applied), | |
| 15023 | 153 | in which case fo-matching is complete, | 
| 10413 | 154 | or elhs is not a pattern, | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 155 | in which case there is nothing better to do; | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 156 | *) | 
| 10413 | 157 | |
| 158 | fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
 | |
| 22360 
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
 wenzelm parents: 
22254diff
changeset | 159 | Thm.eq_thm_prop (thm1, thm2); | 
| 15023 | 160 | |
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 161 | (* FIXME: it seems that the conditions on extra variables are too liberal if | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 162 | prems are nonempty: does solving the prems really guarantee instantiation of | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 163 | all its Vars? Better: a dynamic check each time a rule is applied. | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 164 | *) | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 165 | fun rewrite_rule_extra_vars prems elhs erhs = | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 166 | let | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 167 | val elhss = elhs :: prems; | 
| 74266 | 168 | val tvars = TVars.build (fold TVars.add_tvars elhss); | 
| 169 | val vars = Vars.build (fold Vars.add_vars elhss); | |
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 170 | in | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 171 | erhs |> Term.exists_type (Term.exists_subtype | 
| 74266 | 172 | (fn TVar v => not (TVars.defined tvars v) | _ => false)) orelse | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 173 | erhs |> Term.exists_subterm | 
| 74266 | 174 | (fn Var v => not (Vars.defined vars v) | _ => false) | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 175 | end; | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 176 | |
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 177 | fun rrule_extra_vars elhs thm = | 
| 59582 | 178 | rewrite_rule_extra_vars [] (Thm.term_of elhs) (Thm.full_prop_of thm); | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 179 | |
| 15023 | 180 | fun mk_rrule2 {thm, name, lhs, elhs, perm} =
 | 
| 181 | let | |
| 59582 | 182 | val t = Thm.term_of elhs; | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 183 | val fo = Pattern.first_order t orelse not (Pattern.pattern t); | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 184 | val extra = rrule_extra_vars elhs thm; | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 185 |   in {thm = thm, name = name, lhs = lhs, elhs = elhs, extra = extra, fo = fo, perm = perm} end;
 | 
| 10413 | 186 | |
| 15023 | 187 | (*simple test for looping rewrite rules and stupid orientations*) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 188 | fun default_reorient ctxt prems lhs rhs = | 
| 15023 | 189 | rewrite_rule_extra_vars prems lhs rhs | 
| 190 | orelse | |
| 191 | is_Var (head_of lhs) | |
| 192 | orelse | |
| 16305 | 193 | (* turns t = x around, which causes a headache if x is a local variable - | 
| 194 | usually it is very useful :-( | |
| 195 | is_Free rhs andalso not(is_Free lhs) andalso not(Logic.occs(rhs,lhs)) | |
| 196 | andalso not(exists_subterm is_Var lhs) | |
| 197 | orelse | |
| 198 | *) | |
| 16842 | 199 | exists (fn t => Logic.occs (lhs, t)) (rhs :: prems) | 
| 15023 | 200 | orelse | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 201 | null prems andalso Pattern.matches (Proof_Context.theory_of ctxt) (lhs, rhs) | 
| 10413 | 202 | (*the condition "null prems" is necessary because conditional rewrites | 
| 203 | with extra variables in the conditions may terminate although | |
| 67721 | 204 | the rhs is an instance of the lhs; example: ?m < ?n \<Longrightarrow> f ?n \<equiv> f ?m *) | 
| 15023 | 205 | orelse | 
| 206 | is_Const lhs andalso not (is_Const rhs); | |
| 10413 | 207 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 208 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 209 | (* simplification procedures *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 210 | |
| 78800 | 211 | type proc = Proof.context -> cterm -> thm option; | 
| 212 | ||
| 213 | datatype simproc0 = | |
| 214 | Simproc0 of | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 215 |    {name: string,
 | 
| 61098 | 216 | lhs: term, | 
| 78800 | 217 | proc: proc Morphism.entity, | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 218 | id: stamp * thm list}; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 219 | |
| 78814 | 220 | fun eq_simproc0 (Simproc0 {id = (s1, ths1), ...}, Simproc0 {id = (s2, ths2), ...}) =
 | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 221 | s1 = s2 andalso eq_list Thm.eq_thm_prop (ths1, ths2); | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 222 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 223 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 224 | (* solvers *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 225 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 226 | datatype solver = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 227 | Solver of | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 228 |    {name: string,
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 229 | solver: Proof.context -> int -> tactic, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 230 | id: stamp}; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 231 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 232 | fun mk_solver name solver = Solver {name = name, solver = solver, id = stamp ()};
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 233 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 234 | fun solver_name (Solver {name, ...}) = name;
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 235 | fun solver ctxt (Solver {solver = tac, ...}) = tac ctxt;
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 236 | fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 237 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 238 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 239 | (* simplification sets *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 240 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 241 | (*A simpset contains data required during conversion: | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 242 | rules: discrimination net of rewrite rules; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 243 | prems: current premises; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 244 | depth: simp_depth and exceeded flag; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 245 | congs: association list of congruence rules and | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 246 | a list of `weak' congruence constants. | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 247 | A congruence is `weak' if it avoids normalization of some argument. | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 248 | procs: discrimination net of simplification procedures | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 249 | (functions that prove rewrite rules on the fly); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 250 | mk_rews: | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 251 | mk: turn simplification thms into rewrite rules; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 252 | mk_cong: prepare congruence rules; | 
| 67721 | 253 | mk_sym: turn \<equiv> around; | 
| 254 | mk_eq_True: turn P into P \<equiv> True; | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 255 | term_ord: for ordered rewriting;*) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 256 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 257 | datatype simpset = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 258 | Simpset of | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 259 |    {rules: rrule Net.net,
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 260 | prems: thm list, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 261 | depth: int * bool Unsynchronized.ref} * | 
| 71239 | 262 |    {congs: thm Congtab.table * cong_name list,
 | 
| 78800 | 263 | procs: simproc0 Net.net, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 264 | mk_rews: | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 265 |      {mk: Proof.context -> thm -> thm list,
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 266 | mk_cong: Proof.context -> thm -> thm, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 267 | mk_sym: Proof.context -> thm -> thm option, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 268 | mk_eq_True: Proof.context -> thm -> thm option, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 269 | reorient: Proof.context -> term list -> term -> term -> bool}, | 
| 70586 | 270 | term_ord: term ord, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 271 | subgoal_tac: Proof.context -> int -> tactic, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 272 | loop_tacs: (string * (Proof.context -> int -> tactic)) list, | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 273 | solvers: solver list * solver list}; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 274 | |
| 54728 | 275 | fun internal_ss (Simpset (_, ss2)) = ss2; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 276 | |
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 277 | fun make_ss1 (rules, prems, depth) = {rules = rules, prems = prems, depth = depth};
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 278 | |
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 279 | fun map_ss1 f {rules, prems, depth} = make_ss1 (f (rules, prems, depth));
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 280 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 281 | fun make_ss2 (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) = | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 282 |   {congs = congs, procs = procs, mk_rews = mk_rews, term_ord = term_ord,
 | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 283 | subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers}; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 284 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 285 | fun map_ss2 f {congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers} =
 | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 286 | make_ss2 (f (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers)); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 287 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 288 | fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 289 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 290 | fun dest_ss (Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...})) =
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 291 |  {simps = Net.entries rules
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 292 |     |> map (fn {name, thm, ...} => (name, thm)),
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 293 | procs = Net.entries procs | 
| 78814 | 294 | |> partition_eq eq_simproc0 | 
| 295 |     |> map (fn ps as Simproc0 {name, ...} :: _ => (name, map (fn Simproc0 {lhs, ...} => lhs) ps)),
 | |
| 71239 | 296 | congs = congs |> fst |> Congtab.dest, | 
| 297 | weak_congs = congs |> snd, | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 298 | loopers = map fst loop_tacs, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 299 | unsafe_solvers = map solver_name (#1 solvers), | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 300 | safe_solvers = map solver_name (#2 solvers)}; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 301 | |
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 302 | fun dest_simps (Simpset ({rules, ...}, _)) = map #thm (Net.entries rules);
 | 
| 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 303 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 304 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 305 | (* empty *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 306 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 307 | fun init_ss depth mk_rews term_ord subgoal_tac solvers = | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 308 | make_simpset ((Net.empty, [], depth), | 
| 71239 | 309 | ((Congtab.empty, []), Net.empty, mk_rews, term_ord, subgoal_tac, [], solvers)); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 310 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 311 | fun default_mk_sym _ th = SOME (th RS Drule.symmetric_thm); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 312 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 313 | val empty_ss = | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 314 | init_ss (0, Unsynchronized.ref false) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 315 |     {mk = fn _ => fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [],
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 316 | mk_cong = K I, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 317 | mk_sym = default_mk_sym, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 318 | mk_eq_True = K (K NONE), | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 319 | reorient = default_reorient} | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 320 | Term_Ord.term_ord (K (K no_tac)) ([], []); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 321 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 322 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 323 | (* merge *) (*NOTE: ignores some fields of 2nd simpset*) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 324 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 325 | fun merge_ss (ss1, ss2) = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 326 | if pointer_eq (ss1, ss2) then ss1 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 327 | else | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 328 | let | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 329 |       val Simpset ({rules = rules1, prems = prems1, depth = depth1},
 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 330 |        {congs = (congs1, weak1), procs = procs1, mk_rews, term_ord, subgoal_tac,
 | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 331 | loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1; | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 332 |       val Simpset ({rules = rules2, prems = prems2, depth = depth2},
 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 333 |        {congs = (congs2, weak2), procs = procs2, mk_rews = _, term_ord = _, subgoal_tac = _,
 | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 334 | loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 335 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 336 | val rules' = Net.merge eq_rrule (rules1, rules2); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 337 | val prems' = Thm.merge_thms (prems1, prems2); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 338 | val depth' = if #1 depth1 < #1 depth2 then depth2 else depth1; | 
| 71239 | 339 | val congs' = Congtab.merge (K true) (congs1, congs2); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 340 | val weak' = merge (op =) (weak1, weak2); | 
| 78800 | 341 | val procs' = Net.merge eq_simproc0 (procs1, procs2); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 342 | val loop_tacs' = AList.merge (op =) (K true) (loop_tacs1, loop_tacs2); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 343 | val unsafe_solvers' = merge eq_solver (unsafe_solvers1, unsafe_solvers2); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 344 | val solvers' = merge eq_solver (solvers1, solvers2); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 345 | in | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 346 | make_simpset ((rules', prems', depth'), ((congs', weak'), procs', | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 347 | mk_rews, term_ord, subgoal_tac, loop_tacs', (unsafe_solvers', solvers'))) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 348 | end; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 349 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 350 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 351 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 352 | (** context data **) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 353 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 354 | structure Simpset = Generic_Data | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 355 | ( | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 356 | type T = simpset; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 357 | val empty = empty_ss; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 358 | val merge = merge_ss; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 359 | ); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 360 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 361 | val simpset_of = Simpset.get o Context.Proof; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 362 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 363 | fun map_simpset f = Context.proof_map (Simpset.map f); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 364 | fun map_simpset1 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (map_ss1 f ss1, ss2)); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 365 | fun map_simpset2 f = map_simpset (fn Simpset (ss1, ss2) => Simpset (ss1, map_ss2 f ss2)); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 366 | |
| 71403 | 367 | fun put_simpset ss = map_simpset (K ss); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 368 | |
| 71403 | 369 | fun simpset_map ctxt f ss = ctxt |> put_simpset ss |> f |> simpset_of; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 370 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 371 | val empty_simpset = put_simpset empty_ss; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 372 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 373 | fun map_theory_simpset f thy = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 374 | let | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 375 | val ctxt' = f (Proof_Context.init_global thy); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 376 | val thy' = Proof_Context.theory_of ctxt'; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 377 | in Context.theory_map (Simpset.map (K (simpset_of ctxt'))) thy' end; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 378 | |
| 57859 
29e728588163
more careful treatment of context visibility for rule declarations (see also 39d9c7f175e0, e639d91d9073) -- avoid duplicate warnings;
 wenzelm parents: 
56438diff
changeset | 379 | fun map_ss f = Context.mapping (map_theory_simpset (f o Context_Position.not_really)) f; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 380 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 381 | val clear_simpset = | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 382 |   map_simpset (fn Simpset ({depth, ...}, {mk_rews, term_ord, subgoal_tac, solvers, ...}) =>
 | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 383 | init_ss depth mk_rews term_ord subgoal_tac solvers); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 384 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 385 | |
| 71235 | 386 | (* accessors for tactis *) | 
| 387 | ||
| 388 | fun subgoal_tac ctxt = (#subgoal_tac o internal_ss o simpset_of) ctxt ctxt; | |
| 389 | ||
| 390 | fun loop_tac ctxt = | |
| 391 | FIRST' (map (fn (_, tac) => tac ctxt) (rev ((#loop_tacs o internal_ss o simpset_of) ctxt))); | |
| 392 | ||
| 393 | val solvers = #solvers o internal_ss o simpset_of | |
| 394 | ||
| 395 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 396 | (* simp depth *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 397 | |
| 66934 | 398 | (* | 
| 399 | The simp_depth_limit is meant to abort infinite recursion of the simplifier | |
| 400 | early but should not terminate "normal" executions. | |
| 401 | As of 2017, 25 would suffice; 40 builds in a safety margin. | |
| 402 | *) | |
| 403 | ||
| 69575 | 404 | val simp_depth_limit = Config.declare_int ("simp_depth_limit", \<^here>) (K 40);
 | 
| 405 | val simp_trace_depth_limit = Config.declare_int ("simp_trace_depth_limit", \<^here>) (K 1);
 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 406 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 407 | fun inc_simp_depth ctxt = | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 408 | ctxt |> map_simpset1 (fn (rules, prems, (depth, exceeded)) => | 
| 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 409 | (rules, prems, | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 410 | (depth + 1, | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 411 | if depth = Config.get ctxt simp_trace_depth_limit | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 412 | then Unsynchronized.ref false else exceeded))); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 413 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 414 | fun simp_depth ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 415 |   let val Simpset ({depth = (depth, _), ...}, _) = simpset_of ctxt
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 416 | in depth end; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 417 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 418 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 419 | (* diagnostics *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 420 | |
| 54997 | 421 | exception SIMPLIFIER of string * thm list; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 422 | |
| 69575 | 423 | val simp_debug = Config.declare_bool ("simp_debug", \<^here>) (K false);
 | 
| 424 | val simp_trace = Config.declare_bool ("simp_trace", \<^here>) (K false);
 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 425 | |
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 426 | fun cond_warning ctxt msg = | 
| 57859 
29e728588163
more careful treatment of context visibility for rule declarations (see also 39d9c7f175e0, e639d91d9073) -- avoid duplicate warnings;
 wenzelm parents: 
56438diff
changeset | 427 | if Context_Position.is_really_visible ctxt then warning (msg ()) else (); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 428 | |
| 55031 | 429 | fun cond_tracing' ctxt flag msg = | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 430 | if Config.get ctxt flag then | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 431 | let | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 432 |       val Simpset ({depth = (depth, exceeded), ...}, _) = simpset_of ctxt;
 | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 433 | val depth_limit = Config.get ctxt simp_trace_depth_limit; | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 434 | in | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 435 | if depth > depth_limit then | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 436 | if ! exceeded then () else (tracing "simp_trace_depth_limit exceeded!"; exceeded := true) | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 437 | else (tracing (enclose "[" "]" (string_of_int depth) ^ msg ()); exceeded := false) | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 438 | end | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 439 | else (); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 440 | |
| 55031 | 441 | fun cond_tracing ctxt = cond_tracing' ctxt simp_trace; | 
| 442 | ||
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 443 | fun print_term ctxt s t = | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 444 | s ^ "\n" ^ Syntax.string_of_term ctxt t; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 445 | |
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 446 | fun print_thm ctxt s (name, th) = | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 447 | print_term ctxt (if name = "" then s else s ^ " " ^ quote name ^ ":") (Thm.full_prop_of th); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 448 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 449 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 450 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 451 | (** simpset operations **) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 452 | |
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 453 | (* prems *) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 454 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 455 | fun prems_of ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 456 |   let val Simpset ({prems, ...}, _) = simpset_of ctxt in prems end;
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 457 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 458 | fun add_prems ths = | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 459 | map_simpset1 (fn (rules, prems, depth) => (rules, ths @ prems, depth)); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 460 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 461 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 462 | (* maintain simp rules *) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 463 | |
| 68403 | 464 | fun del_rrule loud (rrule as {thm, elhs, ...}) ctxt =
 | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 465 | ctxt |> map_simpset1 (fn (rules, prems, depth) => | 
| 59582 | 466 | (Net.delete_term eq_rrule (Thm.term_of elhs, rrule) rules, prems, depth)) | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 467 | handle Net.DELETE => | 
| 68403 | 468 | (if not loud then () | 
| 469 | else cond_warning ctxt | |
| 470 |             (fn () => print_thm ctxt "Rewrite rule not in simpset:" ("", thm));
 | |
| 471 | ctxt); | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 472 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 473 | fun insert_rrule (rrule as {thm, name, ...}) ctxt =
 | 
| 55031 | 474 | (cond_tracing ctxt (fn () => print_thm ctxt "Adding rewrite rule" (name, thm)); | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 475 | ctxt |> map_simpset1 (fn (rules, prems, depth) => | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 476 | let | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 477 |       val rrule2 as {elhs, ...} = mk_rrule2 rrule;
 | 
| 61057 | 478 | val rules' = Net.insert_term eq_rrule (Thm.term_of elhs, trim_context_rrule rrule2) rules; | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 479 | in (rules', prems, depth) end) | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 480 | handle Net.INSERT => | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 481 |     (cond_warning ctxt (fn () => print_thm ctxt "Ignoring duplicate rewrite rule:" ("", thm));
 | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 482 | ctxt)); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 483 | |
| 74266 | 484 | val vars_set = Vars.build o Vars.add_vars; | 
| 74227 | 485 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 486 | local | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 487 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 488 | fun vperm (Var _, Var _) = true | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 489 | | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 490 | | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 491 | | vperm (t, u) = (t = u); | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 492 | |
| 74266 | 493 | fun var_perm (t, u) = vperm (t, u) andalso Vars.eq_set (apply2 vars_set (t, u)); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 494 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 495 | in | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 496 | |
| 10413 | 497 | fun decomp_simp thm = | 
| 15023 | 498 | let | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26424diff
changeset | 499 | val prop = Thm.prop_of thm; | 
| 15023 | 500 | val prems = Logic.strip_imp_prems prop; | 
| 501 | val concl = Drule.strip_imp_concl (Thm.cprop_of thm); | |
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 502 | val (lhs, rhs) = Thm.dest_equals concl handle TERM _ => | 
| 54997 | 503 |       raise SIMPLIFIER ("Rewrite rule not a meta-equality", [thm]);
 | 
| 20579 | 504 | val elhs = Thm.dest_arg (Thm.cprop_of (Thm.eta_conversion lhs)); | 
| 59582 | 505 | val erhs = Envir.eta_contract (Thm.term_of rhs); | 
| 15023 | 506 | val perm = | 
| 59582 | 507 | var_perm (Thm.term_of elhs, erhs) andalso | 
| 508 | not (Thm.term_of elhs aconv erhs) andalso | |
| 509 | not (is_Var (Thm.term_of elhs)); | |
| 510 | in (prems, Thm.term_of lhs, elhs, Thm.term_of rhs, perm) end; | |
| 10413 | 511 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 512 | end; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 513 | |
| 12783 | 514 | fun decomp_simp' thm = | 
| 52091 | 515 | let val (_, lhs, _, rhs, _) = decomp_simp thm in | 
| 54997 | 516 |     if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", [thm])
 | 
| 12979 
4c76bce4ce39
decomp_simp': use lhs instead of elhs (preserves more bound variable names);
 wenzelm parents: 
12783diff
changeset | 517 | else (lhs, rhs) | 
| 12783 | 518 | end; | 
| 519 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 520 | fun mk_eq_True ctxt (thm, name) = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 521 |   let val Simpset (_, {mk_rews = {mk_eq_True, ...}, ...}) = simpset_of ctxt in
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 522 | (case mk_eq_True ctxt thm of | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 523 | NONE => [] | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 524 | | SOME eq_True => | 
| 52091 | 525 | let val (_, lhs, elhs, _, _) = decomp_simp eq_True; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 526 |         in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end)
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 527 | end; | 
| 10413 | 528 | |
| 15023 | 529 | (*create the rewrite rule and possibly also the eq_True variant, | 
| 530 | in case there are extra vars on the rhs*) | |
| 52082 | 531 | fun rrule_eq_True ctxt thm name lhs elhs rhs thm2 = | 
| 15023 | 532 |   let val rrule = {thm = thm, name = name, lhs = lhs, elhs = elhs, perm = false} in
 | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 533 | if rewrite_rule_extra_vars [] lhs rhs then | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 534 | mk_eq_True ctxt (thm2, name) @ [rrule] | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 535 | else [rrule] | 
| 10413 | 536 | end; | 
| 537 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 538 | fun mk_rrule ctxt (thm, name) = | 
| 52091 | 539 | let val (prems, lhs, elhs, rhs, perm) = decomp_simp thm in | 
| 15023 | 540 |     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
 | 
| 541 | else | |
| 542 | (*weak test for loops*) | |
| 59582 | 543 | if rewrite_rule_extra_vars prems lhs rhs orelse is_Var (Thm.term_of elhs) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 544 | then mk_eq_True ctxt (thm, name) | 
| 52082 | 545 | else rrule_eq_True ctxt thm name lhs elhs rhs thm | 
| 70472 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 546 |   end |> map (fn {thm, name, lhs, elhs, perm} =>
 | 
| 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 547 |     {thm = Thm.trim_context thm, name = name, lhs = lhs,
 | 
| 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 548 | elhs = Thm.trim_context_cterm elhs, perm = perm}); | 
| 10413 | 549 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 550 | fun orient_rrule ctxt (thm, name) = | 
| 18208 | 551 | let | 
| 52091 | 552 | val (prems, lhs, elhs, rhs, perm) = decomp_simp thm; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 553 |     val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = simpset_of ctxt;
 | 
| 18208 | 554 | in | 
| 15023 | 555 |     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 556 | else if reorient ctxt prems lhs rhs then | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 557 | if reorient ctxt prems rhs lhs | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 558 | then mk_eq_True ctxt (thm, name) | 
| 15023 | 559 | else | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 560 | (case mk_sym ctxt thm of | 
| 18208 | 561 | NONE => [] | 
| 562 | | SOME thm' => | |
| 52091 | 563 | let val (_, lhs', elhs', rhs', _) = decomp_simp thm' | 
| 52082 | 564 | in rrule_eq_True ctxt thm' name lhs' elhs' rhs' thm end) | 
| 565 | else rrule_eq_True ctxt thm name lhs elhs rhs thm | |
| 10413 | 566 | end; | 
| 567 | ||
| 68046 | 568 | fun extract_rews ctxt sym thms = | 
| 569 | let | |
| 570 |     val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt;
 | |
| 571 | val mk = | |
| 572 | if sym then fn ctxt => fn th => (mk ctxt th) RL [Drule.symmetric_thm] | |
| 69137 
90fce429e1bc
Jenkins: run ocaml_setup
 Lars Hupel <lars.hupel@mytum.de> parents: 
68405diff
changeset | 573 | else mk | 
| 68046 | 574 | in maps (fn thm => map (rpair (Thm.get_name_hint thm)) (mk ctxt thm)) thms | 
| 575 | end; | |
| 10413 | 576 | |
| 54982 | 577 | fun extract_safe_rrules ctxt thm = | 
| 68046 | 578 | maps (orient_rrule ctxt) (extract_rews ctxt false [thm]); | 
| 10413 | 579 | |
| 55316 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 580 | fun mk_rrules ctxt thms = | 
| 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 581 | let | 
| 68046 | 582 | val rews = extract_rews ctxt false thms | 
| 55316 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 583 | val raw_rrules = flat (map (mk_rrule ctxt) rews) | 
| 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 584 | in map mk_rrule2 raw_rrules end | 
| 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 585 | |
| 10413 | 586 | |
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 587 | (* add/del rules explicitly *) | 
| 10413 | 588 | |
| 61090 | 589 | local | 
| 590 | ||
| 68046 | 591 | fun comb_simps ctxt comb mk_rrule sym thms = | 
| 592 | let val rews = extract_rews ctxt sym (map (Thm.transfer' ctxt) thms); | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 593 | in fold (fold comb o mk_rrule) rews ctxt end; | 
| 10413 | 594 | |
| 68405 | 595 | (* | 
| 596 | This code checks if the symetric version of a rule is already in the simpset. | |
| 597 | However, the variable names in the two versions of the rule may differ. | |
| 598 | Thus the current test modulo eq_rrule is too weak to be useful | |
| 599 | and needs to be refined. | |
| 600 | ||
| 601 | fun present ctxt rules (rrule as {thm, elhs, ...}) =
 | |
| 602 | (Net.insert_term eq_rrule (Thm.term_of elhs, trim_context_rrule rrule) rules; | |
| 603 | false) | |
| 604 | handle Net.INSERT => | |
| 605 | (cond_warning ctxt | |
| 606 |        (fn () => print_thm ctxt "Symmetric rewrite rule already in simpset:" ("", thm));
 | |
| 607 | true); | |
| 608 | ||
| 609 | fun sym_present ctxt thms = | |
| 610 | let | |
| 611 | val rews = extract_rews ctxt true (map (Thm.transfer' ctxt) thms); | |
| 612 | val rrules = map mk_rrule2 (flat(map (mk_rrule ctxt) rews)) | |
| 613 |     val Simpset({rules, ...},_) = simpset_of ctxt
 | |
| 614 | in exists (present ctxt rules) rrules end | |
| 615 | *) | |
| 61090 | 616 | in | 
| 617 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 618 | fun ctxt addsimps thms = | 
| 68046 | 619 | comb_simps ctxt insert_rrule (mk_rrule ctxt) false thms; | 
| 620 | ||
| 621 | fun addsymsimps ctxt thms = | |
| 622 | comb_simps ctxt insert_rrule (mk_rrule ctxt) true thms; | |
| 10413 | 623 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 624 | fun ctxt delsimps thms = | 
| 68403 | 625 | comb_simps ctxt (del_rrule true) (map mk_rrule2 o mk_rrule ctxt) false thms; | 
| 626 | ||
| 627 | fun delsimps_quiet ctxt thms = | |
| 628 | comb_simps ctxt (del_rrule false) (map mk_rrule2 o mk_rrule ctxt) false thms; | |
| 15023 | 629 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 630 | fun add_simp thm ctxt = ctxt addsimps [thm]; | 
| 68405 | 631 | (* | 
| 632 | with check for presence of symmetric version: | |
| 633 | if sym_present ctxt [thm] | |
| 634 |   then (cond_warning ctxt (fn () => print_thm ctxt "Ignoring rewrite rule:" ("", thm)); ctxt)
 | |
| 635 | else ctxt addsimps [thm]; | |
| 636 | *) | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 637 | fun del_simp thm ctxt = ctxt delsimps [thm]; | 
| 68403 | 638 | fun flip_simp thm ctxt = addsymsimps (delsimps_quiet ctxt [thm]) [thm]; | 
| 15023 | 639 | |
| 61090 | 640 | end; | 
| 641 | ||
| 63221 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 642 | fun init_simpset thms ctxt = ctxt | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 643 | |> Context_Position.set_visible false | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 644 | |> empty_simpset | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 645 | |> fold add_simp thms | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 646 | |> Context_Position.restore_visible ctxt; | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 647 | |
| 57859 
29e728588163
more careful treatment of context visibility for rule declarations (see also 39d9c7f175e0, e639d91d9073) -- avoid duplicate warnings;
 wenzelm parents: 
56438diff
changeset | 648 | |
| 15023 | 649 | (* congs *) | 
| 10413 | 650 | |
| 15023 | 651 | local | 
| 652 | ||
| 653 | fun is_full_cong_prems [] [] = true | |
| 654 | | is_full_cong_prems [] _ = false | |
| 655 | | is_full_cong_prems (p :: prems) varpairs = | |
| 656 | (case Logic.strip_assums_concl p of | |
| 56245 | 657 |         Const ("Pure.eq", _) $ lhs $ rhs =>
 | 
| 15023 | 658 | let val (x, xs) = strip_comb lhs and (y, ys) = strip_comb rhs in | 
| 659 | is_Var x andalso forall is_Bound xs andalso | |
| 20972 | 660 | not (has_duplicates (op =) xs) andalso xs = ys andalso | 
| 20671 | 661 | member (op =) varpairs (x, y) andalso | 
| 19303 | 662 | is_full_cong_prems prems (remove (op =) (x, y) varpairs) | 
| 15023 | 663 | end | 
| 664 | | _ => false); | |
| 665 | ||
| 666 | fun is_full_cong thm = | |
| 10413 | 667 | let | 
| 43597 | 668 | val prems = Thm.prems_of thm and concl = Thm.concl_of thm; | 
| 15023 | 669 | val (lhs, rhs) = Logic.dest_equals concl; | 
| 670 | val (f, xs) = strip_comb lhs and (g, ys) = strip_comb rhs; | |
| 10413 | 671 | in | 
| 20972 | 672 | f = g andalso not (has_duplicates (op =) (xs @ ys)) andalso length xs = length ys andalso | 
| 15023 | 673 | is_full_cong_prems prems (xs ~~ ys) | 
| 10413 | 674 | end; | 
| 675 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 676 | fun mk_cong ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 677 |   let val Simpset (_, {mk_rews = {mk_cong = f, ...}, ...}) = simpset_of ctxt
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 678 | in f ctxt end; | 
| 45620 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45405diff
changeset | 679 | |
| 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45405diff
changeset | 680 | in | 
| 
f2a587696afb
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45405diff
changeset | 681 | |
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 682 | fun add_eqcong thm ctxt = ctxt |> map_simpset2 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 683 | (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 15023 | 684 | let | 
| 45621 | 685 | val (lhs, _) = Logic.dest_equals (Thm.concl_of thm) | 
| 54997 | 686 |         handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", [thm]);
 | 
| 18929 | 687 | (*val lhs = Envir.eta_contract lhs;*) | 
| 45621 | 688 | val a = the (cong_name (head_of lhs)) handle Option.Option => | 
| 54997 | 689 |         raise SIMPLIFIER ("Congruence must start with a constant or free variable", [thm]);
 | 
| 22221 
8a8aa6114a89
changed cong alist - now using AList operations instead of overwrite_warn
 haftmann parents: 
22008diff
changeset | 690 | val (xs, weak) = congs; | 
| 71239 | 691 | val xs' = Congtab.update (a, Thm.trim_context thm) xs; | 
| 22221 
8a8aa6114a89
changed cong alist - now using AList operations instead of overwrite_warn
 haftmann parents: 
22008diff
changeset | 692 | val weak' = if is_full_cong thm then weak else a :: weak; | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 693 | in ((xs', weak'), procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) end); | 
| 10413 | 694 | |
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 695 | fun del_eqcong thm ctxt = ctxt |> map_simpset2 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 696 | (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 15023 | 697 | let | 
| 45621 | 698 | val (lhs, _) = Logic.dest_equals (Thm.concl_of thm) | 
| 54997 | 699 |         handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", [thm]);
 | 
| 18929 | 700 | (*val lhs = Envir.eta_contract lhs;*) | 
| 20057 | 701 | val a = the (cong_name (head_of lhs)) handle Option.Option => | 
| 54997 | 702 |         raise SIMPLIFIER ("Congruence must start with a constant", [thm]);
 | 
| 22221 
8a8aa6114a89
changed cong alist - now using AList operations instead of overwrite_warn
 haftmann parents: 
22008diff
changeset | 703 | val (xs, _) = congs; | 
| 71239 | 704 | val xs' = Congtab.delete_safe a xs; | 
| 705 | val weak' = Congtab.fold (fn (a, th) => if is_full_cong th then I else insert (op =) a) xs' []; | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 706 | in ((xs', weak'), procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) end); | 
| 10413 | 707 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 708 | fun add_cong thm ctxt = add_eqcong (mk_cong ctxt thm) ctxt; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 709 | fun del_cong thm ctxt = del_eqcong (mk_cong ctxt thm) ctxt; | 
| 15023 | 710 | |
| 711 | end; | |
| 10413 | 712 | |
| 713 | ||
| 15023 | 714 | (* simprocs *) | 
| 715 | ||
| 22234 | 716 | datatype simproc = | 
| 717 | Simproc of | |
| 718 |     {name: string,
 | |
| 61098 | 719 | lhss: term list, | 
| 78800 | 720 | proc: proc Morphism.entity, | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 721 | id: stamp * thm list}; | 
| 22008 | 722 | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 723 | fun cert_simproc thy {name, lhss, proc, identifier} =
 | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 724 | Simproc | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 725 |    {name = name,
 | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 726 | lhss = map (Sign.cert_term thy) lhss, | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 727 | proc = proc, | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 728 | id = (stamp (), map (Thm.transfer thy) identifier)}; | 
| 61144 | 729 | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 730 | fun transform_simproc phi (Simproc {name, lhss, proc, id = (stamp, identifier)}) =
 | 
| 22234 | 731 | Simproc | 
| 732 |    {name = name,
 | |
| 61098 | 733 | lhss = map (Morphism.term phi) lhss, | 
| 78114 | 734 | proc = Morphism.transform phi proc, | 
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 735 | id = (stamp, Morphism.fact phi identifier)}; | 
| 78114 | 736 | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 737 | fun trim_context_simproc (Simproc {name, lhss, proc, id = (stamp, identifier)}) =
 | 
| 78114 | 738 | Simproc | 
| 739 |    {name = name,
 | |
| 740 | lhss = lhss, | |
| 741 | proc = Morphism.entity_reset_context proc, | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 742 | id = (stamp, map Thm.trim_context identifier)}; | 
| 22234 | 743 | |
| 15023 | 744 | local | 
| 10413 | 745 | |
| 78800 | 746 | fun add_proc (proc as Simproc0 {name, lhs, ...}) ctxt =
 | 
| 55031 | 747 | (cond_tracing ctxt (fn () => | 
| 61098 | 748 |     print_term ctxt ("Adding simplification procedure " ^ quote name ^ " for") lhs);
 | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 749 | ctxt |> map_simpset2 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 750 | (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 78800 | 751 | (congs, Net.insert_term eq_simproc0 (lhs, proc) procs, | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 752 | mk_rews, term_ord, subgoal_tac, loop_tacs, solvers)) | 
| 15023 | 753 | handle Net.INSERT => | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 754 | (cond_warning ctxt (fn () => "Ignoring duplicate simplification procedure " ^ quote name); | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 755 | ctxt)); | 
| 10413 | 756 | |
| 78800 | 757 | fun del_proc (proc as Simproc0 {name, lhs, ...}) ctxt =
 | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 758 | ctxt |> map_simpset2 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 759 | (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 78800 | 760 | (congs, Net.delete_term eq_simproc0 (lhs, proc) procs, | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 761 | mk_rews, term_ord, subgoal_tac, loop_tacs, solvers)) | 
| 15023 | 762 | handle Net.DELETE => | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 763 | (cond_warning ctxt (fn () => "Simplification procedure " ^ quote name ^ " not in simpset"); | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 764 | ctxt); | 
| 10413 | 765 | |
| 78812 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 766 | fun prep_procs (Simproc {name, lhss, proc, id}) =
 | 
| 
d769a183d51d
simprocs may be distinguished via 'identifier': only works for ML antiquotation (see also 13252110a6fe);
 wenzelm parents: 
78800diff
changeset | 767 |   lhss |> map (fn lhs => Simproc0 {name = name, lhs = lhs, proc = proc, id = id});
 | 
| 22234 | 768 | |
| 15023 | 769 | in | 
| 10413 | 770 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 771 | fun ctxt addsimprocs ps = fold (fold add_proc o prep_procs) ps ctxt; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 772 | fun ctxt delsimprocs ps = fold (fold del_proc o prep_procs) ps ctxt; | 
| 10413 | 773 | |
| 15023 | 774 | end; | 
| 10413 | 775 | |
| 776 | ||
| 777 | (* mk_rews *) | |
| 778 | ||
| 15023 | 779 | local | 
| 780 | ||
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 781 | fun map_mk_rews f = | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 782 | map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 783 | let | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 784 |       val {mk, mk_cong, mk_sym, mk_eq_True, reorient} = mk_rews;
 | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 785 | val (mk', mk_cong', mk_sym', mk_eq_True', reorient') = | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 786 | f (mk, mk_cong, mk_sym, mk_eq_True, reorient); | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 787 |       val mk_rews' = {mk = mk', mk_cong = mk_cong', mk_sym = mk_sym', mk_eq_True = mk_eq_True',
 | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 788 | reorient = reorient'}; | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 789 | in (congs, procs, mk_rews', term_ord, subgoal_tac, loop_tacs, solvers) end); | 
| 15023 | 790 | |
| 791 | in | |
| 10413 | 792 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 793 | fun mksimps ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 794 |   let val Simpset (_, {mk_rews = {mk, ...}, ...}) = simpset_of ctxt
 | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 795 | in mk ctxt end; | 
| 30318 
3d03190d2864
replaced archaic use of rep_ss by Simplifier.mksimps;
 wenzelm parents: 
29269diff
changeset | 796 | |
| 45625 
750c5a47400b
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45621diff
changeset | 797 | fun set_mksimps mk = map_mk_rews (fn (_, mk_cong, mk_sym, mk_eq_True, reorient) => | 
| 18208 | 798 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | 
| 15023 | 799 | |
| 45625 
750c5a47400b
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45621diff
changeset | 800 | fun set_mkcong mk_cong = map_mk_rews (fn (mk, _, mk_sym, mk_eq_True, reorient) => | 
| 18208 | 801 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | 
| 10413 | 802 | |
| 45625 
750c5a47400b
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45621diff
changeset | 803 | fun set_mksym mk_sym = map_mk_rews (fn (mk, mk_cong, _, mk_eq_True, reorient) => | 
| 18208 | 804 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | 
| 10413 | 805 | |
| 45625 
750c5a47400b
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45621diff
changeset | 806 | fun set_mkeqTrue mk_eq_True = map_mk_rews (fn (mk, mk_cong, mk_sym, _, reorient) => | 
| 18208 | 807 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | 
| 808 | ||
| 809 | fun set_reorient reorient = map_mk_rews (fn (mk, mk_cong, mk_sym, mk_eq_True, _) => | |
| 810 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | |
| 15023 | 811 | |
| 812 | end; | |
| 813 | ||
| 14242 
ec70653a02bf
Added access to the mk_rews field (and friends).
 skalberg parents: 
14040diff
changeset | 814 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 815 | (* term_ord *) | 
| 10413 | 816 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 817 | fun set_term_ord term_ord = | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 818 | map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers) => | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 819 | (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers)); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 820 | |
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 821 | |
| 15023 | 822 | (* tactics *) | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 823 | |
| 45625 
750c5a47400b
modernized some old-style infix operations, which were left over from the time of ML proof scripts;
 wenzelm parents: 
45621diff
changeset | 824 | fun set_subgoaler subgoal_tac = | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 825 | map_simpset2 (fn (congs, procs, mk_rews, term_ord, _, loop_tacs, solvers) => | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 826 | (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers)); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 827 | |
| 52037 | 828 | fun ctxt setloop tac = ctxt |> | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 829 | map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, _, solvers) => | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 830 |    (congs, procs, mk_rews, term_ord, subgoal_tac, [("", tac)], solvers));
 | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 831 | |
| 52037 | 832 | fun ctxt addloop (name, tac) = ctxt |> | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 833 | map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 834 | (congs, procs, mk_rews, term_ord, subgoal_tac, | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 835 | AList.update (op =) (name, tac) loop_tacs, solvers)); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 836 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 837 | fun ctxt delloop name = ctxt |> | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 838 | map_simpset2 (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, solvers) => | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 839 | (congs, procs, mk_rews, term_ord, subgoal_tac, | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 840 | (if AList.defined (op =) loop_tacs name then () | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 841 | else cond_warning ctxt (fn () => "No such looper in simpset: " ^ quote name); | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 842 | AList.delete (op =) name loop_tacs), solvers)); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 843 | |
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 844 | fun ctxt setSSolver solver = ctxt |> map_simpset2 | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 845 | (fn (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, (unsafe_solvers, _)) => | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 846 | (congs, procs, mk_rews, term_ord, subgoal_tac, loop_tacs, (unsafe_solvers, [solver]))); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 847 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 848 | fun ctxt addSSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, term_ord, | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 849 | subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, term_ord, | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 850 | subgoal_tac, loop_tacs, (unsafe_solvers, insert eq_solver solver solvers))); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 851 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 852 | fun ctxt setSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, term_ord, | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 853 | subgoal_tac, loop_tacs, (_, solvers)) => (congs, procs, mk_rews, term_ord, | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 854 | subgoal_tac, loop_tacs, ([solver], solvers))); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 855 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 856 | fun ctxt addSolver solver = ctxt |> map_simpset2 (fn (congs, procs, mk_rews, term_ord, | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 857 | subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, term_ord, | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 858 | subgoal_tac, loop_tacs, (insert eq_solver solver unsafe_solvers, solvers))); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 859 | |
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 860 | fun set_solvers solvers = map_simpset2 (fn (congs, procs, mk_rews, term_ord, | 
| 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 861 | subgoal_tac, loop_tacs, _) => (congs, procs, mk_rews, term_ord, | 
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 862 | subgoal_tac, loop_tacs, (solvers, solvers))); | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 863 | |
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 864 | |
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 865 | (* trace operations *) | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 866 | |
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 867 | type trace_ops = | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 868 |  {trace_invoke: {depth: int, term: term} -> Proof.context -> Proof.context,
 | 
| 55316 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 869 |   trace_apply: {unconditional: bool, term: term, thm: thm, rrule: rrule} ->
 | 
| 79737 | 870 | Proof.context -> (Proof.context -> (thm * term) option) -> (thm * term) option, | 
| 79738 | 871 |   trace_simproc: {name: string, cterm: cterm} ->
 | 
| 79737 | 872 | Proof.context -> (Proof.context -> thm option) -> thm option}; | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 873 | |
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 874 | structure Trace_Ops = Theory_Data | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 875 | ( | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 876 | type T = trace_ops; | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 877 | val empty: T = | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 878 |    {trace_invoke = fn _ => fn ctxt => ctxt,
 | 
| 79737 | 879 | trace_apply = fn _ => fn ctxt => fn cont => cont ctxt, | 
| 880 | trace_simproc = fn _ => fn ctxt => fn cont => cont ctxt}; | |
| 54731 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 881 | fun merge (trace_ops, _) = trace_ops; | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 882 | ); | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 883 | |
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 884 | val set_trace_ops = Trace_Ops.put; | 
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 885 | |
| 
384ac33802b0
clarified Trace_Ops: global theory data avoids init of simpset in Pure.thy, which is important to act as neutral element in merge;
 wenzelm parents: 
54729diff
changeset | 886 | val trace_ops = Trace_Ops.get o Proof_Context.theory_of; | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 887 | fun trace_invoke args ctxt = #trace_invoke (trace_ops ctxt) args ctxt; | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 888 | fun trace_apply args ctxt = #trace_apply (trace_ops ctxt) args ctxt; | 
| 79737 | 889 | fun trace_simproc args ctxt = #trace_simproc (trace_ops ctxt) args ctxt; | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 890 | |
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 891 | |
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 892 | |
| 10413 | 893 | (** rewriting **) | 
| 894 | ||
| 895 | (* | |
| 896 | Uses conversions, see: | |
| 897 | L C Paulson, A higher-order implementation of rewriting, | |
| 898 | Science of Computer Programming 3 (1983), pages 119-149. | |
| 899 | *) | |
| 900 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 901 | fun check_conv ctxt msg thm thm' = | 
| 10413 | 902 | let | 
| 36944 | 903 | val thm'' = Thm.transitive thm thm' handle THM _ => | 
| 59690 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 904 | let | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 905 | val nthm' = | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 906 | Thm.transitive (Thm.symmetric (Drule.beta_eta_conversion (Thm.lhs_of thm'))) thm' | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 907 | in Thm.transitive thm nthm' handle THM _ => | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 908 | let | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 909 | val nthm = | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 910 | Thm.transitive thm (Drule.beta_eta_conversion (Thm.rhs_of thm)) | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 911 | in Thm.transitive nthm nthm' end | 
| 
46b635624feb
rhs of eqn is only eta- but not beta-eta-contracted; hence the latter is performed explicitly if needed
 nipkow parents: 
59647diff
changeset | 912 | end | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 913 | val _ = | 
| 55031 | 914 |       if msg then cond_tracing ctxt (fn () => print_thm ctxt "SUCCEEDED" ("", thm'))
 | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 915 | else (); | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 916 | in SOME thm'' end | 
| 10413 | 917 | handle THM _ => | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26424diff
changeset | 918 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26424diff
changeset | 919 | val _ $ _ $ prop0 = Thm.prop_of thm; | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 920 | val _ = | 
| 55032 
b49366215417
back to conditional tracing instead of noisy warning (see also 00e849f5b397): these incidents happen occasionally;
 wenzelm parents: 
55031diff
changeset | 921 | cond_tracing ctxt (fn () => | 
| 
b49366215417
back to conditional tracing instead of noisy warning (see also 00e849f5b397): these incidents happen occasionally;
 wenzelm parents: 
55031diff
changeset | 922 |           print_thm ctxt "Proved wrong theorem (bad subgoaler?)" ("", thm') ^ "\n" ^
 | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 923 | print_term ctxt "Should have proved:" prop0); | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 924 | in NONE end; | 
| 10413 | 925 | |
| 926 | ||
| 927 | (* mk_procrule *) | |
| 928 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 929 | fun mk_procrule ctxt thm = | 
| 70528 
9b3610fe74d6
treat simproc results as atomic -- more compact proof terms;
 wenzelm parents: 
70472diff
changeset | 930 | let | 
| 
9b3610fe74d6
treat simproc results as atomic -- more compact proof terms;
 wenzelm parents: 
70472diff
changeset | 931 | val (prems, lhs, elhs, rhs, _) = decomp_simp thm | 
| 
9b3610fe74d6
treat simproc results as atomic -- more compact proof terms;
 wenzelm parents: 
70472diff
changeset | 932 | val thm' = Thm.close_derivation \<^here> thm; | 
| 
9b3610fe74d6
treat simproc results as atomic -- more compact proof terms;
 wenzelm parents: 
70472diff
changeset | 933 | in | 
| 15023 | 934 | if rewrite_rule_extra_vars prems lhs rhs | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 935 |     then (cond_warning ctxt (fn () => print_thm ctxt "Extra vars on rhs:" ("", thm)); [])
 | 
| 70528 
9b3610fe74d6
treat simproc results as atomic -- more compact proof terms;
 wenzelm parents: 
70472diff
changeset | 936 |     else [mk_rrule2 {thm = thm', name = "", lhs = lhs, elhs = elhs, perm = false}]
 | 
| 10413 | 937 | end; | 
| 938 | ||
| 939 | ||
| 15023 | 940 | (* rewritec: conversion to apply the meta simpset to a term *) | 
| 10413 | 941 | |
| 15023 | 942 | (*Since the rewriting strategy is bottom-up, we avoid re-normalizing already | 
| 943 | normalized terms by carrying around the rhs of the rewrite rule just | |
| 944 | applied. This is called the `skeleton'. It is decomposed in parallel | |
| 945 | with the term. Once a Var is encountered, the corresponding term is | |
| 946 | already in normal form. | |
| 947 | skel0 is a dummy skeleton that is to enforce complete normalization.*) | |
| 948 | ||
| 10413 | 949 | val skel0 = Bound 0; | 
| 950 | ||
| 15023 | 951 | (*Use rhs as skeleton only if the lhs does not contain unnormalized bits. | 
| 952 | The latter may happen iff there are weak congruence rules for constants | |
| 953 | in the lhs.*) | |
| 10413 | 954 | |
| 15023 | 955 | fun uncond_skel ((_, weak), (lhs, rhs)) = | 
| 956 | if null weak then rhs (*optimization*) | |
| 51591 
e4aeb102ad70
amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
 wenzelm parents: 
51590diff
changeset | 957 | else if exists_subterm | 
| 
e4aeb102ad70
amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
 wenzelm parents: 
51590diff
changeset | 958 | (fn Const (a, _) => member (op =) weak (true, a) | 
| 
e4aeb102ad70
amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
 wenzelm parents: 
51590diff
changeset | 959 | | Free (a, _) => member (op =) weak (false, a) | 
| 
e4aeb102ad70
amended uncond_skel to observe notion of cong_name properly -- may affect simplification with Free congs;
 wenzelm parents: 
51590diff
changeset | 960 | | _ => false) lhs then skel0 | 
| 15023 | 961 | else rhs; | 
| 962 | ||
| 963 | (*Behaves like unconditional rule if rhs does not contain vars not in the lhs. | |
| 964 | Otherwise those vars may become instantiated with unnormalized terms | |
| 965 | while the premises are solved.*) | |
| 966 | ||
| 32797 | 967 | fun cond_skel (args as (_, (lhs, rhs))) = | 
| 74266 | 968 | if Vars.subset (vars_set rhs, vars_set lhs) then uncond_skel args | 
| 10413 | 969 | else skel0; | 
| 970 | ||
| 971 | (* | |
| 15023 | 972 | Rewriting -- we try in order: | 
| 10413 | 973 | (1) beta reduction | 
| 974 | (2) unconditional rewrite rules | |
| 975 | (3) conditional rewrite rules | |
| 976 | (4) simplification procedures | |
| 977 | ||
| 978 | IMPORTANT: rewrite rules must not introduce new Vars or TVars! | |
| 979 | *) | |
| 980 | ||
| 52091 | 981 | fun rewritec (prover, maxt) ctxt t = | 
| 10413 | 982 | let | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 983 |     val Simpset ({rules, ...}, {congs, procs, term_ord, ...}) = simpset_of ctxt;
 | 
| 10413 | 984 | val eta_thm = Thm.eta_conversion t; | 
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 985 | val eta_t' = Thm.rhs_of eta_thm; | 
| 59582 | 986 | val eta_t = Thm.term_of eta_t'; | 
| 55316 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 987 | fun rew rrule = | 
| 10413 | 988 | let | 
| 61057 | 989 |         val {thm = thm0, name, lhs, elhs = elhs0, extra, fo, perm} = rrule;
 | 
| 78136 | 990 | val thm = Thm.transfer' ctxt thm0; | 
| 991 | val elhs = Thm.transfer_cterm' ctxt elhs0; | |
| 32797 | 992 | val prop = Thm.prop_of thm; | 
| 20546 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 993 | val (rthm, elhs') = | 
| 
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
 wenzelm parents: 
20330diff
changeset | 994 | if maxt = ~1 orelse not extra then (thm, elhs) | 
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 995 | else (Thm.incr_indexes (maxt + 1) thm, Thm.incr_indexes_cterm (maxt + 1) elhs); | 
| 61057 | 996 | |
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 997 | val insts = | 
| 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 998 | if fo then Thm.first_order_match (elhs', eta_t') | 
| 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 999 | else Thm.match (elhs', eta_t'); | 
| 10413 | 1000 | val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm); | 
| 14643 | 1001 | val prop' = Thm.prop_of thm'; | 
| 74509 | 1002 | val unconditional = Logic.no_prems prop'; | 
| 54725 | 1003 | val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop'); | 
| 55316 
885500f4aa6a
interactive simplifier trace: new panel in Isabelle/jEdit to inspect and modify simplification state
 Lars Hupel <lars.hupel@mytum.de> parents: 
55032diff
changeset | 1004 |         val trace_args = {unconditional = unconditional, term = eta_t, thm = thm', rrule = rrule};
 | 
| 10413 | 1005 | in | 
| 67561 
f0b11413f1c9
clarified signature: prefer proper order operation;
 wenzelm parents: 
66934diff
changeset | 1006 | if perm andalso is_greater_equal (term_ord (rhs', lhs')) | 
| 54725 | 1007 | then | 
| 55031 | 1008 | (cond_tracing ctxt (fn () => | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1009 | print_thm ctxt "Cannot apply permutative rewrite rule" (name, thm) ^ "\n" ^ | 
| 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1010 |             print_thm ctxt "Term does not become smaller:" ("", thm'));
 | 
| 54725 | 1011 | NONE) | 
| 1012 | else | |
| 55031 | 1013 | (cond_tracing ctxt (fn () => | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1014 | print_thm ctxt "Applying instance of rewrite rule" (name, thm)); | 
| 54725 | 1015 | if unconditional | 
| 1016 | then | |
| 55031 | 1017 |            (cond_tracing ctxt (fn () => print_thm ctxt "Rewriting:" ("", thm'));
 | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1018 | trace_apply trace_args ctxt (fn ctxt' => | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1019 | let | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1020 | val lr = Logic.dest_equals prop; | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1021 | val SOME thm'' = check_conv ctxt' false eta_thm thm'; | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1022 | in SOME (thm'', uncond_skel (congs, lr)) end)) | 
| 54725 | 1023 | else | 
| 55031 | 1024 |            (cond_tracing ctxt (fn () => print_thm ctxt "Trying to rewrite:" ("", thm'));
 | 
| 54725 | 1025 | if simp_depth ctxt > Config.get ctxt simp_depth_limit | 
| 55031 | 1026 | then (cond_tracing ctxt (fn () => "simp_depth_limit exceeded - giving up"); NONE) | 
| 54725 | 1027 | else | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1028 | trace_apply trace_args ctxt (fn ctxt' => | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1029 | (case prover ctxt' thm' of | 
| 55031 | 1030 |                   NONE => (cond_tracing ctxt' (fn () => print_thm ctxt' "FAILED" ("", thm')); NONE)
 | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1031 | | SOME thm2 => | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1032 | (case check_conv ctxt' true eta_thm thm2 of | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1033 | NONE => NONE | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1034 | | SOME thm2' => | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1035 | let | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1036 | val concl = Logic.strip_imp_concl prop; | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1037 | val lr = Logic.dest_equals concl; | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1038 | in SOME (thm2', cond_skel (congs, lr)) end))))) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1039 | end; | 
| 10413 | 1040 | |
| 15531 | 1041 | fun rews [] = NONE | 
| 10413 | 1042 | | rews (rrule :: rrules) = | 
| 15531 | 1043 | let val opt = rew rrule handle Pattern.MATCH => NONE | 
| 54725 | 1044 | in (case opt of NONE => rews rrules | some => some) end; | 
| 10413 | 1045 | |
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1046 | fun sort_rrules rrs = | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1047 | let | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1048 |         fun is_simple ({thm, ...}: rrule) =
 | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1049 | (case Thm.prop_of thm of | 
| 56245 | 1050 |             Const ("Pure.eq", _) $ _ $ _ => true
 | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1051 | | _ => false); | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1052 | fun sort [] (re1, re2) = re1 @ re2 | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1053 | | sort (rr :: rrs) (re1, re2) = | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1054 | if is_simple rr | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1055 | then sort rrs (rr :: re1, re2) | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1056 | else sort rrs (re1, rr :: re2); | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1057 | in sort rrs ([], []) end; | 
| 10413 | 1058 | |
| 15531 | 1059 | fun proc_rews [] = NONE | 
| 78800 | 1060 |       | proc_rews (Simproc0 {name, proc, lhs, ...} :: ps) =
 | 
| 61098 | 1061 | if Pattern.matches (Proof_Context.theory_of ctxt) (lhs, Thm.term_of t) then | 
| 55031 | 1062 | (cond_tracing' ctxt simp_debug (fn () => | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1063 |               print_term ctxt ("Trying procedure " ^ quote name ^ " on:") eta_t);
 | 
| 79737 | 1064 | (let | 
| 1065 | val ctxt' = Config.put simp_trace (Config.get ctxt simp_debug) ctxt | |
| 79738 | 1066 |                 val res = trace_simproc {name = name, cterm = eta_t'} ctxt'
 | 
| 79737 | 1067 | (fn ctxt'' => Morphism.form_context' ctxt'' proc eta_t') | 
| 1068 | in case res of | |
| 79731 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1069 | NONE => (cond_tracing' ctxt simp_debug (fn () => "FAILED"); proc_rews ps) | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1070 | | SOME raw_thm => | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1071 | (cond_tracing ctxt (fn () => | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1072 |                      print_thm ctxt ("Procedure " ^ quote name ^ " produced rewrite rule:")
 | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1073 |                        ("", raw_thm));
 | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1074 | (case rews (mk_procrule ctxt raw_thm) of | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1075 | NONE => | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1076 | (cond_tracing ctxt (fn () => | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1077 |                          print_term ctxt ("IGNORED result of simproc " ^ quote name ^
 | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1078 | " -- does not match") (Thm.term_of t)); | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1079 | proc_rews ps) | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1080 | | some => some)) | 
| 
6dbe7910dcfc
simplifier: no trace info from simprocs unless simp_debug = true.
 nipkow parents: 
78814diff
changeset | 1081 | end)) | 
| 10413 | 1082 | else proc_rews ps; | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1083 | in | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1084 | (case eta_t of | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1085 | Abs _ $ _ => SOME (Thm.transitive eta_thm (Thm.beta_conversion false eta_t'), skel0) | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1086 | | _ => | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1087 | (case rews (sort_rrules (Net.match_term rules eta_t)) of | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1088 | NONE => proc_rews (Net.match_term procs eta_t) | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1089 | | some => some)) | 
| 10413 | 1090 | end; | 
| 1091 | ||
| 1092 | ||
| 1093 | (* conversion to apply a congruence rule to a term *) | |
| 1094 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1095 | fun congc prover ctxt maxt cong t = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1096 | let | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1097 | val rthm = Thm.incr_indexes (maxt + 1) cong; | 
| 59582 | 1098 | val rlhs = fst (Thm.dest_equals (Drule.strip_imp_concl (Thm.cprop_of rthm))); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1099 | val insts = Thm.match (rlhs, t) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1100 | (* Thm.match can raise Pattern.MATCH; | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1101 | is handled when congc is called *) | 
| 59582 | 1102 | val thm' = | 
| 1103 | Thm.instantiate insts (Thm.rename_boundvars (Thm.term_of rlhs) (Thm.term_of t) rthm); | |
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1104 | val _ = | 
| 55031 | 1105 |       cond_tracing ctxt (fn () => print_thm ctxt "Applying congruence rule:" ("", thm'));
 | 
| 1106 |     fun err (msg, thm) = (cond_tracing ctxt (fn () => print_thm ctxt msg ("", thm)); NONE);
 | |
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1107 | in | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1108 | (case prover thm' of | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1109 |       NONE => err ("Congruence proof failed.  Could not prove", thm')
 | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1110 | | SOME thm2 => | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1111 | (case check_conv ctxt true (Drule.beta_eta_conversion t) thm2 of | 
| 15531 | 1112 |           NONE => err ("Congruence proof failed.  Should not have proved", thm2)
 | 
| 1113 | | SOME thm2' => | |
| 59582 | 1114 | if op aconv (apply2 Thm.term_of (Thm.dest_equals (Thm.cprop_of thm2'))) | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1115 | then NONE else SOME thm2')) | 
| 10413 | 1116 | end; | 
| 1117 | ||
| 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: 
60324diff
changeset | 1118 | val vA = (("A", 0), propT);
 | 
| 
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: 
60324diff
changeset | 1119 | val vB = (("B", 0), propT);
 | 
| 
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: 
60324diff
changeset | 1120 | val vC = (("C", 0), propT);
 | 
| 10413 | 1121 | |
| 15531 | 1122 | fun transitive1 NONE NONE = NONE | 
| 1123 | | transitive1 (SOME thm1) NONE = SOME thm1 | |
| 1124 | | transitive1 NONE (SOME thm2) = SOME thm2 | |
| 54725 | 1125 | | transitive1 (SOME thm1) (SOME thm2) = SOME (Thm.transitive thm1 thm2); | 
| 10413 | 1126 | |
| 15531 | 1127 | fun transitive2 thm = transitive1 (SOME thm); | 
| 1128 | fun transitive3 thm = transitive1 thm o SOME; | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1129 | |
| 52091 | 1130 | fun bottomc ((simprem, useprem, mutsimp), prover, maxidx) = | 
| 10413 | 1131 | let | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1132 | fun botc skel ctxt t = | 
| 54725 | 1133 | if is_Var skel then NONE | 
| 1134 | else | |
| 1135 | (case subc skel ctxt t of | |
| 1136 | some as SOME thm1 => | |
| 1137 | (case rewritec (prover, maxidx) ctxt (Thm.rhs_of thm1) of | |
| 1138 | SOME (thm2, skel2) => | |
| 1139 | transitive2 (Thm.transitive thm1 thm2) | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1140 | (botc skel2 ctxt (Thm.rhs_of thm2)) | 
| 54725 | 1141 | | NONE => some) | 
| 1142 | | NONE => | |
| 1143 | (case rewritec (prover, maxidx) ctxt t of | |
| 1144 | SOME (thm2, skel2) => transitive2 thm2 | |
| 1145 | (botc skel2 ctxt (Thm.rhs_of thm2)) | |
| 1146 | | NONE => NONE)) | |
| 10413 | 1147 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1148 | and try_botc ctxt t = | 
| 54725 | 1149 | (case botc skel0 ctxt t of | 
| 1150 | SOME trec1 => trec1 | |
| 1151 | | NONE => Thm.reflexive t) | |
| 10413 | 1152 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1153 | and subc skel ctxt t0 = | 
| 55014 
a93f496f6c30
general notion of auxiliary bounds within context;
 wenzelm parents: 
55000diff
changeset | 1154 |         let val Simpset (_, {congs, ...}) = simpset_of ctxt in
 | 
| 59582 | 1155 | (case Thm.term_of t0 of | 
| 74525 
c960bfcb91db
discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
 wenzelm parents: 
74518diff
changeset | 1156 | Abs (a, _, _) => | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1157 | let | 
| 74525 
c960bfcb91db
discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
 wenzelm parents: 
74518diff
changeset | 1158 | val ((v, t'), ctxt') = Variable.dest_abs_cterm t0 ctxt; | 
| 
c960bfcb91db
discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
 wenzelm parents: 
74518diff
changeset | 1159 | val skel' = (case skel of Abs (_, _, sk) => sk | _ => skel0); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1160 | in | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1161 | (case botc skel' ctxt' t' of | 
| 74525 
c960bfcb91db
discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
 wenzelm parents: 
74518diff
changeset | 1162 | SOME thm => SOME (Thm.abstract_rule a v thm) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1163 | | NONE => NONE) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1164 | end | 
| 54725 | 1165 | | t $ _ => | 
| 1166 | (case t of | |
| 56245 | 1167 |                 Const ("Pure.imp", _) $ _  => impc t0 ctxt
 | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1168 | | Abs _ => | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1169 | let val thm = Thm.beta_conversion false t0 | 
| 54725 | 1170 | in | 
| 1171 | (case subc skel0 ctxt (Thm.rhs_of thm) of | |
| 1172 | NONE => SOME thm | |
| 1173 | | SOME thm' => SOME (Thm.transitive thm thm')) | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1174 | end | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1175 | | _ => | 
| 54727 | 1176 | let | 
| 1177 | fun appc () = | |
| 1178 | let | |
| 1179 | val (tskel, uskel) = | |
| 1180 | (case skel of | |
| 1181 | tskel $ uskel => (tskel, uskel) | |
| 1182 | | _ => (skel0, skel0)); | |
| 1183 | val (ct, cu) = Thm.dest_comb t0; | |
| 1184 | in | |
| 1185 | (case botc tskel ctxt ct of | |
| 1186 | SOME thm1 => | |
| 1187 | (case botc uskel ctxt cu of | |
| 1188 | SOME thm2 => SOME (Thm.combination thm1 thm2) | |
| 1189 | | NONE => SOME (Thm.combination thm1 (Thm.reflexive cu))) | |
| 1190 | | NONE => | |
| 1191 | (case botc uskel ctxt cu of | |
| 1192 | SOME thm1 => SOME (Thm.combination (Thm.reflexive ct) thm1) | |
| 1193 | | NONE => NONE)) | |
| 1194 | end; | |
| 1195 | val (h, ts) = strip_comb t; | |
| 54725 | 1196 | in | 
| 1197 | (case cong_name h of | |
| 1198 | SOME a => | |
| 71239 | 1199 | (case Congtab.lookup (fst congs) a of | 
| 61095 | 1200 | NONE => appc () | 
| 54725 | 1201 | | SOME cong => | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1202 | (*post processing: some partial applications h t1 ... tj, j <= length ts, | 
| 67721 | 1203 | may be a redex. Example: map (\<lambda>x. x) = (\<lambda>xs. xs) wrt map_cong*) | 
| 54725 | 1204 | (let | 
| 1205 | val thm = congc (prover ctxt) ctxt maxidx cong t0; | |
| 1206 | val t = the_default t0 (Option.map Thm.rhs_of thm); | |
| 1207 | val (cl, cr) = Thm.dest_comb t | |
| 1208 |                               val dVar = Var(("", 0), dummyT)
 | |
| 1209 | val skel = | |
| 1210 | list_comb (h, replicate (length ts) dVar) | |
| 1211 | in | |
| 1212 | (case botc skel ctxt cl of | |
| 1213 | NONE => thm | |
| 1214 | | SOME thm' => | |
| 1215 | transitive3 thm (Thm.combination thm' (Thm.reflexive cr))) | |
| 1216 | end handle Pattern.MATCH => appc ())) | |
| 1217 | | _ => appc ()) | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1218 | end) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1219 | | _ => NONE) | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1220 | end | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1221 | and impc ct ctxt = | 
| 54725 | 1222 | if mutsimp then mut_impc0 [] ct [] [] ctxt | 
| 1223 | else nonmut_impc ct ctxt | |
| 10413 | 1224 | |
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1225 | and rules_of_prem prem ctxt = | 
| 59582 | 1226 | if maxidx_of_term (Thm.term_of prem) <> ~1 | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1227 | then | 
| 55031 | 1228 | (cond_tracing ctxt (fn () => | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1229 | print_term ctxt "Cannot add premise as rewrite rule because it contains (type) unknowns:" | 
| 59582 | 1230 | (Thm.term_of prem)); | 
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1231 | (([], NONE), ctxt)) | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1232 | else | 
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1233 | let val (asm, ctxt') = Thm.assume_hyps prem ctxt | 
| 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1234 | in ((extract_safe_rrules ctxt' asm, SOME asm), ctxt') end | 
| 10413 | 1235 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1236 | and add_rrules (rrss, asms) ctxt = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1237 | (fold o fold) insert_rrule rrss ctxt |> add_prems (map_filter I asms) | 
| 10413 | 1238 | |
| 23178 | 1239 | and disch r prem eq = | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1240 | let | 
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 1241 | val (lhs, rhs) = Thm.dest_equals (Thm.cprop_of eq); | 
| 54727 | 1242 | val eq' = | 
| 1243 | Thm.implies_elim | |
| 77879 | 1244 | (Thm.instantiate (TVars.empty, Vars.make3 (vA, prem) (vB, lhs) (vC, rhs)) | 
| 74282 | 1245 | Drule.imp_cong) | 
| 54727 | 1246 | (Thm.implies_intr prem eq); | 
| 54725 | 1247 | in | 
| 1248 | if not r then eq' | |
| 1249 | else | |
| 1250 | let | |
| 1251 | val (prem', concl) = Thm.dest_implies lhs; | |
| 54727 | 1252 | val (prem'', _) = Thm.dest_implies rhs; | 
| 1253 | in | |
| 1254 | Thm.transitive | |
| 1255 | (Thm.transitive | |
| 77879 | 1256 | (Thm.instantiate (TVars.empty, Vars.make3 (vA, prem') (vB, prem) (vC, concl)) | 
| 74282 | 1257 | Drule.swap_prems_eq) | 
| 54727 | 1258 | eq') | 
| 77879 | 1259 | (Thm.instantiate (TVars.empty, Vars.make3 (vA, prem) (vB, prem'') (vC, concl)) | 
| 74282 | 1260 | Drule.swap_prems_eq) | 
| 54725 | 1261 | end | 
| 10413 | 1262 | end | 
| 1263 | ||
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1264 | and rebuild [] _ _ _ _ eq = eq | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1265 | | rebuild (prem :: prems) concl (_ :: rrss) (_ :: asms) ctxt eq = | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1266 | let | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1267 | val ctxt' = add_rrules (rev rrss, rev asms) ctxt; | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1268 | val concl' = | 
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 1269 | Drule.mk_implies (prem, the_default concl (Option.map Thm.rhs_of eq)); | 
| 54727 | 1270 | val dprem = Option.map (disch false prem); | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1271 | in | 
| 52091 | 1272 | (case rewritec (prover, maxidx) ctxt' concl' of | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1273 | NONE => rebuild prems concl' rrss asms ctxt (dprem eq) | 
| 54727 | 1274 | | SOME (eq', _) => | 
| 1275 | transitive2 (fold (disch false) prems (the (transitive3 (dprem eq) eq'))) | |
| 1276 | (mut_impc0 (rev prems) (Thm.rhs_of eq') (rev rrss) (rev asms) ctxt)) | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1277 | end | 
| 15023 | 1278 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1279 | and mut_impc0 prems concl rrss asms ctxt = | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1280 | let | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1281 | val prems' = strip_imp_prems concl; | 
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1282 | val ((rrss', asms'), ctxt') = fold_map rules_of_prem prems' ctxt |>> split_list; | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1283 | in | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1284 | mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss') | 
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1285 | (asms @ asms') [] [] [] [] ctxt' ~1 ~1 | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1286 | end | 
| 15023 | 1287 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1288 | and mut_impc [] concl [] [] prems' rrss' asms' eqns ctxt changed k = | 
| 33245 | 1289 | transitive1 (fold (fn (eq1, prem) => fn eq2 => transitive1 eq1 | 
| 1290 | (Option.map (disch false prem) eq2)) (eqns ~~ prems') NONE) | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1291 | (if changed > 0 then | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1292 | mut_impc (rev prems') concl (rev rrss') (rev asms') | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1293 | [] [] [] [] ctxt ~1 changed | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1294 | else rebuild prems' concl rrss' asms' ctxt | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1295 | (botc skel0 (add_rrules (rev rrss', rev asms') ctxt) concl)) | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1296 | |
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1297 | | mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1298 | prems' rrss' asms' eqns ctxt changed k = | 
| 54725 | 1299 | (case (if k = 0 then NONE else botc skel0 (add_rrules | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1300 | (rev rrss' @ rrss, rev asms' @ asms) ctxt) prem) of | 
| 15531 | 1301 | NONE => mut_impc prems concl rrss asms (prem :: prems') | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1302 | (rrs :: rrss') (asm :: asms') (NONE :: eqns) ctxt changed | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1303 | (if k = 0 then 0 else k - 1) | 
| 54725 | 1304 | | SOME eqn => | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1305 | let | 
| 22902 
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
 wenzelm parents: 
22892diff
changeset | 1306 | val prem' = Thm.rhs_of eqn; | 
| 59582 | 1307 | val tprems = map Thm.term_of prems; | 
| 77863 
760515c45864
revert b43ee37926a9 due to problems with AFP/PAPP_Impossibility;
 wenzelm parents: 
77808diff
changeset | 1308 | val i = 1 + fold Integer.max (map (fn p => | 
| 
760515c45864
revert b43ee37926a9 due to problems with AFP/PAPP_Impossibility;
 wenzelm parents: 
77808diff
changeset | 1309 | find_index (fn q => q aconv p) tprems) (Thm.hyps_of eqn)) ~1; | 
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1310 | val ((rrs', asm'), ctxt') = rules_of_prem prem' ctxt; | 
| 54725 | 1311 | in | 
| 1312 | mut_impc prems concl rrss asms (prem' :: prems') | |
| 1313 | (rrs' :: rrss') (asm' :: asms') | |
| 1314 | (SOME (fold_rev (disch true) | |
| 1315 | (take i prems) | |
| 1316 | (Drule.imp_cong_rule eqn (Thm.reflexive (Drule.list_implies | |
| 1317 | (drop i prems, concl))))) :: eqns) | |
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1318 | ctxt' (length prems') ~1 | 
| 54725 | 1319 | end) | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1320 | |
| 54725 | 1321 | (*legacy code -- only for backwards compatibility*) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1322 | and nonmut_impc ct ctxt = | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1323 | let | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1324 | val (prem, conc) = Thm.dest_implies ct; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1325 | val thm1 = if simprem then botc skel0 ctxt prem else NONE; | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1326 | val prem1 = the_default prem (Option.map Thm.rhs_of thm1); | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1327 | val ctxt1 = | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1328 | if not useprem then ctxt | 
| 54984 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1329 | else | 
| 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1330 | let val ((rrs, asm), ctxt') = rules_of_prem prem1 ctxt | 
| 
da70ab8531f4
more elementary management of declared hyps, below structure Assumption;
 wenzelm parents: 
54982diff
changeset | 1331 | in add_rrules ([rrs], [asm]) ctxt' end; | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1332 | in | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1333 | (case botc skel0 ctxt1 conc of | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1334 | NONE => | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1335 | (case thm1 of | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1336 | NONE => NONE | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1337 | | SOME thm1' => SOME (Drule.imp_cong_rule thm1' (Thm.reflexive conc))) | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1338 | | SOME thm2 => | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1339 | let val thm2' = disch false prem1 thm2 in | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1340 | (case thm1 of | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1341 | NONE => SOME thm2' | 
| 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1342 | | SOME thm1' => | 
| 36944 | 1343 | SOME (Thm.transitive (Drule.imp_cong_rule thm1' (Thm.reflexive conc)) thm2')) | 
| 38834 
658fcba35ed7
more careful treatment of context visibility flag wrt. spurious warnings;
 wenzelm parents: 
38715diff
changeset | 1344 | end) | 
| 54725 | 1345 | end; | 
| 10413 | 1346 | |
| 54725 | 1347 | in try_botc end; | 
| 10413 | 1348 | |
| 1349 | ||
| 67721 | 1350 | (* Meta-rewriting: rewrites t to u and returns the theorem t \<equiv> u *) | 
| 10413 | 1351 | |
| 1352 | (* | |
| 1353 | Parameters: | |
| 1354 | mode = (simplify A, | |
| 1355 | use A in simplifying B, | |
| 1356 | use prems of B (if B is again a meta-impl.) to simplify A) | |
| 67721 | 1357 | when simplifying A \<Longrightarrow> B | 
| 10413 | 1358 | prover: how to solve premises in conditional rewrites and congruences | 
| 1359 | *) | |
| 1360 | ||
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1361 | fun rewrite_cterm mode prover raw_ctxt raw_ct = | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1362 | let | 
| 60324 | 1363 | val ct = raw_ct | 
| 78136 | 1364 | |> Thm.transfer_cterm' raw_ctxt | 
| 60324 | 1365 | |> Thm.adjust_maxidx_cterm ~1; | 
| 59586 | 1366 | val maxidx = Thm.maxidx_of_cterm ct; | 
| 52091 | 1367 | |
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1368 | val ctxt = | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1369 | raw_ctxt | 
| 71318 
1be996d8bb98
proper proof body context for Simplifier plugins (solvers, loopers, ...) -- avoid crash due to Subgoal.FOCUS (before e58bc223f46c);
 wenzelm parents: 
71239diff
changeset | 1370 | |> Variable.set_body true | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1371 | |> Context_Position.set_visible false | 
| 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1372 | |> inc_simp_depth | 
| 59582 | 1373 |       |> (fn ctxt => trace_invoke {depth = simp_depth ctxt, term = Thm.term_of ct} ctxt);
 | 
| 54729 
c5cd7a58cf2d
generic trace operations for main steps of Simplifier;
 wenzelm parents: 
54728diff
changeset | 1374 | |
| 55028 
00e849f5b397
clarified Simplifier diagnostics -- simplified ML;
 wenzelm parents: 
55014diff
changeset | 1375 | val _ = | 
| 55031 | 1376 | cond_tracing ctxt (fn () => | 
| 59582 | 1377 | print_term ctxt "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" (Thm.term_of ct)); | 
| 70472 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 1378 | in | 
| 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 1379 | ct | 
| 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 1380 | |> bottomc (mode, Option.map (Drule.flexflex_unique (SOME ctxt)) oo prover, maxidx) ctxt | 
| 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 1381 | |> Thm.solve_constraints | 
| 
cf66d2db97fe
more robust and convenient treatment of implicit context;
 wenzelm parents: 
69575diff
changeset | 1382 | end; | 
| 10413 | 1383 | |
| 21708 | 1384 | val simple_prover = | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
59058diff
changeset | 1385 | SINGLE o (fn ctxt => ALLGOALS (resolve_tac ctxt (prems_of ctxt))); | 
| 21708 | 1386 | |
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 1387 | fun rewrite0 ctxt full = rewrite_cterm (full, false, false) simple_prover ctxt; | 
| 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 1388 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1389 | fun rewrite _ _ [] = Thm.reflexive | 
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 1390 | | rewrite ctxt full thms = rewrite0 (init_simpset thms ctxt) full; | 
| 11672 | 1391 | |
| 78453 
3fdf3c5cfa9d
performance tuning: prefer static simpset within functional closure of morphism (with notable impact on specifications "in" class, e.g. AFP/No_FTL_observers);
 wenzelm parents: 
78136diff
changeset | 1392 | fun rewrite0_rule ctxt = Conv.fconv_rule (rewrite0 ctxt true); | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1393 | fun rewrite_rule ctxt = Conv.fconv_rule o rewrite ctxt true; | 
| 21708 | 1394 | |
| 15023 | 1395 | (*simple term rewriting -- no proof*) | 
| 16458 | 1396 | fun rewrite_term thy rules procs = | 
| 17203 | 1397 | Pattern.rewrite_term thy (map decomp_simp' rules) procs; | 
| 15023 | 1398 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1399 | fun rewrite_thm mode prover ctxt = Conv.fconv_rule (rewrite_cterm mode prover ctxt); | 
| 10413 | 1400 | |
| 23536 
60a1672e298e
moved (asm_)rewrite_goal_tac from goal.ML to meta_simplifier.ML (no longer depends on SELECT_GOAL);
 wenzelm parents: 
23221diff
changeset | 1401 | (*Rewrite the subgoals of a proof state (represented by a theorem)*) | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1402 | fun rewrite_goals_rule ctxt thms th = | 
| 23584 | 1403 | Conv.fconv_rule (Conv.prems_conv ~1 (rewrite_cterm (true, true, true) simple_prover | 
| 63221 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1404 | (init_simpset thms ctxt))) th; | 
| 10413 | 1405 | |
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1406 | |
| 21708 | 1407 | (** meta-rewriting tactics **) | 
| 1408 | ||
| 28839 
32d498cf7595
eliminated rewrite_tac/fold_tac, which are not well-formed tactics due to change of main conclusion;
 wenzelm parents: 
28620diff
changeset | 1409 | (*Rewrite all subgoals*) | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1410 | fun rewrite_goals_tac ctxt defs = PRIMITIVE (rewrite_goals_rule ctxt defs); | 
| 21708 | 1411 | |
| 28839 
32d498cf7595
eliminated rewrite_tac/fold_tac, which are not well-formed tactics due to change of main conclusion;
 wenzelm parents: 
28620diff
changeset | 1412 | (*Rewrite one subgoal*) | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1413 | fun generic_rewrite_goal_tac mode prover_tac ctxt i thm = | 
| 25203 
e5b2dd8db7c8
asm_rewrite_goal_tac: avoiding PRIMITIVE lets informative exceptions (from simprocs) get through;
 wenzelm parents: 
24707diff
changeset | 1414 | if 0 < i andalso i <= Thm.nprems_of thm then | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1415 | Seq.single (Conv.gconv_rule (rewrite_cterm mode (SINGLE o prover_tac) ctxt) i thm) | 
| 25203 
e5b2dd8db7c8
asm_rewrite_goal_tac: avoiding PRIMITIVE lets informative exceptions (from simprocs) get through;
 wenzelm parents: 
24707diff
changeset | 1416 | else Seq.empty; | 
| 23536 
60a1672e298e
moved (asm_)rewrite_goal_tac from goal.ML to meta_simplifier.ML (no longer depends on SELECT_GOAL);
 wenzelm parents: 
23221diff
changeset | 1417 | |
| 63221 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1418 | fun rewrite_goal_tac ctxt thms = | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1419 | generic_rewrite_goal_tac (true, false, false) (K no_tac) (init_simpset thms ctxt); | 
| 23536 
60a1672e298e
moved (asm_)rewrite_goal_tac from goal.ML to meta_simplifier.ML (no longer depends on SELECT_GOAL);
 wenzelm parents: 
23221diff
changeset | 1420 | |
| 46707 | 1421 | (*Prunes all redundant parameters from the proof state by rewriting.*) | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1422 | fun prune_params_tac ctxt = rewrite_goals_tac ctxt [Drule.triv_forall_equality]; | 
| 21708 | 1423 | |
| 1424 | ||
| 1425 | (* for folding definitions, handling critical pairs *) | |
| 1426 | ||
| 1427 | (*The depth of nesting in a term*) | |
| 32797 | 1428 | fun term_depth (Abs (_, _, t)) = 1 + term_depth t | 
| 1429 | | term_depth (f $ t) = 1 + Int.max (term_depth f, term_depth t) | |
| 21708 | 1430 | | term_depth _ = 0; | 
| 1431 | ||
| 59582 | 1432 | val lhs_of_thm = #1 o Logic.dest_equals o Thm.prop_of; | 
| 21708 | 1433 | |
| 67721 | 1434 | (*folding should handle critical pairs! E.g. K \<equiv> Inl 0, S \<equiv> Inr (Inl 0) | 
| 21708 | 1435 | Returns longest lhs first to avoid folding its subexpressions.*) | 
| 1436 | fun sort_lhs_depths defs = | |
| 1437 | let val keylist = AList.make (term_depth o lhs_of_thm) defs | |
| 1438 | val keys = sort_distinct (rev_order o int_ord) (map #2 keylist) | |
| 1439 | in map (AList.find (op =) keylist) keys end; | |
| 1440 | ||
| 36944 | 1441 | val rev_defs = sort_lhs_depths o map Thm.symmetric; | 
| 21708 | 1442 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1443 | fun fold_rule ctxt defs = fold (rewrite_rule ctxt) (rev_defs defs); | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
54731diff
changeset | 1444 | fun fold_goals_tac ctxt defs = EVERY (map (rewrite_goals_tac ctxt) (rev_defs defs)); | 
| 21708 | 1445 | |
| 1446 | ||
| 67721 | 1447 | (* HHF normal form: \<And> before \<Longrightarrow>, outermost \<And> generalized *) | 
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1448 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1449 | local | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1450 | |
| 78046 
78deba4fdf27
tuned: more accurate check (is_norm_hhf protect);
 wenzelm parents: 
77879diff
changeset | 1451 | fun gen_norm_hhf protect ss ctxt0 th0 = | 
| 71177 | 1452 | let | 
| 1453 | val (ctxt, th) = Thm.join_transfer_context (ctxt0, th0); | |
| 1454 | val th' = | |
| 78046 
78deba4fdf27
tuned: more accurate check (is_norm_hhf protect);
 wenzelm parents: 
77879diff
changeset | 1455 | if Drule.is_norm_hhf protect (Thm.prop_of th) then th | 
| 71177 | 1456 | else | 
| 1457 | Conv.fconv_rule (rewrite_cterm (true, false, false) (K (K NONE)) (put_simpset ss ctxt)) th; | |
| 1458 | in th' |> Thm.adjust_maxidx_thm ~1 |> Variable.gen_all ctxt end; | |
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1459 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1460 | val hhf_ss = | 
| 63221 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1461 | Context.the_local_context () | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1462 | |> init_simpset Drule.norm_hhf_eqs | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1463 | |> simpset_of; | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1464 | |
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
51611diff
changeset | 1465 | val hhf_protect_ss = | 
| 63221 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1466 | Context.the_local_context () | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1467 | |> init_simpset Drule.norm_hhf_eqs | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1468 | |> add_eqcong Drule.protect_cong | 
| 
7d43fbbaba28
avoid warnings on duplicate rules in the given list;
 wenzelm parents: 
62913diff
changeset | 1469 | |> simpset_of; | 
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1470 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1471 | in | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1472 | |
| 78046 
78deba4fdf27
tuned: more accurate check (is_norm_hhf protect);
 wenzelm parents: 
77879diff
changeset | 1473 | val norm_hhf = gen_norm_hhf {protect = false} hhf_ss;
 | 
| 
78deba4fdf27
tuned: more accurate check (is_norm_hhf protect);
 wenzelm parents: 
77879diff
changeset | 1474 | val norm_hhf_protect = gen_norm_hhf {protect = true} hhf_protect_ss;
 | 
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1475 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1476 | end; | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1477 | |
| 10413 | 1478 | end; | 
| 1479 | ||
| 41228 
e1fce873b814
renamed structure MetaSimplifier to raw_Simplifer, to emphasize its meaning;
 wenzelm parents: 
41227diff
changeset | 1480 | structure Basic_Meta_Simplifier: BASIC_RAW_SIMPLIFIER = Raw_Simplifier; | 
| 32738 | 1481 | open Basic_Meta_Simplifier; |