src/Pure/logic.ML
author wenzelm
Thu Dec 22 00:28:58 2005 +0100 (2005-12-22)
changeset 18469 324245a561b5
parent 18248 929659a46ecf
child 18499 567370efb6d7
permissions -rw-r--r--
mk_conjunction: proper treatment of bounds;
added dest_conjunction(s);
wenzelm@9460
     1
(*  Title:      Pure/logic.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@9460
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   Cambridge University 1992
clasohm@0
     5
wenzelm@9460
     6
Abstract syntax operations of the Pure meta-logic.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@9460
     9
signature LOGIC =
wenzelm@4345
    10
sig
wenzelm@18181
    11
  val is_all: term -> bool
wenzelm@18181
    12
  val mk_equals: term * term -> term
wenzelm@18181
    13
  val dest_equals: term -> term * term
wenzelm@18181
    14
  val is_equals: term -> bool
wenzelm@18181
    15
  val mk_implies: term * term -> term
wenzelm@18181
    16
  val dest_implies: term -> term * term
wenzelm@18181
    17
  val is_implies: term -> bool
wenzelm@18181
    18
  val list_implies: term list * term -> term
wenzelm@18181
    19
  val strip_imp_prems: term -> term list
wenzelm@18181
    20
  val strip_imp_concl: term -> term
wenzelm@18181
    21
  val strip_prems: int * term list * term -> term list * term
wenzelm@18181
    22
  val count_prems: term * int -> int
wenzelm@18181
    23
  val nth_prem: int * term -> term
wenzelm@18181
    24
  val mk_conjunction: term * term -> term
wenzelm@12757
    25
  val mk_conjunction_list: term list -> term
wenzelm@18469
    26
  val dest_conjunction: term -> term * term
wenzelm@18469
    27
  val dest_conjunctions: term -> term list
wenzelm@18181
    28
  val strip_horn: term -> term list * term
wenzelm@18181
    29
  val mk_cond_defpair: term list -> term * term -> string * term
wenzelm@18181
    30
  val mk_defpair: term * term -> string * term
wenzelm@18181
    31
  val mk_type: typ -> term
wenzelm@18181
    32
  val dest_type: term -> typ
wenzelm@18181
    33
  val mk_inclass: typ * class -> term
wenzelm@18181
    34
  val dest_inclass: term -> typ * class
wenzelm@18181
    35
  val protectC: term
wenzelm@18181
    36
  val protect: term -> term
wenzelm@18181
    37
  val unprotect: term -> term
wenzelm@18181
    38
  val occs: term * term -> bool
wenzelm@18181
    39
  val close_form: term -> term
wenzelm@18181
    40
  val incr_indexes: typ list * int -> term -> term
wenzelm@18181
    41
  val incr_tvar: int -> typ -> typ
wenzelm@18181
    42
  val lift_abs: int -> term -> term -> term
wenzelm@18181
    43
  val lift_all: int -> term -> term -> term
wenzelm@18181
    44
  val strip_assums_hyp: term -> term list
wenzelm@9460
    45
  val strip_assums_concl: term -> term
wenzelm@18181
    46
  val strip_params: term -> (string * typ) list
wenzelm@18181
    47
  val has_meta_prems: term -> int -> bool
wenzelm@18181
    48
  val flatten_params: int -> term -> term
wenzelm@18181
    49
  val auto_rename: bool ref
wenzelm@18181
    50
  val set_rename_prefix: string -> unit
clasohm@0
    51
  val list_rename_params: string list * term -> term
wenzelm@18181
    52
  val assum_pairs: int * term -> (term*term)list
wenzelm@18181
    53
  val varify: term -> term
wenzelm@18181
    54
  val unvarify: term -> term
wenzelm@18181
    55
  val get_goal: term -> int -> term
wenzelm@18181
    56
  val goal_params: term -> int -> term * term list
wenzelm@18181
    57
  val prems_of_goal: term -> int -> term list
wenzelm@18181
    58
  val concl_of_goal: term -> int -> term
wenzelm@4345
    59
end;
clasohm@0
    60
paulson@1500
    61
structure Logic : LOGIC =
clasohm@0
    62
struct
wenzelm@398
    63
wenzelm@4345
    64
clasohm@0
    65
(*** Abstract syntax operations on the meta-connectives ***)
clasohm@0
    66
nipkow@5041
    67
(** all **)
nipkow@5041
    68
nipkow@5041
    69
fun is_all (Const ("all", _) $ _) = true
nipkow@5041
    70
  | is_all _ = false;
nipkow@5041
    71
nipkow@5041
    72
clasohm@0
    73
(** equality **)
clasohm@0
    74
paulson@1835
    75
(*Make an equality.  DOES NOT CHECK TYPE OF u*)
lcp@64
    76
fun mk_equals(t,u) = equals(fastype_of t) $ t $ u;
clasohm@0
    77
clasohm@0
    78
fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
clasohm@0
    79
  | dest_equals t = raise TERM("dest_equals", [t]);
clasohm@0
    80
wenzelm@637
    81
fun is_equals (Const ("==", _) $ _ $ _) = true
wenzelm@637
    82
  | is_equals _ = false;
wenzelm@637
    83
wenzelm@637
    84
clasohm@0
    85
(** implies **)
clasohm@0
    86
clasohm@0
    87
fun mk_implies(A,B) = implies $ A $ B;
clasohm@0
    88
clasohm@0
    89
fun dest_implies (Const("==>",_) $ A $ B)  =  (A,B)
clasohm@0
    90
  | dest_implies A = raise TERM("dest_implies", [A]);
clasohm@0
    91
nipkow@5041
    92
fun is_implies (Const ("==>", _) $ _ $ _) = true
nipkow@5041
    93
  | is_implies _ = false;
nipkow@5041
    94
wenzelm@4822
    95
clasohm@0
    96
(** nested implications **)
clasohm@0
    97
clasohm@0
    98
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
wenzelm@18181
    99
fun list_implies ([], B) = B
wenzelm@18181
   100
  | list_implies (A::As, B) = implies $ A $ list_implies(As,B);
clasohm@0
   101
clasohm@0
   102
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
   103
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
   104
  | strip_imp_prems _ = [];
clasohm@0
   105
clasohm@0
   106
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
   107
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
   108
  | strip_imp_concl A = A : term;
clasohm@0
   109
clasohm@0
   110
(*Strip and return premises: (i, [], A1==>...Ai==>B)
wenzelm@9460
   111
    goes to   ([Ai, A(i-1),...,A1] , B)         (REVERSED)
clasohm@0
   112
  if  i<0 or else i too big then raises  TERM*)
wenzelm@9460
   113
fun strip_prems (0, As, B) = (As, B)
wenzelm@9460
   114
  | strip_prems (i, As, Const("==>", _) $ A $ B) =
wenzelm@9460
   115
        strip_prems (i-1, A::As, B)
clasohm@0
   116
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   117
wenzelm@16130
   118
(*Count premises -- quicker than (length o strip_prems) *)
clasohm@0
   119
fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1)
clasohm@0
   120
  | count_prems (_,n) = n;
