src/FOLP/simp.ML
author skalberg
Thu Mar 03 12:43:01 2005 +0100 (2005-03-03)
changeset 15570 8d8c70b41bab
parent 15531 08c8dad8e399
child 15574 b1d1b5bfc464
permissions -rw-r--r--
Move towards standard functions.
clasohm@0
     1
(*  Title:      FOLP/simp
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
FOLP version of...
clasohm@0
     7
clasohm@0
     8
Generic simplifier, suitable for most logics.  (from Provers)
clasohm@0
     9
clasohm@0
    10
This version allows instantiation of Vars in the subgoal, since the proof
clasohm@0
    11
term must change.
clasohm@0
    12
*)
clasohm@0
    13
clasohm@0
    14
signature SIMP_DATA =
clasohm@0
    15
sig
clasohm@0
    16
  val case_splits  : (thm * string) list
clasohm@0
    17
  val dest_red     : term -> term * term * term
clasohm@0
    18
  val mk_rew_rules : thm -> thm list
clasohm@0
    19
  val norm_thms    : (thm*thm) list (* [(?x>>norm(?x), norm(?x)>>?x), ...] *)
clasohm@0
    20
  val red1         : thm        (*  ?P>>?Q  ==>  ?P  ==>  ?Q  *)
clasohm@0
    21
  val red2         : thm        (*  ?P>>?Q  ==>  ?Q  ==>  ?P  *)
clasohm@0
    22
  val refl_thms    : thm list
clasohm@0
    23
  val subst_thms   : thm list   (* [ ?a>>?b ==> ?P(?a) ==> ?P(?b), ...] *)
clasohm@0
    24
  val trans_thms   : thm list
clasohm@0
    25
end;
clasohm@0
    26
clasohm@0
    27
clasohm@0
    28
infix 4 addrews addcongs delrews delcongs setauto;
clasohm@0
    29
clasohm@0
    30
signature SIMP =
clasohm@0
    31
sig
clasohm@0
    32
  type simpset
clasohm@0
    33
  val empty_ss  : simpset
clasohm@0
    34
  val addcongs  : simpset * thm list -> simpset
clasohm@0
    35
  val addrews   : simpset * thm list -> simpset
clasohm@0
    36
  val delcongs  : simpset * thm list -> simpset
clasohm@0
    37
  val delrews   : simpset * thm list -> simpset
clasohm@0
    38
  val dest_ss   : simpset -> thm list * thm list
clasohm@0
    39
  val print_ss  : simpset -> unit
clasohm@0
    40
  val setauto   : simpset * (int -> tactic) -> simpset
clasohm@0
    41
  val ASM_SIMP_CASE_TAC : simpset -> int -> tactic
clasohm@0
    42
  val ASM_SIMP_TAC      : simpset -> int -> tactic
clasohm@0
    43
  val CASE_TAC          : simpset -> int -> tactic
clasohm@0
    44
  val SIMP_CASE2_TAC    : simpset -> int -> tactic
clasohm@0
    45
  val SIMP_THM          : simpset -> thm -> thm
clasohm@0
    46
  val SIMP_TAC          : simpset -> int -> tactic
clasohm@0
    47
  val SIMP_CASE_TAC     : simpset -> int -> tactic
clasohm@0
    48
  val mk_congs          : theory -> string list -> thm list
clasohm@0
    49
  val mk_typed_congs    : theory -> (string * string) list -> thm list
clasohm@0
    50
(* temporarily disabled:
clasohm@0
    51
  val extract_free_congs        : unit -> thm list
clasohm@0
    52
*)
clasohm@0
    53
  val tracing   : bool ref
clasohm@0
    54
end;
clasohm@0
    55
clasohm@0
    56
functor SimpFun (Simp_data: SIMP_DATA) : SIMP = 
clasohm@0
    57
struct
clasohm@0
    58
clasohm@0
    59
local open Simp_data Logic in
clasohm@0
    60
clasohm@0
    61
(*For taking apart reductions into left, right hand sides*)
clasohm@0
    62
val lhs_of = #2 o dest_red;
clasohm@0
    63
val rhs_of = #3 o dest_red;
clasohm@0
    64
clasohm@0
    65
(*** Indexing and filtering of theorems ***)
clasohm@0
    66
wenzelm@13105
    67
fun eq_brl ((b1,th1),(b2,th2)) = b1=b2 andalso Drule.eq_thm_prop (th1,th2);
clasohm@0
    68
clasohm@0
    69
(*insert a thm in a discrimination net by its lhs*)
clasohm@0
    70
fun lhs_insert_thm (th,net) =
clasohm@0
    71
    Net.insert_term((lhs_of (concl_of th), (false,th)), net, eq_brl)
clasohm@0
    72
    handle  Net.INSERT => net;
clasohm@0
    73
clasohm@0
    74
(*match subgoal i against possible theorems in the net.
clasohm@0
    75
  Similar to match_from_nat_tac, but the net does not contain numbers;
clasohm@0
    76
  rewrite rules are not ordered.*)
clasohm@0
    77
fun net_tac net =
clasohm@0
    78
  SUBGOAL(fn (prem,i) => 
clasohm@1459
    79
          resolve_tac (Net.unify_term net (strip_assums_concl prem)) i);
clasohm@0
    80
clasohm@0
    81
(*match subgoal i against possible theorems indexed by lhs in the net*)
clasohm@0
    82
