src/Provers/simplifier.ML
author wenzelm
Fri Jan 10 10:27:57 1997 +0100 (1997-01-10 ago)
changeset 2503 7590fd5ce3c7
parent 1770 608050b43bee
child 2509 0a7169d89b7a
permissions -rw-r--r--
cleaned up (real changes next time);
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.
wenzelm@2503
     7
wenzelm@2503
     8
TODO:
wenzelm@2503
     9
  - stamps to identify funs / tacs
wenzelm@2503
    10
  - merge: fail if incompatible funs
nipkow@1
    11
*)
clasohm@1260
    12
nipkow@1770
    13
infix 4 addsimps addeqcongs addsolver delsimps
wenzelm@2503
    14
  setsolver setloop setmksimps setsubgoaler;
clasohm@0
    15
clasohm@0
    16
signature SIMPLIFIER =
clasohm@0
    17
sig
clasohm@0
    18
  type simpset
wenzelm@2503
    19
  val empty_ss: simpset
wenzelm@2503
    20
  val rep_ss: simpset -> {simps: thm list, congs: thm list}
wenzelm@2503
    21
  val prems_of_ss: simpset -> thm list
wenzelm@2503
    22
  val setloop: simpset * (int -> tactic) -> simpset
wenzelm@2503
    23
  val setsolver: simpset * (thm list -> int -> tactic) -> simpset
wenzelm@2503
    24
  val addsolver: simpset * (thm list -> int -> tactic) -> simpset
wenzelm@2503
    25
  val setsubgoaler: simpset * (simpset -> int -> tactic) -> simpset
wenzelm@2503
    26
  val setmksimps: simpset * (thm -> thm list) -> simpset
clasohm@0
    27
  val addsimps: simpset * thm list -> simpset
nipkow@88
    28
  val delsimps: simpset * thm list -> simpset
wenzelm@2503
    29
  val addeqcongs: simpset * thm list -> simpset
clasohm@0
    30
  val merge_ss: simpset * simpset -> simpset
clasohm@1243
    31
  val simpset: simpset ref
clasohm@1243
    32
  val Addsimps: thm list -> unit
clasohm@1243
    33
  val Delsimps: thm list -> unit
wenzelm@2503
    34
  val          simp_tac: simpset -> int -> tactic
wenzelm@2503
    35
  val      asm_simp_tac: simpset -> int -> tactic
wenzelm@2503
    36
  val     full_simp_tac: simpset -> int -> tactic
wenzelm@2503
    37
  val asm_full_simp_tac: simpset -> int -> tactic
wenzelm@2503
    38
  val          Simp_tac: int -> tactic
oheimb@1676
    39
  val      Asm_simp_tac: int -> tactic
oheimb@1676
    40
  val     Full_simp_tac: int -> tactic
clasohm@1243
    41
  val Asm_full_simp_tac: int -> tactic
clasohm@0
    42
end;
clasohm@0
    43
wenzelm@2503
    44
wenzelm@2503
    45
structure Simplifier: SIMPLIFIER =
clasohm@0
    46
struct
clasohm@0
    47
wenzelm@2503
    48
(** simplification sets **)
wenzelm@2503
    49
wenzelm@2503
    50
(* type simpset *)
wenzelm@2503
    51
clasohm@0
    52
datatype simpset =
wenzelm@2503
    53
  Simpset of {
wenzelm@2503
    54
    mss: meta_simpset,
wenzelm@2503
    55
    simps: thm list,
wenzelm@2503
    56
    congs: thm list,
wenzelm@2503
    57
    subgoal_tac: simpset -> int -> tactic,
wenzelm@2503
    58
    finish_tac: thm list -> int -> tactic,
wenzelm@2503
    59
    loop_tac: int -> tactic};
wenzelm@2503
    60
wenzelm@2503
    61
fun make_ss (mss, simps, congs, subgoal_tac, finish_tac, loop_tac) =
wenzelm@2503
    62
  Simpset {mss = mss, simps = simps, congs = congs,
wenzelm@2503
    63
    subgoal_tac = subgoal_tac, finish_tac = finish_tac,
wenzelm@2503
    64
    loop_tac = loop_tac};
clasohm@0
    65
clasohm@0
    66