clasohm@0
   121
wenzelm@16130
   122
(*Select Ai from A1 ==>...Ai==>B*)
wenzelm@16130
   123
fun nth_prem (1, Const ("==>", _) $ A $ _) = A
wenzelm@16130
   124
  | nth_prem (i, Const ("==>", _) $ _ $ B) = nth_prem (i - 1, B)
wenzelm@16130
   125
  | nth_prem (_, A) = raise TERM ("nth_prem", [A]);
wenzelm@16130
   126
berghofe@13659
   127
(*strip a proof state (Horn clause):
berghofe@13659
   128
  B1 ==> ... Bn ==> C   goes to   ([B1, ..., Bn], C)    *)
berghofe@13659
   129
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
berghofe@13659
   130
wenzelm@4822
   131
wenzelm@12137
   132
(** conjunction **)
wenzelm@12137
   133
wenzelm@12137
   134
fun mk_conjunction (t, u) =
wenzelm@18469
   135
  Term.list_all ([("X", propT)],
wenzelm@18469
   136
    mk_implies (list_implies (map (Term.incr_boundvars 1) [t, u], Bound 0), Bound 0));
wenzelm@12137
   137
wenzelm@12757
   138
fun mk_conjunction_list [] = Term.all propT $ Abs ("dummy", propT, mk_implies (Bound 0, Bound 0))
wenzelm@12757
   139
  | mk_conjunction_list ts = foldr1 mk_conjunction ts;
wenzelm@12137
   140
wenzelm@18469
   141
