src/Provers/simplifier.ML
author wenzelm
Fri Jul 25 13:18:09 1997 +0200 (1997-07-25 ago)
changeset 3577 9715b6e3ec5f
parent 3557 9546f8185c43
child 3728 f92594f65af6
permissions -rw-r--r--
added prems argument to simplification procedures;
clasohm@1243
     1
(*  Title:      Provers/simplifier.ML
nipkow@1
     2
    ID:         $Id$
wenzelm@3557
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
nipkow@1
     4
wenzelm@3577
     5
Generic simplifier, suitable for most logics.  See also Pure/thm.ML
wenzelm@3577
     6
for the actual meta level rewriting engine.
nipkow@1
     7
*)
clasohm@1260
     8
wenzelm@3551
     9
infix 4
wenzelm@3551
    10
  setsubgoaler setloop addloop setSSolver addSSolver setSolver
wenzelm@3551
    11
  addSolver setmksimps addsimps delsimps addeqcongs deleqcongs
wenzelm@3551
    12
  settermless addsimprocs delsimprocs;
oheimb@2567
    13
clasohm@0
    14
clasohm@0
    15
signature SIMPLIFIER =
clasohm@0
    16
sig
wenzelm@2509
    17
  type simproc
wenzelm@3577
    18
  val mk_simproc: string -> cterm list
wenzelm@3577
    19
    -> (Sign.sg -> thm list -> term -> thm option) -> simproc
wenzelm@2509
    20
  val conv_prover: (term * term -> term) -> thm -> (thm -> thm)
wenzelm@3557
    21
    -> tactic -> (int -> tactic) -> Sign.sg -> term -> term -> thm
clasohm@0
    22
  type simpset
wenzelm@2503
    23
  val empty_ss: simpset
wenzelm@3551
    24
  val rep_ss: simpset ->
wenzelm@3551
    25
   {mss: meta_simpset,
wenzelm@3551
    26
    subgoal_tac:        simpset -> int -> tactic,
wenzelm@3551
    27
    loop_tac:                      int -> tactic,
wenzelm@3551
    28
           finish_tac: thm list -> int -> tactic,
wenzelm@3551
    29
    unsafe_finish_tac: thm list -> int -> tactic};
wenzelm@3551
    30
  val print_ss: simpset -> unit
oheimb@2629
    31
  val setsubgoaler: simpset *  (simpset -> int -> tactic) -> simpset
oheimb@2629
    32
  val setloop:      simpset *             (int -> tactic) -> simpset
oheimb@2629
    33
  val addloop:      simpset *             (int -> tactic) -> simpset
oheimb@2629
    34
  val setSSolver:   simpset * (thm list -> int -> tactic) -> simpset
oheimb@2629
    35
  val addSSolver:   simpset * (thm list -> int -> tactic) -> simpset
oheimb@2629
    36
  val setSolver:    simpset * (thm list -> int -> tactic) -> simpset
oheimb@2629
    37
  val addSolver:    simpset * (thm list -> int -> tactic) -> simpset
wenzelm@3577
    38
  val setmksimps:   simpset * (thm -> thm list) -> simpset
wenzelm@3577
    39
  val settermless:  simpset * (term * term -> bool) -> simpset
wenzelm@3577
    40
  val addsimps:     simpset * thm list -> simpset
wenzelm@3577
    41
  val delsimps:     simpset * thm list -> simpset
wenzelm@3577
    42
  val addeqcongs:   simpset * thm list -> simpset
wenzelm@3577
    43
  val deleqcongs:   simpset * thm list -> simpset
wenzelm@3577
    44
  val addsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    45
  val delsimprocs:  simpset * simproc list -> simpset
wenzelm@3577
    46
  val merge_ss:     simpset * simpset -> simpset
wenzelm@3577
    47
  val prems_of_ss:  simpset -> thm list
wenzelm@3577
    48
  val simpset:      simpset ref
clasohm@1243
    49
  val Addsimps: thm list -> unit
clasohm@1243
    50
  val Delsimps: thm list -> unit
wenzelm@2509
    51
  val Addsimprocs: simproc list -> unit