fun lhs_net_tac net =
clasohm@0
    83
  SUBGOAL(fn (prem,i) => 
clasohm@1459
    84
          biresolve_tac (Net.unify_term net
clasohm@1459
    85
                       (lhs_of (strip_assums_concl prem))) i);
clasohm@0
    86
skalberg@15570
    87
fun nth_subgoal i thm = List.nth(prems_of thm,i-1);
clasohm@0
    88
clasohm@0
    89
fun goal_concl i thm = strip_assums_concl(nth_subgoal i thm);
clasohm@0
    90
clasohm@0
    91
fun lhs_of_eq i thm = lhs_of(goal_concl i thm)
clasohm@0
    92
and rhs_of_eq i thm = rhs_of(goal_concl i thm);
clasohm@0
    93
clasohm@0
    94
fun var_lhs(thm,i) =
clasohm@0
    95
let fun var(Var _) = true
clasohm@0
    96
      | var(Abs(_,_,t)) = var t
clasohm@0
    97
      | var(f$_) = var f
clasohm@0
    98
      | var _ = false;
clasohm@0
    99
in var(lhs_of_eq i thm) end;
clasohm@0
   100
clasohm@0
   101
fun contains_op opns =
clasohm@0
   102
    let fun contains(Const(s,_)) = s mem opns |
clasohm@0
   103
            contains(s$t) = contains s orelse contains t |
clasohm@0
   104
            contains(Abs(_,_,t)) = contains t |
clasohm@0
   105
            contains _ = false;
clasohm@0
   106
    in contains end;
clasohm@0
   107
clasohm@0
   108
fun may_match(match_ops,i) = contains_op match_ops o lhs_of_eq i;
clasohm@0
   109
clasohm@0
   110
val (normI_thms,normE_thms) = split_list norm_thms;
clasohm@0
   111
clasohm@0
   112
(*Get the norm constants from norm_thms*)
clasohm@0
   113
val norms =
clasohm@0
   114
  let fun norm thm = 
clasohm@0
   115
      case lhs_of(concl_of thm) of
clasohm@1459
   116
          Const(n,_)$_ => n
clasohm@1459
   117
        | _ => (prths normE_thms; error"No constant in lhs of a norm_thm")
clasohm@0
   118
  in map norm normE_thms end;
clasohm@0
   119
clasohm@0
   120
fun lhs_is_NORM(thm,i) = case lhs_of_eq i thm of
clasohm@1459
   121
        Const(s,_)$_ => s mem norms | _ => false;
clasohm@0
   122
clasohm@0
   123
val refl_tac = resolve_tac refl_thms;
clasohm@0
   124
clasohm@0
   125
fun find_res thms thm =
clasohm@0
   126
    let fun find [] = (prths thms; error"Check Simp_Data")
wenzelm@6969
   127
          | find(th::thms) = thm RS th handle THM _ => find thms
clasohm@0
   128
    in find thms end;
clasohm@0
   129
clasohm@0
   130
val mk_trans = find_res trans_thms;
clasohm@0
   131
clasohm@0
   132
fun mk_trans2 thm =
clasohm@0
   133
let fun mk[] = error"Check transitivity"
wenzelm@6969
   134
      | mk(t::ts) = (thm RSN (2,t))  handle THM _  => mk ts
clasohm@0
   135
in mk trans_thms end;
clasohm@0
   136
clasohm@0
   137
(*Applies tactic and returns the first resulting state, FAILS if none!*)
wenzelm@4271
   138