fun dest_conjunction
wenzelm@18469
   142
      (t as Const ("all", _) $ Abs (_, _,
wenzelm@18469
   143
        Const ("==>", _) $ (Const ("==>", _) $ A $ (Const ("==>", _) $ B $ Bound 0)) $ Bound 0)) =
wenzelm@18469
   144
      if Term.loose_bvar1 (A, 0) orelse Term.loose_bvar1 (B, 0)
wenzelm@18469
   145
      then raise TERM ("dest_conjunction", [t])
wenzelm@18469
   146
      else (Term.incr_boundvars ~1 A, Term.incr_boundvars ~1 B)
wenzelm@18469
   147
  | dest_conjunction t = raise TERM ("dest_conjunction", [t]);
wenzelm@18469
   148
wenzelm@18469
   149
fun dest_conjunctions t =
wenzelm@18469
   150
  (case try dest_conjunction t of
wenzelm@18469
   151
    NONE => [t]
wenzelm@18469
   152
  | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
wenzelm@18469
   153
wenzelm@18469
   154
wenzelm@12137
   155
wenzelm@4822
   156
(** definitions **)
wenzelm@4822
   157
wenzelm@4822
   158
fun mk_cond_defpair As (lhs, rhs) =
wenzelm@4822
   159
  (case Term.head_of lhs of
wenzelm@4822
   160
    Const (name, _) =>
wenzelm@4822
   161
      (Sign.base_name name ^ "_def", list_implies (As, mk_equals (lhs, rhs)))
wenzelm@4822
   162
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
wenzelm@4822
   163
wenzelm@4822
   164
fun mk_defpair lhs_rhs = mk_cond_defpair [] lhs_rhs;
wenzelm@4822
   165
wenzelm@4822
   166
wenzelm@398
   167
(** types as terms **)
wenzelm@398
   168
wenzelm@398
   169
fun mk_type ty = Const ("TYPE", itselfT ty);
wenzelm@398
   170
wenzelm@398
   171
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   172
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   173
wenzelm@4822
   174
wenzelm@447
   175
(** class constraints **)
wenzelm@398
   176
wenzelm@398
   177
fun mk_inclass (ty, c) =
wenzelm@398
   178
  Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   179
wenzelm@398
   180
fun dest_inclass (t as Const (c_class, _) $ ty) =
wenzelm@398
   181
      ((dest_type ty, Sign.class_of_const c_class)
wenzelm@398
   182
        handle TERM _ => raise TERM ("dest_inclass", [t]))
wenzelm@398
   183
  | dest_inclass t = raise TERM ("dest_inclass", [t]);
wenzelm@398
   184
clasohm@0
   185
wenzelm@18029
   186
(** protected propositions **)
wenzelm@9460
   187
wenzelm@18029
   188
val protectC = Const ("prop", propT --> propT);
wenzelm@18029
   189
fun protect t = protectC $ t;
wenzelm@9460
   190
wenzelm@18029
   191
fun unprotect (Const ("prop", _) $ t) = t
wenzelm@18029
   192
  | unprotect t = raise TERM ("unprotect", [t]);
wenzelm@9460
   193
wenzelm@9460
   194
wenzelm@18181
   195
clasohm@0
   196
(*** Low-level term operations ***)
clasohm@0
   197
clasohm@0
   198
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   199
  The term t must contain no loose bound variables*)
wenzelm@16846
   200
fun occs (t, u) = exists_subterm (fn s => t aconv s) u;
clasohm@0
   201
clasohm@0
   202
(*Close up a formula over all free variables by quantification*)
clasohm@0
   203
fun close_form A =
wenzelm@4443
   204
  list_all_free (sort_wrt fst (map dest_Free (term_frees A)), A);
clasohm@0
   205
clasohm@0
   206
clasohm@0
   207
(*** Specialized operations for resolution... ***)
clasohm@0
   208
wenzelm@16879
   209
local exception SAME in
wenzelm@16879
   210
wenzelm@16879
   211
fun incrT k =
wenzelm@16879
   212
  let
wenzelm@16879
   213
    fun incr (TVar ((a, i), S)) = TVar ((a, i + k), S)
wenzelm@16879
   214
      | incr (Type (a, Ts)) = Type (a, incrs Ts)
wenzelm@16879
   215
      | incr _ = raise SAME
wenzelm@16879
   216
    and incrs (T :: Ts) =
wenzelm@16879
   217
        (incr T :: (incrs Ts handle SAME => Ts)
wenzelm@16879
   218
          handle SAME => T :: incrs Ts)
wenzelm@16879
   219
      | incrs [] = raise SAME;
wenzelm@16879
   220
  in incr end;
wenzelm@16879
   221
clasohm@0
   222
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   223
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
paulson@17120
   224
fun incr_indexes ([], 0) t = t
wenzelm@18029
   225
  | incr_indexes (Ts, k) t =
wenzelm@16879
   226
  let
wenzelm@18029
   227
    val n = length Ts;
wenzelm@16879
   228
    val incrT = if k = 0 then I else incrT k;
wenzelm@16879
   229
wenzelm@16879
   230
    fun incr lev (Var ((x, i), T)) =
wenzelm@18029
   231
          Unify.combound (Var ((x, i + k), Ts ---> (incrT T handle SAME => T)), lev, n)
wenzelm@16879
   232
      | incr lev (Abs (x, T, body)) =
wenzelm@16879
   233
          (Abs (x, incrT T, incr (lev + 1) body handle SAME => body)
wenzelm@16879
   234
            handle SAME => Abs (x, T, incr (lev + 1) body))
wenzelm@16879
   235
      | incr lev (t $ u) =
wenzelm@16879
   236
          (incr lev t $ (incr lev u handle SAME => u)
wenzelm@16879
   237
            handle SAME => t $ incr lev u)
wenzelm@16879
   238
      | incr _ (Const (c, T)) = Const (c, incrT T)
wenzelm@16879
   239
      | incr _ (Free (x, T)) = Free (x, incrT T)
wenzelm@16879
   240
      | incr _ (t as Bound _) = t;
wenzelm@16879
   241
  in incr 0 t handle SAME => t end;
wenzelm@16879
   242
wenzelm@16879
   243
fun incr_tvar 0 T = T
wenzelm@16879
   244
  | incr_tvar k T = incrT k T handle SAME => T;
wenzelm@16879
   245
wenzelm@16879
   246
end;
wenzelm@16879
   247
clasohm@0
   248
wenzelm@18248
   249
(* Lifting functions from subgoal and increment:
wenzelm@18029
   250
    lift_abs operates on terms
wenzelm@18029
   251
    lift_all operates on propositions *)
wenzelm@18029
   252
wenzelm@18029
   253
fun lift_abs inc =
wenzelm@18029
   254
  let
wenzelm@18029
   255
    fun lift Ts (Const ("==>", _) $ _ $ B) t = lift Ts B t
wenzelm@18248
   256
      | lift Ts (Const ("all", _) $ Abs (a, T, B)) t = Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   257
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   258
  in lift [] end;
wenzelm@18029
   259
wenzelm@18029
   260
fun lift_all inc =
wenzelm@18029
   261
  let
wenzelm@18029
   262
    fun lift Ts ((c as Const ("==>", _)) $ A $ B) t = c $ A $ lift Ts B t
wenzelm@18248
   263
      | lift Ts ((c as Const ("all", _)) $ Abs (a, T, B)) t = c $ Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   264
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   265
  in lift [] end;
clasohm@0
   266
clasohm@0
   267
(*Strips assumptions in goal, yielding list of hypotheses.   *)
clasohm@0
   268
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
clasohm@0
   269
  | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
clasohm@0
   270
  | strip_assums_hyp B = [];
clasohm@0
   271
clasohm@0
   272
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   273
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   274
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   275
  | strip_assums_concl B = B;
clasohm@0
   276
clasohm@0
   277
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   278
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   279
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   280
  | strip_params B = [];
clasohm@0
   281
wenzelm@9667
   282
(*test for meta connectives in prems of a 'subgoal'*)
wenzelm@9667
   283
fun has_meta_prems prop i =
wenzelm@9667
   284
  let
wenzelm@9667
   285
    fun is_meta (Const ("==>", _) $ _ $ _) = true
wenzelm@10442
   286
      | is_meta (Const ("==", _) $ _ $ _) = true
wenzelm@9667
   287
      | is_meta (Const ("all", _) $ _) = true
wenzelm@9667
   288
      | is_meta _ = false;
wenzelm@9667
   289
  in
berghofe@13659
   290
    (case strip_prems (i, [], prop) of
wenzelm@9667
   291
      (B :: _, _) => exists is_meta (strip_assums_hyp B)
wenzelm@9667
   292
    | _ => false) handle TERM _ => false
wenzelm@9667
   293
  end;
wenzelm@9483
   294
clasohm@0
   295
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
wenzelm@9460
   296
    where j is the total number of parameters (precomputed)
clasohm@0
   297
  If n>0 then deletes assumption n. *)