wenzelm@2509
    52
  val Delsimprocs: simproc list -> unit
oheimb@2629
    53
  val               simp_tac: simpset -> int -> tactic
oheimb@2629
    54
  val           asm_simp_tac: simpset -> int -> tactic
oheimb@2629
    55
  val          full_simp_tac: simpset -> int -> tactic
oheimb@2629
    56
  val      asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    57
  val safe_asm_full_simp_tac: simpset -> int -> tactic
oheimb@2629
    58
  val               Simp_tac:            int -> tactic
oheimb@2629
    59
  val           Asm_simp_tac:            int -> tactic
oheimb@2629
    60
  val          Full_simp_tac:            int -> tactic
oheimb@2629
    61
  val      Asm_full_simp_tac:            int -> tactic
wenzelm@3557
    62
  val          simplify: simpset -> thm -> thm
wenzelm@3557
    63
  val      asm_simplify: simpset -> thm -> thm
wenzelm@3557
    64
  val     full_simplify: simpset -> thm -> thm
wenzelm@3557
    65
  val asm_full_simplify: simpset -> thm -> thm
clasohm@0
    66
end;
clasohm@0
    67
wenzelm@2503
    68
wenzelm@2503
    69
structure Simplifier: SIMPLIFIER =
clasohm@0
    70
struct
clasohm@0
    71
wenzelm@2509
    72
wenzelm@2509
    73
(** simplification procedures **)
wenzelm@2509
    74
wenzelm@2509
    75
(* datatype simproc *)
wenzelm@2509
    76
wenzelm@2509
    77
datatype simproc =
wenzelm@3577
    78
  Simproc of string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp;
wenzelm@2509
    79
wenzelm@3557
    80
fun mk_simproc name lhss proc =
wenzelm@3557
    81
  Simproc (name, map (Thm.cterm_fun Logic.varify) lhss, proc, stamp ());
wenzelm@3557
    82
wenzelm@3551
    83
fun rep_simproc (Simproc args) = args;
wenzelm@2509
    84
wenzelm@2509
    85
wenzelm@2509
    86
(* generic conversion prover *)		(* FIXME move?, rename? *)
wenzelm@2509
    87
wenzelm@2509
    88
fun conv_prover mk_eqv eqv_refl mk_meta_eq expand_tac norm_tac sg t u =
wenzelm@2509
    89
  let
wenzelm@2509
    90
    val X = Free (gensym "X.", fastype_of t);
wenzelm@2509
    91
    val goal = Logic.mk_implies (mk_eqv (X, t), mk_eqv (X, u));
wenzelm@2509
    92
    val pre_result =
wenzelm@2509
    93
      prove_goalw_cterm [] (cterm_of sg goal)   (*goal: X=t ==> X=u*)
wenzelm@2509
    94
        (fn prems => [
wenzelm@2509
    95
          expand_tac,				(*expand u*)
wenzelm@2509
    96
          ALLGOALS (cut_facts_tac prems),
wenzelm@2509
    97
          ALLGOALS norm_tac]);			(*normalize both t and u*)
wenzelm@2509
    98
  in
wenzelm@2509
    99
    mk_meta_eq (eqv_refl RS pre_result)         (*final result: t==u*)
wenzelm@2509
   100
  end
wenzelm@2509
   101
  handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@2509
   102
    (string_of_cterm (cterm_of sg (mk_eqv (t, u)))));
wenzelm@2509
   103
wenzelm@2509
   104
wenzelm@2509
   105
wenzelm@2503
   106
(** simplification sets **)
wenzelm@2503
   107
wenzelm@2503
   108
(* type simpset *)
wenzelm@2503
   109
clasohm@0
   110
datatype simpset =
wenzelm@2503
   111
  Simpset of {
wenzelm@2503
   112
    mss: meta_simpset,
oheimb@2629
   113
    subgoal_tac:        simpset -> int -> tactic,
oheimb@2629
   114
    loop_tac:                      int -> tactic,
oheimb@2629
   115
           finish_tac: thm list -> int -> tactic,
oheimb@2629
   116
    unsafe_finish_tac: thm list -> int -> tactic};
