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