src/Pure/logic.ML
author wenzelm
Thu Jul 16 21:29:02 2009 +0200 (2009-07-16)
changeset 32023 2d071ac5032f
parent 32020 9abf5d66606e
child 32026 9898880061df
permissions -rw-r--r--
export incr_tvar_same;
tuned;
wenzelm@9460
     1
(*  Title:      Pure/logic.ML
wenzelm@9460
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@29276
     3
    Author:     Makarius
clasohm@0
     4
wenzelm@9460
     5
Abstract syntax operations of the Pure meta-logic.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@9460
     8
signature LOGIC =
wenzelm@4345
     9
sig
wenzelm@27334
    10
  val all: term -> term -> term
wenzelm@27334
    11
  val is_all: term -> bool
wenzelm@27334
    12
  val dest_all: term -> (string * typ) * term
wenzelm@18181
    13
  val mk_equals: term * term -> term
wenzelm@18181
    14
  val dest_equals: term -> term * term
wenzelm@27334
    15
  val implies: term
wenzelm@18181
    16
  val mk_implies: term * term -> term
wenzelm@18181
    17
  val dest_implies: term -> term * term
wenzelm@18181
    18
  val list_implies: term list * term -> term
wenzelm@18181
    19
  val strip_imp_prems: term -> term list
wenzelm@18181
    20
  val strip_imp_concl: term -> term
wenzelm@18181
    21
  val strip_prems: int * term list * term -> term list * term
wenzelm@21576
    22
  val count_prems: term -> int
wenzelm@18181
    23
  val nth_prem: int * term -> term
wenzelm@23418
    24
  val true_prop: 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@23418
    28
  val mk_conjunction_balanced: term list -> term
wenzelm@18469
    29
  val dest_conjunction: term -> term * term
wenzelm@19425
    30
  val dest_conjunction_list: term -> term list
wenzelm@23418
    31
  val dest_conjunction_balanced: int -> term -> term list
wenzelm@18469
    32
  val dest_conjunctions: term -> term list
wenzelm@18181
    33
  val strip_horn: term -> term list * term
wenzelm@21520
    34
  val mk_type: typ -> term
wenzelm@18938
    35
  val dest_type: term -> typ
wenzelm@21520
    36
  val type_map: (term -> term) -> typ -> typ
wenzelm@18938
    37
  val const_of_class: class -> string
wenzelm@18938
    38
  val class_of_const: string -> class
wenzelm@31943
    39
  val mk_of_class: typ * class -> term
wenzelm@31943
    40
  val dest_of_class: term -> typ * class
wenzelm@20630
    41
  val name_classrel: string * string -> string
wenzelm@19406
    42
  val mk_classrel: class * class -> term
wenzelm@19406
    43
  val dest_classrel: term -> class * class
wenzelm@20630
    44
  val name_arities: arity -> string list
wenzelm@20630
    45
  val name_arity: string * sort list * class -> string
wenzelm@20630
    46
  val mk_arities: arity -> term list
wenzelm@19406
    47
  val dest_arity: term -> string * sort list * class
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@32023
    57
  val incr_tvar_same: int -> typ Same.operation
wenzelm@32023
    58
  val incr_tvar: int -> typ -> typ
wenzelm@18181
    59
  val incr_indexes: typ list * int -> term -> term
wenzelm@18181
    60
  val lift_abs: int -> term -> term -> term
wenzelm@18181
    61
  val lift_all: int -> term -> term -> term
wenzelm@18181
    62
  val strip_assums_hyp: term -> term list
wenzelm@9460
    63
  val strip_assums_concl: term -> term
wenzelm@18181
    64
  val strip_params: term -> (string * typ) list
wenzelm@23597
    65
  val has_meta_prems: term -> bool
wenzelm@18181
    66
  val flatten_params: int -> term -> term
clasohm@0
    67
  val list_rename_params: string list * term -> term
wenzelm@18181
    68
  val assum_pairs: int * term -> (term*term)list
wenzelm@30554
    69
  val assum_problems: int * term -> (term -> term) * term list * term
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@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@27334
    88
fun all v t = Const ("all", (Term.fastype_of v --> propT) --> propT) $ lambda v t;
wenzelm@27334
    89
wenzelm@27334
    90
fun is_all (Const ("all", _) $ Abs _) = true
wenzelm@27334
    91
  | is_all _ = false;
wenzelm@27334
    92
wenzelm@27334
    93
fun dest_all (Const ("all", _) $ Abs (abs as (_, T, _))) =
wenzelm@27334
    94
      let val (x, b) = Term.dest_abs abs  (*potentially slow*)
wenzelm@27334
    95
      in ((x, T), b) end
wenzelm@18762
    96
  | dest_all t = raise TERM ("dest_all", [t]);
wenzelm@18762
    97
wenzelm@18762
    98
clasohm@0
    99
(** equality **)
clasohm@0
   100
wenzelm@27334
   101
fun mk_equals (t, u) =
wenzelm@27334
   102
  let val T = Term.fastype_of t
wenzelm@27334
   103
  in Const ("==", T --> T --> propT) $ t $ u end;
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@27334
   111
val implies = Const ("==>", propT --> propT --> propT);
wenzelm@27334
   112
wenzelm@20883
   113
fun mk_implies (A, B) = implies $ A $ B;
clasohm@0
   114
wenzelm@20883
   115
fun dest_implies (Const ("==>", _) $ A $ B) = (A, B)
wenzelm@20883
   116
  | dest_implies A = raise TERM ("dest_implies", [A]);
nipkow@5041
   117
wenzelm@4822
   118
clasohm@0
   119
(** nested implications **)
clasohm@0
   120
clasohm@0
   121
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
wenzelm@18181
   122
fun list_implies ([], B) = B
wenzelm@18181
   123
  | list_implies (A::As, B) = implies $ A $ list_implies(As,B);
clasohm@0
   124
clasohm@0
   125
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
   126
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
   127
  | strip_imp_prems _ = [];
clasohm@0
   128
clasohm@0
   129
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
   130
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
   131
  | strip_imp_concl A = A : term;
clasohm@0
   132
clasohm@0
   133
(*Strip and return premises: (i, [], A1==>...Ai==>B)
wenzelm@9460
   134
    goes to   ([Ai, A(i-1),...,A1] , B)         (REVERSED)
clasohm@0
   135
  if  i<0 or else i too big then raises  TERM*)
wenzelm@9460
   136
fun strip_prems (0, As, B) = (As, B)
wenzelm@9460
   137
  | strip_prems (i, As, Const("==>", _) $ A $ B) =
wenzelm@9460
   138
        strip_prems (i-1, A::As, B)
clasohm@0
   139
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   140
wenzelm@16130
   141
(*Count premises -- quicker than (length o strip_prems) *)
wenzelm@21576
   142
fun count_prems (Const ("==>", _) $ _ $ B) = 1 + count_prems B
wenzelm@21576
   143
  | count_prems _ = 0;
clasohm@0
   144
wenzelm@16130
   145
(*Select Ai from A1 ==>...Ai==>B*)
wenzelm@16130
   146
fun nth_prem (1, Const ("==>", _) $ A $ _) = A
wenzelm@16130
   147
  | nth_prem (i, Const ("==>", _) $ _ $ B) = nth_prem (i - 1, B)
wenzelm@16130
   148
  | nth_prem (_, A) = raise TERM ("nth_prem", [A]);
wenzelm@16130
   149
berghofe@13659
   150
(*strip a proof state (Horn clause):
berghofe@13659
   151
  B1 ==> ... Bn ==> C   goes to   ([B1, ..., Bn], C)    *)
berghofe@13659
   152
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
berghofe@13659
   153
wenzelm@4822
   154
wenzelm@23418
   155
wenzelm@12137
   156
(** conjunction **)
wenzelm@12137
   157
wenzelm@23418
   158
val true_prop = Term.all propT $ Abs ("dummy", propT, mk_implies (Bound 0, Bound 0));
wenzelm@26424
   159
val conjunction = Const ("Pure.conjunction", propT --> propT --> propT);
wenzelm@19125
   160
wenzelm@23418
   161
wenzelm@28856
   162
(*A &&& B*)
wenzelm@19125
   163
fun mk_conjunction (A, B) = conjunction $ A $ B;
wenzelm@12137
   164
wenzelm@28856
   165
(*A &&& B &&& C -- improper*)
wenzelm@23418
   166
fun mk_conjunction_list [] = true_prop
wenzelm@12757
   167
  | mk_conjunction_list ts = foldr1 mk_conjunction ts;
wenzelm@12137
   168
wenzelm@28856
   169
(*(A &&& B) &&& (C &&& D) -- balanced*)
wenzelm@23418
   170
fun mk_conjunction_balanced [] = true_prop
wenzelm@23418
   171
  | mk_conjunction_balanced ts = BalancedTree.make mk_conjunction ts;
wenzelm@23418
   172
wenzelm@18499
   173
wenzelm@28856
   174
(*A &&& B*)
wenzelm@26424
   175
fun dest_conjunction (Const ("Pure.conjunction", _) $ A $ B) = (A, B)
wenzelm@18469
   176
  | dest_conjunction t = raise TERM ("dest_conjunction", [t]);
wenzelm@18469
   177
wenzelm@28856
   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@28856
   184
(*(A &&& B) &&& (C &&& D) -- balanced*)
wenzelm@23418
   185
fun dest_conjunction_balanced 0 _ = []
wenzelm@23418
   186
  | dest_conjunction_balanced n t = BalancedTree.dest dest_conjunction n t;
wenzelm@23418
   187
wenzelm@28856
   188
(*((A &&& B) &&& C) &&& D &&& E -- flat*)
wenzelm@18469
   189
fun dest_conjunctions t =
wenzelm@18469
   190
  (case try dest_conjunction t of
wenzelm@18469
   191
    NONE => [t]
wenzelm@18469
   192
  | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
wenzelm@18469
   193
wenzelm@18469
   194
wenzelm@12137
   195
wenzelm@398
   196
(** types as terms **)
wenzelm@398
   197
wenzelm@19391
   198
fun mk_type ty = Const ("TYPE", Term.itselfT ty);
wenzelm@398
   199
wenzelm@398
   200
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   201
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   202
wenzelm@21520
   203
fun type_map f = dest_type o f o mk_type;
wenzelm@21520
   204
wenzelm@4822
   205
wenzelm@19406
   206
wenzelm@19406
   207
(** type classes **)
wenzelm@19406
   208
wenzelm@19406
   209
(* const names *)
wenzelm@398
   210
wenzelm@18938
   211
val classN = "_class";
wenzelm@18938
   212
wenzelm@18938
   213
val const_of_class = suffix classN;
wenzelm@19406
   214
wenzelm@18938
   215
fun class_of_const c = unsuffix classN c
wenzelm@18938
   216
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@18938
   217
wenzelm@19406
   218
wenzelm@31943
   219
(* class membership *)
wenzelm@19406
   220
wenzelm@31943
   221
fun mk_of_class (ty, c) =
wenzelm@19391
   222
  Const (const_of_class c, Term.itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   223
wenzelm@31943
   224
fun dest_of_class (t as Const (c_class, _) $ ty) = (dest_type ty, class_of_const c_class)
wenzelm@31943
   225
  | dest_of_class t = raise TERM ("dest_of_class", [t]);
wenzelm@398
   226
clasohm@0
   227
wenzelm@19406
   228
(* class relations *)
wenzelm@19406
   229
wenzelm@20630
   230
fun name_classrel (c1, c2) =
wenzelm@30364
   231
  Long_Name.base_name c1 ^ "_" ^ Long_Name.base_name c2;
wenzelm@20630
   232
wenzelm@31943
   233
fun mk_classrel (c1, c2) = mk_of_class (Term.aT [c1], c2);
wenzelm@19406
   234
wenzelm@19406
   235
fun dest_classrel tm =
wenzelm@31943
   236
  (case dest_of_class tm of
wenzelm@19406
   237
    (TVar (_, [c1]), c2) => (c1, c2)
wenzelm@19406
   238
  | _ => raise TERM ("dest_classrel", [tm]));
wenzelm@19406
   239
wenzelm@19406
   240
wenzelm@19406
   241
(* type arities *)
wenzelm@19406
   242
wenzelm@20630
   243
fun name_arities (t, _, S) =
wenzelm@30364
   244
  let val b = Long_Name.base_name t
wenzelm@30364
   245
  in S |> map (fn c => Long_Name.base_name c ^ "_" ^ b) end;
wenzelm@20630
   246
wenzelm@20630
   247
fun name_arity (t, dom, c) = hd (name_arities (t, dom, [c]));
wenzelm@20630
   248
wenzelm@19406
   249
fun mk_arities (t, Ss, S) =
wenzelm@24848
   250
  let val T = Type (t, ListPair.map TFree (Name.invents Name.context Name.aT (length Ss), Ss))
wenzelm@31943
   251
  in map (fn c => mk_of_class (T, c)) S end;
wenzelm@19406
   252
wenzelm@19406
   253
fun dest_arity tm =
wenzelm@19406
   254
  let
wenzelm@19406
   255
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@19406
   256
wenzelm@31943
   257
    val (ty, c) = dest_of_class tm;
wenzelm@19406
   258
    val (t, tvars) =
wenzelm@19406
   259
      (case ty of
wenzelm@19406
   260
        Type (t, tys) => (t, map dest_TVar tys handle TYPE _ => err ())
wenzelm@19406
   261
      | _ => err ());
wenzelm@19406
   262
    val Ss =
wenzelm@19406
   263
      if has_duplicates (eq_fst (op =)) tvars then err ()
wenzelm@19406
   264
      else map snd tvars;
wenzelm@19406
   265
  in (t, Ss, c) end;
wenzelm@19406
   266
wenzelm@19406
   267
wenzelm@18938
   268
wenzelm@28448
   269
(** protected propositions and embedded terms **)
wenzelm@9460
   270
wenzelm@18029
   271
val protectC = Const ("prop", propT --> propT);
wenzelm@18029
   272
fun protect t = protectC $ t;
wenzelm@9460
   273
wenzelm@18029
   274
fun unprotect (Const ("prop", _) $ t) = t
wenzelm@18029
   275
  | unprotect t = raise TERM ("unprotect", [t]);
wenzelm@9460
   276
wenzelm@9460
   277
wenzelm@26424
   278
fun mk_term t = Const ("Pure.term", Term.fastype_of t --> propT) $ t;
wenzelm@19775
   279
wenzelm@26424
   280
fun dest_term (Const ("Pure.term", _) $ t) = t
wenzelm@19775
   281
  | dest_term t = raise TERM ("dest_term", [t]);
wenzelm@19775
   282
wenzelm@19775
   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@19425
   293
  Term.list_all_free (rev (Term.add_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@32023
   307
fun incr_tvar_same k = Term_Subst.map_atypsT_same
wenzelm@32020
   308
  (fn TVar ((a, i), S) => TVar ((a, i + k), S)
wenzelm@32020
   309
    | _ => raise Same.SAME);
wenzelm@16879
   310
wenzelm@32023
   311
fun incr_tvar 0 T = T
wenzelm@32023
   312
  | incr_tvar k T = incr_tvar_same k T handle Same.SAME => T;
wenzelm@32023
   313
clasohm@0
   314
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   315
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
paulson@17120
   316
fun incr_indexes ([], 0) t = t
wenzelm@18029
   317
  | incr_indexes (Ts, k) t =
wenzelm@32020
   318
      let
wenzelm@32020
   319
        val n = length Ts;
wenzelm@32023
   320
        val incrT = if k = 0 then I else incr_tvar_same k;
wenzelm@16879
   321
wenzelm@32020
   322
        fun incr lev (Var ((x, i), T)) =
wenzelm@32020
   323
              combound (Var ((x, i + k), Ts ---> Same.commit incrT T), lev, n)
wenzelm@32020
   324
          | incr lev (Abs (x, T, body)) =
wenzelm@32020
   325
              (Abs (x, incrT T, incr (lev + 1) body handle Same.SAME => body)
wenzelm@32020
   326
                handle Same.SAME => Abs (x, T, incr (lev + 1) body))
wenzelm@32020
   327
          | incr lev (t $ u) =
wenzelm@32020
   328
              (incr lev t $ (incr lev u handle Same.SAME => u)
wenzelm@32020
   329
                handle Same.SAME => t $ incr lev u)
wenzelm@32020
   330
          | incr _ (Const (c, T)) = Const (c, incrT T)
wenzelm@32020
   331
          | incr _ (Free (x, T)) = Free (x, incrT T)
wenzelm@32020
   332
          | incr _ (t as Bound _) = t;
wenzelm@32020
   333
      in incr 0 t handle Same.SAME => t end;
wenzelm@16879
   334
clasohm@0
   335
wenzelm@18248
   336
(* Lifting functions from subgoal and increment:
wenzelm@18029
   337
    lift_abs operates on terms
wenzelm@18029
   338
    lift_all operates on propositions *)
wenzelm@18029
   339
wenzelm@18029
   340
fun lift_abs inc =
wenzelm@18029
   341
  let
wenzelm@18029
   342
    fun lift Ts (Const ("==>", _) $ _ $ B) t = lift Ts B t
wenzelm@18248
   343
      | lift Ts (Const ("all", _) $ Abs (a, T, B)) t = Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   344
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   345
  in lift [] end;
wenzelm@18029
   346
wenzelm@18029
   347
fun lift_all inc =
wenzelm@18029
   348
  let
wenzelm@18029
   349
    fun lift Ts ((c as Const ("==>", _)) $ A $ B) t = c $ A $ lift Ts B t
wenzelm@18248
   350
      | lift Ts ((c as Const ("all", _)) $ Abs (a, T, B)) t = c $ Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   351
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   352
  in lift [] end;
clasohm@0
   353
clasohm@0
   354
(*Strips assumptions in goal, yielding list of hypotheses.   *)
berghofe@21016
   355
fun strip_assums_hyp B =
berghofe@21016
   356
  let
berghofe@21016
   357
    fun strip Hs (Const ("==>", _) $ H $ B) = strip (H :: Hs) B
berghofe@21016
   358
      | strip Hs (Const ("all", _) $ Abs (a, T, t)) =
berghofe@21016
   359
          strip (map (incr_boundvars 1) Hs) t
berghofe@21016
   360
      | strip Hs B = rev Hs
berghofe@21016
   361
  in strip [] B end;
clasohm@0
   362
clasohm@0
   363
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   364
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   365
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   366
  | strip_assums_concl B = B;
clasohm@0
   367
clasohm@0
   368
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   369
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   370
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   371
  | strip_params B = [];
clasohm@0
   372
wenzelm@23597
   373
(*test for nested meta connectives in prems*)
wenzelm@23597
   374
val has_meta_prems =
wenzelm@9667
   375
  let
wenzelm@23597
   376
    fun is_meta (Const ("==", _) $ _ $ _) = true
wenzelm@23597
   377
      | is_meta (Const ("==>", _) $ _ $ _) = true
wenzelm@9667
   378
      | is_meta (Const ("all", _) $ _) = true
wenzelm@9667
   379
      | is_meta _ = false;
wenzelm@23597
   380
    fun ex_meta (Const ("==>", _) $ A $ B) = is_meta A orelse ex_meta B
wenzelm@23597
   381
      | ex_meta (Const ("all", _) $ Abs (_, _, B)) = ex_meta B
wenzelm@23597
   382
      | ex_meta _ = false;
wenzelm@23597
   383
  in ex_meta end;
wenzelm@9483
   384
clasohm@0
   385
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
wenzelm@9460
   386
    where j is the total number of parameters (precomputed)
clasohm@0
   387
  If n>0 then deletes assumption n. *)
wenzelm@9460
   388
fun remove_params j n A =
clasohm@0
   389
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   390
    else case A of
wenzelm@9460
   391
        Const("==>", _) $ H $ B =>
wenzelm@9460
   392
          if n=1 then                           (remove_params j (n-1) B)
wenzelm@9460
   393
          else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   394
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   395
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   396
             else A;
clasohm@0
   397
clasohm@0
   398
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   399
  if n>0 then deletes assumption n. *)
clasohm@0
   400
fun flatten_params n A =
clasohm@0
   401
    let val params = strip_params A;
berghofe@25939
   402
        val vars = ListPair.zip (Name.variant_list [] (map #1 params),
berghofe@25939
   403
                                 map #2 params)
clasohm@0
   404
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   405
    end;
clasohm@0
   406
clasohm@0
   407
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   408
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   409
      implies $ A $ list_rename_params (cs, B)
wenzelm@27334
   410
  | list_rename_params (c::cs, (a as Const("all",_)) $ Abs(_,T,t)) =
wenzelm@27334
   411
      a $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   412
  | list_rename_params (cs, B) = B;
clasohm@0
   413
wenzelm@32008
   414
wenzelm@32008
   415
wenzelm@19806
   416
(*** Treatment of "assume", "erule", etc. ***)
clasohm@0
   417
wenzelm@16879
   418
(*Strips assumptions in goal yielding
paulson@15451
   419
   HS = [Hn,...,H1],   params = [xm,...,x1], and B,
wenzelm@16879
   420
  where x1...xm are the parameters. This version (21.1.2005) REQUIRES
wenzelm@16879
   421
  the the parameters to be flattened, but it allows erule to work on
paulson@15451
   422
  assumptions of the form !!x. phi. Any !! after the outermost string
paulson@15451
   423
  will be regarded as belonging to the conclusion, and left untouched.
paulson@15454
   424
  Used ONLY by assum_pairs.
paulson@15454
   425
      Unless nasms<0, it can terminate the recursion early; that allows
paulson@15454
   426
  erule to work on assumptions of the form P==>Q.*)
paulson@15454
   427
fun strip_assums_imp (0, Hs, B) = (Hs, B)  (*recursion terminated by nasms*)
wenzelm@16879
   428
  | strip_assums_imp (nasms, Hs, Const("==>", _) $ H $ B) =
paulson@15454
   429
      strip_assums_imp (nasms-1, H::Hs, B)
paulson@15454
   430
  | strip_assums_imp (_, Hs, B) = (Hs, B); (*recursion terminated by B*)
paulson@15454
   431
wenzelm@32008
   432
(*Strips OUTER parameters only.*)
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@30554
   452
fun assum_problems (nasms, A) =
wenzelm@30554
   453
  let
wenzelm@30554
   454
    val (params, A') = strip_assums_all ([], A)
wenzelm@30554
   455
    val (Hs, B) = strip_assums_imp (nasms, [], A')
wenzelm@30554
   456
    fun abspar t = rlist_abs (params, t)
wenzelm@30554
   457
  in (abspar, rev Hs, B) end;
wenzelm@30554
   458
wenzelm@19806
   459
wenzelm@19806
   460
(* global schematic variables *)
wenzelm@19806
   461
wenzelm@19806
   462
fun bad_schematic xi = "Illegal schematic variable: " ^ quote (Term.string_of_vname xi);
wenzelm@19806
   463
fun bad_fixed x = "Illegal fixed variable: " ^ quote x;
wenzelm@19806
   464
wenzelm@32020
   465
fun varifyT_same ty = ty
wenzelm@32020
   466
  |> Term_Subst.map_atypsT_same
wenzelm@32020
   467
    (fn TFree (a, S) => TVar ((a, 0), S)
wenzelm@31981
   468
      | TVar (ai, _) => raise TYPE (bad_schematic ai, [ty], []));
wenzelm@19806
   469
wenzelm@32020
   470
fun unvarifyT_same ty = ty
wenzelm@32020
   471
  |> Term_Subst.map_atypsT_same
wenzelm@32020
   472
    (fn TVar ((a, 0), S) => TFree (a, S)
wenzelm@31981
   473
      | TVar (ai, _) => raise TYPE (bad_schematic ai, [ty], [])
wenzelm@31981
   474
      | TFree (a, _) => raise TYPE (bad_fixed a, [ty], []));
clasohm@0
   475
wenzelm@32020
   476
val varifyT = Same.commit varifyT_same;
wenzelm@32020
   477
val unvarifyT = Same.commit unvarifyT_same;
wenzelm@31981
   478
wenzelm@31981
   479
fun varify tm = tm
wenzelm@32020
   480
  |> Same.commit (Term_Subst.map_aterms_same
wenzelm@32020
   481
    (fn Free (x, T) => Var ((x, 0), T)
wenzelm@19806
   482
      | Var (xi, _) => raise TERM (bad_schematic xi, [tm])
wenzelm@32020
   483
      | _ => raise Same.SAME))
wenzelm@32020
   484
  |> Same.commit (Term_Subst.map_types_same varifyT_same)
wenzelm@19879
   485
  handle TYPE (msg, _, _) => raise TERM (msg, [tm]);
wenzelm@19806
   486
wenzelm@31981
   487
fun unvarify tm = tm
wenzelm@32020
   488
  |> Same.commit (Term_Subst.map_aterms_same
wenzelm@32020
   489
    (fn Var ((x, 0), T) => Free (x, T)
wenzelm@19806
   490
      | Var (xi, _) => raise TERM (bad_schematic xi, [tm])
wenzelm@19806
   491
      | Free (x, _) => raise TERM (bad_fixed x, [tm])
wenzelm@32020
   492
      | _ => raise Same.SAME))
wenzelm@32020
   493
  |> Same.commit (Term_Subst.map_types_same unvarifyT_same)
wenzelm@19879
   494
  handle TYPE (msg, _, _) => raise TERM (msg, [tm]);
wenzelm@19806
   495
berghofe@13799
   496
wenzelm@16862
   497
(* goal states *)
wenzelm@16862
   498
wenzelm@16862
   499
fun get_goal st i = nth_prem (i, st)
wenzelm@16862
   500
  handle TERM _ => error "Goal number out of range";
berghofe@13799
   501
berghofe@13799
   502
(*reverses parameters for substitution*)
berghofe@13799
   503
fun goal_params st i =
berghofe@13799
   504
  let val gi = get_goal st i
wenzelm@29276
   505
      val rfrees = map Free (Term.rename_wrt_term gi (strip_params gi))
berghofe@13799
   506
  in (gi, rfrees) end;
berghofe@13799
   507
berghofe@13799
   508
fun concl_of_goal st i =
berghofe@13799
   509
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   510
      val B = strip_assums_concl gi
berghofe@13799
   511
  in subst_bounds (rfrees, B) end;
berghofe@13799
   512
berghofe@13799
   513
fun prems_of_goal st i =
berghofe@13799
   514
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   515
      val As = strip_assums_hyp gi
berghofe@13799
   516
  in map (fn A => subst_bounds (rfrees, A)) As end;
berghofe@13799
   517
clasohm@0
   518
end;