src/Provers/simplifier.ML
author lcp
Thu Mar 30 13:48:30 1995 +0200 (1995-03-30 ago)
changeset 983 6f80fed73e29
parent 433 1e4f420523ae
child 1243 fa09705a5890
permissions -rw-r--r--
Precedence of infixes is now 4 (just above that of :=)
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
*)
lcp@983
     9
infix 4 addsimps addeqcongs delsimps
lcp@983
    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
nipkow@406
    31
functor SimplifierFUN():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,
nipkow@433
    57
     loop_tac= DETERM o 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
nipkow@1
   130
fun NEWSUBGOALS tac tacf =
nipkow@1
   131
  STATE(fn state0 =>
nipkow@1
   132
    tac THEN STATE(fn state1 => tacf(nprems_of state1 - nprems_of state0)));
nipkow@1
   133
nipkow@217
   134
fun gen_simp_tac mode =
nipkow@217
   135
  fn (SS{mss,simps,congs,subgoal_tac,finish_tac,loop_tac}) =>
clasohm@0
   136
  let fun solve_all_tac mss =
clasohm@0
   137
        let val ss = SS{mss=mss,simps=simps,congs=congs,
clasohm@0
   138
                        subgoal_tac=subgoal_tac,
clasohm@0
   139
                        finish_tac=finish_tac, loop_tac=loop_tac}
nipkow@1
   140
            val solve1_tac =
nipkow@1
   141
              NEWSUBGOALS (subgoal_tac ss 1)
nipkow@1
   142
                          (fn n => if n<0 then all_tac else no_tac)
nipkow@1
   143
        in DEPTH_SOLVE(solve1_tac) end
clasohm@0
   144
clasohm@0
   145
      fun simp_loop i thm =
nipkow@217
   146
        tapply(asm_rewrite_goal_tac mode solve_all_tac mss i THEN
nipkow@1
   147
               (finish_tac (prems_of_mss mss) i  ORELSE  looper i),
clasohm@0
   148
               thm)
nipkow@1
   149
      and allsimp i n = EVERY(map (fn j => simp_loop_tac (i+j)) (n downto 0))
nipkow@1
   150
      and looper i = TRY(NEWSUBGOALS (loop_tac i) (allsimp i))
clasohm@0
   151
      and simp_loop_tac i = Tactic(simp_loop i)
clasohm@0
   152
nipkow@217
   153
  in simp_loop_tac end;
clasohm@0
   154
nipkow@217
   155
val asm_full_simp_tac = gen_simp_tac (true,true);
nipkow@217
   156
val asm_simp_tac = gen_simp_tac (false,true);
nipkow@217
   157
val simp_tac = gen_simp_tac (false,false);
clasohm@0
   158
clasohm@0
   159
end;
nipkow@406
   160
nipkow@406
   161
structure Simplifier = SimplifierFUN();