src/Pure/logic.ML
author wenzelm
Sat Mar 15 18:08:00 2008 +0100 (2008-03-15)
changeset 26279 e8440c90c474
parent 25939 ddea202704b4
child 26424 a6cad32a27b0
permissions -rw-r--r--
removed obsolete fact_index.ML;
added facts.ML;
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@18762
    11
  val dest_all: term -> typ * term
wenzelm@18181
    12
  val mk_equals: term * term -> term
wenzelm@18181
    13
  val dest_equals: term -> term * term
wenzelm@18181
    14
  val mk_implies: term * term -> term
wenzelm@18181
    15
  val dest_implies: term -> term * term
wenzelm@18181
    16
  val list_implies: term list * term -> term
wenzelm@18181
    17
  val strip_imp_prems: term -> term list
wenzelm@18181
    18
  val strip_imp_concl: term -> term
wenzelm@18181
    19
  val strip_prems: int * term list * term -> term list * term
wenzelm@21576
    20
  val count_prems: term -> int
wenzelm@18181
    21
  val nth_prem: int * term -> term
wenzelm@23418
    22
  val true_prop: 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@23418
    26
  val mk_conjunction_balanced: term list -> term
wenzelm@18469
    27
  val dest_conjunction: term -> term * term
wenzelm@19425
    28
  val dest_conjunction_list: term -> term list
wenzelm@23418
    29
  val dest_conjunction_balanced: int -> term -> term list
wenzelm@18469
    30
  val dest_conjunctions: term -> term list
wenzelm@18181
    31
  val strip_horn: term -> term list * term
wenzelm@21520
    32
  val mk_type: typ -> term
wenzelm@18938
    33
  val dest_type: term -> typ
wenzelm@21520
    34
  val type_map: (term -> term) -> typ -> typ
wenzelm@18938
    35
  val const_of_class: class -> string
wenzelm@18938
    36
  val class_of_const: string -> class
wenzelm@18938
    37
  val mk_inclass: typ * class -> term
wenzelm@18938
    38
  val dest_inclass: term -> typ * class
wenzelm@20630
    39
  val name_classrel: string * string -> string
wenzelm@19406
    40
  val mk_classrel: class * class -> term
wenzelm@19406
    41
  val dest_classrel: term -> class * class
wenzelm@20630
    42
  val name_arities: arity -> string list
wenzelm@20630
    43
  val name_arity: string * sort list * class -> string
wenzelm@20630
    44
  val mk_arities: arity -> term list
wenzelm@19406
    45
  val dest_arity: term -> string * sort list * class
wenzelm@18181
    46
  val protectC: term
wenzelm@18181
    47
  val protect: term -> term
wenzelm@18181
    48
  val unprotect: term -> term
wenzelm@19775
    49
  val mk_term: term -> term
wenzelm@19775
    50
  val dest_term: term -> term
wenzelm@18181
    51
  val occs: term * term -> bool
wenzelm@18181
    52
  val close_form: term -> term
wenzelm@18938
    53
  val combound: term * int * int -> term
wenzelm@18938
    54
  val rlist_abs: (string * typ) list * term -> term
wenzelm@18181
    55
  val incr_indexes: typ list * int -> term -> term
wenzelm@18181
    56
  val incr_tvar: int -> typ -> typ
wenzelm@18181
    57
  val lift_abs: int -> term -> term -> term
wenzelm@18181
    58
  val lift_all: int -> term -> term -> term
wenzelm@18181
    59
  val strip_assums_hyp: term -> term list
wenzelm@9460
    60
  val strip_assums_concl: term -> term
wenzelm@18181
    61
  val strip_params: term -> (string * typ) list
wenzelm@23597
    62
  val has_meta_prems: term -> bool
wenzelm@18181
    63
  val flatten_params: int -> term -> term
clasohm@0
    64
  val list_rename_params: string list * term -> term
wenzelm@18181
    65
  val assum_pairs: int * term -> (term*term)list
