src/Provers/simplifier.ML
author nipkow
Fri Oct 29 11:54:50 1993 +0100 (1993-10-29 ago)
changeset 88 9df4dfedee01
parent 17 b35851cafd3e
child 146 dbee71d43339
permissions -rw-r--r--
added infix delsimps
nipkow@1
     1
(*  Title:      Provers/simplifier
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
 
nipkow@1
     8
*)
nipkow@88
     9
infix addsimps addeqcongs delsimps
clasohm@0
    10
      setsolver setloop setmksimps setsubgoaler;
clasohm@0
    11
clasohm@0
    12
signature SIMPLIFIER =
clasohm@0
    13
sig
clasohm@0
    14
  type simpset
nipkow@1
    15
  val addeqcongs: simpset * thm list -> simpset
clasohm@0
    16
  val addsimps: simpset * thm list -> simpset
nipkow@88
    17
  val delsimps: simpset * thm list -> simpset
clasohm@0
    18
  val asm_full_simp_tac: simpset -> int -> tactic
clasohm@0
    19
  val asm_simp_tac: simpset -> int -> tactic
clasohm@0
    20
  val empty_ss: simpset
clasohm@0
    21
  val merge_ss: simpset * simpset -> simpset
clasohm@0
    22
  val prems_of_ss: simpset -> thm list
clasohm@0
    23
  val rep_ss: simpset -> {simps: thm list, congs: thm list}
clasohm@0
    24
  val setsolver: simpset * (thm list -> int -> tactic) -> simpset
clasohm@0
    25
  val setloop: simpset * (int -> tactic) -> simpset
clasohm@0
    26
  val setmksimps: simpset * (thm -> thm list) -> simpset
clasohm@0
    27
  val setsubgoaler: simpset * (simpset -> int -> tactic) -> simpset
clasohm@0
    28
  val simp_tac: simpset -> int -> tactic
clasohm@0
    29
end;
clasohm@0
    30
clasohm@0
    31
structure Simplifier:SIMPLIFIER =
clasohm@0
    32
struct
clasohm@0
    33
clasohm@0
    34
datatype simpset =
clasohm@0
    35
  SS of {mss: meta_simpset,
clasohm@0
    36
         simps: thm list,
clasohm@0
    37
         congs: thm list,
clasohm@0
    38
         subgoal_tac: simpset -> int -> tactic,
clasohm@0
    39
         finish_tac: thm list -> int -> tactic,
clasohm@0
    40
         loop_tac: int -> tactic};
clasohm@0
    41
clasohm@0
    42
val empty_ss =
clasohm@0
    43
  SS{mss=empty_mss,
clasohm@0
    44
     simps= [],
clasohm@0
    45
     congs= [],
clasohm@0
    46
     subgoal_tac= K(K no_tac),
clasohm@0
    47
     finish_tac= K(K no_tac),
clasohm@0
    48
     loop_tac= K no_tac};
clasohm@0
    49
clasohm@0
    50
clasohm@0
    51
fun (SS{mss,simps,congs,subgoal_tac,finish_tac,...}) setloop loop_tac =
clasohm@0
    52
  SS{mss=mss,
clasohm@0
    53
     simps= simps,
clasohm@0
    54
     congs= congs,
clasohm@0
    55
     subgoal_tac= subgoal_tac,
clasohm@0
    56
     finish_tac=finish_tac,
clasohm@0
    57
     loop_tac=loop_tac};
clasohm@0
    58
clasohm@0
    59
fun (SS{mss,simps,congs,subgoal_tac,loop_tac,...}) setsolver finish_tac =
clasohm@0
    60
  SS{mss=mss,
clasohm@0
    61
     simps= simps,
clasohm@0
    62
     congs= congs,
clasohm@0
    63
     subgoal_tac= subgoal_tac,
clasohm@0
    64
     finish_tac=finish_tac,
clasohm@0
    65
     loop_tac=loop_tac};
clasohm@0
    66
clasohm@0
    67
fun (SS{mss,simps,congs,finish_tac,loop_tac,...}) setsubgoaler subgoal_tac =
clasohm@0
    68
  SS{mss=mss,
clasohm@0
    69
     simps= simps,
clasohm@0
    70
     congs= congs,
clasohm@0
    71
     subgoal_tac= subgoal_tac,
clasohm@0
    72
     finish_tac=finish_tac,
clasohm@0
    73
     loop_tac=loop_tac};
clasohm@0
    74
clasohm@0
    75
fun (SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) setmksimps mk_simps =
clasohm@0
    76
  SS{mss=Thm.set_mk_rews(mss,mk_simps),
clasohm@0
    77
     simps= simps,
clasohm@0
    78
     congs= congs,
clasohm@0
    79
     subgoal_tac= subgoal_tac,
clasohm@0
    80
     finish_tac=finish_tac,
clasohm@0
    81
     loop_tac=loop_tac};
clasohm@0
    82
clasohm@0
    83
fun (SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) addsimps rews =
nipkow@88
    84
  let val rews' = flat(map (Thm.mk_rews_of_mss mss) rews) in
