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