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