wenzelm@19806
    66
  val varifyT: typ -> typ
wenzelm@19806
    67
  val unvarifyT: typ -> typ
wenzelm@18181
    68
  val varify: term -> term
wenzelm@18181
    69
  val unvarify: term -> term
wenzelm@19806
    70
  val legacy_varifyT: typ -> typ
wenzelm@19806
    71
  val legacy_unvarifyT: typ -> typ
wenzelm@19806
    72
  val legacy_varify: term -> term
wenzelm@19806
    73
  val legacy_unvarify: term -> term
wenzelm@18181
    74
  val get_goal: term -> int -> term
wenzelm@18181
    75
  val goal_params: term -> int -> term * term list
wenzelm@18181
    76
  val prems_of_goal: term -> int -> term list
wenzelm@18181
    77
  val concl_of_goal: term -> int -> term
wenzelm@4345
    78
end;
clasohm@0
    79
paulson@1500
    80
structure Logic : LOGIC =
clasohm@0
    81
struct
wenzelm@398
    82
wenzelm@4345
    83
clasohm@0
    84
(*** Abstract syntax operations on the meta-connectives ***)
clasohm@0
    85
wenzelm@23238
    86
(** all **)
nipkow@5041
    87
wenzelm@18762
    88
fun dest_all (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ A) = (T, A)
wenzelm@18762
    89
  | dest_all t = raise TERM ("dest_all", [t]);
wenzelm@18762
    90
wenzelm@18762
    91
clasohm@0
    92
(** equality **)
clasohm@0
    93
wenzelm@20883
    94
fun mk_equals (t, u) = Term.equals (Term.fastype_of t) $ t $ u;
clasohm@0
    95
wenzelm@20883
    96
fun dest_equals (Const ("==", _) $ t $ u) = (t, u)
wenzelm@20883
    97
  | dest_equals t = raise TERM ("dest_equals", [t]);
wenzelm@637
    98
wenzelm@637
    99
clasohm@0
   100
(** implies **)
clasohm@0
   101
wenzelm@20883
   102
fun mk_implies (A, B) = implies $ A $ B;
clasohm@0
   103
wenzelm@20883
   104
fun dest_implies (Const ("==>", _) $ A $ B) = (A, B)
wenzelm@20883
   105
  | dest_implies A = raise TERM ("dest_implies", [A]);
nipkow@5041
   106
wenzelm@4822
   107
clasohm@0
   108
(** nested implications **)
clasohm@0
   109
clasohm@0
   110
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
wenzelm@18181
   111
fun list_implies ([], B) = B
wenzelm@18181
   112
  | list_implies (A::As, B) = implies $ A $ list_implies(As,B);
clasohm@0
   113
clasohm@0
   114
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
   115
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
   116
  | strip_imp_prems _ = [];
clasohm@0
   117
clasohm@0
   118
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
   119
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
   120
  | strip_imp_concl A = A : term;
clasohm@0
   121
clasohm@0
   122
(*Strip and return premises: (i, [], A1==>...Ai==>B)
wenzelm@9460
   123
    goes to   ([Ai, A(i-1),...,A1] , B)         (REVERSED)
clasohm@0
   124
  if  i<0 or else i too big then raises  TERM*)
wenzelm@9460
   125
fun strip_prems (0, As, B) = (As, B)
wenzelm@9460
   126
  | strip_prems (i, As, Const("==>", _) $ A $ B) =
wenzelm@9460
   127
        strip_prems (i-1, A::As, B)
clasohm@0
   128
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   129
wenzelm@16130
   130
(*Count premises -- quicker than (length o strip_prems) *)
wenzelm@21576
   131
fun count_prems (Const ("==>", _) $ _ $ B) = 1 + count_prems B
wenzelm@21576
   132
  | count_prems _ = 0;
clasohm@0
   133
wenzelm@16130
   134
(*Select Ai from A1 ==>...Ai==>B*)
wenzelm@16130
   135
