| author | wenzelm | 
| Wed, 02 Aug 2006 22:27:02 +0200 | |
| changeset 20308 | ddb7e7129481 | 
| parent 20289 | ba7a7c56bed5 | 
| child 20330 | 6192478fdba5 | 
| permissions | -rw-r--r-- | 
| 10413 | 1 | (* Title: Pure/meta_simplifier.ML | 
| 2 | ID: $Id$ | |
| 11672 | 3 | Author: Tobias Nipkow and Stefan Berghofer | 
| 10413 | 4 | |
| 11672 | 5 | Meta-level Simplification. | 
| 10413 | 6 | *) | 
| 7 | ||
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 8 | infix 4 | 
| 15023 | 9 | addsimps delsimps addeqcongs deleqcongs addcongs delcongs addsimprocs delsimprocs | 
| 15199 | 10 | setmksimps setmkcong setmksym setmkeqTrue settermless setsubgoaler | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 11 | setloop' setloop addloop addloop' delloop setSSolver addSSolver setSolver addSolver; | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 12 | |
| 11672 | 13 | signature BASIC_META_SIMPLIFIER = | 
| 14 | sig | |
| 15023 | 15 | val debug_simp: bool ref | 
| 11672 | 16 | val trace_simp: bool ref | 
| 13828 | 17 | val simp_depth_limit: int ref | 
| 16042 | 18 | val trace_simp_depth_limit: int ref | 
| 15023 | 19 | type rrule | 
| 16807 | 20 | val eq_rrule: rrule * rrule -> bool | 
| 15023 | 21 | type cong | 
| 22 | type simpset | |
| 23 | type proc | |
| 17614 | 24 | type solver | 
| 25 | val mk_solver': string -> (simpset -> int -> tactic) -> solver | |
| 26 | val mk_solver: string -> (thm list -> int -> tactic) -> solver | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 27 | val rep_ss: simpset -> | 
| 15023 | 28 |    {rules: rrule Net.net,
 | 
| 29 | prems: thm list, | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 30 | bounds: int * ((string * typ) * string) list, | 
| 20289 | 31 | context: Proof.context option} * | 
| 15023 | 32 |    {congs: (string * cong) list * string list,
 | 
| 33 | procs: proc Net.net, | |
| 34 | mk_rews: | |
| 35 |      {mk: thm -> thm list,
 | |
| 36 | mk_cong: thm -> thm, | |
| 37 | mk_sym: thm -> thm option, | |
| 18208 | 38 | mk_eq_True: thm -> thm option, | 
| 39 | reorient: theory -> term list -> term -> term -> bool}, | |
| 15023 | 40 | termless: term * term -> bool, | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 41 | subgoal_tac: simpset -> int -> tactic, | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 42 | loop_tacs: (string * (simpset -> int -> tactic)) list, | 
| 15023 | 43 | solvers: solver list * solver list} | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 44 | val print_ss: simpset -> unit | 
| 15023 | 45 | val empty_ss: simpset | 
| 46 | val merge_ss: simpset * simpset -> simpset | |
| 47 | type simproc | |
| 48 | val mk_simproc: string -> cterm list -> | |
| 16458 | 49 | (theory -> simpset -> term -> thm option) -> simproc | 
| 15023 | 50 | val add_prems: thm list -> simpset -> simpset | 
| 51 | val prems_of_ss: simpset -> thm list | |
| 52 | val addsimps: simpset * thm list -> simpset | |
| 53 | val delsimps: simpset * thm list -> simpset | |
| 54 | val addeqcongs: simpset * thm list -> simpset | |
| 55 | val deleqcongs: simpset * thm list -> simpset | |
| 56 | val addcongs: simpset * thm list -> simpset | |
| 57 | val delcongs: simpset * thm list -> simpset | |
| 58 | val addsimprocs: simpset * simproc list -> simpset | |
| 59 | val delsimprocs: simpset * simproc list -> simpset | |
| 60 | val setmksimps: simpset * (thm -> thm list) -> simpset | |
| 61 | val setmkcong: simpset * (thm -> thm) -> simpset | |
| 62 | val setmksym: simpset * (thm -> thm option) -> simpset | |
| 63 | val setmkeqTrue: simpset * (thm -> thm option) -> simpset | |
| 64 | val settermless: simpset * (term * term -> bool) -> simpset | |
| 65 | val setsubgoaler: simpset * (simpset -> int -> tactic) -> simpset | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 66 | val setloop': simpset * (simpset -> int -> tactic) -> simpset | 
| 15023 | 67 | val setloop: simpset * (int -> tactic) -> simpset | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 68 | val addloop': simpset * (string * (simpset -> int -> tactic)) -> simpset | 
| 15023 | 69 | val addloop: simpset * (string * (int -> tactic)) -> simpset | 
| 70 | val delloop: simpset * string -> simpset | |
| 71 | val setSSolver: simpset * solver -> simpset | |
| 72 | val addSSolver: simpset * solver -> simpset | |
| 73 | val setSolver: simpset * solver -> simpset | |
| 74 | val addSolver: simpset * solver -> simpset | |
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 75 | val norm_hhf: thm -> thm | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 76 | val norm_hhf_protect: thm -> thm | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 77 | end; | 
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 78 | |
| 10413 | 79 | signature META_SIMPLIFIER = | 
| 80 | sig | |
| 11672 | 81 | include BASIC_META_SIMPLIFIER | 
| 10413 | 82 | exception SIMPLIFIER of string * thm | 
| 17966 
34e420fa03ad
moved various simplification tactics and rules to simplifier.ML;
 wenzelm parents: 
17897diff
changeset | 83 | val solver: simpset -> solver -> int -> tactic | 
| 15023 | 84 | val clear_ss: simpset -> simpset | 
| 85 | exception SIMPROC_FAIL of string * exn | |
| 16458 | 86 | val simproc_i: theory -> string -> term list | 
| 87 | -> (theory -> simpset -> term -> thm option) -> simproc | |
| 88 | val simproc: theory -> string -> string list | |
| 89 | -> (theory -> simpset -> term -> thm option) -> simproc | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 90 | val inherit_context: simpset -> simpset -> simpset | 
| 20289 | 91 | val the_context: simpset -> Proof.context | 
| 92 | val context: Proof.context -> simpset -> simpset | |
| 17897 | 93 | val theory_context: theory -> simpset -> simpset | 
| 17723 | 94 | val debug_bounds: bool ref | 
| 18208 | 95 | val set_reorient: (theory -> term list -> term -> term -> bool) -> simpset -> simpset | 
| 17966 
34e420fa03ad
moved various simplification tactics and rules to simplifier.ML;
 wenzelm parents: 
17897diff
changeset | 96 | val set_solvers: solver list -> simpset -> simpset | 
| 11672 | 97 | val rewrite_cterm: bool * bool * bool -> | 
| 15023 | 98 | (simpset -> thm -> thm option) -> simpset -> cterm -> thm | 
| 99 | val rewrite_aux: (simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm | |
| 100 | val simplify_aux: (simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm | |
| 16458 | 101 | val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term | 
| 15023 | 102 | val rewrite_thm: bool * bool * bool -> | 
| 103 | (simpset -> thm -> thm option) -> simpset -> thm -> thm | |
| 104 | val rewrite_goals_rule_aux: (simpset -> thm -> thm option) -> thm list -> thm -> thm | |
| 105 | val rewrite_goal_rule: bool * bool * bool -> | |
| 106 | (simpset -> thm -> thm option) -> simpset -> int -> thm -> thm | |
| 10413 | 107 | end; | 
| 108 | ||
| 15023 | 109 | structure MetaSimplifier: META_SIMPLIFIER = | 
| 10413 | 110 | struct | 
| 111 | ||
| 15023 | 112 | |
| 113 | (** datatype simpset **) | |
| 114 | ||
| 115 | (* rewrite rules *) | |
| 10413 | 116 | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 117 | type rrule = {thm: thm, name: string, lhs: term, elhs: cterm, fo: bool, perm: bool};
 | 
| 15023 | 118 | |
| 119 | (*thm: the rewrite rule; | |
| 120 | name: name of theorem from which rewrite rule was extracted; | |
| 121 | lhs: the left-hand side; | |
| 122 | elhs: the etac-contracted lhs; | |
| 123 | fo: use first-order matching; | |
| 124 | perm: the rewrite rule is permutative; | |
| 125 | ||
| 12603 | 126 | Remarks: | 
| 10413 | 127 | - elhs is used for matching, | 
| 15023 | 128 | lhs only for preservation of bound variable names; | 
| 10413 | 129 | - fo is set iff | 
| 130 | either elhs is first-order (no Var is applied), | |
| 15023 | 131 | in which case fo-matching is complete, | 
| 10413 | 132 | or elhs is not a pattern, | 
| 15023 | 133 | in which case there is nothing better to do;*) | 
| 10413 | 134 | |
| 135 | fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) =
 | |
| 15023 | 136 | Drule.eq_thm_prop (thm1, thm2); | 
| 137 | ||
| 138 | ||
| 139 | (* congruences *) | |
| 140 | ||
| 141 | type cong = {thm: thm, lhs: cterm};
 | |
| 10413 | 142 | |
| 12603 | 143 | fun eq_cong ({thm = thm1, ...}: cong, {thm = thm2, ...}: cong) =
 | 
| 15023 | 144 | Drule.eq_thm_prop (thm1, thm2); | 
| 10413 | 145 | |
| 146 | ||
| 17614 | 147 | (* simplification sets, procedures, and solvers *) | 
| 15023 | 148 | |
| 149 | (*A simpset contains data required during conversion: | |
| 10413 | 150 | rules: discrimination net of rewrite rules; | 
| 15023 | 151 | prems: current premises; | 
| 15249 
0da6b3075bfa
Replaced list of bound variables in simpset by maximal index of bound
 berghofe parents: 
15199diff
changeset | 152 | bounds: maximal index of bound variables already used | 
| 15023 | 153 | (for generating new names when rewriting under lambda abstractions); | 
| 10413 | 154 | congs: association list of congruence rules and | 
| 155 | a list of `weak' congruence constants. | |
| 156 | A congruence is `weak' if it avoids normalization of some argument. | |
| 157 | procs: discrimination net of simplification procedures | |
| 158 | (functions that prove rewrite rules on the fly); | |
| 15023 | 159 | mk_rews: | 
| 160 | mk: turn simplification thms into rewrite rules; | |
| 161 | mk_cong: prepare congruence rules; | |
| 162 | mk_sym: turn == around; | |
| 163 | mk_eq_True: turn P into P == True; | |
| 164 | termless: relation for ordered rewriting;*) | |
| 15011 | 165 | |
| 15023 | 166 | type mk_rews = | 
| 167 |  {mk: thm -> thm list,
 | |
| 168 | mk_cong: thm -> thm, | |
| 169 | mk_sym: thm -> thm option, | |
| 18208 | 170 | mk_eq_True: thm -> thm option, | 
| 171 | reorient: theory -> term list -> term -> term -> bool}; | |
| 15023 | 172 | |
| 173 | datatype simpset = | |
| 174 | Simpset of | |
| 175 |    {rules: rrule Net.net,
 | |
| 10413 | 176 | prems: thm list, | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 177 | bounds: int * ((string * typ) * string) list, | 
| 20289 | 178 | context: Proof.context option} * | 
| 15023 | 179 |    {congs: (string * cong) list * string list,
 | 
| 180 | procs: proc Net.net, | |
| 181 | mk_rews: mk_rews, | |
| 11504 | 182 | termless: term * term -> bool, | 
| 15011 | 183 | subgoal_tac: simpset -> int -> tactic, | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 184 | loop_tacs: (string * (simpset -> int -> tactic)) list, | 
| 15023 | 185 | solvers: solver list * solver list} | 
| 186 | and proc = | |
| 187 | Proc of | |
| 188 |    {name: string,
 | |
| 189 | lhs: cterm, | |
| 16458 | 190 | proc: theory -> simpset -> term -> thm option, | 
| 17614 | 191 | id: stamp} | 
| 192 | and solver = | |
| 193 | Solver of | |
| 194 |    {name: string,
 | |
| 195 | solver: simpset -> int -> tactic, | |
| 15023 | 196 | id: stamp}; | 
| 197 | ||
| 198 | ||
| 199 | fun rep_ss (Simpset args) = args; | |
| 10413 | 200 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 201 | fun make_ss1 (rules, prems, bounds, context) = | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 202 |   {rules = rules, prems = prems, bounds = bounds, context = context};
 | 
| 15023 | 203 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 204 | fun map_ss1 f {rules, prems, bounds, context} =
 | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 205 | make_ss1 (f (rules, prems, bounds, context)); | 
| 10413 | 206 | |
| 15023 | 207 | fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) = | 
| 208 |   {congs = congs, procs = procs, mk_rews = mk_rews, termless = termless,
 | |
| 209 | subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers}; | |
| 210 | ||
| 211 | fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers} =
 | |
