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