wenzelm@9460
   298
fun remove_params j n A =
clasohm@0
   299
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   300
    else case A of
wenzelm@9460
   301
        Const("==>", _) $ H $ B =>
wenzelm@9460
   302
          if n=1 then                           (remove_params j (n-1) B)
wenzelm@9460
   303
          else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   304
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   305
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   306
             else A;
clasohm@0
   307
clasohm@0
   308
(** Auto-renaming of parameters in subgoals **)
clasohm@0
   309
clasohm@0
   310
val auto_rename = ref false
clasohm@0
   311
and rename_prefix = ref "ka";
clasohm@0
   312
clasohm@0
   313
(*rename_prefix is not exported; it is set by this function.*)
clasohm@0
   314
fun set_rename_prefix a =
wenzelm@4693
   315
    if a<>"" andalso forall Symbol.is_letter (Symbol.explode a)
clasohm@0
   316
    then  (rename_prefix := a;  auto_rename := true)
clasohm@0
   317
    else  error"rename prefix must be nonempty and consist of letters";
clasohm@0
   318
clasohm@0
   319
(*Makes parameters in a goal have distinctive names (not guaranteed unique!)
clasohm@0
   320
  A name clash could cause the printer to rename bound vars;
clasohm@0
   321
    then res_inst_tac would not work properly.*)
