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