clasohm@0
    85
  SS{mss= Thm.add_simps(mss,rews'),
clasohm@0
    86
     simps= rews' @ simps,
clasohm@0
    87
     congs= congs,
clasohm@0
    88
     subgoal_tac= subgoal_tac,
clasohm@0
    89
     finish_tac=finish_tac,
clasohm@0
    90
     loop_tac=loop_tac}
clasohm@0
    91
  end;
clasohm@0
    92
nipkow@88
    93
fun (SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) delsimps rews =
nipkow@88
    94
  let val rews' = flat(map (Thm.mk_rews_of_mss mss) rews) in
nipkow@88
    95
  SS{mss= Thm.del_simps(mss,rews'),
nipkow@88
    96
     simps= foldl (gen_rem eq_thm) (simps,rews'),
nipkow@88
    97
     congs= congs,
nipkow@88
    98
     subgoal_tac= subgoal_tac,
nipkow@88
    99
     finish_tac=finish_tac,
nipkow@88
   100
     loop_tac=loop_tac}
nipkow@88
   101
  end;
nipkow@88
   102
nipkow@1
   103
fun (SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) addeqcongs newcongs =
clasohm@0
   104
  SS{mss= Thm.add_congs(mss,newcongs),
clasohm@0
   105
     simps= simps,
clasohm@0
   106
     congs= newcongs @ congs,
clasohm@0
   107
     subgoal_tac= subgoal_tac,
clasohm@0
   108
     finish_tac=finish_tac,
clasohm@0
   109
     loop_tac=loop_tac};
clasohm@0
   110
clasohm@0
   111
fun merge_ss(SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac},
clasohm@0
   112
             SS{simps=simps2,congs=congs2,...}) =
clasohm@0
   113
  let val simps' = gen_union eq_thm (simps,simps2)
clasohm@0
   114
      val congs' = gen_union eq_thm (congs,congs2)
clasohm@0
   115
      val mss' = Thm.set_mk_rews(empty_mss,Thm.mk_rews_of_mss mss)
clasohm@0
   116
      val mss' = Thm.add_simps(mss',simps')
clasohm@0
   117
      val mss' = Thm.add_congs(mss',congs')
clasohm@0
   118
  in SS{mss=         mss',
clasohm@0
   119
        simps=       simps,
clasohm@0
   120
        congs=       congs',
clasohm@0
   121
        subgoal_tac= subgoal_tac,
clasohm@0
   122
        finish_tac=  finish_tac,
clasohm@0
   123
        loop_tac=    loop_tac}
clasohm@0
   124
  end;
clasohm@0
   125
clasohm@0
   126
fun prems_of_ss(SS{mss,...}) = prems_of_mss mss;
clasohm@0
   127
clasohm@0
   128
fun rep_ss(SS{simps,congs,...}) = {simps=simps,congs=congs};
clasohm@0
   129
clasohm@0
   130
fun add_asms (SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) prems =
clasohm@0
   131
  let val rews = flat(map (mk_rews_of_mss mss) prems)
clasohm@0
   132
  in SS{mss= add_prems(add_simps(mss,rews),prems), simps=simps, congs=congs,
clasohm@0
   133
        subgoal_tac=subgoal_tac,finish_tac=finish_tac,
clasohm@0
   134
        loop_tac=loop_tac}
clasohm@0
   135
  end;
clasohm@0
   136
nipkow@1
   137
fun NEWSUBGOALS tac tacf =
nipkow@1
   138
  STATE(fn state0 =>
nipkow@1
   139
    tac THEN STATE(fn state1 => tacf(nprems_of state1 - nprems_of state0)));
nipkow@1
   140
clasohm@0
   141
fun asm_full_simp_tac(SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) =
clasohm@0
   142
  let fun solve_all_tac mss =
clasohm@0
   143
        let val ss = SS{mss=mss,simps=simps,congs=congs,
clasohm@0
   144
                        subgoal_tac=subgoal_tac,
clasohm@0
   145
                        finish_tac=finish_tac, loop_tac=loop_tac}
nipkow@1
   146
            val solve1_tac =
nipkow@1
   147
              NEWSUBGOALS (subgoal_tac ss 1)
nipkow@1
   148
                          (fn n => if n<0 then all_tac else no_tac)
nipkow@1
   149
        in DEPTH_SOLVE(solve1_tac) end
clasohm@0
   150
clasohm@0
   151
      fun simp_loop i thm =
clasohm@0
   152
        tapply(asm_rewrite_goal_tac solve_all_tac mss i THEN
nipkow@1
   153
               (finish_tac (prems_of_mss mss) i  ORELSE  looper i),
clasohm@0
   154
               thm)
nipkow@1
   155
      and allsimp i n = EVERY(map (fn j => simp_loop_tac (i+j)) (n downto 0))
nipkow@1
   156
      and looper i = TRY(NEWSUBGOALS (loop_tac i) (allsimp i))
clasohm@0
   157
      and simp_loop_tac i = Tactic(simp_loop i)
clasohm@0
   158
nipkow@17
   159
  in fn i => COND (has_fewer_prems 0) no_tac (simp_loop_tac i) end;
clasohm@0
   160
clasohm@0
   161
fun asm_simp_tac ss =
clasohm@0
   162
      METAHYPS(fn prems => asm_full_simp_tac (add_asms ss prems) 1);
clasohm@0
   163
clasohm@0
   164
fun simp_tac ss = METAHYPS(fn _ => asm_full_simp_tac ss 1);
clasohm@0
   165
clasohm@0
   166
end;