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