fun one_result(tac,thm) = case Seq.pull(tac thm) of
skalberg@15531
   139
        SOME(thm',_) => thm'
skalberg@15531
   140
      | NONE => raise THM("Simplifier: could not continue", 0, [thm]);
clasohm@0
   141
clasohm@0
   142
fun res1(thm,thms,i) = one_result(resolve_tac thms i,thm);
clasohm@0
   143
clasohm@0
   144
clasohm@0
   145
(**** Adding "NORM" tags ****)
clasohm@0
   146
clasohm@0
   147
(*get name of the constant from conclusion of a congruence rule*)
clasohm@0
   148
fun cong_const cong = 
clasohm@0
   149
    case head_of (lhs_of (concl_of cong)) of
clasohm@1459
   150
        Const(c,_) => c
clasohm@1459
   151
      | _ => ""                 (*a placeholder distinct from const names*);
clasohm@0
   152
clasohm@0
   153
(*true if the term is an atomic proposition (no ==> signs) *)
clasohm@0
   154
val atomic = null o strip_assums_hyp;
clasohm@0
   155
clasohm@0
   156
(*ccs contains the names of the constants possessing congruence rules*)
clasohm@0
   157
fun add_hidden_vars ccs =
clasohm@0
   158
  let fun add_hvars(tm,hvars) = case tm of
clasohm@1459
   159
              Abs(_,_,body) => add_term_vars(body,hvars)
clasohm@1459
   160
            | _$_ => let val (f,args) = strip_comb tm 
clasohm@1459
   161
                     in case f of
clasohm@1459
   162
                            Const(c,T) => 
clasohm@1459
   163
                                if c mem ccs
skalberg@15570
   164
                                then Library.foldr add_hvars (args,hvars)
clasohm@1459
   165
                                else add_term_vars(tm,hvars)
clasohm@1459
   166
                          | _ => add_term_vars(tm,hvars)
clasohm@1459
   167
                     end
clasohm@1459
   168
            | _ => hvars;
clasohm@0
   169
  in add_hvars end;
clasohm@0
   170
clasohm@0
   171
fun add_new_asm_vars new_asms =
clasohm@0
   172
    let fun itf((tm,at),vars) =
clasohm@1459
   173
                if at then vars else add_term_vars(tm,vars)
clasohm@1459
   174
        fun add_list(tm,al,vars) = let val (_,tml) = strip_comb tm
clasohm@1459
   175
                in if length(tml)=length(al)
skalberg@15570
   176
                   then Library.foldr itf (tml~~al,vars)
clasohm@1459
   177
                   else vars
clasohm@1459
   178
                end
clasohm@1459
   179
        fun add_vars (tm,vars) = case tm of
clasohm@1459
   180
                  Abs (_,_,body) => add_vars(body,vars)
clasohm@1459
   181
                | r$s => (case head_of tm of
clasohm@1459
   182
                          Const(c,T) => (case assoc(new_asms,c) of
skalberg@15531
   183
                                  NONE => add_vars(r,add_vars(s,vars))
skalberg@15531
   184
                                | SOME(al) => add_list(tm,al,vars))
clasohm@1459
   185
                        | _ => add_vars(r,add_vars(s,vars)))
clasohm@1459
   186
                | _ => vars
clasohm@0
   187
    in add_vars end;
clasohm@0
   188
clasohm@0
   189
clasohm@0
   190
fun add_norms(congs,ccs,new_asms) thm =
clasohm@0
   191
let val thm' = mk_trans2 thm;
clasohm@0
   192
(* thm': [?z -> l; Prems; r -> ?t] ==> ?z -> ?t *)
clasohm@0
   193
    val nops = nprems_of thm'
clasohm@0
   194
    val lhs = rhs_of_eq 1 thm'
clasohm@0
   195
    val rhs = lhs_of_eq nops thm'
clasohm@0
   196
    val asms = tl(rev(tl(prems_of thm')))
skalberg@15570
   197
    val hvars = Library.foldr (add_hidden_vars ccs) (lhs::rhs::asms,[])
clasohm@0
   198
    val hvars = add_new_asm_vars new_asms (rhs,hvars)
clasohm@0
   199
    fun it_asms (asm,hvars) =
clasohm@1459
   200
        if atomic asm then add_new_asm_vars new_asms (asm,hvars)
clasohm@1459
   201
        else add_term_frees(asm,hvars)
skalberg@15570
   202
    val hvars = Library.foldr it_asms (asms,hvars)
clasohm@0
   203
    val hvs = map (#1 o dest_Var) hvars
clasohm@0
   204
    val refl1_tac = refl_tac 1
paulson@3537
   205
    fun norm_step_tac st = st |>
paulson@3537
   206
	 (case head_of(rhs_of_eq 1 st) of
paulson@3537
   207
	    Var(ixn,_) => if ixn mem hvs then refl1_tac
paulson@3537
   208
			  else resolve_tac normI_thms 1 ORELSE refl1_tac
paulson@3537
   209
	  | Const _ => resolve_tac normI_thms 1 ORELSE
paulson@3537
   210
		       resolve_tac congs 1 ORELSE refl1_tac
paulson@3537
   211
	  | Free _ => resolve_tac congs 1 ORELSE refl1_tac
paulson@3537
   212
	  | _ => refl1_tac)
paulson@3537
   213
    val add_norm_tac = DEPTH_FIRST (has_fewer_prems nops) norm_step_tac
skalberg@15531
   214
    val SOME(thm'',_) = Seq.pull(add_norm_tac thm')
clasohm@0
   215
in thm'' end;
clasohm@0
   216
clasohm@0
   217
fun add_norm_tags congs =
clasohm@0
   218
    let val ccs = map cong_const congs
skalberg@15570
   219
        val new_asms = List.filter (exists not o #2)
clasohm@1459
   220
                (ccs ~~ (map (map atomic o prems_of) congs));
clasohm@0
   221
    in add_norms(congs,ccs,new_asms) end;
clasohm@0
   222
clasohm@0
   223
fun normed_rews congs =
clasohm@0
   224
  let val add_norms = add_norm_tags congs;
clasohm@0
   225
  in fn thm => map (varifyT o add_norms o mk_trans) (mk_rew_rules(freezeT thm))
clasohm@0
   226
  end;
clasohm@0
   227
clasohm@1459
   228
fun NORM norm_lhs_tac = EVERY'[rtac red2 , norm_lhs_tac, refl_tac];
clasohm@0
   229
clasohm@0
   230
val trans_norms = map mk_trans normE_thms;
clasohm@0
   231
clasohm@0
   232
clasohm@0
   233
(* SIMPSET *)
clasohm@0
   234
clasohm@0
   235
datatype simpset =
clasohm@1459
   236
        SS of {auto_tac: int -> tactic,
clasohm@1459
   237
               congs: thm list,
clasohm@1459
   238
               cong_net: thm Net.net,
clasohm@1459
   239
               mk_simps: thm -> thm list,
clasohm@1459
   240
               simps: (thm * thm list) list,
clasohm@1459
   241
               simp_net: thm Net.net}
clasohm@0
   242
clasohm@0
   243
val empty_ss = SS{auto_tac= K no_tac, congs=[], cong_net=Net.empty,
clasohm@1459
   244
                  mk_simps=normed_rews[], simps=[], simp_net=Net.empty};
clasohm@0
   245
clasohm@0
   246
(** Insertion of congruences and rewrites **)
clasohm@0
   247
clasohm@0
   248
(*insert a thm in a thm net*)
clasohm@0
   249
fun insert_thm_warn (th,net) = 
wenzelm@13105
   250
  Net.insert_term((concl_of th, th), net, Drule.eq_thm_prop)
clasohm@0
   251
  handle Net.INSERT => 
clasohm@0
   252
    (writeln"\nDuplicate rewrite or congruence rule:"; print_thm th;
clasohm@0
   253
     net);
clasohm@0
   254
skalberg@15570
   255
val insert_thms = Library.foldr insert_thm_warn;
clasohm@0
   256
clasohm@0
   257
fun addrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thm) =
clasohm@0
   258
let val thms = mk_simps thm
clasohm@0
   259
in SS{auto_tac=auto_tac,congs=congs, cong_net=cong_net, mk_simps=mk_simps,
clasohm@0
   260
      simps = (thm,thms)::simps, simp_net = insert_thms(thms,simp_net)}
clasohm@0
   261
end;
clasohm@0
   262
skalberg@15570
   263
val op addrews = Library.foldl addrew;
clasohm@0
   264
clasohm@0
   265
fun op addcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thms) =
clasohm@0
   266
let val congs' = thms @ congs;
clasohm@0
   267
in SS{auto_tac=auto_tac, congs= congs',
clasohm@0
   268
      cong_net= insert_thms (map mk_trans thms,cong_net),
clasohm@0
   269
      mk_simps= normed_rews congs', simps=simps, simp_net=simp_net}
clasohm@0
   270
end;
clasohm@0
   271
clasohm@0
   272
(** Deletion of congruences and rewrites **)
clasohm@0
   273
clasohm@0
   274
(*delete a thm from a thm net*)
clasohm@0
   275
fun delete_thm_warn (th,net) = 
wenzelm@13105
   276
  Net.delete_term((concl_of th, th), net, Drule.eq_thm_prop)
clasohm@0
   277
  handle Net.DELETE => 
clasohm@0
   278
    (writeln"\nNo such rewrite or congruence rule:";  print_thm th;
clasohm@0
   279
     net);
clasohm@0
   280
skalberg@15570
   281
val delete_thms = Library.foldr delete_thm_warn;
clasohm@0
   282
clasohm@0
   283
fun op delcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thms) =
skalberg@15570
   284
let val congs' = Library.foldl (gen_rem Drule.eq_thm_prop) (congs,thms)
clasohm@0
   285
in SS{auto_tac=auto_tac, congs= congs',
clasohm@0
   286
      cong_net= delete_thms(map mk_trans thms,cong_net),
clasohm@0
   287
      mk_simps= normed_rews congs', simps=simps, simp_net=simp_net}
clasohm@0
   288
end;
clasohm@0
   289
clasohm@0
   290
fun delrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thm) =
clasohm@0
   291
let fun find((p as (th,ths))::ps',ps) =
wenzelm@13105
   292
          if Drule.eq_thm_prop(thm,th) then (ths,ps@ps') else find(ps',p::ps)
clasohm@0
   293
      | find([],simps') = (writeln"\nNo such rewrite or congruence rule:";
clasohm@1459
   294
                           print_thm thm;
clasohm@1459
   295
                           ([],simps'))
clasohm@0
   296
    val (thms,simps') = find(simps,[])
clasohm@0
   297
in SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
clasohm@0
   298
      simps = simps', simp_net = delete_thms(thms,simp_net)}
clasohm@0
   299
end;
clasohm@0
   300
skalberg@15570
   301
val op delrews = Library.foldl delrew;
clasohm@0
   302
clasohm@0
   303
clasohm@0
   304
fun op setauto(SS{congs,cong_net,mk_simps,simps,simp_net,...}, auto_tac) =
clasohm@0
   305
    SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
clasohm@0
   306
       simps=simps, simp_net=simp_net};
clasohm@0
   307
clasohm@0
   308
clasohm@0
   309
(** Inspection of a simpset **)
clasohm@0
   310
clasohm@0
   311
fun dest_ss(SS{congs,simps,...}) = (congs, map #1 simps);
clasohm@0
   312
clasohm@0
   313
fun print_ss(SS{congs,simps,...}) =
clasohm@1459
   314
        (writeln"Congruences:"; prths congs;
clasohm@1459
   315
         writeln"Rewrite Rules:"; prths (map #1 simps); ());
clasohm@0
   316
clasohm@0
   317
clasohm@0
   318
(* Rewriting with conditionals *)
clasohm@0
   319
clasohm@0
   320
val (case_thms,case_consts) = split_list case_splits;
clasohm@0
   321
val case_rews = map mk_trans case_thms;
clasohm@0
   322
clasohm@0
   323
fun if_rewritable ifc i thm =
clasohm@0
   324
    let val tm = goal_concl i thm
clasohm@1459
   325
        fun nobound(Abs(_,_,tm),j,k) = nobound(tm,j,k+1)
clasohm@1459
   326
          | nobound(s$t,j,k) = nobound(s,j,k) andalso nobound(t,j,k)
clasohm@1459
   327
          | nobound(Bound n,j,k) = n < k orelse k+j <= n
clasohm@1459
   328
          | nobound(_) = true;
clasohm@1459
   329
        fun check_args(al,j) = forall (fn t => nobound(t,j,0)) al
clasohm@1459
   330
        fun find_if(Abs(_,_,tm),j) = find_if(tm,j+1)
clasohm@1459
   331
          | find_if(tm as s$t,j) = let val (f,al) = strip_comb tm in
clasohm@1459
   332
                case f of Const(c,_) => if c=ifc then check_args(al,j)
clasohm@1459
   333
                        else find_if(s,j) orelse find_if(t,j)
clasohm@1459
   334
                | _ => find_if(s,j) orelse find_if(t,j) end
clasohm@1459
   335
          | find_if(_) = false;
clasohm@0
   336
    in find_if(tm,0) end;
clasohm@0
   337
clasohm@0
   338
fun IF1_TAC cong_tac i =
paulson@1512
   339
    let fun seq_try (ifth::ifths,ifc::ifcs) thm = 
paulson@1512
   340
                (COND (if_rewritable ifc i) (DETERM(rtac ifth i))
paulson@1512
   341
                        (seq_try(ifths,ifcs))) thm
paulson@1512
   342
              | seq_try([],_) thm = no_tac thm
paulson@1512
   343
        and try_rew thm = (seq_try(case_rews,case_consts) ORELSE one_subt) thm
clasohm@1459
   344
        and one_subt thm =
clasohm@1459
   345
                let val test = has_fewer_prems (nprems_of thm + 1)
paulson@1512
   346
                    fun loop thm = 
paulson@1512
   347
			COND test no_tac
paulson@1512
   348
                          ((try_rew THEN DEPTH_FIRST test (refl_tac i))
paulson@1512
   349
			   ORELSE (refl_tac i THEN loop)) thm
paulson@1512
   350
                in (cong_tac THEN loop) thm end
paulson@1512
   351
    in COND (may_match(case_consts,i)) try_rew no_tac end;
clasohm@0
   352
clasohm@0
   353
fun CASE_TAC (SS{cong_net,...}) i =
clasohm@0
   354
let val cong_tac = net_tac cong_net i
clasohm@0
   355
in NORM (IF1_TAC cong_tac) i end;
clasohm@0
   356
clasohm@0
   357
(* Rewriting Automaton *)
clasohm@0
   358
clasohm@0
   359
datatype cntrl = STOP | MK_EQ | ASMS of int | SIMP_LHS | REW | REFL | TRUE
clasohm@1459
   360
               | PROVE | POP_CS | POP_ARTR | IF;
clasohm@0
   361
(*
wenzelm@5963
   362
fun pr_cntrl c = case c of STOP => std_output("STOP") | MK_EQ => std_output("MK_EQ") |
wenzelm@5963
   363
ASMS i => print_int i | POP_ARTR => std_output("POP_ARTR") |
wenzelm@5963
   364
SIMP_LHS => std_output("SIMP_LHS") | REW => std_output("REW") | REFL => std_output("REFL") |
wenzelm@5963
   365
TRUE => std_output("TRUE") | PROVE => std_output("PROVE") | POP_CS => std_output("POP_CS") | IF
wenzelm@5963
   366
=> std_output("IF");
clasohm@0
   367
*)
clasohm@0
   368
fun simp_refl([],_,ss) = ss
clasohm@0
   369
  | simp_refl(a'::ns,a,ss) = if a'=a then simp_refl(ns,a,SIMP_LHS::REFL::ss)
clasohm@1459
   370
        else simp_refl(ns,a,ASMS(a)::SIMP_LHS::REFL::POP_ARTR::ss);
clasohm@0
   371
clasohm@0
   372
(** Tracing **)
clasohm@0
   373
clasohm@0
   374
val tracing = ref false;
clasohm@0
   375
clasohm@0
   376
(*Replace parameters by Free variables in P*)
clasohm@0
   377
fun variants_abs ([],P) = P
clasohm@0
   378
  | variants_abs ((a,T)::aTs, P) =
clasohm@0
   379
      variants_abs (aTs, #2 (variant_abs(a,T,P)));
clasohm@0
   380
clasohm@0
   381
(*Select subgoal i from proof state; substitute parameters, for printing*)
clasohm@0
   382
fun prepare_goal i st =
clasohm@0
   383
    let val subgi = nth_subgoal i st
clasohm@1459
   384
        val params = rev(strip_params subgi)
clasohm@0
   385
    in variants_abs (params, strip_assums_concl subgi) end;
clasohm@0
   386
clasohm@0
   387
(*print lhs of conclusion of subgoal i*)
clasohm@0
   388
fun pr_goal_lhs i st =
clasohm@0
   389
    writeln (Sign.string_of_term (#sign(rep_thm st)) 
clasohm@1459
   390
             (lhs_of (prepare_goal i st)));
clasohm@0
   391
clasohm@0
   392
(*print conclusion of subgoal i*)
clasohm@0
   393
fun pr_goal_concl i st =
clasohm@0
   394
    writeln (Sign.string_of_term (#sign(rep_thm st)) (prepare_goal i st)) 
clasohm@0
   395
clasohm@0
   396
(*print subgoals i to j (inclusive)*)
clasohm@0
   397
fun pr_goals (i,j) st =
clasohm@0
   398
    if i>j then ()
clasohm@0
   399
    else (pr_goal_concl i st;  pr_goals (i+1,j) st);
clasohm@0
   400
clasohm@0
   401
(*Print rewrite for tracing; i=subgoal#, n=number of new subgoals,
clasohm@0
   402
  thm=old state, thm'=new state *)
clasohm@0
   403
fun pr_rew (i,n,thm,thm',not_asms) =
clasohm@0
   404
    if !tracing
clasohm@0
   405
    then (if not_asms then () else writeln"Assumption used in";
clasohm@0
   406
          pr_goal_lhs i thm; writeln"->"; pr_goal_lhs (i+n) thm';
clasohm@1459
   407
          if n>0 then (writeln"Conditions:"; pr_goals (i, i+n-1) thm')
clasohm@0
   408
          else ();
clasohm@0
   409
          writeln"" )
clasohm@0
   410
    else ();
clasohm@0
   411
clasohm@0
   412
(* Skip the first n hyps of a goal, and return the rest in generalized form *)
clasohm@0
   413
fun strip_varify(Const("==>", _) $ H $ B, n, vs) =
clasohm@1459
   414
        if n=0 then subst_bounds(vs,H)::strip_varify(B,0,vs)
clasohm@1459
   415
        else strip_varify(B,n-1,vs)
clasohm@0
   416
  | strip_varify(Const("all",_)$Abs(_,T,t), n, vs) =
clasohm@1459
   417
        strip_varify(t,n,Var(("?",length vs),T)::vs)
clasohm@0
   418
  | strip_varify  _  = [];
clasohm@0
   419
clasohm@0
   420
fun execute(ss,if_fl,auto_tac,cong_tac,net,i,thm) = let
clasohm@0
   421
clasohm@0
   422
fun simp_lhs(thm,ss,anet,ats,cs) =
clasohm@0
   423
    if var_lhs(thm,i) then (ss,thm,anet,ats,cs) else
clasohm@0
   424
    if lhs_is_NORM(thm,i) then (ss, res1(thm,trans_norms,i), anet,ats,cs)
wenzelm@4271
   425
    else case Seq.pull(cong_tac i thm) of
skalberg@15531
   426
            SOME(thm',_) =>
clasohm@1459
   427
                    let val ps = prems_of thm and ps' = prems_of thm';
clasohm@1459
   428
                        val n = length(ps')-length(ps);
skalberg@15570
   429
                        val a = length(strip_assums_hyp(List.nth(ps,i-1)))
clasohm@1459
   430
                        val l = map (fn p => length(strip_assums_hyp(p)))
skalberg@15570
   431
                                    (Library.take(n,Library.drop(i-1,ps')));
clasohm@1459
   432
                    in (simp_refl(rev(l),a,REW::ss),thm',anet,ats,cs) end
skalberg@15531
   433
          | NONE => (REW::ss,thm,anet,ats,cs);
clasohm@0
   434
clasohm@0
   435
(*NB: the "Adding rewrites:" trace will look strange because assumptions
clasohm@0
   436
      are represented by rules, generalized over their parameters*)
clasohm@0
   437
fun add_asms(ss,thm,a,anet,ats,cs) =
clasohm@0
   438
    let val As = strip_varify(nth_subgoal i thm, a, []);
clasohm@1459
   439
        val thms = map (trivial o cterm_of(#sign(rep_thm(thm))))As;
skalberg@15570
   440
        val new_rws = List.concat(map mk_rew_rules thms);
skalberg@15570
   441
        val rwrls = map mk_trans (List.concat(map mk_rew_rules thms));
skalberg@15570
   442
        val anet' = Library.foldr lhs_insert_thm (rwrls,anet)
clasohm@0
   443
    in  if !tracing andalso not(null new_rws)
clasohm@1459
   444
        then (writeln"Adding rewrites:";  prths new_rws;  ())
clasohm@1459
   445
        else ();
clasohm@1459
   446
        (ss,thm,anet',anet::ats,cs) 
clasohm@0
   447
    end;
clasohm@0
   448
wenzelm@4271
   449
fun rew(seq,thm,ss,anet,ats,cs, more) = case Seq.pull seq of
skalberg@15531
   450
      SOME(thm',seq') =>
clasohm@1459
   451
            let val n = (nprems_of thm') - (nprems_of thm)
clasohm@1459
   452
            in pr_rew(i,n,thm,thm',more);
clasohm@1459
   453
               if n=0 then (SIMP_LHS::ss, thm', anet, ats, cs)
clasohm@1459
   454
               else ((replicate n PROVE) @ (POP_CS::SIMP_LHS::ss),
clasohm@1459
   455
                     thm', anet, ats, (ss,thm,anet,ats,seq',more)::cs)
clasohm@1459
   456
            end
skalberg@15531
   457
    | NONE => if more
paulson@1512
   458
            then rew((lhs_net_tac anet i THEN assume_tac i) thm,
clasohm@1459
   459
                     thm,ss,anet,ats,cs,false)
clasohm@1459
   460
            else (ss,thm,anet,ats,cs);
clasohm@0
   461
clasohm@0
   462
fun try_true(thm,ss,anet,ats,cs) =
wenzelm@4271
   463
    case Seq.pull(auto_tac i thm) of
skalberg@15531
   464
      SOME(thm',_) => (ss,thm',anet,ats,cs)
skalberg@15531
   465
    | NONE => let val (ss0,thm0,anet0,ats0,seq,more)::cs0 = cs
clasohm@1459
   466
              in if !tracing
clasohm@1459
   467
                 then (writeln"*** Failed to prove precondition. Normal form:";
clasohm@1459
   468
                       pr_goal_concl i thm;  writeln"")
clasohm@1459
   469
                 else ();
clasohm@1459
   470
                 rew(seq,thm0,ss0,anet0,ats0,cs0,more)
clasohm@1459
   471
              end;
clasohm@0
   472
clasohm@0
   473
fun if_exp(thm,ss,anet,ats,cs) =
wenzelm@4271
   474
        case Seq.pull (IF1_TAC (cong_tac i) i thm) of
skalberg@15531
   475
                SOME(thm',_) => (SIMP_LHS::IF::ss,thm',anet,ats,cs)
skalberg@15531
   476
              | NONE => (ss,thm,anet,ats,cs);
clasohm@0
   477
clasohm@0
   478
fun step(s::ss, thm, anet, ats, cs) = case s of
clasohm@1459
   479
          MK_EQ => (ss, res1(thm,[red2],i), anet, ats, cs)
clasohm@1459
   480
        | ASMS(a) => add_asms(ss,thm,a,anet,ats,cs)
clasohm@1459
   481
        | SIMP_LHS => simp_lhs(thm,ss,anet,ats,cs)
paulson@1512
   482
        | REW => rew(net_tac net i thm,thm,ss,anet,ats,cs,true)
clasohm@1459
   483
        | REFL => (ss, res1(thm,refl_thms,i), anet, ats, cs)
clasohm@1459
   484
        | TRUE => try_true(res1(thm,refl_thms,i),ss,anet,ats,cs)
clasohm@1459
   485
        | PROVE => (if if_fl then MK_EQ::SIMP_LHS::IF::TRUE::ss
clasohm@1459
   486
                    else MK_EQ::SIMP_LHS::TRUE::ss, thm, anet, ats, cs)
clasohm@1459
   487
        | POP_ARTR => (ss,thm,hd ats,tl ats,cs)
clasohm@1459
   488
        | POP_CS => (ss,thm,anet,ats,tl cs)
clasohm@1459
   489
        | IF => if_exp(thm,ss,anet,ats,cs);
clasohm@0
   490
clasohm@0
   491
fun exec(state as (s::ss, thm, _, _, _)) =
clasohm@1459
   492
        if s=STOP then thm else exec(step(state));
clasohm@0
   493
clasohm@0
   494
in exec(ss, thm, Net.empty, [], []) end;
clasohm@0
   495
clasohm@0
   496
clasohm@0
   497
fun EXEC_TAC(ss,fl) (SS{auto_tac,cong_net,simp_net,...}) =
clasohm@0
   498
let val cong_tac = net_tac cong_net
paulson@1512
   499
in fn i => 
paulson@1512
   500
    (fn thm =>
wenzelm@4271
   501
     if i <= 0 orelse nprems_of thm < i then Seq.empty
wenzelm@4271
   502
     else Seq.single(execute(ss,fl,auto_tac,cong_tac,simp_net,i,thm)))
paulson@1512
   503
    THEN TRY(auto_tac i)
clasohm@0
   504
end;
clasohm@0
   505
clasohm@0
   506
val SIMP_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,REFL,STOP],false);
clasohm@0
   507
val SIMP_CASE_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,IF,REFL,STOP],false);
clasohm@0
   508
clasohm@0
   509
val ASM_SIMP_TAC = EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,REFL,STOP],false);
clasohm@0
   510
val ASM_SIMP_CASE_TAC = EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,IF,REFL,STOP],false);
clasohm@0
   511
clasohm@0
   512
val SIMP_CASE2_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,IF,REFL,STOP],true);
clasohm@0
   513
clasohm@0
   514
fun REWRITE (ss,fl) (SS{auto_tac,cong_net,simp_net,...}) =
clasohm@0
   515
let val cong_tac = net_tac cong_net
clasohm@0
   516
in fn thm => let val state = thm RSN (2,red1)
clasohm@1459
   517
             in execute(ss,fl,auto_tac,cong_tac,simp_net,1,state) end
clasohm@0
   518
end;
clasohm@0
   519
clasohm@0
   520
val SIMP_THM = REWRITE ([ASMS(0),SIMP_LHS,IF,REFL,STOP],false);
clasohm@0
   521
clasohm@0
   522
clasohm@0
   523
(* Compute Congruence rules for individual constants using the substition
clasohm@0
   524
   rules *)
clasohm@0
   525
clasohm@0
   526
val subst_thms = map standard subst_thms;
clasohm@0
   527
clasohm@0
   528
clasohm@0
   529
fun exp_app(0,t) = t
clasohm@0
   530
  | exp_app(i,t) = exp_app(i-1,t $ Bound (i-1));
clasohm@0
   531
clasohm@0
   532
fun exp_abs(Type("fun",[T1,T2]),t,i) =
clasohm@1459
   533
        Abs("x"^string_of_int i,T1,exp_abs(T2,t,i+1))
clasohm@0
   534
  | exp_abs(T,t,i) = exp_app(i,t);
clasohm@0
   535
clasohm@0
   536
fun eta_Var(ixn,T) = exp_abs(T,Var(ixn,T),0);
clasohm@0
   537
clasohm@0
   538
clasohm@0
   539
fun Pinst(f,fT,(eq,eqT),k,i,T,yik,Ts) =
clasohm@0
   540
let fun xn_list(x,n) =
clasohm@1459
   541
        let val ixs = map (fn i => (x^(radixstring(26,"a",i)),0)) (0 upto n);
skalberg@15570
   542
        in ListPair.map eta_Var (ixs, Library.take(n+1,Ts)) end
clasohm@0
   543
    val lhs = list_comb(f,xn_list("X",k-1))
clasohm@0
   544
    val rhs = list_comb(f,xn_list("X",i-1) @ [Bound 0] @ yik)
clasohm@0
   545
in Abs("", T, Const(eq,[fT,fT]--->eqT) $ lhs $ rhs) end;
clasohm@0
   546
clasohm@0
   547
fun find_subst tsig T =
clasohm@0
   548
let fun find (thm::thms) =
clasohm@1459
   549
        let val (Const(_,cT), va, vb) = dest_red(hd(prems_of thm));
clasohm@1459
   550
            val [P] = add_term_vars(concl_of thm,[]) \\ [va,vb]
clasohm@1459
   551
            val eqT::_ = binder_types cT
skalberg@15531
   552
        in if Type.typ_instance tsig (T,eqT) then SOME(thm,va,vb,P)
clasohm@1459
   553
           else find thms
clasohm@1459
   554
        end
skalberg@15531
   555
      | find [] = NONE
clasohm@0
   556
in find subst_thms end;
clasohm@0
   557
clasohm@0
   558
fun mk_cong sg (f,aTs,rT) (refl,eq) =
wenzelm@14643
   559
let val tsig = Sign.tsig_of sg;
clasohm@0
   560
    val k = length aTs;
clasohm@0
   561
    fun ri((subst,va as Var(_,Ta),vb as Var(_,Tb),P),i,si,T,yik) =
clasohm@1459
   562
        let val ca = cterm_of sg va
clasohm@1459
   563
            and cx = cterm_of sg (eta_Var(("X"^si,0),T))
clasohm@1459
   564
            val cb = cterm_of sg vb
clasohm@1459
   565
            and cy = cterm_of sg (eta_Var(("Y"^si,0),T))
clasohm@1459
   566
            val cP = cterm_of sg P
clasohm@1459
   567
            and cp = cterm_of sg (Pinst(f,rT,eq,k,i,T,yik,aTs))
clasohm@1459
   568
        in cterm_instantiate [(ca,cx),(cb,cy),(cP,cp)] subst end;
clasohm@0
   569
    fun mk(c,T::Ts,i,yik) =
clasohm@1459
   570
        let val si = radixstring(26,"a",i)
clasohm@1459
   571
        in case find_subst tsig T of
skalberg@15531
   572
             NONE => mk(c,Ts,i-1,eta_Var(("X"^si,0),T)::yik)
skalberg@15531
   573
           | SOME s => let val c' = c RSN (2,ri(s,i,si,T,yik))
clasohm@1459
   574
                       in mk(c',Ts,i-1,eta_Var(("Y"^si,0),T)::yik) end
clasohm@1459
   575
        end
clasohm@0
   576
      | mk(c,[],_,_) = c;
clasohm@0
   577
in mk(refl,rev aTs,k-1,[]) end;
clasohm@0
   578
clasohm@0
   579
fun mk_cong_type sg (f,T) =
clasohm@0
   580
let val (aTs,rT) = strip_type T;
wenzelm@14643
   581
    val tsig = Sign.tsig_of sg;
clasohm@0
   582
    fun find_refl(r::rs) =
clasohm@1459
   583
        let val (Const(eq,eqT),_,_) = dest_red(concl_of r)
wenzelm@14772
   584
        in if Type.typ_instance tsig (rT, hd(binder_types eqT))
skalberg@15531
   585
           then SOME(r,(eq,body_type eqT)) else find_refl rs
clasohm@1459
   586
        end
skalberg@15531
   587
      | find_refl([]) = NONE;
clasohm@0
   588
in case find_refl refl_thms of
skalberg@15531
   589
     NONE => []  |  SOME(refl) => [mk_cong sg (f,aTs,rT) refl]
clasohm@0
   590
end;
clasohm@0
   591
clasohm@0
   592
fun mk_cong_thy thy f =
clasohm@0
   593
let val sg = sign_of thy;
wenzelm@611
   594
    val T = case Sign.const_type sg f of
skalberg@15531
   595
                NONE => error(f^" not declared") | SOME(T) => T;
clasohm@0
   596
    val T' = incr_tvar 9 T;
clasohm@0
   597
in mk_cong_type sg (Const(f,T'),T') end;
clasohm@0
   598
skalberg@15570
   599
fun mk_congs thy = List.concat o map (mk_cong_thy thy);
clasohm@0
   600
clasohm@0
   601
fun mk_typed_congs thy =
clasohm@0
   602
let val sg = sign_of thy;
wenzelm@7645
   603
    val S0 = Sign.defaultS sg;
clasohm@0
   604
    fun readfT(f,s) =
skalberg@15531
   605
        let val T = incr_tvar 9 (Sign.read_typ(sg,K(SOME(S0))) s);
clasohm@1459
   606
            val t = case Sign.const_type sg f of
skalberg@15531
   607
                      SOME(_) => Const(f,T) | NONE => Free(f,T)
clasohm@1459
   608
        in (t,T) end
skalberg@15570
   609
in List.concat o map (mk_cong_type sg o readfT) end;
clasohm@0
   610
clasohm@0
   611
end (* local *)
clasohm@0
   612
end (* SIMP *);