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