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