fun nth_prem (1, Const ("==>", _) $ A $ _) = A
wenzelm@16130
   136
  | nth_prem (i, Const ("==>", _) $ _ $ B) = nth_prem (i - 1, B)
wenzelm@16130
   137
  | nth_prem (_, A) = raise TERM ("nth_prem", [A]);
wenzelm@16130
   138
berghofe@13659
   139
(*strip a proof state (Horn clause):
berghofe@13659
   140
  B1 ==> ... Bn ==> C   goes to   ([B1, ..., Bn], C)    *)
berghofe@13659
   141
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
berghofe@13659
   142
wenzelm@4822
   143
wenzelm@23418
   144
wenzelm@12137
   145
(** conjunction **)
wenzelm@12137
   146
wenzelm@23418
   147
val true_prop = Term.all propT $ Abs ("dummy", propT, mk_implies (Bound 0, Bound 0));
wenzelm@19125
   148
val conjunction = Const ("ProtoPure.conjunction", propT --> propT --> propT);
wenzelm@19125
   149
wenzelm@23418
   150
wenzelm@18499
   151
(*A && B*)
wenzelm@19125
   152
fun mk_conjunction (A, B) = conjunction $ A $ B;
wenzelm@12137
   153
wenzelm@18499
   154
(*A && B && C -- improper*)
wenzelm@23418
   155
fun mk_conjunction_list [] = true_prop
wenzelm@12757
   156
  | mk_conjunction_list ts = foldr1 mk_conjunction ts;
wenzelm@12137
   157
wenzelm@23418
   158
(*(A && B) && (C && D) -- balanced*)
wenzelm@23418
   159
fun mk_conjunction_balanced [] = true_prop
wenzelm@23418
   160
  | mk_conjunction_balanced ts = BalancedTree.make mk_conjunction ts;
wenzelm@23418
   161
wenzelm@18499
   162
wenzelm@18499
   163
(*A && B*)
wenzelm@19125
   164
fun dest_conjunction (Const ("ProtoPure.conjunction", _) $ A $ B) = (A, B)
wenzelm@18469
   165
  | dest_conjunction t = raise TERM ("dest_conjunction", [t]);
wenzelm@18469
   166
wenzelm@19425
   167
(*A && B && C -- improper*)
wenzelm@19425
   168
fun dest_conjunction_list t =
wenzelm@19425
   169
  (case try dest_conjunction t of
wenzelm@19425
   170
    NONE => [t]
wenzelm@19425
   171
  | SOME (A, B) => A :: dest_conjunction_list B);
wenzelm@19425
   172
wenzelm@23418
   173
(*(A && B) && (C && D) -- balanced*)
wenzelm@23418
   174
fun dest_conjunction_balanced 0 _ = []
wenzelm@23418
   175
  | dest_conjunction_balanced n t = BalancedTree.dest dest_conjunction n t;
wenzelm@23418
   176
wenzelm@18499
   177
(*((A && B) && C) && D && E -- flat*)
wenzelm@18469
   178
