src/Pure/logic.ML
author wenzelm
Wed Jun 07 02:01:28 2006 +0200 (2006-06-07)
changeset 19806 f860b7a98445
parent 19775 06cb6743adf6
child 19879 6a346150611a
permissions -rw-r--r--
renamed Type.(un)varifyT to Logic.(un)varifyT;
made (un)varify strict wrt. global context -- may use legacy_(un)varify as workaround;
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@18762
    12
  val dest_all: term -> typ * term
wenzelm@18181
    13
  val mk_equals: term * term -> term
wenzelm@18181
    14
  val dest_equals: term -> term * term
wenzelm@18181
    15
  val is_equals: term -> bool
wenzelm@18181
    16
  val mk_implies: term * term -> term
wenzelm@18181
    17
  val dest_implies: term -> term * term
wenzelm@18181
    18
  val is_implies: term -> bool
wenzelm@18181
    19
  val list_implies: term list * term -> term
wenzelm@18181
    20
  val strip_imp_prems: term -> term list
wenzelm@18181
    21
  val strip_imp_concl: term -> term
wenzelm@18181
    22
  val strip_prems: int * term list * term -> term list * term
wenzelm@18181
    23
  val count_prems: term * int -> int
wenzelm@18181
    24
  val nth_prem: int * term -> term
wenzelm@19125
    25
  val conjunction: term
wenzelm@18181
    26
  val mk_conjunction: term * term -> term
wenzelm@12757
    27
  val mk_conjunction_list: term list -> term
wenzelm@18499
    28
  val mk_conjunction_list2: term list list -> term
wenzelm@18469
    29
  val dest_conjunction: term -> term * term
wenzelm@19425
    30
  val dest_conjunction_list: term -> term list
wenzelm@18469
    31
  val dest_conjunctions: term -> term list
wenzelm@18181
    32
  val strip_horn: term -> term list * term
wenzelm@18938
    33
  val dest_type: term -> typ
wenzelm@18938
    34
  val const_of_class: class -> string
wenzelm@18938
    35
  val class_of_const: string -> class
wenzelm@18938
    36
  val mk_inclass: typ * class -> term
wenzelm@18938
    37
  val dest_inclass: term -> typ * class
wenzelm@19406
    38
  val mk_classrel: class * class -> term
wenzelm@19406
    39
  val dest_classrel: term -> class * class
wenzelm@19406
    40
  val mk_arities: string * sort list * sort -> term list
wenzelm@19406
    41
  val dest_arity: term -> string * sort list * class
wenzelm@18938
    42
  val dest_def: Pretty.pp -> (term -> bool) -> (string -> bool) -> (string -> bool) ->
wenzelm@18938
    43
    term -> (term * term) * term
wenzelm@18938
    44
  val abs_def: term -> term * term
wenzelm@18181
    45
  val mk_cond_defpair: term list -> term * term -> string * term
wenzelm@18181
    46
  val mk_defpair: term * term -> string * term
wenzelm@18181
    47
  val mk_type: typ -> term
wenzelm@18181
    48
  val protectC: term
wenzelm@18181
    49
  val protect: term -> term
wenzelm@18181
    50
  val unprotect: term -> term
wenzelm@19775
    51
  val mk_term: term -> term
wenzelm@19775
    52
  val dest_term: term -> term
wenzelm@18181
    53
  val occs: term * term -> bool
wenzelm@18181
    54
  val close_form: term -> term
wenzelm@18938
    55
  val combound: term * int * int -> term
wenzelm@18938
    56
  val rlist_abs: (string * typ) list * term -> term
wenzelm@18181
    57
  val incr_indexes: typ list * int -> term -> term
wenzelm@18181
    58
  val incr_tvar: int -> typ -> typ
wenzelm@18181
    59
  val lift_abs: int -> term -> term -> term
wenzelm@18181
    60
  val lift_all: int -> term -> term -> term
wenzelm@18181
    61
  val strip_assums_hyp: term -> term list
