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