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