wenzelm@9460
    62
  val strip_assums_concl: term -> term
wenzelm@18181
    63
  val strip_params: term -> (string * typ) list
wenzelm@18181
    64
  val has_meta_prems: term -> int -> bool
wenzelm@18181
    65
  val flatten_params: int -> term -> term
wenzelm@18181
    66
  val auto_rename: bool ref
wenzelm@18181
    67
  val set_rename_prefix: string -> unit
clasohm@0
    68
  val list_rename_params: string list * term -> term
wenzelm@18181
    69
  val assum_pairs: int * term -> (term*term)list
wenzelm@19806
    70
  val varifyT: typ -> typ
wenzelm@19806
    71
  val unvarifyT: typ -> typ
wenzelm@18181
    72
  val varify: term -> term
wenzelm@18181
    73
  val unvarify: term -> term
wenzelm@19806
    74
  val legacy_varifyT: typ -> typ
wenzelm@19806
    75
  val legacy_unvarifyT: typ -> typ
wenzelm@19806
    76
  val legacy_varify: term -> term
wenzelm@19806
    77
  val legacy_unvarify: term -> term
wenzelm@18181
    78
  val get_goal: term -> int -> term
wenzelm@18181
    79
  val goal_params: term -> int -> term * term list
wenzelm@18181
    80
  val prems_of_goal: term -> int -> term list
wenzelm@18181
    81
  val concl_of_goal: term -> int -> term
wenzelm@4345
    82
end;
clasohm@0
    83
paulson@1500
    84
structure Logic : LOGIC =
clasohm@0
    85
struct
wenzelm@398
    86
wenzelm@4345
    87
clasohm@0
    88
(*** Abstract syntax operations on the meta-connectives ***)
clasohm@0
    89
nipkow@5041
    90
(** all **)
nipkow@5041
    91
nipkow@5041
    92
fun is_all (Const ("all", _) $ _) = true
nipkow@5041
    93
  | is_all _ = false;
nipkow@5041
    94
wenzelm@18762
    95
fun dest_all (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ A) = (T, A)
wenzelm@18762
    96
  | dest_all t = raise TERM ("dest_all", [t]);
wenzelm@18762
    97
wenzelm@18762
    98
nipkow@5041
    99
clasohm@0
   100
(** equality **)
clasohm@0
   101
paulson@1835
   102
(*Make an equality.  DOES NOT CHECK TYPE OF u*)
lcp@64
   103
fun mk_equals(t,u) = equals(fastype_of t) $ t $ u;
clasohm@0
   104
clasohm@0
   105
fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
clasohm@0
   106
  | dest_equals t = raise TERM("dest_equals", [t]);
clasohm@0
   107
wenzelm@637
   108
fun is_equals (Const ("==", _) $ _ $ _) = true
wenzelm@637
   109
  | is_equals _ = false;
wenzelm@637
   110
wenzelm@637
   111
clasohm@0
   112
(** implies **)
clasohm@0
   113
clasohm@0
   114
fun mk_implies(A,B) = implies $ A $ B;
clasohm@0
   115
clasohm@0
   116
fun dest_implies (Const("==>",_) $ A $ B)  =  (A,B)
clasohm@0
   117
  | dest_implies A = raise TERM("dest_implies", [A]);
clasohm@0
   118
nipkow@5041
   119
fun is_implies (Const ("==>", _) $ _ $ _) = true
nipkow@5041
   120
  | is_implies _ = false;
nipkow@5041
   121
wenzelm@4822
   122
clasohm@0
   123
(** nested implications **)
clasohm@0
   124
clasohm@0
   125
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
wenzelm@18181
   126
fun list_implies ([], B) = B
wenzelm@18181
   127
  | list_implies (A::As, B) = implies $ A $ list_implies(As,B);
clasohm@0
   128
clasohm@0
   129
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
   130
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
   131
  | strip_imp_prems _ = [];
clasohm@0
   132
clasohm@0
   133
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
   134
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
   135
  | strip_imp_concl A = A : term;