fun dest_conjunctions t =
wenzelm@18469
   179
  (case try dest_conjunction t of
wenzelm@18469
   180
    NONE => [t]
wenzelm@18469
   181
  | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
wenzelm@18469
   182
wenzelm@18469
   183
wenzelm@12137
   184
wenzelm@398
   185
(** types as terms **)
wenzelm@398
   186
wenzelm@19391
   187
fun mk_type ty = Const ("TYPE", Term.itselfT ty);
wenzelm@398
   188
wenzelm@398
   189
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   190
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   191
wenzelm@21520
   192
fun type_map f = dest_type o f o mk_type;
wenzelm@21520
   193
wenzelm@4822
   194
wenzelm@19406
   195
wenzelm@19406
   196
(** type classes **)
wenzelm@19406
   197
wenzelm@19406
   198
(* const names *)
wenzelm@398
   199
wenzelm@18938
   200
val classN = "_class";
wenzelm@18938
   201
wenzelm@18938
   202
val const_of_class = suffix classN;
wenzelm@19406
   203
wenzelm@18938
   204
fun class_of_const c = unsuffix classN c
wenzelm@18938
   205
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@18938
   206
wenzelm@19406
   207
wenzelm@19406
   208
(* class constraints *)
wenzelm@19406
   209
wenzelm@398
   210
fun mk_inclass (ty, c) =
wenzelm@19391
   211
  Const (const_of_class c, Term.itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   212
wenzelm@19406
   213
fun dest_inclass (t as Const (c_class, _) $ ty) = (dest_type ty, class_of_const c_class)
wenzelm@398
   214
  | dest_inclass t = raise TERM ("dest_inclass", [t]);
wenzelm@398
   215
clasohm@0
   216
wenzelm@19406
   217
(* class relations *)
wenzelm@19406
   218
wenzelm@20630
   219
fun name_classrel (c1, c2) =
wenzelm@20630
   220
  NameSpace.base c1 ^ "_" ^ NameSpace.base c2;
wenzelm@20630
   221
wenzelm@19406
   222
fun mk_classrel (c1, c2) = mk_inclass (Term.aT [c1], c2);
wenzelm@19406
   223
wenzelm@19406
   224
fun dest_classrel tm =
wenzelm@19406
   225
  (case dest_inclass tm of
wenzelm@19406
   226
    (TVar (_, [c1]), c2) => (c1, c2)
wenzelm@19406
   227
  | _ => raise TERM ("dest_classrel", [tm]));
wenzelm@19406
   228
wenzelm@19406
   229
wenzelm@19406
   230
(* type arities *)
wenzelm@19406
   231
wenzelm@20630
   232
fun name_arities (t, _, S) =
wenzelm@20630
   233
  let val b = NameSpace.base t
wenzelm@20630
   234
  in S |> map (fn c => NameSpace.base c ^ "_" ^ b) end;
wenzelm@20630
   235
wenzelm@20630
   236
fun name_arity (t, dom, c) = hd (name_arities (t, dom, [c]));
wenzelm@20630
   237
wenzelm@19406
   238
fun mk_arities (t, Ss, S) =
wenzelm@24848
   239
  let val T = Type (t, ListPair.map TFree (Name.invents Name.context Name.aT (length Ss), Ss))
wenzelm@19406
   240
  in map (fn c => mk_inclass (T, c)) S end;
wenzelm@19406
   241
wenzelm@19406
   242
fun dest_arity tm =
wenzelm@19406
   243
  let
wenzelm@19406
   244
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@19406
   245
wenzelm@19406
   246
    val (ty, c) = dest_inclass tm;
wenzelm@19406
   247
    val (t, tvars) =
wenzelm@19406
   248
      (case ty of
wenzelm@19406
   249
        Type (t, tys) => (t, map dest_TVar tys handle TYPE _ => err ())
wenzelm@19406
   250
      | _ => err ());
wenzelm@19406
   251
    val Ss =
wenzelm@19406
   252
      if has_duplicates (eq_fst (op =)) tvars then err ()
wenzelm@19406
   253
      else map snd tvars;
wenzelm@19406
   254
  in (t, Ss, c) end;
wenzelm@19406
   255
wenzelm@19406
   256
wenzelm@18938
   257
wenzelm@24258
   258
  (** protected propositions and embedded terms **)
wenzelm@9460
   259
wenzelm@18029
   260
val protectC = Const ("prop", propT --> propT);
wenzelm@18029
   261
fun protect t = protectC $ t;
wenzelm@9460
   262
wenzelm@18029
   263
fun unprotect (Const ("prop", _) $ t) = t
wenzelm@18029
   264
  | unprotect t = raise TERM ("unprotect", [t]);
wenzelm@9460
   265
wenzelm@9460
   266
wenzelm@19775
   267
fun mk_term t = Const ("ProtoPure.term", Term.fastype_of t --> propT) $ t;
wenzelm@19775
   268
wenzelm@19775
   269
fun dest_term (Const ("ProtoPure.term", _) $ t) = t
wenzelm@19775
   270
  | dest_term t = raise TERM ("dest_term", [t]);
wenzelm@19775
   271
wenzelm@19775
   272
wenzelm@18181
   273
clasohm@0
   274
(*** Low-level term operations ***)
clasohm@0
   275
clasohm@0
   276
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   277
  The term t must contain no loose bound variables*)
wenzelm@16846
   278
fun occs (t, u) = exists_subterm (fn s => t aconv s) u;
clasohm@0
   279
clasohm@0
   280
(*Close up a formula over all free variables by quantification*)
clasohm@0
   281
fun close_form A =
wenzelm@19425
   282
  Term.list_all_free (rev (Term.add_frees A []), A);
clasohm@0
   283
clasohm@0
   284
wenzelm@18938
   285
clasohm@0
   286
(*** Specialized operations for resolution... ***)
clasohm@0
   287
wenzelm@18938
   288
(*computes t(Bound(n+k-1),...,Bound(n))  *)
wenzelm@18938
   289
fun combound (t, n, k) =
wenzelm@18938
   290
    if  k>0  then  combound (t,n+1,k-1) $ (Bound n)  else  t;
wenzelm@18938
   291
wenzelm@18938
   292
(* ([xn,...,x1], t)   ======>   (x1,...,xn)t *)
wenzelm@18938
   293
fun rlist_abs ([], body) = body
wenzelm@18938
   294
  | rlist_abs ((a,T)::pairs, body) = rlist_abs(pairs, Abs(a, T, body));
wenzelm@18938
   295
wenzelm@18938
   296
wenzelm@16879
   297
local exception SAME in
wenzelm@16879
   298
wenzelm@16879
   299
fun incrT k =
wenzelm@16879
   300
  let
wenzelm@16879
   301
    fun incr (TVar ((a, i), S)) = TVar ((a, i + k), S)
wenzelm@16879
   302
      | incr (Type (a, Ts)) = Type (a, incrs Ts)
wenzelm@16879
   303
      | incr _ = raise SAME
wenzelm@16879
   304
    and incrs (T :: Ts) =
wenzelm@16879
   305
        (incr T :: (incrs Ts handle SAME => Ts)
wenzelm@16879
   306
          handle SAME => T :: incrs Ts)
wenzelm@16879
   307
      | incrs [] = raise SAME;
wenzelm@16879
   308
  in incr end;
wenzelm@16879
   309
clasohm@0
   310
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   311
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
paulson@17120
   312
fun incr_indexes ([], 0) t = t
wenzelm@18029
   313
  | incr_indexes (Ts, k) t =
wenzelm@16879
   314
  let
wenzelm@18029
   315
    val n = length Ts;
wenzelm@16879
   316
    val incrT = if k = 0 then I else incrT k;
wenzelm@16879
   317
wenzelm@16879
   318
    fun incr lev (Var ((x, i), T)) =
wenzelm@18938
   319
          combound (Var ((x, i + k), Ts ---> (incrT T handle SAME => T)), lev, n)
wenzelm@16879
   320
      | incr lev (Abs (x, T, body)) =
wenzelm@16879
   321
          (Abs (x, incrT T, incr (lev + 1) body handle SAME => body)
wenzelm@16879
   322
            handle SAME => Abs (x, T, incr (lev + 1) body))
wenzelm@16879
   323
      | incr lev (t $ u) =
wenzelm@16879
   324
          (incr lev t $ (incr lev u handle SAME => u)
wenzelm@16879
   325
            handle SAME => t $ incr lev u)
wenzelm@16879
   326
      | incr _ (Const (c, T)) = Const (c, incrT T)
wenzelm@16879
   327
      | incr _ (Free (x, T)) = Free (x, incrT T)
wenzelm@16879
   328
      | incr _ (t as Bound _) = t;
wenzelm@16879
   329
  in incr 0 t handle SAME => t end;
wenzelm@16879
   330
wenzelm@16879
   331
fun incr_tvar 0 T = T
wenzelm@16879
   332
  | incr_tvar k T = incrT k T handle SAME => T;
wenzelm@16879
   333
wenzelm@16879
   334
end;
wenzelm@16879
   335
clasohm@0
   336
wenzelm@18248
   337
(* Lifting functions from subgoal and increment:
wenzelm@18029
   338
    lift_abs operates on terms
wenzelm@18029
   339
    lift_all operates on propositions *)
wenzelm@18029
   340
wenzelm@18029
   341
fun lift_abs inc =
wenzelm@18029
   342
  let
wenzelm@18029
   343
    fun lift Ts (Const ("==>", _) $ _ $ B) t = lift Ts B t
wenzelm@18248
   344
      | lift Ts (Const ("all", _) $ Abs (a, T, B)) t = Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   345
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   346
  in lift [] end;
wenzelm@18029
   347
wenzelm@18029
   348
fun lift_all inc =
wenzelm@18029
   349
  let
wenzelm@18029
   350
    fun lift Ts ((c as Const ("==>", _)) $ A $ B) t = c $ A $ lift Ts B t
wenzelm@18248
   351
      | lift Ts ((c as Const ("all", _)) $ Abs (a, T, B)) t = c $ Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   352
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   353
  in lift [] end;
clasohm@0
   354
clasohm@0
   355
(*Strips assumptions in goal, yielding list of hypotheses.   *)
berghofe@21016
   356
fun strip_assums_hyp B =
berghofe@21016
   357
  let
berghofe@21016
   358
    fun strip Hs (Const ("==>", _) $ H $ B) = strip (H :: Hs) B
berghofe@21016
   359
      | strip Hs (Const ("all", _) $ Abs (a, T, t)) =
berghofe@21016
   360
          strip (map (incr_boundvars 1) Hs) t
berghofe@21016
   361
      | strip Hs B = rev Hs
berghofe@21016
   362
  in strip [] B end;
clasohm@0
   363
clasohm@0
   364
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   365
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   366
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   367
  | strip_assums_concl B = B;
clasohm@0
   368
clasohm@0
   369
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   370
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   371
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   372
  | strip_params B = [];
clasohm@0
   373
wenzelm@23597
   374
(*test for nested meta connectives in prems*)
wenzelm@23597
   375
val has_meta_prems =
wenzelm@9667
   376
  let
wenzelm@23597
   377
    fun is_meta (Const ("==", _) $ _ $ _) = true
wenzelm@23597
   378
      | is_meta (Const ("==>", _) $ _ $ _) = true
wenzelm@9667
   379
      | is_meta (Const ("all", _) $ _) = true
wenzelm@9667
   380
      | is_meta _ = false;
wenzelm@23597
   381
    fun ex_meta (Const ("==>", _) $ A $ B) = is_meta A orelse ex_meta B
wenzelm@23597
   382
      | ex_meta (Const ("all", _) $ Abs (_, _, B)) = ex_meta B
wenzelm@23597
   383
      | ex_meta _ = false;
wenzelm@23597
   384
  in ex_meta end;
wenzelm@9483
   385
clasohm@0
   386
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
wenzelm@9460
   387
    where j is the total number of parameters (precomputed)
clasohm@0
   388
  If n>0 then deletes assumption n. *)