clasohm@0
   322
fun rename_vars (a, []) = []
clasohm@0
   323
  | rename_vars (a, (_,T)::vars) =
wenzelm@12902
   324
        (a,T) :: rename_vars (Symbol.bump_string a, vars);
clasohm@0
   325
clasohm@0
   326
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   327
  if n>0 then deletes assumption n. *)
clasohm@0
   328
fun flatten_params n A =
clasohm@0
   329
    let val params = strip_params A;
wenzelm@9460
   330
        val vars = if !auto_rename
wenzelm@9460
   331
                   then rename_vars (!rename_prefix, params)
wenzelm@9460
   332
                   else ListPair.zip (variantlist(map #1 params,[]),
wenzelm@9460
   333
                                      map #2 params)
clasohm@0
   334
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   335
    end;
clasohm@0
   336
clasohm@0
   337
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   338
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   339
      implies $ A $ list_rename_params (cs, B)
wenzelm@9460
   340
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) =
clasohm@0
   341
      all T $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   342
  | list_rename_params (cs, B) = B;
clasohm@0
   343
paulson@15451
   344
(*** Treatmsent of "assume", "erule", etc. ***)
clasohm@0
   345
wenzelm@16879
   346
(*Strips assumptions in goal yielding
paulson@15451
   347
   HS = [Hn,...,H1],   params = [xm,...,x1], and B,
wenzelm@16879
   348
  where x1...xm are the parameters. This version (21.1.2005) REQUIRES
wenzelm@16879
   349
  the the parameters to be flattened, but it allows erule to work on
paulson@15451
   350
  assumptions of the form !!x. phi. Any !! after the outermost string
paulson@15451
   351
  will be regarded as belonging to the conclusion, and left untouched.
paulson@15454
   352
  Used ONLY by assum_pairs.
paulson@15454
   353
      Unless nasms<0, it can terminate the recursion early; that allows
paulson@15454
   354
  erule to work on assumptions of the form P==>Q.*)
paulson@15454
   355
fun strip_assums_imp (0, Hs, B) = (Hs, B)  (*recursion terminated by nasms*)
wenzelm@16879
   356
  | strip_assums_imp (nasms, Hs, Const("==>", _) $ H $ B) =
paulson@15454
   357
      strip_assums_imp (nasms-1, H::Hs, B)
paulson@15454
   358
  | strip_assums_imp (_, Hs, B) = (Hs, B); (*recursion terminated by B*)
paulson@15454
   359
clasohm@0
   360
paulson@15451
   361
(*Strips OUTER parameters only, unlike similar legacy versions.*)
paulson@15451
   362
fun strip_assums_all (params, Const("all",_)$Abs(a,T,t)) =
paulson@15451
   363
      strip_assums_all ((a,T)::params, t)
paulson@15451
   364
  | strip_assums_all (params, B) = (params, B);
clasohm@0
   365
clasohm@0
   366
(*Produces disagreement pairs, one for each assumption proof, in order.
clasohm@0
   367
  A is the first premise of the lifted rule, and thus has the form
paulson@15454
   368
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B).
paulson@15454
   369
  nasms is the number of assumptions in the original subgoal, needed when B
paulson@15454
   370
    has the form B1 ==> B2: it stops B1 from being taken as an assumption. *)
