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