wenzelm@9460
   389
fun remove_params j n A =
clasohm@0
   390
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   391
    else case A of
wenzelm@9460
   392
        Const("==>", _) $ H $ B =>
wenzelm@9460
   393
          if n=1 then                           (remove_params j (n-1) B)
wenzelm@9460
   394
          else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   395
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   396
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   397
             else A;
clasohm@0
   398
clasohm@0
   399
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   400
  if n>0 then deletes assumption n. *)
clasohm@0
   401
fun flatten_params n A =
clasohm@0
   402
    let val params = strip_params A;
berghofe@25939
   403
        val vars = ListPair.zip (Name.variant_list [] (map #1 params),
berghofe@25939
   404
                                 map #2 params)
clasohm@0
   405
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   406
    end;
clasohm@0
   407
clasohm@0
   408
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   409
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   410
      implies $ A $ list_rename_params (cs, B)
wenzelm@9460
   411
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) =
clasohm@0
   412
      all T $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   413
  | list_rename_params (cs, B) = B;
clasohm@0
   414
wenzelm@19806
   415
(*** Treatment of "assume", "erule", etc. ***)
clasohm@0
   416
wenzelm@16879
   417
(*Strips assumptions in goal yielding
paulson@15451
   418
   HS = [Hn,...,H1],   params = [xm,...,x1], and B,
wenzelm@16879
   419
  where x1...xm are the parameters. This version (21.1.2005) REQUIRES
wenzelm@16879
   420
  the the parameters to be flattened, but it allows erule to work on
paulson@15451
   421
  assumptions of the form !!x. phi. Any !! after the outermost string
paulson@15451
   422
  will be regarded as belonging to the conclusion, and left untouched.
paulson@15454
   423
  Used ONLY by assum_pairs.
paulson@15454
   424
      Unless nasms<0, it can terminate the recursion early; that allows
paulson@15454
   425
  erule to work on assumptions of the form P==>Q.*)
paulson@15454
   426
fun strip_assums_imp (0, Hs, B) = (Hs, B)  (*recursion terminated by nasms*)
wenzelm@16879
   427
  | strip_assums_imp (nasms, Hs, Const("==>", _) $ H $ B) =
paulson@15454
   428
      strip_assums_imp (nasms-1, H::Hs, B)
paulson@15454
   429
  | strip_assums_imp (_, Hs, B) = (Hs, B); (*recursion terminated by B*)
paulson@15454
   430
clasohm@0
   431
paulson@15451
   432
(*Strips OUTER parameters only, unlike similar legacy versions.*)
paulson@15451
   433
fun strip_assums_all (params, Const("all",_)$Abs(a,T,t)) =
paulson@15451
   434
      strip_assums_all ((a,T)::params, t)
paulson@15451
   435
  | strip_assums_all (params, B) = (params, B);
clasohm@0
   436
clasohm@0
   437
(*Produces disagreement pairs, one for each assumption proof, in order.
clasohm@0
   438
  A is the first premise of the lifted rule, and thus has the form
paulson@15454
   439
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B).
paulson@15454
   440
  nasms is the number of assumptions in the original subgoal, needed when B
paulson@15454
   441
    has the form B1 ==> B2: it stops B1 from being taken as an assumption. *)