val empty_ss =
wenzelm@2503
    67
  make_ss (Thm.empty_mss, [], [], K (K no_tac), K (K no_tac), K no_tac);
wenzelm@2503
    68
wenzelm@2503
    69
fun rep_ss (Simpset {simps, congs, ...}) = {simps = simps, congs = congs};
wenzelm@2503
    70
wenzelm@2503
    71
fun prems_of_ss (Simpset {mss, ...}) = Thm.prems_of_mss mss;
wenzelm@2503
    72
wenzelm@2503
    73
wenzelm@2503
    74
(* extend simpsets *)
wenzelm@2503
    75
wenzelm@2503
    76
fun (Simpset {mss, simps, congs, subgoal_tac, finish_tac, loop_tac = _})
wenzelm@2503
    77
    setloop loop_tac =
wenzelm@2503
    78
  make_ss (mss, simps, congs, subgoal_tac, finish_tac, DETERM o loop_tac);
wenzelm@2503
    79
wenzelm@2503
    80
fun (Simpset {mss, simps, congs, subgoal_tac, finish_tac = _, loop_tac})
wenzelm@2503
    81
    setsolver finish_tac =
wenzelm@2503
    82
  make_ss (mss, simps, congs, subgoal_tac, finish_tac, loop_tac);
wenzelm@2503
    83
wenzelm@2503
    84
fun (Simpset {mss, simps, congs, subgoal_tac, loop_tac, finish_tac})
wenzelm@2503
    85
    addsolver tac =
