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