clasohm@0
   136
clasohm@0
   137
(*Strip and return premises: (i, [], A1==>...Ai==>B)
wenzelm@9460
   138
    goes to   ([Ai, A(i-1),...,A1] , B)         (REVERSED)
clasohm@0
   139
  if  i<0 or else i too big then raises  TERM*)
wenzelm@9460
   140
fun strip_prems (0, As, B) = (As, B)
wenzelm@9460
   141
  | strip_prems (i, As, Const("==>", _) $ A $ B) =
wenzelm@9460
   142
        strip_prems (i-1, A::As, B)
clasohm@0
   143
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   144
wenzelm@16130
   145
(*Count premises -- quicker than (length o strip_prems) *)
clasohm@0
   146
fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1)
clasohm@0
   147
  | count_prems (_,n) = n;
clasohm@0
   148
wenzelm@16130
   149
(*Select Ai from A1 ==>...Ai==>B*)
wenzelm@16130
   150
fun nth_prem (1, Const ("==>", _) $ A $ _) = A
wenzelm@16130
   151
  | nth_prem (i, Const ("==>", _) $ _ $ B) = nth_prem (i - 1, B)
wenzelm@16130
   152
  | nth_prem (_, A) = raise TERM ("nth_prem", [A]);
wenzelm@16130
   153
berghofe@13659
   154
(*strip a proof state (Horn clause):
berghofe@13659
   155
  B1 ==> ... Bn ==> C   goes to   ([B1, ..., Bn], C)    *)
berghofe@13659
   156
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
berghofe@13659
   157
wenzelm@4822
   158
wenzelm@12137
   159
(** conjunction **)
wenzelm@12137
   160
wenzelm@19125
   161
val conjunction = Const ("ProtoPure.conjunction", propT --> propT --> propT);
wenzelm@19125
   162
wenzelm@18499
   163
(*A && B*)
wenzelm@19125
   164
fun mk_conjunction (A, B) = conjunction $ A $ B;
wenzelm@12137
   165
wenzelm@18499
   166
(*A && B && C -- improper*)
wenzelm@12757
   167
fun mk_conjunction_list [] = Term.all propT $ Abs ("dummy", propT, mk_implies (Bound 0, Bound 0))
wenzelm@12757
   168
  | mk_conjunction_list ts = foldr1 mk_conjunction ts;
wenzelm@12137
   169
wenzelm@18499
   170
(*(A1 && B1 && C1) && (A2 && B2 && C2 && D2) && A3 && B3 -- improper*)
wenzelm@18499
   171
fun mk_conjunction_list2 tss =
wenzelm@18499
   172
  mk_conjunction_list (map mk_conjunction_list (filter_out null tss));
wenzelm@18499
   173
wenzelm@18499
   174
(*A && B*)
wenzelm@19125
   175
fun dest_conjunction (Const ("ProtoPure.conjunction", _) $ A $ B) = (A, B)
wenzelm@18469
   176
  | dest_conjunction t = raise TERM ("dest_conjunction", [t]);
wenzelm@18469
   177
wenzelm@19425
   178
(*A && B && C -- improper*)
wenzelm@19425
   179
fun dest_conjunction_list t =
wenzelm@19425
   180
  (case try dest_conjunction t of
wenzelm@19425
   181
    NONE => [t]
wenzelm@19425
   182
  | SOME (A, B) => A :: dest_conjunction_list B);
wenzelm@19425
   183
wenzelm@18499
   184
(*((A && B) && C) && D && E -- flat*)
wenzelm@18469
   185