paulson@15454
   442
fun assum_pairs(nasms,A) =
paulson@15451
   443
  let val (params, A') = strip_assums_all ([],A)
paulson@15454
   444
      val (Hs,B) = strip_assums_imp (nasms,[],A')
wenzelm@18938
   445
      fun abspar t = rlist_abs(params, t)
paulson@15451
   446
      val D = abspar B
paulson@15451
   447
      fun pairrev ([], pairs) = pairs
paulson@15451
   448
        | pairrev (H::Hs, pairs) = pairrev(Hs,  (abspar H, D) :: pairs)
paulson@15451
   449
  in  pairrev (Hs,[])
clasohm@0
   450
  end;
clasohm@0
   451
wenzelm@19806
   452
wenzelm@19806
   453
(* global schematic variables *)
wenzelm@19806
   454
wenzelm@19806
   455
fun bad_schematic xi = "Illegal schematic variable: " ^ quote (Term.string_of_vname xi);
wenzelm@19806
   456
fun bad_fixed x = "Illegal fixed variable: " ^ quote x;
wenzelm@19806
   457
wenzelm@19806
   458
fun varifyT ty = ty |> Term.map_atyps
wenzelm@19806
   459
  (fn TFree (a, S) => TVar ((a, 0), S)
wenzelm@19806
   460
    | TVar (ai, _) => raise TYPE (bad_schematic ai, [ty], []));
wenzelm@19806
   461
wenzelm@19806
   462
fun unvarifyT ty = ty |> Term.map_atyps
wenzelm@19806
   463
  (fn TVar ((a, 0), S) => TFree (a, S)
wenzelm@19806
   464
    | TVar (ai, _) => raise TYPE (bad_schematic ai, [ty], [])
wenzelm@19806
   465
    | TFree (a, _) => raise TYPE (bad_fixed a, [ty], []));
clasohm@0
   466
wenzelm@19806
   467
fun varify tm =
wenzelm@19806
   468
  tm |> Term.map_aterms
wenzelm@19806
   469
    (fn Free (x, T) => Var ((x, 0), T)
wenzelm@19806
   470
      | Var (xi, _) => raise TERM (bad_schematic xi, [tm])
wenzelm@19806
   471
      | t => t)
wenzelm@20548
   472
  |> Term.map_types varifyT
wenzelm@19879
   473
  handle TYPE (msg, _, _) => raise TERM (msg, [tm]);
wenzelm@19806
   474
wenzelm@19806
   475
fun unvarify tm =
wenzelm@19806
   476
  tm |> Term.map_aterms
wenzelm@19806
   477
    (fn Var ((x, 0), T) => Free (x, T)
wenzelm@19806
   478
      | Var (xi, _) => raise TERM (bad_schematic xi, [tm])
wenzelm@19806
   479
      | Free (x, _) => raise TERM (bad_fixed x, [tm])
wenzelm@19806
   480
      | t => t)
wenzelm@20548
   481
  |> Term.map_types unvarifyT
wenzelm@19879
   482
  handle TYPE (msg, _, _) => raise TERM (msg, [tm]);
wenzelm@19806
   483
wenzelm@19806
   484
val legacy_varifyT = Term.map_atyps (fn TFree (a, S) => TVar ((a, 0), S) | T => T);
wenzelm@19806
   485
val legacy_unvarifyT = Term.map_atyps (fn TVar ((a, 0), S) => TFree (a, S) | T => T);
wenzelm@19806
   486
wenzelm@19806
   487
val legacy_varify =
wenzelm@19806
   488
  Term.map_aterms (fn Free (x, T) => Var ((x, 0), T) | t => t) #>
wenzelm@20548
   489
  Term.map_types legacy_varifyT;
wenzelm@19806
   490
wenzelm@19806
   491
val legacy_unvarify =
wenzelm@19806
   492
  Term.map_aterms (fn Var ((x, 0), T) => Free (x, T) | t => t) #>
wenzelm@20548
   493
  Term.map_types legacy_unvarifyT;
lcp@546
   494
berghofe@13799
   495
wenzelm@16862
   496
(* goal states *)
wenzelm@16862
   497
wenzelm@16862
   498
fun get_goal st i = nth_prem (i, st)
wenzelm@16862
   499
  handle TERM _ => error "Goal number out of range";
berghofe@13799
   500
berghofe@13799
   501
(*reverses parameters for substitution*)
berghofe@13799
   502
fun goal_params st i =
berghofe@13799
   503
  let val gi = get_goal st i
berghofe@14137
   504
      val rfrees = map Free (rename_wrt_term gi (strip_params gi))
berghofe@13799
   505
  in (gi, rfrees) end;
berghofe@13799
   506
berghofe@13799
   507
fun concl_of_goal st i =
berghofe@13799
   508
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   509
      val B = strip_assums_concl gi
berghofe@13799
   510
  in subst_bounds (rfrees, B) end;
berghofe@13799
   511
berghofe@13799
   512
fun prems_of_goal st i =
berghofe@13799
   513
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   514
      val As = strip_assums_hyp gi
berghofe@13799
   515
  in map (fn A => subst_bounds (rfrees, A)) As end;
berghofe@13799
   516
clasohm@0
   517
end;