wenzelm@2503
   117
wenzelm@3551
   118
fun make_ss (mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac) =
wenzelm@3551
   119
  Simpset {mss = mss, subgoal_tac = subgoal_tac, loop_tac = loop_tac,
oheimb@2629
   120
    finish_tac = finish_tac, unsafe_finish_tac = unsafe_finish_tac};
clasohm@0
   121
clasohm@0
   122
val empty_ss =
wenzelm@3551
   123
  make_ss (Thm.empty_mss, K (K no_tac), K no_tac, K (K no_tac), K (K no_tac));
wenzelm@3551
   124
wenzelm@3551
   125
fun rep_ss (Simpset args) = args;
wenzelm@3551
   126
fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
wenzelm@3551
   127
wenzelm@3551
   128
wenzelm@3551
   129
(* print simpsets *)
wenzelm@2503
   130
wenzelm@3551
   131
fun print_ss ss =
wenzelm@3551
   132
  let
wenzelm@3551
   133
    val Simpset {mss, ...} = ss;
wenzelm@3551
   134
    val {simps, procs, congs} = Thm.dest_mss mss;
wenzelm@2503
   135
wenzelm@3551
   136
    val pretty_thms = map Display.pretty_thm;
wenzelm@3551
   137
    fun pretty_proc (name, lhss) =
wenzelm@3551
   138
      Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss);
wenzelm@3551
   139
  in
wenzelm@3551
   140
    Pretty.writeln (Pretty.big_list "simplification rules:" (pretty_thms simps));
wenzelm@3551
   141
    Pretty.writeln (Pretty.big_list "simplification procedures:" (map pretty_proc procs));
wenzelm@3551
   142
    Pretty.writeln (Pretty.big_list "congruences:" (pretty_thms congs))
wenzelm@3551
   143
  end;
wenzelm@2503
   144
wenzelm@2503
   145
wenzelm@2503
   146
(* extend simpsets *)
wenzelm@2503
   147
wenzelm@3551
   148