| 212 | make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers)); | |
| 213 | ||
| 214 | fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2); | |
| 10413 | 215 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 216 | fun map_simpset f (Simpset ({rules, prems, bounds, context},
 | 
| 15023 | 217 |     {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers})) =
 | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 218 | make_simpset (f ((rules, prems, bounds, context), | 
| 15023 | 219 | (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers))); | 
| 10413 | 220 | |
| 15023 | 221 | fun map_simpset1 f (Simpset (r1, r2)) = Simpset (map_ss1 f r1, r2); | 
| 222 | fun map_simpset2 f (Simpset (r1, r2)) = Simpset (r1, map_ss2 f r2); | |
| 223 | ||
| 17614 | 224 | fun prems_of_ss (Simpset ({prems, ...}, _)) = prems;
 | 
| 225 | ||
| 226 | ||
| 227 | fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = (id1 = id2);
 | |
| 228 | ||
| 229 | fun mk_solver' name solver = Solver {name = name, solver = solver, id = stamp ()};
 | |
| 230 | fun mk_solver name solver = mk_solver' name (solver o prems_of_ss); | |
| 231 | ||
| 232 | fun solver_name (Solver {name, ...}) = name;
 | |
| 17966 
34e420fa03ad
moved various simplification tactics and rules to simplifier.ML;
 wenzelm parents: 
17897diff
changeset | 233 | fun solver ss (Solver {solver = tac, ...}) = tac ss;
 | 
| 17614 | 234 | fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2);
 | 
| 235 | val merge_solvers = gen_merge_lists eq_solver; | |
| 236 | ||
| 15023 | 237 | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 238 | (* diagnostics *) | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 239 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 240 | exception SIMPLIFIER of string * thm; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 241 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 242 | val debug_simp = ref false; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 243 | val trace_simp = ref false; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 244 | val simp_depth = ref 0; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 245 | val simp_depth_limit = ref 100; | 
| 18573 | 246 | val trace_simp_depth_limit = ref 1; | 
| 247 | val trace_simp_depth_limit_exceeded = ref false; | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 248 | local | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 249 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 250 | fun println a = | 
| 18573 | 251 | if ! simp_depth > ! trace_simp_depth_limit | 
| 252 | then if !trace_simp_depth_limit_exceeded then () | |
| 253 | else (tracing "trace_simp_depth_limit exceeded!"; | |
| 254 | trace_simp_depth_limit_exceeded := true) | |
| 255 | else (tracing (enclose "[" "]" (string_of_int (! simp_depth)) ^ a); | |
| 256 | trace_simp_depth_limit_exceeded := false); | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 257 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 258 | fun prnt warn a = if warn then warning a else println a; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 259 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 260 | fun show_bounds (Simpset ({bounds = (_, bs), ...}, _)) t =
 | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 261 | let | 
| 20146 | 262 | val names = Term.declare_term_names t Name.context; | 
| 263 | val xs = rev (#1 (Name.variants (rev (map #2 bs)) names)); | |
| 17614 | 264 | fun subst (((b, T), _), x') = (Free (b, T), Syntax.mark_boundT (x', T)); | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 265 | in Term.subst_atomic (ListPair.map subst (bs, xs)) t end; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 266 | |
| 17705 | 267 | in | 
| 268 | ||
| 269 | fun print_term warn a ss thy t = prnt warn (a ^ "\n" ^ | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 270 | Sign.string_of_term thy (if ! debug_simp then t else show_bounds ss t)); | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 271 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 272 | fun debug warn a = if ! debug_simp then prnt warn a else (); | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 273 | fun trace warn a = if ! trace_simp then prnt warn a else (); | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 274 | |
| 17705 | 275 | fun debug_term warn a ss thy t = if ! debug_simp then print_term warn a ss thy t else (); | 
| 276 | fun trace_term warn a ss thy t = if ! trace_simp then print_term warn a ss thy t else (); | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 277 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 278 | fun trace_cterm warn a ss ct = | 
| 17705 | 279 | if ! trace_simp then print_term warn a ss (Thm.theory_of_cterm ct) (Thm.term_of ct) else (); | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 280 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 281 | fun trace_thm a ss th = | 
| 17705 | 282 | if ! trace_simp then print_term false a ss (Thm.theory_of_thm th) (Thm.full_prop_of th) else (); | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 283 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 284 | fun trace_named_thm a ss (th, name) = | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 285 | if ! trace_simp then | 
| 17705 | 286 | print_term false (if name = "" then a else a ^ " " ^ quote name ^ ":") ss | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 287 | (Thm.theory_of_thm th) (Thm.full_prop_of th) | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 288 | else (); | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 289 | |
| 17705 | 290 | fun warn_thm a ss th = print_term true a ss (Thm.theory_of_thm th) (Thm.full_prop_of th); | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 291 | |
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 292 | fun cond_warn_thm a (ss as Simpset ({context, ...}, _)) th =
 | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 293 | if is_some context then () else warn_thm a ss th; | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 294 | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 295 | end; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 296 | |
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 297 | |
| 15023 | 298 | (* print simpsets *) | 
| 10413 | 299 | |
| 15023 | 300 | fun print_ss ss = | 
| 301 | let | |
| 15034 | 302 | val pretty_thms = map Display.pretty_thm; | 
| 15023 | 303 | |
| 15034 | 304 | fun pretty_cong (name, th) = | 
| 305 | Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, Display.pretty_thm th]; | |
| 15023 | 306 | fun pretty_proc (name, lhss) = | 
| 307 | Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss); | |
| 15034 | 308 | |
| 309 |     val Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...}) = ss;
 | |