wenzelm@2503
    86
  make_ss (mss, simps, congs, subgoal_tac,
wenzelm@2503
    87
    fn hyps => finish_tac hyps ORELSE' tac hyps, loop_tac);
wenzelm@2503
    88
wenzelm@2503
    89
fun (Simpset {mss, simps, congs, subgoal_tac = _, finish_tac, loop_tac})
wenzelm@2503
    90
    setsubgoaler subgoal_tac =
wenzelm@2503
    91
  make_ss (mss, simps, congs, subgoal_tac, finish_tac, loop_tac);
wenzelm@2503
    92
wenzelm@2503
    93
fun (Simpset {mss, simps, congs, subgoal_tac, finish_tac, loop_tac})
wenzelm@2503
    94
    setmksimps mk_simps =
wenzelm@2503
    95
  make_ss (Thm.set_mk_rews (mss, mk_simps), simps, congs,
wenzelm@2503
    96
    subgoal_tac, finish_tac, loop_tac);
wenzelm@2503
    97
wenzelm@2503
    98
fun (Simpset {mss, simps, congs, subgoal_tac, finish_tac, loop_tac})
wenzelm@2503
    99
    addsimps rews =
wenzelm@2503
   100
  let val rews' = flat (map (Thm.mk_rews_of_mss mss) rews) in
wenzelm@2503
   101
    make_ss (Thm.add_simps (mss, rews'), rews' @ simps,
wenzelm@2503
   102
      congs, subgoal_tac, finish_tac, loop_tac)
wenzelm@2503
   103
  end;
wenzelm@2503
   104
wenzelm@2503
   105
fun (Simpset {mss, simps, congs, subgoal_tac, finish_tac, loop_tac})
wenzelm@2503
   106
    delsimps rews =
wenzelm@2503
   107
  let val rews' = flat (map (Thm.mk_rews_of_mss mss) rews) in
wenzelm@2503
   108
    make_ss (Thm.del_simps (mss, rews'),
wenzelm@2503
   109
      foldl (gen_rem eq_thm) (simps, rews'),
wenzelm@2503
   110
      congs, subgoal_tac, finish_tac, loop_tac)
wenzelm@2503
   111
  end;
wenzelm@2503
   112
wenzelm@2503
   113
fun (Simpset {mss, simps, congs, subgoal_tac, finish_tac, loop_tac})
wenzelm@2503
   114
    addeqcongs newcongs =
wenzelm@2503
   115
  make_ss (Thm.add_congs (mss, newcongs),
wenzelm@2503
   116
    simps, newcongs @ congs, subgoal_tac, finish_tac, loop_tac);
wenzelm@2503
   117
wenzelm@2503
   118
wenzelm@2503
   119
(* merge simpsets *)
wenzelm@2503
   120
wenzelm@2503
   121
(*prefers first simpset*)
wenzelm@2503
   122
fun merge_ss (Simpset {mss, simps, congs, subgoal_tac, finish_tac, loop_tac},
wenzelm@2503
   123
    Simpset {simps = simps2, congs = congs2, ...}) =
wenzelm@2503
   124
  let
wenzelm@2503
   125
    val simps' = gen_union eq_thm (simps, simps2);
wenzelm@2503
   126
    val congs' = gen_union eq_thm (congs, congs2);
wenzelm@2503
   127
    val mss' = Thm.set_mk_rews (empty_mss, Thm.mk_rews_of_mss mss);
wenzelm@2503
   128
    val mss' = Thm.add_simps (mss', simps');
wenzelm@2503
   129
    val mss' = Thm.add_congs (mss', congs');
wenzelm@2503
   130
  in
wenzelm@2503
   131
    make_ss (mss', simps', congs', subgoal_tac, finish_tac, loop_tac)
wenzelm@2503
   132
  end;
wenzelm@2503
   133
wenzelm@2503
   134
wenzelm@2503
   135
(* the current simpset *)
clasohm@0
   136
clasohm@1243
   137
val simpset = ref empty_ss;
clasohm@0
   138
wenzelm@2503
   139
fun Addsimps rews = (simpset := ! simpset addsimps rews);
wenzelm@2503
   140
fun Delsimps rews = (simpset := ! simpset delsimps rews);
clasohm@0
   141
clasohm@0
   142
clasohm@1243
   143
wenzelm@2503
   144
(** simplification tactics **)
clasohm@0
   145
nipkow@1
   146
fun NEWSUBGOALS tac tacf =
wenzelm@2503
   147
  STATE (fn state0 =>
wenzelm@2503
   148
    tac THEN STATE (fn state1 => tacf (nprems_of state1 - nprems_of state0)));
nipkow@1
   149
nipkow@217
   150
fun gen_simp_tac mode =
clasohm@1260
   151
  fn (Simpset{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) =>
clasohm@0
   152
  let fun solve_all_tac mss =
clasohm@1260
   153
        let val ss = Simpset{mss=mss,simps=simps,congs=congs,
clasohm@1260
   154
                             subgoal_tac=subgoal_tac,
clasohm@1260
   155
                             finish_tac=finish_tac, loop_tac=loop_tac}
nipkow@1
   156
            val solve1_tac =
nipkow@1
   157
              NEWSUBGOALS (subgoal_tac ss 1)
nipkow@1
   158
                          (fn n => if n<0 then all_tac else no_tac)
nipkow@1
   159
        in DEPTH_SOLVE(solve1_tac) end
clasohm@0
   160
paulson@1512
   161
      fun simp_loop_tac i thm =
wenzelm@2503
   162
          (asm_rewrite_goal_tac mode solve_all_tac mss i THEN
wenzelm@2503
   163
           (finish_tac (prems_of_mss mss) i  ORELSE  looper i))  thm
nipkow@1
   164
      and allsimp i n = EVERY(map (fn j => simp_loop_tac (i+j)) (n downto 0))
nipkow@1
   165
      and looper i = TRY(NEWSUBGOALS (loop_tac i) (allsimp i))
nipkow@217
   166
  in simp_loop_tac end;
clasohm@0
   167
wenzelm@2503
   168
val          simp_tac = gen_simp_tac (false, false);
wenzelm@2503
   169
val      asm_simp_tac = gen_simp_tac (false, true);
wenzelm@2503
   170
val     full_simp_tac = gen_simp_tac (true,  false);
wenzelm@2503
   171
val asm_full_simp_tac = gen_simp_tac (true,  true);
clasohm@0
   172
wenzelm@2503
   173
fun          Simp_tac i =          simp_tac (! simpset) i;
wenzelm@2503
   174
fun      Asm_simp_tac i =      asm_simp_tac (! simpset) i;
wenzelm@2503
   175
fun     Full_simp_tac i =     full_simp_tac (! simpset) i;
wenzelm@2503
   176
fun Asm_full_simp_tac i = asm_full_simp_tac (! simpset) i;
nipkow@406
   177
clasohm@1243
   178
end;