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