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