fun (Simpset {mss, subgoal_tac = _, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   149
    setsubgoaler subgoal_tac =
wenzelm@3551
   150
  make_ss (mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
oheimb@2629
   151
wenzelm@3551
   152
fun (Simpset {mss, subgoal_tac, loop_tac = _, finish_tac, unsafe_finish_tac})
wenzelm@3551
   153
    setloop loop_tac =
wenzelm@3551
   154
  make_ss (mss, subgoal_tac, DETERM o loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2503
   155
wenzelm@3551
   156
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   157
    addloop tac =
wenzelm@3551
   158
  make_ss (mss, subgoal_tac, loop_tac ORELSE' (DETERM o tac), finish_tac, unsafe_finish_tac);
oheimb@2567
   159
wenzelm@3551
   160
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac = _, unsafe_finish_tac})
wenzelm@3551
   161
    setSSolver finish_tac =
wenzelm@3551
   162
  make_ss (mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2503
   163
wenzelm@3551
   164
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   165
    addSSolver tac =
wenzelm@3551
   166
  make_ss (mss, subgoal_tac, loop_tac, fn hyps => finish_tac hyps ORELSE' tac hyps,
wenzelm@3551
   167
    unsafe_finish_tac);
wenzelm@2503
   168
wenzelm@3551
   169
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac = _})
wenzelm@3551
   170
    setSolver unsafe_finish_tac =
wenzelm@3551
   171
  make_ss (mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2503
   172
wenzelm@3551
   173
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   174
    addSolver tac =
wenzelm@3551
   175
  make_ss (mss, subgoal_tac, loop_tac, finish_tac,
wenzelm@3551
   176
    fn hyps => unsafe_finish_tac hyps ORELSE' tac hyps);
wenzelm@2503
   177
wenzelm@3551
   178
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   179
    setmksimps mk_simps =
wenzelm@2645
   180
  make_ss (Thm.set_mk_rews (mss, map (Thm.strip_shyps o Drule.zero_var_indexes) o mk_simps),
oheimb@2629
   181
    subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2509
   182
wenzelm@3551
   183
fun (Simpset {mss, subgoal_tac, loop_tac,  finish_tac, unsafe_finish_tac})
wenzelm@3551
   184
    settermless termless =
wenzelm@3551
   185
  make_ss (Thm.set_termless (mss, termless), subgoal_tac, loop_tac,
wenzelm@3551
   186
    finish_tac, unsafe_finish_tac);
wenzelm@3551
   187
wenzelm@3551
   188
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   189
    addsimps rews =
wenzelm@2503
   190
  let val rews' = flat (map (Thm.mk_rews_of_mss mss) rews) in
wenzelm@3551
   191
    make_ss (Thm.add_simps (mss, rews'), subgoal_tac, loop_tac,
wenzelm@3551
   192
      finish_tac, unsafe_finish_tac)
wenzelm@2503
   193
  end;
wenzelm@2503
   194
wenzelm@3551
   195
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   196
    delsimps rews =
wenzelm@2503
   197
  let val rews' = flat (map (Thm.mk_rews_of_mss mss) rews) in
wenzelm@3551
   198
    make_ss (Thm.del_simps (mss, rews'), subgoal_tac, loop_tac,
wenzelm@3551
   199
      finish_tac, unsafe_finish_tac)
wenzelm@2503
   200
  end;
wenzelm@2503
   201
wenzelm@3551
   202
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   203
    addeqcongs newcongs =
wenzelm@3551
   204
  make_ss (Thm.add_congs (mss, newcongs), subgoal_tac, loop_tac,
wenzelm@3551
   205
    finish_tac, unsafe_finish_tac);
wenzelm@2509
   206
wenzelm@3551
   207
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   208
    deleqcongs oldcongs =
wenzelm@3551
   209
  make_ss (Thm.del_congs (mss, oldcongs), subgoal_tac, loop_tac,
wenzelm@3551
   210
    finish_tac, unsafe_finish_tac);
oheimb@2629
   211
wenzelm@3551
   212
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   213
    addsimprocs simprocs =
wenzelm@3551
   214
  make_ss
wenzelm@3551
   215
    (Thm.add_simprocs (mss, map rep_simproc simprocs),
wenzelm@3551
   216
      subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2509
   217
wenzelm@3551
   218
fun (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac})
wenzelm@3551
   219
    delsimprocs simprocs =
wenzelm@3551
   220
  make_ss
wenzelm@3551
   221
    (Thm.del_simprocs (mss, map rep_simproc simprocs),
wenzelm@3551
   222
      subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2503
   223
wenzelm@2503
   224
wenzelm@3551
   225
(* merge simpsets *)	(*NOTE: ignores tactics of 2nd simpset*)
wenzelm@2503
   226
wenzelm@3551
   227
fun merge_ss
wenzelm@3551
   228
   (Simpset {mss = mss1, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac},
wenzelm@3551
   229
    Simpset {mss = mss2, ...}) =
wenzelm@3551
   230
  make_ss (Thm.merge_mss (mss1, mss2),
wenzelm@3551
   231
    subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@2503
   232
wenzelm@2503
   233
wenzelm@3557
   234
wenzelm@3557
   235
(** the current simpset **)
clasohm@0
   236
clasohm@1243
   237
val simpset = ref empty_ss;
clasohm@0
   238
wenzelm@2503
   239
fun Addsimps rews = (simpset := ! simpset addsimps rews);
wenzelm@2503
   240
fun Delsimps rews = (simpset := ! simpset delsimps rews);
clasohm@0
   241
wenzelm@2509
   242
fun Addsimprocs procs = (simpset := ! simpset addsimprocs procs);
wenzelm@2509
   243
fun Delsimprocs procs = (simpset := ! simpset delsimprocs procs);
wenzelm@2509
   244
clasohm@0
   245
wenzelm@3557
   246
wenzelm@2503
   247
(** simplification tactics **)
clasohm@0
   248
wenzelm@3551
   249
fun NEWSUBGOALS tac tacf st0 =
wenzelm@3551
   250
  st0 |> (tac THEN (fn st1 => tacf (nprems_of st1 - nprems_of st0) st1));
nipkow@1
   251
wenzelm@3557
   252
fun solve_all_tac (subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac) mss =
wenzelm@3557
   253
  let
wenzelm@3557
   254
    val ss =
wenzelm@3557
   255
      make_ss (mss, subgoal_tac, loop_tac, unsafe_finish_tac, unsafe_finish_tac);
wenzelm@3557
   256
    val solve1_tac =
wenzelm@3557
   257
      NEWSUBGOALS (subgoal_tac ss 1) (fn n => if n < 0 then all_tac else no_tac);
wenzelm@3557
   258
  in DEPTH_SOLVE solve1_tac end;
wenzelm@3557
   259
wenzelm@3557
   260
oheimb@2629
   261
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@2629
   262
fun basic_gen_simp_tac mode =
wenzelm@3551
   263
  fn (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac}) =>
wenzelm@3551
   264
    let
paulson@1512
   265
      fun simp_loop_tac i thm =
wenzelm@3557
   266
        (asm_rewrite_goal_tac mode
wenzelm@3557
   267
          (solve_all_tac (subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac)) mss i
wenzelm@3557
   268
        THEN (finish_tac (prems_of_mss mss) i ORELSE looper i)) thm
wenzelm@3551
   269
      and allsimp i n = EVERY (map (fn j => simp_loop_tac (i + j)) (n downto 0))
wenzelm@3551
   270
      and looper i = TRY (NEWSUBGOALS (loop_tac i) (allsimp i));
nipkow@217
   271
  in simp_loop_tac end;
clasohm@0
   272
wenzelm@3551
   273
fun gen_simp_tac mode (ss as Simpset {unsafe_finish_tac, ...}) =
wenzelm@3551
   274
  basic_gen_simp_tac mode (ss setSSolver unsafe_finish_tac);
wenzelm@3551
   275
oheimb@2629
   276
wenzelm@2503
   277
val          simp_tac = gen_simp_tac (false, false);
wenzelm@2503
   278
val      asm_simp_tac = gen_simp_tac (false, true);
wenzelm@2503
   279
val     full_simp_tac = gen_simp_tac (true,  false);
wenzelm@2503
   280
val asm_full_simp_tac = gen_simp_tac (true,  true);
clasohm@0
   281
oheimb@2629
   282
(*not totally safe: may instantiate unknowns that appear also in other subgoals*)
oheimb@2629
   283
val safe_asm_full_simp_tac = basic_gen_simp_tac (true, true);
oheimb@2629
   284
wenzelm@2503
   285
fun          Simp_tac i =          simp_tac (! simpset) i;
wenzelm@2503
   286
fun      Asm_simp_tac i =      asm_simp_tac (! simpset) i;
wenzelm@2503
   287
fun     Full_simp_tac i =     full_simp_tac (! simpset) i;
wenzelm@2503
   288
fun Asm_full_simp_tac i = asm_full_simp_tac (! simpset) i;
nipkow@406
   289
wenzelm@3557
   290
wenzelm@3557
   291
wenzelm@3557
   292
(** simplification meta rules **)
wenzelm@3557
   293
wenzelm@3557
   294
fun simp mode (Simpset {mss, subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac}) thm =
wenzelm@3557
   295
  let
wenzelm@3557
   296
    val tacf = solve_all_tac (subgoal_tac, loop_tac, finish_tac, unsafe_finish_tac);
wenzelm@3557
   297
    fun prover m th = apsome fst (Sequence.pull (tacf m th));
wenzelm@3557
   298
  in
wenzelm@3557
   299
    Drule.rewrite_thm mode prover mss thm
wenzelm@3557
   300
  end;
wenzelm@3557
   301
wenzelm@3557
   302
val          simplify = simp (false, false);
wenzelm@3557
   303
val      asm_simplify = simp (false, true);
wenzelm@3557
   304
val     full_simplify = simp (true, false);
wenzelm@3557
   305
val asm_full_simplify = simp (true, true);
wenzelm@3557
   306
wenzelm@3557
   307
clasohm@1243
   308
end;