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