paulson@15454
   371
fun assum_pairs(nasms,A) =
paulson@15451
   372
  let val (params, A') = strip_assums_all ([],A)
paulson@15454
   373
      val (Hs,B) = strip_assums_imp (nasms,[],A')
paulson@15451
   374
      fun abspar t = Unify.rlist_abs(params, t)
paulson@15451
   375
      val D = abspar B
paulson@15451
   376
      fun pairrev ([], pairs) = pairs
paulson@15451
   377
        | pairrev (H::Hs, pairs) = pairrev(Hs,  (abspar H, D) :: pairs)
paulson@15451
   378
  in  pairrev (Hs,[])
clasohm@0
   379
  end;
clasohm@0
   380
clasohm@0
   381
(*Converts Frees to Vars and TFrees to TVars so that axioms can be written
clasohm@0
   382
  without (?) everywhere*)
wenzelm@16862
   383
fun varify (Const(a, T)) = Const (a, Type.varifyT T)
wenzelm@16862
   384
  | varify (Free (a, T)) = Var ((a, 0), Type.varifyT T)
wenzelm@16862
   385
  | varify (Var (ixn, T)) = Var (ixn, Type.varifyT T)
wenzelm@16862
   386
  | varify (t as Bound _) = t
wenzelm@16862
   387
  | varify (Abs (a, T, body)) = Abs (a, Type.varifyT T, varify body)
wenzelm@16862
   388
  | varify (f $ t) = varify f $ varify t;
clasohm@0
   389
lcp@546
   390
(*Inverse of varify.  Converts axioms back to their original form.*)
wenzelm@16862
   391
fun unvarify (Const (a, T)) = Const (a, Type.unvarifyT T)
wenzelm@16862
   392
  | unvarify (Free (a, T)) = Free (a, Type.unvarifyT T)
wenzelm@16862
   393
  | unvarify (Var ((a, 0), T)) = Free (a, Type.unvarifyT T)
wenzelm@16862
   394
  | unvarify (Var (ixn, T)) = Var (ixn, Type.unvarifyT T)  (*non-0 index!*)
wenzelm@16862
   395
  | unvarify (t as Bound _) = t
wenzelm@16862
   396
  | unvarify (Abs (a, T, body)) = Abs (a, Type.unvarifyT T, unvarify body)
wenzelm@16862
   397
  | unvarify (f $ t) = unvarify f $ unvarify t;
lcp@546
   398
berghofe@13799
   399
wenzelm@16862
   400
(* goal states *)
wenzelm@16862
   401
wenzelm@16862
   402
fun get_goal st i = nth_prem (i, st)
wenzelm@16862
   403
  handle TERM _ => error "Goal number out of range";
berghofe@13799
   404
berghofe@13799
   405
(*reverses parameters for substitution*)
berghofe@13799
   406
fun goal_params st i =
berghofe@13799
   407
  let val gi = get_goal st i
berghofe@14137
   408
      val rfrees = map Free (rename_wrt_term gi (strip_params gi))
berghofe@13799
   409
  in (gi, rfrees) end;
berghofe@13799
   410
berghofe@13799
   411
fun concl_of_goal st i =
berghofe@13799
   412
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   413
      val B = strip_assums_concl gi
berghofe@13799
   414
  in subst_bounds (rfrees, B) end;
berghofe@13799
   415
berghofe@13799
   416
fun prems_of_goal st i =
berghofe@13799
   417
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   418
      val As = strip_assums_hyp gi
berghofe@13799
   419
  in map (fn A => subst_bounds (rfrees, A)) As end;
berghofe@13799
   420
clasohm@0
   421
end;