| 16807 | 310 | val smps = map #thm (Net.entries rules); | 
| 15034 | 311 |     val cngs = map (fn (name, {thm, ...}) => (name, thm)) (#1 congs);
 | 
| 16807 | 312 | val prcs = Net.entries procs |> | 
| 313 |       map (fn Proc {name, lhs, id, ...} => ((name, lhs), id))
 | |
| 17496 | 314 | |> partition_eq (eq_snd (op =)) | 
| 17756 | 315 | |> map (fn ps => (fst (fst (hd ps)), map (snd o fst) ps)) | 
| 316 | |> Library.sort_wrt fst; | |
| 15023 | 317 | in | 
| 15034 | 318 | [Pretty.big_list "simplification rules:" (pretty_thms smps), | 
| 319 | Pretty.big_list "simplification procedures:" (map pretty_proc prcs), | |
| 320 | Pretty.big_list "congruences:" (map pretty_cong cngs), | |
| 15088 | 321 |       Pretty.strs ("loopers:" :: map (quote o #1) loop_tacs),
 | 
| 322 |       Pretty.strs ("unsafe solvers:" :: map (quote o solver_name) (#1 solvers)),
 | |
| 323 |       Pretty.strs ("safe solvers:" :: map (quote o solver_name) (#2 solvers))]
 | |
| 15023 | 324 | |> Pretty.chunks |> Pretty.writeln | 
| 13828 | 325 | end; | 
| 10413 | 326 | |
| 15023 | 327 | |
| 328 | (* simprocs *) | |
| 329 | ||
| 330 | exception SIMPROC_FAIL of string * exn; | |
| 331 | ||
| 332 | datatype simproc = Simproc of proc list; | |
| 333 | ||
| 334 | fun mk_simproc name lhss proc = | |
| 335 | let val id = stamp () in | |
| 336 | Simproc (lhss |> map (fn lhs => | |
| 337 |       Proc {name = name, lhs = lhs, proc = proc, id = id}))
 | |
| 338 | end; | |
| 339 | ||
| 19798 | 340 | (* FIXME avoid global thy and Logic.varify *) | 
| 16458 | 341 | fun simproc_i thy name = mk_simproc name o map (Thm.cterm_of thy o Logic.varify); | 
| 16807 | 342 | fun simproc thy name = simproc_i thy name o map (Sign.read_term thy); | 
| 15023 | 343 | |
| 15011 | 344 | |
| 10413 | 345 | |
| 346 | (** simpset operations **) | |
| 347 | ||
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 348 | (* context *) | 
| 10413 | 349 | |
| 17614 | 350 | fun eq_bound (x: string, (y, _)) = x = y; | 
| 351 | ||
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 352 | fun add_bound bound = map_simpset1 (fn (rules, prems, (count, bounds), context) => | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 353 | (rules, prems, (count + 1, bound :: bounds), context)); | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 354 | |
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 355 | fun add_prems ths = map_simpset1 (fn (rules, prems, bounds, context) => | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 356 | (rules, ths @ prems, bounds, context)); | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 357 | |
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 358 | fun inherit_context (Simpset ({bounds, context, ...}, _)) =
 | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 359 | map_simpset1 (fn (rules, prems, _, _) => (rules, prems, bounds, context)); | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 360 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 361 | fun the_context (Simpset ({context = SOME ctxt, ...}, _)) = ctxt
 | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 362 | | the_context _ = raise Fail "Simplifier: no proof context in simpset"; | 
| 10413 | 363 | |
| 17897 | 364 | fun context ctxt = | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 365 | map_simpset1 (fn (rules, prems, bounds, _) => (rules, prems, bounds, SOME ctxt)); | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 366 | |
| 17897 | 367 | val theory_context = context o Context.init_proof; | 
| 368 | ||
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 369 | fun fallback_context _ (ss as Simpset ({context = SOME _, ...}, _)) = ss
 | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 370 | | fallback_context thy ss = | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 371 | (warning "Simplifier: no proof context in simpset -- fallback to theory context!"; | 
| 17897 | 372 | theory_context thy ss); | 
| 373 | ||
| 374 | ||
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 375 | (* maintain simp rules *) | 
| 10413 | 376 | |
| 15023 | 377 | fun mk_rrule2 {thm, name, lhs, elhs, perm} =
 | 
| 378 | let | |
| 379 | val fo = Pattern.first_order (term_of elhs) orelse not (Pattern.pattern (term_of elhs)) | |
| 380 |   in {thm = thm, name = name, lhs = lhs, elhs = elhs, fo = fo, perm = perm} end;
 | |
| 10413 | 381 | |
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 382 | fun del_rrule (rrule as {thm, elhs, ...}) ss =
 | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 383 | ss |> map_simpset1 (fn (rules, prems, bounds, context) => | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 384 | (Net.delete_term eq_rrule (term_of elhs, rrule) rules, prems, bounds, context)) | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 385 | handle Net.DELETE => (cond_warn_thm "Rewrite rule not in simpset:" ss thm; ss); | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 386 | |
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 387 | fun insert_rrule (rrule as {thm, name, lhs, elhs, perm}) ss =
 | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 388 | (trace_named_thm "Adding rewrite rule" ss (thm, name); | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 389 | ss |> map_simpset1 (fn (rules, prems, bounds, context) => | 
| 15023 | 390 | let | 
| 391 |       val rrule2 as {elhs, ...} = mk_rrule2 rrule;
 | |
| 16807 | 392 | val rules' = Net.insert_term eq_rrule (term_of elhs, rrule2) rules; | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 393 | in (rules', prems, bounds, context) end) | 
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 394 | handle Net.INSERT => (cond_warn_thm "Ignoring duplicate rewrite rule:" ss thm; ss)); | 
| 10413 | 395 | |
| 396 | fun vperm (Var _, Var _) = true | |
| 397 | | vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) | |
| 398 | | vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) | |
| 399 | | vperm (t, u) = (t = u); | |
| 400 | ||
| 401 | fun var_perm (t, u) = | |
| 20197 
ffc64d90fc1f
use Term.add_vars instead of obsolete term_varnames;
 wenzelm parents: 
20146diff
changeset | 402 | vperm (t, u) andalso gen_eq_set (op =) (Term.add_vars t [], Term.add_vars u []); | 
| 10413 | 403 | |
| 404 | (* FIXME: it seems that the conditions on extra variables are too liberal if | |
| 405 | prems are nonempty: does solving the prems really guarantee instantiation of | |
| 406 | all its Vars? Better: a dynamic check each time a rule is applied. | |
| 407 | *) | |
| 408 | fun rewrite_rule_extra_vars prems elhs erhs = | |
| 20197 
ffc64d90fc1f
use Term.add_vars instead of obsolete term_varnames;
 wenzelm parents: 
20146diff
changeset | 409 | not (Term.add_vars erhs [] subset fold Term.add_vars (elhs :: prems) []) | 
| 10413 | 410 | orelse | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19303diff
changeset | 411 | not (term_tvars erhs subset (term_tvars elhs union maps term_tvars prems)); | 
| 10413 | 412 | |
| 15023 | 413 | (*simple test for looping rewrite rules and stupid orientations*) | 
| 18208 | 414 | fun default_reorient thy prems lhs rhs = | 
| 15023 | 415 | rewrite_rule_extra_vars prems lhs rhs | 
| 416 | orelse | |
| 417 | is_Var (head_of lhs) | |
| 418 | orelse | |
| 16305 | 419 | (* turns t = x around, which causes a headache if x is a local variable - | 
| 420 | usually it is very useful :-( | |
| 421 | is_Free rhs andalso not(is_Free lhs) andalso not(Logic.occs(rhs,lhs)) | |
| 422 | andalso not(exists_subterm is_Var lhs) | |
| 423 | orelse | |
| 424 | *) | |
| 16842 | 425 | exists (fn t => Logic.occs (lhs, t)) (rhs :: prems) | 
| 15023 | 426 | orelse | 
| 17203 | 427 | null prems andalso Pattern.matches thy (lhs, rhs) | 
| 10413 | 428 | (*the condition "null prems" is necessary because conditional rewrites | 
| 429 | with extra variables in the conditions may terminate although | |
| 15023 | 430 | the rhs is an instance of the lhs; example: ?m < ?n ==> f(?n) == f(?m)*) | 
| 431 | orelse | |
| 432 | is_Const lhs andalso not (is_Const rhs); | |
| 10413 | 433 | |
| 434 | fun decomp_simp thm = | |
| 15023 | 435 | let | 
| 16458 | 436 |     val {thy, prop, ...} = Thm.rep_thm thm;
 | 
| 15023 | 437 | val prems = Logic.strip_imp_prems prop; | 
| 438 | val concl = Drule.strip_imp_concl (Thm.cprop_of thm); | |
| 439 | val (lhs, rhs) = Drule.dest_equals concl handle TERM _ => | |
| 440 |       raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm);
 | |
| 441 | val (_, elhs) = Drule.dest_equals (Thm.cprop_of (Thm.eta_conversion lhs)); | |
| 16665 | 442 | val elhs = if term_of elhs aconv term_of lhs then lhs else elhs; (*share identical copies*) | 
| 18929 | 443 | val erhs = Envir.eta_contract (term_of rhs); | 
| 15023 | 444 | val perm = | 
| 445 | var_perm (term_of elhs, erhs) andalso | |
| 446 | not (term_of elhs aconv erhs) andalso | |
| 447 | not (is_Var (term_of elhs)); | |
| 16458 | 448 | in (thy, prems, term_of lhs, elhs, term_of rhs, perm) end; | 
| 10413 | 449 | |
| 12783 | 450 | fun decomp_simp' thm = | 
| 12979 
4c76bce4ce39
decomp_simp': use lhs instead of elhs (preserves more bound variable names);
 wenzelm parents: 
12783diff
changeset | 451 | let val (_, _, lhs, _, rhs, _) = decomp_simp thm in | 
| 12783 | 452 |     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 | 453 | else (lhs, rhs) | 
| 12783 | 454 | end; | 
| 455 | ||
| 15023 | 456 | fun mk_eq_True (Simpset (_, {mk_rews = {mk_eq_True, ...}, ...})) (thm, name) =
 | 
| 457 | (case mk_eq_True thm of | |
| 15531 | 458 | NONE => [] | 
| 459 | | SOME eq_True => | |
| 15023 | 460 | let val (_, _, lhs, elhs, _, _) = decomp_simp eq_True | 
| 461 |       in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end);
 | |
| 10413 | 462 | |
| 15023 | 463 | (*create the rewrite rule and possibly also the eq_True variant, | 
| 464 | in case there are extra vars on the rhs*) | |
| 465 | fun rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm2) = | |
| 466 |   let val rrule = {thm = thm, name = name, lhs = lhs, elhs = elhs, perm = false} in
 | |
| 20197 
ffc64d90fc1f
use Term.add_vars instead of obsolete term_varnames;
 wenzelm parents: 
20146diff
changeset | 467 | if Term.add_vars rhs [] subset Term.add_vars lhs [] andalso | 
| 15023 | 468 | term_tvars rhs subset term_tvars lhs then [rrule] | 
| 469 | else mk_eq_True ss (thm2, name) @ [rrule] | |
| 10413 | 470 | end; | 
| 471 | ||
| 15023 | 472 | fun mk_rrule ss (thm, name) = | 
| 473 | let val (_, prems, lhs, elhs, rhs, perm) = decomp_simp thm in | |
| 474 |     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
 | |
| 475 | else | |
| 476 | (*weak test for loops*) | |
| 477 | if rewrite_rule_extra_vars prems lhs rhs orelse is_Var (term_of elhs) | |
| 478 | then mk_eq_True ss (thm, name) | |
| 479 | else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm) | |
| 10413 | 480 | end; | 
| 481 | ||
| 15023 | 482 | fun orient_rrule ss (thm, name) = | 
| 18208 | 483 | let | 
| 484 | val (thy, prems, lhs, elhs, rhs, perm) = decomp_simp thm; | |
| 485 |     val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = ss;
 | |
| 486 | in | |
| 15023 | 487 |     if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}]
 | 
| 16458 | 488 | else if reorient thy prems lhs rhs then | 
| 489 | if reorient thy prems rhs lhs | |
| 15023 | 490 | then mk_eq_True ss (thm, name) | 
| 491 | else | |
| 18208 | 492 | (case mk_sym thm of | 
| 493 | NONE => [] | |
| 494 | | SOME thm' => | |
| 495 | let val (_, _, lhs', elhs', rhs', _) = decomp_simp thm' | |
| 496 | in rrule_eq_True (thm', name, lhs', elhs', rhs', ss, thm) end) | |
| 15023 | 497 | else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm) | 
| 10413 | 498 | end; | 
| 499 | ||
| 15199 | 500 | fun extract_rews (Simpset (_, {mk_rews = {mk, ...}, ...}), thms) =
 | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19303diff
changeset | 501 | maps (fn thm => map (rpair (Thm.name_of_thm thm)) (mk thm)) thms; | 
| 10413 | 502 | |
| 15023 | 503 | fun extract_safe_rrules (ss, thm) = | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19303diff
changeset | 504 | maps (orient_rrule ss) (extract_rews (ss, [thm])); | 
| 10413 | 505 | |
| 506 | ||
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 507 | (* add/del rules explicitly *) | 
| 10413 | 508 | |
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 509 | fun comb_simps comb mk_rrule (ss, thms) = | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 510 | let | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 511 | val rews = extract_rews (ss, thms); | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 512 | in fold (fold comb o mk_rrule) rews ss end; | 
| 10413 | 513 | |
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 514 | fun ss addsimps thms = | 
| 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 515 | comb_simps insert_rrule (mk_rrule ss) (ss, thms); | 
| 10413 | 516 | |
| 15023 | 517 | fun ss delsimps thms = | 
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 518 | comb_simps del_rrule (map mk_rrule2 o mk_rrule ss) (ss, thms); | 
| 15023 | 519 | |
| 520 | ||
| 521 | (* congs *) | |
| 10413 | 522 | |
| 15531 | 523 | fun cong_name (Const (a, _)) = SOME a | 
| 524 |   | cong_name (Free (a, _)) = SOME ("Free: " ^ a)
 | |
| 525 | | cong_name _ = NONE; | |
| 13835 
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
 ballarin parents: 
13828diff
changeset | 526 | |
| 15023 | 527 | local | 
| 528 | ||
| 529 | fun is_full_cong_prems [] [] = true | |
| 530 | | is_full_cong_prems [] _ = false | |
| 531 | | is_full_cong_prems (p :: prems) varpairs = | |
| 532 | (case Logic.strip_assums_concl p of | |
| 533 |         Const ("==", _) $ lhs $ rhs =>
 | |
| 534 | let val (x, xs) = strip_comb lhs and (y, ys) = strip_comb rhs in | |
| 535 | is_Var x andalso forall is_Bound xs andalso | |
| 536 | null (findrep xs) andalso xs = ys andalso | |
| 537 | (x, y) mem varpairs andalso | |
| 19303 | 538 | is_full_cong_prems prems (remove (op =) (x, y) varpairs) | 
| 15023 | 539 | end | 
| 540 | | _ => false); | |
| 541 | ||
| 542 | fun is_full_cong thm = | |
| 10413 | 543 | let | 
| 15023 | 544 | val prems = prems_of thm and concl = concl_of thm; | 
| 545 | val (lhs, rhs) = Logic.dest_equals concl; | |
| 546 | val (f, xs) = strip_comb lhs and (g, ys) = strip_comb rhs; | |
| 10413 | 547 | in | 
| 15023 | 548 | f = g andalso null (findrep (xs @ ys)) andalso length xs = length ys andalso | 
| 549 | is_full_cong_prems prems (xs ~~ ys) | |
| 10413 | 550 | end; | 
| 551 | ||
| 15023 | 552 | fun add_cong (ss, thm) = ss |> | 
| 553 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => | |
| 554 | let | |
| 555 | val (lhs, _) = Drule.dest_equals (Drule.strip_imp_concl (Thm.cprop_of thm)) | |
| 556 |         handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", thm);
 | |
| 18929 | 557 | (*val lhs = Envir.eta_contract lhs;*) | 
| 20057 | 558 | val a = the (cong_name (head_of (term_of lhs))) handle Option.Option => | 
| 15023 | 559 |         raise SIMPLIFIER ("Congruence must start with a constant or free variable", thm);
 | 
| 560 | val (alist, weak) = congs; | |
| 561 |       val alist2 = overwrite_warn (alist, (a, {lhs = lhs, thm = thm}))
 | |
| 562 |         ("Overwriting congruence rule for " ^ quote a);
 | |
| 563 | val weak2 = if is_full_cong thm then weak else a :: weak; | |
| 564 | in ((alist2, weak2), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end); | |
| 10413 | 565 | |
| 15023 | 566 | fun del_cong (ss, thm) = ss |> | 
| 567 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => | |
| 568 | let | |
| 569 | val (lhs, _) = Logic.dest_equals (Thm.concl_of thm) handle TERM _ => | |
| 570 |         raise SIMPLIFIER ("Congruence not a meta-equality", thm);
 | |
| 18929 | 571 | (*val lhs = Envir.eta_contract lhs;*) | 
| 20057 | 572 | val a = the (cong_name (head_of lhs)) handle Option.Option => | 
| 15023 | 573 |         raise SIMPLIFIER ("Congruence must start with a constant", thm);
 | 
| 574 | val (alist, _) = congs; | |
| 15570 | 575 | val alist2 = List.filter (fn (x, _) => x <> a) alist; | 
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19303diff
changeset | 576 |       val weak2 = alist2 |> map_filter (fn (a, {thm, ...}: cong) =>
 | 
| 15531 | 577 | if is_full_cong thm then NONE else SOME a); | 
| 15023 | 578 | in ((alist2, weak2), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end); | 
| 10413 | 579 | |
| 15023 | 580 | fun mk_cong (Simpset (_, {mk_rews = {mk_cong = f, ...}, ...})) = f;
 | 
| 581 | ||
| 582 | in | |
| 583 | ||
| 15570 | 584 | val (op addeqcongs) = Library.foldl add_cong; | 
| 585 | val (op deleqcongs) = Library.foldl del_cong; | |
| 15023 | 586 | |
| 587 | fun ss addcongs congs = ss addeqcongs map (mk_cong ss) congs; | |
| 588 | fun ss delcongs congs = ss deleqcongs map (mk_cong ss) congs; | |
| 589 | ||
| 590 | end; | |
| 10413 | 591 | |
| 592 | ||
| 15023 | 593 | (* simprocs *) | 
| 594 | ||
| 595 | local | |
| 10413 | 596 | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 597 | fun add_proc (proc as Proc {name, lhs, ...}) ss =
 | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 598 |  (trace_cterm false ("Adding simplification procedure " ^ quote name ^ " for") ss lhs;
 | 
| 15023 | 599 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => | 
| 16807 | 600 | (congs, Net.insert_term eq_proc (term_of lhs, proc) procs, | 
| 15023 | 601 | mk_rews, termless, subgoal_tac, loop_tacs, solvers)) ss | 
| 602 | handle Net.INSERT => | |
| 603 |     (warning ("Ignoring duplicate simplification procedure " ^ quote name); ss));
 | |