fun dest_conjunctions t =
wenzelm@18469
   186
  (case try dest_conjunction t of
wenzelm@18469
   187
    NONE => [t]
wenzelm@18469
   188
  | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
wenzelm@18469
   189
wenzelm@18469
   190
wenzelm@12137
   191
wenzelm@398
   192
(** types as terms **)
wenzelm@398
   193
wenzelm@19391
   194
fun mk_type ty = Const ("TYPE", Term.itselfT ty);
wenzelm@398
   195
wenzelm@398
   196
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   197
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   198
wenzelm@4822
   199
wenzelm@19406
   200
wenzelm@19406
   201
(** type classes **)
wenzelm@19406
   202
wenzelm@19406
   203
(* const names *)
wenzelm@398
   204
wenzelm@18938
   205
val classN = "_class";
wenzelm@18938
   206
wenzelm@18938
   207
val const_of_class = suffix classN;
wenzelm@19406
   208
wenzelm@18938
   209
fun class_of_const c = unsuffix classN c
wenzelm@18938
   210
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@18938
   211
wenzelm@19406
   212
wenzelm@19406
   213
(* class constraints *)
wenzelm@19406
   214
wenzelm@398
   215
fun mk_inclass (ty, c) =
wenzelm@19391
   216
  Const (const_of_class c, Term.itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   217
wenzelm@19406
   218
fun dest_inclass (t as Const (c_class, _) $ ty) = (dest_type ty, class_of_const c_class)
wenzelm@398
   219
  | dest_inclass t = raise TERM ("dest_inclass", [t]);
wenzelm@398
   220
clasohm@0
   221
wenzelm@19406
   222
(* class relations *)
wenzelm@19406
   223
wenzelm@19406
   224
fun mk_classrel (c1, c2) = mk_inclass (Term.aT [c1], c2);
wenzelm@19406
   225
wenzelm@19406
   226
fun dest_classrel tm =
wenzelm@19406
   227
  (case dest_inclass tm of
wenzelm@19406
   228
    (TVar (_, [c1]), c2) => (c1, c2)
wenzelm@19406
   229
  | _ => raise TERM ("dest_classrel", [tm]));
wenzelm@19406
   230
wenzelm@19406
   231
wenzelm@19406
   232
(* type arities *)
wenzelm@19406
   233
wenzelm@19406
   234
fun mk_arities (t, Ss, S) =
wenzelm@19406
   235
  let val T = Type (t, ListPair.map TFree (Term.invent_names [] "'a" (length Ss), Ss))
wenzelm@19406
   236
  in map (fn c => mk_inclass (T, c)) S end;
wenzelm@19406
   237
wenzelm@19406
   238
fun dest_arity tm =
wenzelm@19406
   239
  let
wenzelm@19406
   240
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@19406
   241
wenzelm@19406
   242
    val (ty, c) = dest_inclass tm;
wenzelm@19406
   243
    val (t, tvars) =
wenzelm@19406
   244
      (case ty of
wenzelm@19406
   245
        Type (t, tys) => (t, map dest_TVar tys handle TYPE _ => err ())
wenzelm@19406
   246
      | _ => err ());
wenzelm@19406
   247
    val Ss =
wenzelm@19406
   248
      if has_duplicates (eq_fst (op =)) tvars then err ()
wenzelm@19406
   249
      else map snd tvars;
wenzelm@19406
   250
  in (t, Ss, c) end;
wenzelm@19406
   251
wenzelm@19406
   252
wenzelm@18938
   253
wenzelm@18938
   254
(** definitions **)
wenzelm@18938
   255
wenzelm@19103
   256
fun term_kind (Const _) = "existing constant "
wenzelm@19103
   257
  | term_kind (Free _) = "free variable "
wenzelm@19103
   258
  | term_kind (Bound _) = "bound variable "
wenzelm@19103
   259
  | term_kind _ = "";
wenzelm@19103
   260
wenzelm@18938
   261
(*c x == t[x] to !!x. c x == t[x]*)
wenzelm@18938
   262
fun dest_def pp check_head is_fixed is_fixedT eq =
wenzelm@18938
   263
  let
wenzelm@18938
   264
    fun err msg = raise TERM (msg, [eq]);
wenzelm@19071
   265
    val eq_vars = Term.strip_all_vars eq;
wenzelm@19071
   266
    val eq_body = Term.strip_all_body eq;
wenzelm@19071
   267
wenzelm@19071
   268
    val display_terms = commas_quote o map (Pretty.string_of_term pp o Syntax.bound_vars eq_vars);
wenzelm@18938
   269
    val display_types = commas_quote o map (Pretty.string_of_typ pp);
wenzelm@18938
   270
wenzelm@19071
   271
    val (raw_lhs, rhs) = dest_equals eq_body handle TERM _ => err "Not a meta-equality (==)";
wenzelm@19071
   272
    val lhs = Envir.beta_eta_contract raw_lhs;
wenzelm@19071
   273
    val (head, args) = Term.strip_comb lhs;
wenzelm@18938
   274
    val head_tfrees = Term.add_tfrees head [];
wenzelm@18938
   275
wenzelm@18938
   276
    fun check_arg (Bound _) = true
wenzelm@18938
   277
      | check_arg (Free (x, _)) = not (is_fixed x)
wenzelm@18938
   278
      | check_arg (Const ("TYPE", Type ("itself", [TFree _]))) = true
wenzelm@18938
   279
      | check_arg _ = false;
wenzelm@18938
   280
    fun close_arg (Bound _) t = t
wenzelm@18938
   281
      | close_arg x t = Term.all (Term.fastype_of x) $ lambda x t;
wenzelm@18938
   282
wenzelm@18938
   283
    val lhs_bads = filter_out check_arg args;
wenzelm@18964
   284
    val lhs_dups = duplicates (op aconv) args;
wenzelm@18938
   285
    val rhs_extras = Term.fold_aterms (fn v as Free (x, _) =>
wenzelm@18938
   286
      if is_fixed x orelse member (op aconv) args v then I
wenzelm@18938
   287
      else insert (op aconv) v | _ => I) rhs [];
wenzelm@18938
   288
    val rhs_extrasT = Term.fold_aterms (Term.fold_types (fn v as TFree (a, S) =>
wenzelm@18938
   289
      if is_fixedT a orelse member (op =) head_tfrees (a, S) then I
wenzelm@18938
   290
      else insert (op =) v | _ => I)) rhs [];
wenzelm@18938
   291
  in
wenzelm@18938
   292
    if not (check_head head) then
wenzelm@19103
   293
      err ("Bad head of lhs: " ^ term_kind head ^ display_terms [head])
wenzelm@18938
   294
    else if not (null lhs_bads) then
wenzelm@18938
   295
      err ("Bad arguments on lhs: " ^ display_terms lhs_bads)
wenzelm@18938
   296
    else if not (null lhs_dups) then
wenzelm@18938
   297
      err ("Duplicate arguments on lhs: " ^ display_terms lhs_dups)
wenzelm@18938
   298
    else if not (null rhs_extras) then
wenzelm@18938
   299
      err ("Extra variables on rhs: " ^ display_terms rhs_extras)
wenzelm@18938
   300
    else if not (null rhs_extrasT) then
wenzelm@18938
   301
      err ("Extra type variables on rhs: " ^ display_types rhs_extrasT)
wenzelm@18938
   302
    else if exists_subterm (fn t => t aconv head) rhs then
wenzelm@18938
   303
      err "Entity to be defined occurs on rhs"
wenzelm@19071
   304
    else ((lhs, rhs), fold_rev close_arg args (Term.list_all (eq_vars, (mk_equals (lhs, rhs)))))
wenzelm@18938
   305
  end;
wenzelm@18938
   306
wenzelm@18938
   307
(*!!x. c x == t[x] to c == %x. t[x]*)
wenzelm@18938
   308
fun abs_def eq =
wenzelm@18938
   309
  let
wenzelm@18938
   310
    val body = Term.strip_all_body eq;
wenzelm@18938
   311
    val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
wenzelm@18938
   312
    val (lhs, rhs) = dest_equals (Term.subst_bounds (vars, body));
wenzelm@18938
   313
    val (lhs', args) = Term.strip_comb lhs;
wenzelm@18938
   314
    val rhs' = Term.list_abs_free (map Term.dest_Free args, rhs);
wenzelm@18938
   315
  in (lhs', rhs') end;
wenzelm@18938
   316
wenzelm@18938
   317
fun mk_cond_defpair As (lhs, rhs) =
wenzelm@18938
   318
  (case Term.head_of lhs of
wenzelm@18938
   319
    Const (name, _) =>
wenzelm@18938
   320
      (NameSpace.base name ^ "_def", list_implies (As, mk_equals (lhs, rhs)))
wenzelm@18938
   321
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
wenzelm@18938
   322
wenzelm@18938
   323
fun mk_defpair lhs_rhs = mk_cond_defpair [] lhs_rhs;
wenzelm@18938
   324
wenzelm@18938
   325
wenzelm@19406
   326
wenzelm@19775
   327
(** protected propositions and embedded terms **)
wenzelm@9460
   328
wenzelm@18029
   329
val protectC = Const ("prop", propT --> propT);
wenzelm@18029
   330
fun protect t = protectC $ t;
wenzelm@9460
   331
wenzelm@18029
   332
fun unprotect (Const ("prop", _) $ t) = t
wenzelm@18029
   333
  | unprotect t = raise TERM ("unprotect", [t]);
wenzelm@9460
   334
wenzelm@9460
   335
wenzelm@19775
   336
fun mk_term t = Const ("ProtoPure.term", Term.fastype_of t --> propT) $ t;
wenzelm@19775
   337
wenzelm@19775
   338
fun dest_term (Const ("ProtoPure.term", _) $ t) = t
wenzelm@19775
   339
  | dest_term t = raise TERM ("dest_term", [t]);
wenzelm@19775
   340
wenzelm@19775
   341
wenzelm@18181
   342
clasohm@0
   343
(*** Low-level term operations ***)
clasohm@0
   344
clasohm@0
   345
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   346
  The term t must contain no loose bound variables*)
wenzelm@16846
   347
fun occs (t, u) = exists_subterm (fn s => t aconv s) u;
clasohm@0
   348
clasohm@0
   349
(*Close up a formula over all free variables by quantification*)
clasohm@0
   350
fun close_form A =
wenzelm@19425
   351
  Term.list_all_free (rev (Term.add_frees A []), A);
clasohm@0
   352
clasohm@0
   353
wenzelm@18938
   354
clasohm@0
   355
(*** Specialized operations for resolution... ***)
clasohm@0
   356
wenzelm@18938
   357
(*computes t(Bound(n+k-1),...,Bound(n))  *)
wenzelm@18938
   358
fun combound (t, n, k) =
wenzelm@18938
   359
    if  k>0  then  combound (t,n+1,k-1) $ (Bound n)  else  t;
wenzelm@18938
   360
wenzelm@18938
   361
(* ([xn,...,x1], t)   ======>   (x1,...,xn)t *)
wenzelm@18938
   362
fun rlist_abs ([], body) = body
wenzelm@18938
   363
  | rlist_abs ((a,T)::pairs, body) = rlist_abs(pairs, Abs(a, T, body));
wenzelm@18938
   364
wenzelm@18938
   365
wenzelm@16879
   366
local exception SAME in
wenzelm@16879
   367
wenzelm@16879
   368
fun incrT k =
wenzelm@16879
   369
  let
wenzelm@16879
   370
    fun incr (TVar ((a, i), S)) = TVar ((a, i + k), S)
wenzelm@16879
   371
      | incr (Type (a, Ts)) = Type (a, incrs Ts)
wenzelm@16879
   372
      | incr _ = raise SAME
wenzelm@16879
   373
    and incrs (T :: Ts) =
wenzelm@16879
   374
        (incr T :: (incrs Ts handle SAME => Ts)
wenzelm@16879
   375
          handle SAME => T :: incrs Ts)
wenzelm@16879
   376
      | incrs [] = raise SAME;
wenzelm@16879
   377
  in incr end;
wenzelm@16879
   378
clasohm@0
   379
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   380
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
paulson@17120
   381
fun incr_indexes ([], 0) t = t
wenzelm@18029
   382
  | incr_indexes (Ts, k) t =
wenzelm@16879
   383
  let
wenzelm@18029
   384
    val n = length Ts;
wenzelm@16879
   385
    val incrT = if k = 0 then I else incrT k;
wenzelm@16879
   386
wenzelm@16879
   387
    fun incr lev (Var ((x, i), T)) =
wenzelm@18938
   388
          combound (Var ((x, i + k), Ts ---> (incrT T handle SAME => T)), lev, n)
wenzelm@16879
   389
      | incr lev (Abs (x, T, body)) =
wenzelm@16879
   390
          (Abs (x, incrT T, incr (lev + 1) body handle SAME => body)
wenzelm@16879
   391
            handle SAME => Abs (x, T, incr (lev + 1) body))
wenzelm@16879
   392
      | incr lev (t $ u) =
wenzelm@16879
   393
          (incr lev t $ (incr lev u handle SAME => u)
wenzelm@16879
   394
            handle SAME => t $ incr lev u)
wenzelm@16879
   395
      | incr _ (Const (c, T)) = Const (c, incrT T)
wenzelm@16879
   396
      | incr _ (Free (x, T)) = Free (x, incrT T)
wenzelm@16879
   397
      | incr _ (t as Bound _) = t;
wenzelm@16879
   398
  in incr 0 t handle SAME => t end;
wenzelm@16879
   399
wenzelm@16879
   400
fun incr_tvar 0 T = T
wenzelm@16879
   401
  | incr_tvar k T = incrT k T handle SAME => T;
wenzelm@16879
   402
wenzelm@16879
   403
end;
wenzelm@16879
   404
clasohm@0
   405
wenzelm@18248
   406
(* Lifting functions from subgoal and increment:
wenzelm@18029
   407
    lift_abs operates on terms
wenzelm@18029
   408
    lift_all operates on propositions *)
wenzelm@18029
   409
wenzelm@18029
   410
fun lift_abs inc =
wenzelm@18029
   411
  let
wenzelm@18029
   412
    fun lift Ts (Const ("==>", _) $ _ $ B) t = lift Ts B t
wenzelm@18248
   413
      | lift Ts (Const ("all", _) $ Abs (a, T, B)) t = Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   414
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   415
  in lift [] end;
wenzelm@18029
   416
wenzelm@18029
   417
fun lift_all inc =
wenzelm@18029
   418
  let
wenzelm@18029
   419
    fun lift Ts ((c as Const ("==>", _)) $ A $ B) t = c $ A $ lift Ts B t
wenzelm@18248
   420
      | lift Ts ((c as Const ("all", _)) $ Abs (a, T, B)) t = c $ Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   421
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   422
  in lift [] end;
clasohm@0
   423
clasohm@0
   424
(*Strips assumptions in goal, yielding list of hypotheses.   *)
clasohm@0
   425
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
clasohm@0
   426
  | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
clasohm@0
   427
  | strip_assums_hyp B = [];
clasohm@0
   428
clasohm@0
   429
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   430
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   431
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   432
  | strip_assums_concl B = B;
clasohm@0
   433
clasohm@0
   434
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   435
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   436
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   437
  | strip_params B = [];
clasohm@0
   438
wenzelm@9667
   439
(*test for meta connectives in prems of a 'subgoal'*)
wenzelm@9667
   440
fun has_meta_prems prop i =
wenzelm@9667
   441
  let
wenzelm@9667
   442
    fun is_meta (Const ("==>", _) $ _ $ _) = true
wenzelm@10442
   443
      | is_meta (Const ("==", _) $ _ $ _) = true
wenzelm@9667
   444
      | is_meta (Const ("all", _) $ _) = true
wenzelm@9667
   445
      | is_meta _ = false;
wenzelm@9667
   446
  in
berghofe@13659
   447
    (case strip_prems (i, [], prop) of
wenzelm@9667
   448
      (B :: _, _) => exists is_meta (strip_assums_hyp B)
wenzelm@9667
   449
    | _ => false) handle TERM _ => false
wenzelm@9667
   450
  end;
wenzelm@9483
   451
clasohm@0
   452
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
wenzelm@9460
   453
    where j is the total number of parameters (precomputed)
clasohm@0
   454
  If n>0 then deletes assumption n. *)
wenzelm@9460
   455
fun remove_params j n A =
clasohm@0
   456
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   457
    else case A of
wenzelm@9460
   458
        Const("==>", _) $ H $ B =>
wenzelm@9460
   459
          if n=1 then                           (remove_params j (n-1) B)
wenzelm@9460
   460
          else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   461
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   462
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   463
             else A;
clasohm@0
   464
clasohm@0
   465
(** Auto-renaming of parameters in subgoals **)
clasohm@0
   466
clasohm@0
   467
val auto_rename = ref false
clasohm@0
   468
and rename_prefix = ref "ka";
clasohm@0
   469
clasohm@0
   470
(*rename_prefix is not exported; it is set by this function.*)
clasohm@0
   471
fun set_rename_prefix a =
wenzelm@4693
   472
    if a<>"" andalso forall Symbol.is_letter (Symbol.explode a)
clasohm@0
   473
    then  (rename_prefix := a;  auto_rename := true)
clasohm@0
   474
    else  error"rename prefix must be nonempty and consist of letters";
clasohm@0
   475
clasohm@0
   476
(*Makes parameters in a goal have distinctive names (not guaranteed unique!)
clasohm@0
   477
  A name clash could cause the printer to rename bound vars;
clasohm@0
   478
    then res_inst_tac would not work properly.*)
clasohm@0
   479
fun rename_vars (a, []) = []
clasohm@0
   480
  | rename_vars (a, (_,T)::vars) =
wenzelm@12902
   481
        (a,T) :: rename_vars (Symbol.bump_string a, vars);
clasohm@0
   482
clasohm@0
   483
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   484
  if n>0 then deletes assumption n. *)
clasohm@0
   485
fun flatten_params n A =
clasohm@0
   486
    let val params = strip_params A;
wenzelm@9460
   487
        val vars = if !auto_rename
wenzelm@9460
   488
                   then rename_vars (!rename_prefix, params)
wenzelm@9460
   489
                   else ListPair.zip (variantlist(map #1 params,[]),
wenzelm@9460
   490
                                      map #2 params)
clasohm@0
   491
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   492
    end;
clasohm@0
   493
clasohm@0
   494
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   495
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   496
      implies $ A $ list_rename_params (cs, B)
wenzelm@9460
   497
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) =
clasohm@0
   498
      all T $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   499
  | list_rename_params (cs, B) = B;
clasohm@0
   500
wenzelm@19806
   501
(*** Treatment of "assume", "erule", etc. ***)
clasohm@0
   502
wenzelm@16879
   503
(*Strips assumptions in goal yielding
paulson@15451
   504
   HS = [Hn,...,H1],   params = [xm,...,x1], and B,
wenzelm@16879
   505
  where x1...xm are the parameters. This version (21.1.2005) REQUIRES
wenzelm@16879
   506
  the the parameters to be flattened, but it allows erule to work on
paulson@15451
   507
  assumptions of the form !!x. phi. Any !! after the outermost string
paulson@15451
   508
  will be regarded as belonging to the conclusion, and left untouched.
paulson@15454
   509
  Used ONLY by assum_pairs.
paulson@15454
   510
      Unless nasms<0, it can terminate the recursion early; that allows
paulson@15454
   511
  erule to work on assumptions of the form P==>Q.*)
paulson@15454
   512
fun strip_assums_imp (0, Hs, B) = (Hs, B)  (*recursion terminated by nasms*)
wenzelm@16879
   513
  | strip_assums_imp (nasms, Hs, Const("==>", _) $ H $ B) =