| 10413 | 604 | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 605 | fun del_proc (proc as Proc {name, lhs, ...}) ss =
 | 
| 15023 | 606 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => | 
| 16807 | 607 | (congs, Net.delete_term eq_proc (term_of lhs, proc) procs, | 
| 15023 | 608 | mk_rews, termless, subgoal_tac, loop_tacs, solvers)) ss | 
| 609 | handle Net.DELETE => | |
| 610 |     (warning ("Simplification procedure " ^ quote name ^ " not in simpset"); ss);
 | |
| 10413 | 611 | |
| 15023 | 612 | in | 
| 10413 | 613 | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 614 | fun ss addsimprocs ps = fold (fn Simproc procs => fold add_proc procs) ps ss; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 615 | fun ss delsimprocs ps = fold (fn Simproc procs => fold del_proc procs) ps ss; | 
| 10413 | 616 | |
| 15023 | 617 | end; | 
| 10413 | 618 | |
| 619 | ||
| 620 | (* mk_rews *) | |
| 621 | ||
| 15023 | 622 | local | 
| 623 | ||
| 18208 | 624 | fun map_mk_rews f = map_simpset2 (fn (congs, procs, {mk, mk_cong, mk_sym, mk_eq_True, reorient},
 | 
| 15023 | 625 | termless, subgoal_tac, loop_tacs, solvers) => | 
| 18208 | 626 | let | 
| 627 | val (mk', mk_cong', mk_sym', mk_eq_True', reorient') = | |
| 628 | f (mk, mk_cong, mk_sym, mk_eq_True, reorient); | |
| 629 |     val mk_rews' = {mk = mk', mk_cong = mk_cong', mk_sym = mk_sym', mk_eq_True = mk_eq_True',
 | |
| 630 | reorient = reorient'}; | |
| 631 | in (congs, procs, mk_rews', termless, subgoal_tac, loop_tacs, solvers) end); | |
| 15023 | 632 | |
| 633 | in | |
| 10413 | 634 | |
| 18208 | 635 | fun ss setmksimps mk = ss |> map_mk_rews (fn (_, mk_cong, mk_sym, mk_eq_True, reorient) => | 
| 636 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | |
| 15023 | 637 | |
| 18208 | 638 | fun ss setmkcong mk_cong = ss |> map_mk_rews (fn (mk, _, mk_sym, mk_eq_True, reorient) => | 
| 639 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | |
| 10413 | 640 | |
| 18208 | 641 | fun ss setmksym mk_sym = ss |> map_mk_rews (fn (mk, mk_cong, _, mk_eq_True, reorient) => | 
| 642 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | |
| 10413 | 643 | |
| 18208 | 644 | fun ss setmkeqTrue mk_eq_True = ss |> map_mk_rews (fn (mk, mk_cong, mk_sym, _, reorient) => | 
| 645 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | |
| 646 | ||
| 647 | fun set_reorient reorient = map_mk_rews (fn (mk, mk_cong, mk_sym, mk_eq_True, _) => | |
| 648 | (mk, mk_cong, mk_sym, mk_eq_True, reorient)); | |
| 15023 | 649 | |
| 650 | end; | |
| 651 | ||
| 14242 
ec70653a02bf
Added access to the mk_rews field (and friends).
 skalberg parents: 
14040diff
changeset | 652 | |
| 10413 | 653 | (* termless *) | 
| 654 | ||
| 15023 | 655 | fun ss settermless termless = ss |> | 
| 656 | map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers) => | |
| 657 | (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers)); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 658 | |
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 659 | |
| 15023 | 660 | (* tactics *) | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 661 | |
| 15023 | 662 | fun ss setsubgoaler subgoal_tac = ss |> | 
| 663 | map_simpset2 (fn (congs, procs, mk_rews, termless, _, loop_tacs, solvers) => | |
| 664 | (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers)); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 665 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 666 | fun ss setloop' tac = ss |> | 
| 15023 | 667 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, _, solvers) => | 
| 668 |    (congs, procs, mk_rews, termless, subgoal_tac, [("", tac)], solvers));
 | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 669 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 670 | fun ss setloop tac = ss setloop' (K tac); | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 671 | |
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 672 | fun ss addloop' (name, tac) = ss |> | 
| 15023 | 673 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => | 
| 674 | (congs, procs, mk_rews, termless, subgoal_tac, | |
| 675 |       overwrite_warn (loop_tacs, (name, tac)) ("Overwriting looper " ^ quote name),
 | |
| 676 | solvers)); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 677 | |
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 678 | fun ss addloop (name, tac) = ss addloop' (name, K tac); | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 679 | |
| 15023 | 680 | fun ss delloop name = ss |> | 
| 681 | map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => | |
| 17756 | 682 | let val loop_tacs' = filter_out (equal name o fst) loop_tacs in | 
| 15034 | 683 | if length loop_tacs <> length loop_tacs' then () | 
| 684 |       else warning ("No such looper in simpset: " ^ quote name);
 | |
| 685 | (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs', solvers) | |
| 15023 | 686 | end); | 
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 687 | |
| 15023 | 688 | fun ss setSSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, | 
| 689 | subgoal_tac, loop_tacs, (unsafe_solvers, _)) => | |
| 690 | (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, [solver]))); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 691 | |
| 15023 | 692 | fun ss addSSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, | 
| 693 | subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless, | |
| 694 | subgoal_tac, loop_tacs, (unsafe_solvers, merge_solvers solvers [solver]))); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 695 | |
| 15023 | 696 | fun ss setSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, | 
| 697 | subgoal_tac, loop_tacs, (_, solvers)) => (congs, procs, mk_rews, termless, | |
| 698 | subgoal_tac, loop_tacs, ([solver], solvers))); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 699 | |
| 15023 | 700 | fun ss addSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, | 
| 701 | subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless, | |
| 702 | subgoal_tac, loop_tacs, (merge_solvers unsafe_solvers [solver], solvers))); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 703 | |
| 15023 | 704 | fun set_solvers solvers = map_simpset2 (fn (congs, procs, mk_rews, termless, | 
| 705 | subgoal_tac, loop_tacs, _) => (congs, procs, mk_rews, termless, | |
| 706 | subgoal_tac, loop_tacs, (solvers, solvers))); | |
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 707 | |
| 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 708 | |
| 18208 | 709 | (* empty *) | 
| 710 | ||
| 711 | fun init_ss mk_rews termless subgoal_tac solvers = | |
| 712 | make_simpset ((Net.empty, [], (0, []), NONE), | |
| 713 | (([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers)); | |
| 714 | ||
| 715 | fun clear_ss (ss as Simpset (_, {mk_rews, termless, subgoal_tac, solvers, ...})) =
 | |
| 716 | init_ss mk_rews termless subgoal_tac solvers | |
| 717 | |> inherit_context ss; | |
| 718 | ||
| 719 | val basic_mk_rews: mk_rews = | |
| 720 |  {mk = fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [],
 | |
| 721 | mk_cong = I, | |
| 722 | mk_sym = SOME o Drule.symmetric_fun, | |
| 723 | mk_eq_True = K NONE, | |
| 724 | reorient = default_reorient}; | |
| 725 | ||
| 726 | val empty_ss = init_ss basic_mk_rews Term.termless (K (K no_tac)) ([], []); | |
| 727 | ||
| 728 | ||
| 729 | (* merge *) (*NOTE: ignores some fields of 2nd simpset*) | |
| 730 | ||
| 731 | fun merge_ss (ss1, ss2) = | |
| 732 | let | |
| 733 |     val Simpset ({rules = rules1, prems = prems1, bounds = bounds1, context = _},
 | |
| 734 |      {congs = (congs1, weak1), procs = procs1, mk_rews, termless, subgoal_tac,
 | |
| 735 | loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1; | |
| 736 |     val Simpset ({rules = rules2, prems = prems2, bounds = bounds2, context = _},
 | |
| 737 |      {congs = (congs2, weak2), procs = procs2, mk_rews = _, termless = _, subgoal_tac = _,
 | |
| 738 | loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2; | |
| 739 | ||
| 740 | val rules' = Net.merge eq_rrule (rules1, rules2); | |
| 741 | val prems' = gen_merge_lists Drule.eq_thm_prop prems1 prems2; | |
| 742 | val bounds' = if #1 bounds1 < #1 bounds2 then bounds2 else bounds1; | |
| 743 | val congs' = gen_merge_lists (eq_cong o pairself #2) congs1 congs2; | |
| 744 | val weak' = merge_lists weak1 weak2; | |
| 745 | val procs' = Net.merge eq_proc (procs1, procs2); | |
| 746 | val loop_tacs' = merge_alists loop_tacs1 loop_tacs2; | |
| 747 | val unsafe_solvers' = merge_solvers unsafe_solvers1 unsafe_solvers2; | |
| 748 | val solvers' = merge_solvers solvers1 solvers2; | |
| 749 | in | |
| 750 | make_simpset ((rules', prems', bounds', NONE), ((congs', weak'), procs', | |
| 751 | mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers'))) | |
| 752 | end; | |
| 753 | ||
| 754 | ||
| 15006 
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier.  Next step:
 skalberg parents: 
15001diff
changeset | 755 | |
| 10413 | 756 | (** rewriting **) | 
| 757 | ||
| 758 | (* | |
| 759 | Uses conversions, see: | |
| 760 | L C Paulson, A higher-order implementation of rewriting, | |
| 761 | Science of Computer Programming 3 (1983), pages 119-149. | |
| 762 | *) | |
| 763 | ||
| 15023 | 764 | val dest_eq = Drule.dest_equals o Thm.cprop_of; | 
| 765 | val lhs_of = #1 o dest_eq; | |
| 766 | val rhs_of = #2 o dest_eq; | |
| 10413 | 767 | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 768 | fun check_conv msg ss thm thm' = | 
| 10413 | 769 | let | 
| 770 | val thm'' = transitive thm (transitive | |
| 15001 
fb2141a9f8c0
Moved conversion rules from MetaSimplifier to Drule. refl_implies removed
 skalberg parents: 
14981diff
changeset | 771 | (symmetric (Drule.beta_eta_conversion (lhs_of thm'))) thm') | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 772 | in if msg then trace_thm "SUCCEEDED" ss thm' else (); SOME thm'' end | 
| 10413 | 773 | handle THM _ => | 
| 16458 | 774 |     let val {thy, prop = _ $ _ $ prop0, ...} = Thm.rep_thm thm in
 | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 775 | trace_thm "Proved wrong thm (Check subgoaler?)" ss thm'; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 776 | trace_term false "Should have proved:" ss thy prop0; | 
| 15531 | 777 | NONE | 
| 10413 | 778 | end; | 
| 779 | ||
| 780 | ||
| 781 | (* mk_procrule *) | |
| 782 | ||
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 783 | fun mk_procrule ss thm = | 
| 15023 | 784 | let val (_, prems, lhs, elhs, rhs, _) = decomp_simp thm in | 
| 785 | if rewrite_rule_extra_vars prems lhs rhs | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 786 | then (warn_thm "Extra vars on rhs:" ss thm; []) | 
| 15023 | 787 |     else [mk_rrule2 {thm = thm, name = "", lhs = lhs, elhs = elhs, perm = false}]
 | 
| 10413 | 788 | end; | 
| 789 | ||
| 790 | ||
| 15023 | 791 | (* rewritec: conversion to apply the meta simpset to a term *) | 
| 10413 | 792 | |
| 15023 | 793 | (*Since the rewriting strategy is bottom-up, we avoid re-normalizing already | 
| 794 | normalized terms by carrying around the rhs of the rewrite rule just | |
| 795 | applied. This is called the `skeleton'. It is decomposed in parallel | |
| 796 | with the term. Once a Var is encountered, the corresponding term is | |
| 797 | already in normal form. | |
| 798 | skel0 is a dummy skeleton that is to enforce complete normalization.*) | |
| 799 | ||
| 10413 | 800 | val skel0 = Bound 0; | 
| 801 | ||
| 15023 | 802 | (*Use rhs as skeleton only if the lhs does not contain unnormalized bits. | 
| 803 | The latter may happen iff there are weak congruence rules for constants | |
| 804 | in the lhs.*) | |
| 10413 | 805 | |
| 15023 | 806 | fun uncond_skel ((_, weak), (lhs, rhs)) = | 
| 807 | if null weak then rhs (*optimization*) | |
| 808 | else if exists_Const (fn (c, _) => c mem weak) lhs then skel0 | |
| 809 | else rhs; | |
| 810 | ||
| 811 | (*Behaves like unconditional rule if rhs does not contain vars not in the lhs. | |
| 812 | Otherwise those vars may become instantiated with unnormalized terms | |
| 813 | while the premises are solved.*) | |
| 814 | ||
| 815 | fun cond_skel (args as (congs, (lhs, rhs))) = | |
| 20197 
ffc64d90fc1f
use Term.add_vars instead of obsolete term_varnames;
 wenzelm parents: 
20146diff
changeset | 816 | if Term.add_vars rhs [] subset Term.add_vars lhs [] then uncond_skel args | 
| 10413 | 817 | else skel0; | 
| 818 | ||
| 819 | (* | |
| 15023 | 820 | Rewriting -- we try in order: | 
| 10413 | 821 | (1) beta reduction | 
| 822 | (2) unconditional rewrite rules | |
| 823 | (3) conditional rewrite rules | |
| 824 | (4) simplification procedures | |
| 825 | ||
| 826 | IMPORTANT: rewrite rules must not introduce new Vars or TVars! | |
| 827 | *) | |
| 828 | ||
| 16458 | 829 | fun rewritec (prover, thyt, maxt) ss t = | 
| 10413 | 830 | let | 
| 15023 | 831 |     val Simpset ({rules, ...}, {congs, procs, termless, ...}) = ss;
 | 
| 10413 | 832 | val eta_thm = Thm.eta_conversion t; | 
| 833 | val eta_t' = rhs_of eta_thm; | |
| 834 | val eta_t = term_of eta_t'; | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 835 |     fun rew {thm, name, lhs, elhs, fo, perm} =
 | 
| 10413 | 836 | let | 
| 16458 | 837 |         val {thy, prop, maxidx, ...} = rep_thm thm;
 | 
| 10413 | 838 | val (rthm, elhs') = if maxt = ~1 then (thm, elhs) | 
| 839 | else (Thm.incr_indexes (maxt+1) thm, Thm.cterm_incr_indexes (maxt+1) elhs); | |
| 840 | val insts = if fo then Thm.cterm_first_order_match (elhs', eta_t') | |
| 841 | else Thm.cterm_match (elhs', eta_t'); | |
| 842 | val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm); | |
| 14643 | 843 | val prop' = Thm.prop_of thm'; | 
| 10413 | 844 | val unconditional = (Logic.count_prems (prop',0) = 0); | 
| 845 | val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop') | |
| 846 | in | |
| 11295 | 847 | if perm andalso not (termless (rhs', lhs')) | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 848 | then (trace_named_thm "Cannot apply permutative rewrite rule" ss (thm, name); | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 849 | trace_thm "Term does not become smaller:" ss thm'; NONE) | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 850 | else (trace_named_thm "Applying instance of rewrite rule" ss (thm, name); | 
| 10413 | 851 | if unconditional | 
| 852 | then | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 853 | (trace_thm "Rewriting:" ss thm'; | 
| 10413 | 854 | let val lr = Logic.dest_equals prop; | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 855 | val SOME thm'' = check_conv false ss eta_thm thm' | 
| 15531 | 856 | in SOME (thm'', uncond_skel (congs, lr)) end) | 
| 10413 | 857 | else | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 858 | (trace_thm "Trying to rewrite:" ss thm'; | 
| 16042 | 859 | if !simp_depth > !simp_depth_limit | 
| 860 | then let val s = "simp_depth_limit exceeded - giving up" | |
| 861 | in trace false s; warning s; NONE end | |
| 862 | else | |
| 863 | case prover ss thm' of | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 864 | NONE => (trace_thm "FAILED" ss thm'; NONE) | 
| 15531 | 865 | | SOME thm2 => | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 866 | (case check_conv true ss eta_thm thm2 of | 
| 15531 | 867 | NONE => NONE | | 
| 868 | SOME thm2' => | |
| 10413 | 869 | let val concl = Logic.strip_imp_concl prop | 
| 870 | val lr = Logic.dest_equals concl | |
| 16042 | 871 | in SOME (thm2', cond_skel (congs, lr)) end))) | 
| 10413 | 872 | end | 
| 873 | ||
| 15531 | 874 | fun rews [] = NONE | 
| 10413 | 875 | | rews (rrule :: rrules) = | 
| 15531 | 876 | let val opt = rew rrule handle Pattern.MATCH => NONE | 
| 877 | in case opt of NONE => rews rrules | some => some end; | |
| 10413 | 878 | |
| 879 | fun sort_rrules rrs = let | |
| 14643 | 880 |       fun is_simple({thm, ...}:rrule) = case Thm.prop_of thm of
 | 
| 10413 | 881 |                                       Const("==",_) $ _ $ _ => true
 | 
| 12603 | 882 | | _ => false | 
| 10413 | 883 | fun sort [] (re1,re2) = re1 @ re2 | 
| 12603 | 884 | | sort (rr::rrs) (re1,re2) = if is_simple rr | 
| 10413 | 885 | then sort rrs (rr::re1,re2) | 
| 886 | else sort rrs (re1,rr::re2) | |
| 887 | in sort rrs ([],[]) end | |
| 888 | ||
| 15531 | 889 | fun proc_rews [] = NONE | 
| 15023 | 890 |       | proc_rews (Proc {name, proc, lhs, ...} :: ps) =
 | 
| 17203 | 891 | if Pattern.matches thyt (Thm.term_of lhs, Thm.term_of t) then | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 892 |             (debug_term false ("Trying procedure " ^ quote name ^ " on:") ss thyt eta_t;
 | 
| 13486 
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
 wenzelm parents: 
13458diff
changeset | 893 | case transform_failure (curry SIMPROC_FAIL name) | 
| 16458 | 894 | (fn () => proc thyt ss eta_t) () of | 
| 15531 | 895 | NONE => (debug false "FAILED"; proc_rews ps) | 
| 896 | | SOME raw_thm => | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 897 |                  (trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") ss raw_thm;
 | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 898 | (case rews (mk_procrule ss raw_thm) of | 
| 15531 | 899 |                     NONE => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^
 | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 900 | " -- does not match") ss t; proc_rews ps) | 
| 10413 | 901 | | some => some))) | 
| 902 | else proc_rews ps; | |
| 903 | in case eta_t of | |
| 15531 | 904 | Abs _ $ _ => SOME (transitive eta_thm | 
| 12155 
13c5469b4bb3
congc now returns None if congruence rule has no effect.
 berghofe parents: 
11886diff
changeset | 905 | (beta_conversion false eta_t'), skel0) | 
| 10413 | 906 | | _ => (case rews (sort_rrules (Net.match_term rules eta_t)) of | 
| 15531 | 907 | NONE => proc_rews (Net.match_term procs eta_t) | 
| 10413 | 908 | | some => some) | 
| 909 | end; | |
| 910 | ||
| 911 | ||
| 912 | (* conversion to apply a congruence rule to a term *) | |
| 913 | ||
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 914 | fun congc prover ss maxt {thm=cong,lhs=lhs} t =
 | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 915 | let val rthm = Thm.incr_indexes (maxt+1) cong; | 
| 10413 | 916 | val rlhs = fst (Drule.dest_equals (Drule.strip_imp_concl (cprop_of rthm))); | 
| 917 | val insts = Thm.cterm_match (rlhs, t) | |
| 918 | (* Pattern.match can raise Pattern.MATCH; | |
| 919 | is handled when congc is called *) | |
| 920 | val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm); | |
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 921 | val unit = trace_thm "Applying congruence rule:" ss thm'; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 922 | fun err (msg, thm) = (trace_thm msg ss thm; NONE) | 
| 10413 | 923 | in case prover thm' of | 
| 15531 | 924 |        NONE => err ("Congruence proof failed.  Could not prove", thm')
 | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 925 | | SOME thm2 => (case check_conv true ss (Drule.beta_eta_conversion t) thm2 of | 
| 15531 | 926 |           NONE => err ("Congruence proof failed.  Should not have proved", thm2)
 | 
| 927 | | SOME thm2' => | |
| 12155 
13c5469b4bb3
congc now returns None if congruence rule has no effect.
 berghofe parents: 
11886diff
changeset | 928 | if op aconv (pairself term_of (dest_equals (cprop_of thm2'))) | 
| 15531 | 929 | then NONE else SOME thm2') | 
| 10413 | 930 | end; | 
| 931 | ||
| 932 | val (cA, (cB, cC)) = | |
| 933 | apsnd dest_equals (dest_implies (hd (cprems_of Drule.imp_cong))); | |
| 934 | ||
| 15531 | 935 | fun transitive1 NONE NONE = NONE | 
| 936 | | transitive1 (SOME thm1) NONE = SOME thm1 | |
| 937 | | transitive1 NONE (SOME thm2) = SOME thm2 | |
| 938 | | transitive1 (SOME thm1) (SOME thm2) = SOME (transitive thm1 thm2) | |
| 10413 | 939 | |
| 15531 | 940 | fun transitive2 thm = transitive1 (SOME thm); | 
| 941 | fun transitive3 thm = transitive1 thm o SOME; | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 942 | |
| 16458 | 943 | fun bottomc ((simprem, useprem, mutsimp), prover, thy, maxidx) = | 
| 10413 | 944 | let | 
| 15023 | 945 | fun botc skel ss t = | 
| 15531 | 946 | if is_Var skel then NONE | 
| 10413 | 947 | else | 
| 15023 | 948 | (case subc skel ss t of | 
| 15531 | 949 | some as SOME thm1 => | 
| 16458 | 950 | (case rewritec (prover, thy, maxidx) ss (rhs_of thm1) of | 
| 15531 | 951 | SOME (thm2, skel2) => | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 952 | transitive2 (transitive thm1 thm2) | 
| 15023 | 953 | (botc skel2 ss (rhs_of thm2)) | 
| 15531 | 954 | | NONE => some) | 
| 955 | | NONE => | |
| 16458 | 956 | (case rewritec (prover, thy, maxidx) ss t of | 
| 15531 | 957 | SOME (thm2, skel2) => transitive2 thm2 | 
| 15023 | 958 | (botc skel2 ss (rhs_of thm2)) | 
| 15531 | 959 | | NONE => NONE)) | 
| 10413 | 960 | |
| 15023 | 961 | and try_botc ss t = | 
| 962 | (case botc skel0 ss t of | |
| 15531 | 963 | SOME trec1 => trec1 | NONE => (reflexive t)) | 
| 10413 | 964 | |
| 15023 | 965 |     and subc skel (ss as Simpset ({bounds, ...}, {congs, ...})) t0 =
 | 
| 10413 | 966 | (case term_of t0 of | 
| 967 | Abs (a, T, t) => | |
| 15023 | 968 | let | 
| 20079 
ec5c8584487c
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
20057diff
changeset | 969 | val b = Name.bound (#1 bounds); | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 970 | val (v, t') = Thm.dest_abs (SOME b) t0; | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 971 | val b' = #1 (Term.dest_Free (Thm.term_of v)); | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 972 | val _ = conditional (b <> b') (fn () => | 
| 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 973 |                    warning ("Simplifier: renamed bound variable " ^ quote b ^ " to " ^ quote b'));
 | 
| 17614 | 974 | val ss' = add_bound ((b', T), a) ss; | 
| 15023 | 975 | val skel' = case skel of Abs (_, _, sk) => sk | _ => skel0; | 
| 976 | in case botc skel' ss' t' of | |
| 15531 | 977 | SOME thm => SOME (abstract_rule a v thm) | 
| 978 | | NONE => NONE | |
| 10413 | 979 | end | 
| 980 | | t $ _ => (case t of | |
| 15023 | 981 |              Const ("==>", _) $ _  => impc t0 ss
 | 
| 10413 | 982 | | Abs _ => | 
| 983 | let val thm = beta_conversion false t0 | |
| 15023 | 984 | in case subc skel0 ss (rhs_of thm) of | 
| 15531 | 985 | NONE => SOME thm | 
| 986 | | SOME thm' => SOME (transitive thm thm') | |
| 10413 | 987 | end | 
| 988 | | _ => | |
| 989 | let fun appc () = | |
| 990 | let | |
| 991 | val (tskel, uskel) = case skel of | |
| 992 | tskel $ uskel => (tskel, uskel) | |
| 993 | | _ => (skel0, skel0); | |
| 10767 
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
 wenzelm parents: 
10413diff
changeset | 994 | val (ct, cu) = Thm.dest_comb t0 | 
| 10413 | 995 | in | 
| 15023 | 996 | (case botc tskel ss ct of | 
| 15531 | 997 | SOME thm1 => | 
| 15023 | 998 | (case botc uskel ss cu of | 
| 15531 | 999 | SOME thm2 => SOME (combination thm1 thm2) | 
| 1000 | | NONE => SOME (combination thm1 (reflexive cu))) | |
| 1001 | | NONE => | |
| 15023 | 1002 | (case botc uskel ss cu of | 
| 15531 | 1003 | SOME thm1 => SOME (combination (reflexive ct) thm1) | 
| 1004 | | NONE => NONE)) | |
| 10413 | 1005 | end | 
| 1006 | val (h, ts) = strip_comb t | |
| 13835 
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
 ballarin parents: 
13828diff
changeset | 1007 | in case cong_name h of | 
| 15531 | 1008 | SOME a => | 
| 17232 | 1009 | (case AList.lookup (op =) (fst congs) a of | 
| 15531 | 1010 | NONE => appc () | 
| 1011 | | SOME cong => | |
| 15023 | 1012 | (*post processing: some partial applications h t1 ... tj, j <= length ts, | 
| 1013 | may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*) | |
| 10413 | 1014 | (let | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 1015 | val thm = congc (prover ss) ss maxidx cong t0; | 
| 19502 | 1016 | val t = the_default t0 (Option.map rhs_of thm); | 
| 10767 
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
 wenzelm parents: 
10413diff
changeset | 1017 | val (cl, cr) = Thm.dest_comb t | 
| 10413 | 1018 |                              val dVar = Var(("", 0), dummyT)
 | 
| 1019 | val skel = | |
| 1020 | list_comb (h, replicate (length ts) dVar) | |
| 15023 | 1021 | in case botc skel ss cl of | 
| 15531 | 1022 | NONE => thm | 
| 1023 | | SOME thm' => transitive3 thm | |
| 12155 
13c5469b4bb3
congc now returns None if congruence rule has no effect.
 berghofe parents: 
11886diff
changeset | 1024 | (combination thm' (reflexive cr)) | 
| 20057 | 1025 | end handle Pattern.MATCH => appc ())) | 
| 10413 | 1026 | | _ => appc () | 
| 1027 | end) | |
| 15531 | 1028 | | _ => NONE) | 
| 10413 | 1029 | |
| 15023 | 1030 | and impc ct ss = | 
| 1031 | if mutsimp then mut_impc0 [] ct [] [] ss else nonmut_impc ct ss | |
| 10413 | 1032 | |
| 15023 | 1033 | and rules_of_prem ss prem = | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1034 | if maxidx_of_term (term_of prem) <> ~1 | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1035 | then (trace_cterm true | 
| 16985 
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
 wenzelm parents: 
16938diff
changeset | 1036 | "Cannot add premise as rewrite rule because it contains (type) unknowns:" ss prem; ([], NONE)) | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1037 | else | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1038 | let val asm = assume prem | 
| 15531 | 1039 | in (extract_safe_rrules (ss, asm), SOME asm) end | 
| 10413 | 1040 | |
| 15023 | 1041 | and add_rrules (rrss, asms) ss = | 
| 20028 
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
 wenzelm parents: 
19798diff
changeset | 1042 | (fold o fold) insert_rrule rrss ss |> add_prems (map_filter I asms) | 
| 10413 | 1043 | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1044 | and disch r (prem, eq) = | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1045 | let | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1046 | val (lhs, rhs) = dest_eq eq; | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1047 | val eq' = implies_elim (Thm.instantiate | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1048 | ([], [(cA, prem), (cB, lhs), (cC, rhs)]) Drule.imp_cong) | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1049 | (implies_intr prem eq) | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1050 | in if not r then eq' else | 
| 10413 | 1051 | let | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1052 | val (prem', concl) = dest_implies lhs; | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1053 | val (prem'', _) = dest_implies rhs | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1054 | in transitive (transitive | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1055 | (Thm.instantiate ([], [(cA, prem'), (cB, prem), (cC, concl)]) | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1056 | Drule.swap_prems_eq) eq') | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1057 | (Thm.instantiate ([], [(cA, prem), (cB, prem''), (cC, concl)]) | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1058 | Drule.swap_prems_eq) | 
| 10413 | 1059 | end | 
| 1060 | end | |
| 1061 | ||
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1062 | and rebuild [] _ _ _ _ eq = eq | 
| 15023 | 1063 | | rebuild (prem :: prems) concl (rrs :: rrss) (asm :: asms) ss eq = | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1064 | let | 
| 15023 | 1065 | val ss' = add_rrules (rev rrss, rev asms) ss; | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1066 | val concl' = | 
| 19502 | 1067 | Drule.mk_implies (prem, the_default concl (Option.map rhs_of eq)); | 
| 15570 | 1068 | val dprem = Option.map (curry (disch false) prem) | 
| 16458 | 1069 | in case rewritec (prover, thy, maxidx) ss' concl' of | 
| 15531 | 1070 | NONE => rebuild prems concl' rrss asms ss (dprem eq) | 
| 15570 | 1071 | | SOME (eq', _) => transitive2 (Library.foldl (disch false o swap) | 
| 19502 | 1072 | (the (transitive3 (dprem eq) eq'), prems)) | 
| 15023 | 1073 | (mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) ss) | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1074 | end | 
| 15023 | 1075 | |
| 1076 | and mut_impc0 prems concl rrss asms ss = | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1077 | let | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1078 | val prems' = strip_imp_prems concl; | 
| 15023 | 1079 | val (rrss', asms') = split_list (map (rules_of_prem ss) prems') | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1080 | in mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss') | 
| 15023 | 1081 | (asms @ asms') [] [] [] [] ss ~1 ~1 | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1082 | end | 
| 15023 | 1083 | |
| 1084 | and mut_impc [] concl [] [] prems' rrss' asms' eqns ss changed k = | |
| 15570 | 1085 | transitive1 (Library.foldl (fn (eq2, (eq1, prem)) => transitive1 eq1 | 
| 1086 | (Option.map (curry (disch false) prem) eq2)) (NONE, eqns ~~ prems')) | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1087 | (if changed > 0 then | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1088 | mut_impc (rev prems') concl (rev rrss') (rev asms') | 
| 15023 | 1089 | [] [] [] [] ss ~1 changed | 
| 1090 | else rebuild prems' concl rrss' asms' ss | |
| 1091 | (botc skel0 (add_rrules (rev rrss', rev asms') ss) concl)) | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1092 | |
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1093 | | mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms) | 
| 15023 | 1094 | prems' rrss' asms' eqns ss changed k = | 
| 15531 | 1095 | case (if k = 0 then NONE else botc skel0 (add_rrules | 
| 15023 | 1096 | (rev rrss' @ rrss, rev asms' @ asms) ss) prem) of | 
| 15531 | 1097 | NONE => mut_impc prems concl rrss asms (prem :: prems') | 
| 1098 | (rrs :: rrss') (asm :: asms') (NONE :: eqns) ss changed | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1099 | (if k = 0 then 0 else k - 1) | 
| 15531 | 1100 | | SOME eqn => | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1101 | let | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1102 | val prem' = rhs_of eqn; | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1103 | val tprems = map term_of prems; | 
| 15570 | 1104 | val i = 1 + Library.foldl Int.max (~1, map (fn p => | 
| 19618 | 1105 | find_index (fn q => q aconv p) tprems) (#hyps (rep_thm eqn))); | 
| 15023 | 1106 | val (rrs', asm') = rules_of_prem ss prem' | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1107 | in mut_impc prems concl rrss asms (prem' :: prems') | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 1108 | (rrs' :: rrss') (asm' :: asms') (SOME (foldr (disch true) | 
| 18470 | 1109 | (Drule.imp_cong_rule eqn (reflexive (Drule.list_implies | 
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 1110 | (Library.drop (i, prems), concl)))) (Library.take (i, prems))) :: eqns) ss (length prems') ~1 | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1111 | end | 
| 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1112 | |
| 15023 | 1113 | (*legacy code - only for backwards compatibility*) | 
| 1114 | and nonmut_impc ct ss = | |
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1115 | let val (prem, conc) = dest_implies ct; | 
| 15531 | 1116 | val thm1 = if simprem then botc skel0 ss prem else NONE; | 
| 19502 | 1117 | val prem1 = the_default prem (Option.map rhs_of thm1); | 
| 15023 | 1118 | val ss1 = if not useprem then ss else add_rrules | 
| 1119 | (apsnd single (apfst single (rules_of_prem ss prem1))) ss | |
| 1120 | in (case botc skel0 ss1 conc of | |
| 15531 | 1121 | NONE => (case thm1 of | 
| 1122 | NONE => NONE | |
| 18470 | 1123 | | SOME thm1' => SOME (Drule.imp_cong_rule thm1' (reflexive conc))) | 
| 15531 | 1124 | | SOME thm2 => | 
| 13607 
6908230623a3
Completely reimplemented mutual simplification of premises.
 berghofe parents: 
13569diff
changeset | 1125 | let val thm2' = disch false (prem1, thm2) | 
| 10413 | 1126 | in (case thm1 of | 
| 15531 | 1127 | NONE => SOME thm2' | 
| 1128 | | SOME thm1' => | |
| 18470 | 1129 | SOME (transitive (Drule.imp_cong_rule thm1' (reflexive conc)) thm2')) | 
| 10413 | 1130 | end) | 
| 1131 | end | |
| 1132 | ||
| 15023 | 1133 | in try_botc end; | 
| 10413 | 1134 | |
| 1135 | ||
| 15023 | 1136 | (* Meta-rewriting: rewrites t to u and returns the theorem t==u *) | 
| 10413 | 1137 | |
| 1138 | (* | |
| 1139 | Parameters: | |
| 1140 | mode = (simplify A, | |
| 1141 | use A in simplifying B, | |
| 1142 | use prems of B (if B is again a meta-impl.) to simplify A) | |
| 1143 | when simplifying A ==> B | |
| 1144 | prover: how to solve premises in conditional rewrites and congruences | |
| 1145 | *) | |
| 1146 | ||
| 17705 | 1147 | val debug_bounds = ref false; | 
| 1148 | ||
| 1149 | fun check_bounds ss ct = conditional (! debug_bounds) (fn () => | |
| 17614 | 1150 | let | 
| 1151 |     val Simpset ({bounds = (_, bounds), ...}, _) = ss;
 | |
| 1152 | val bs = fold_aterms (fn Free (x, _) => | |
| 20079 
ec5c8584487c
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
20057diff
changeset | 1153 | if Name.is_bound x andalso not (AList.defined eq_bound bounds x) | 
| 17614 | 1154 | then insert (op =) x else I | 
| 1155 | | _ => I) (term_of ct) []; | |
| 17705 | 1156 | in | 
| 1157 | if null bs then () | |
| 17723 | 1158 |     else print_term true ("Simplifier: term contains loose bounds: " ^ commas_quote bs) ss
 | 
| 17705 | 1159 | (Thm.theory_of_cterm ct) (Thm.term_of ct) | 
| 1160 | end); | |
| 17614 | 1161 | |
| 19052 
113dbd65319e
rewrite_cterm: Thm.adjust_maxidx prevents unnecessary increments on rules;
 wenzelm parents: 
18929diff
changeset | 1162 | fun rewrite_cterm mode prover raw_ss raw_ct = | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1163 | let | 
| 20260 | 1164 | val ct = Thm.adjust_maxidx_cterm ~1 raw_ct; | 
| 17882 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1165 |     val {thy, t, maxidx, ...} = Thm.rep_cterm ct;
 | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1166 | val ss = fallback_context thy raw_ss; | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1167 | val _ = inc simp_depth; | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1168 | val _ = conditional (!simp_depth mod 20 = 0) (fn () => | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1169 |       warning ("Simplification depth " ^ string_of_int (! simp_depth)));
 | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1170 | val _ = trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ss ct; | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1171 | val _ = check_bounds ss ct; | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1172 | val res = bottomc (mode, Option.map Drule.flexflex_unique oo prover, thy, maxidx) ss ct | 
| 
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
 wenzelm parents: 
17756diff
changeset | 1173 | in dec simp_depth; res end | 
| 20057 | 1174 | handle exn => (dec simp_depth; raise exn); (* FIXME avoid handling of generic exceptions *) | 
| 10413 | 1175 | |
| 11760 | 1176 | (*Rewrite a cterm*) | 
| 17897 | 1177 | fun rewrite_aux _ _ [] ct = Thm.reflexive ct | 
| 1178 | | rewrite_aux prover full thms ct = | |
| 1179 | rewrite_cterm (full, false, false) prover | |
| 1180 | (theory_context (Thm.theory_of_cterm ct) empty_ss addsimps thms) ct; | |
| 11672 | 1181 | |
| 10413 | 1182 | (*Rewrite a theorem*) | 
| 17897 | 1183 | fun simplify_aux _ _ [] th = th | 
| 1184 | | simplify_aux prover full thms th = | |
| 1185 | Drule.fconv_rule (rewrite_cterm (full, false, false) prover | |
| 1186 | (theory_context (Thm.theory_of_thm th) empty_ss addsimps thms)) th; | |
| 10413 | 1187 | |
| 15023 | 1188 | (*simple term rewriting -- no proof*) | 
| 16458 | 1189 | fun rewrite_term thy rules procs = | 
| 17203 | 1190 | Pattern.rewrite_term thy (map decomp_simp' rules) procs; | 
| 15023 | 1191 | |
| 1192 | fun rewrite_thm mode prover ss = Drule.fconv_rule (rewrite_cterm mode prover ss); | |
| 10413 | 1193 | |
| 1194 | (*Rewrite the subgoals of a proof state (represented by a theorem) *) | |
| 19142 
99a72b8c9974
rewrite_goals_rule_aux: actually use prems if present;
 wenzelm parents: 
19052diff
changeset | 1195 | fun rewrite_goals_rule_aux prover thms th = | 
| 
99a72b8c9974
rewrite_goals_rule_aux: actually use prems if present;
 wenzelm parents: 
19052diff
changeset | 1196 | Drule.fconv_rule (Drule.goals_conv (K true) (rewrite_cterm (true, true, true) prover | 
| 
99a72b8c9974
rewrite_goals_rule_aux: actually use prems if present;
 wenzelm parents: 
19052diff
changeset | 1197 | (theory_context (Thm.theory_of_thm th) empty_ss addsimps thms))) th; | 
| 10413 | 1198 | |
| 15023 | 1199 | (*Rewrite the subgoal of a proof state (represented by a theorem)*) | 
| 15011 | 1200 | fun rewrite_goal_rule mode prover ss i thm = | 
| 10413 | 1201 | if 0 < i andalso i <= nprems_of thm | 
| 15011 | 1202 | then Drule.fconv_rule (Drule.goals_conv (fn j => j=i) (rewrite_cterm mode prover ss)) thm | 
| 10413 | 1203 |   else raise THM("rewrite_goal_rule",i,[thm]);
 | 
| 1204 | ||
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1205 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1206 | (* HHF normal form: !! before ==>, outermost !! generalized *) | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1207 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1208 | local | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1209 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1210 | fun gen_norm_hhf ss = | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1211 | (not o Drule.is_norm_hhf o Thm.prop_of) ? | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1212 | Drule.fconv_rule (rewrite_cterm (true, false, false) (K (K NONE)) ss) | 
| 20260 | 1213 | #> Thm.adjust_maxidx_thm ~1 | 
| 20228 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1214 | #> Drule.gen_all; | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1215 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1216 | val ss = theory_context ProtoPure.thy empty_ss addsimps [Drule.norm_hhf_eq]; | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1217 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1218 | in | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1219 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1220 | val norm_hhf = gen_norm_hhf ss; | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1221 | val norm_hhf_protect = gen_norm_hhf (ss addeqcongs [Drule.protect_cong]); | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1222 | |
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1223 | end; | 
| 
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
 wenzelm parents: 
20197diff
changeset | 1224 | |
| 10413 | 1225 | end; | 
| 1226 | ||
| 11672 | 1227 | structure BasicMetaSimplifier: BASIC_META_SIMPLIFIER = MetaSimplifier; | 
| 1228 | open BasicMetaSimplifier; |