src/Pure/variable.ML
author wenzelm
Thu Aug 15 16:02:47 2019 +0200 (9 months ago)
changeset 70533 031620901fcd
parent 70311 e49bf4ebf330
child 70586 57df8a85317a
permissions -rw-r--r--
support for (fully reconstructed) proof terms in Scala;
proper cache_typs;
wenzelm@19899
     1
(*  Title:      Pure/variable.ML
wenzelm@19899
     2
    Author:     Makarius
wenzelm@19899
     3
wenzelm@19899
     4
Fixed type/term variables and polymorphic term abbreviations.
wenzelm@19899
     5
*)
wenzelm@19899
     6
wenzelm@19899
     7
signature VARIABLE =
wenzelm@19899
     8
sig
wenzelm@20303
     9
  val names_of: Proof.context -> Name.context
wenzelm@20303
    10
  val binds_of: Proof.context -> (typ * term) Vartab.table
wenzelm@24765
    11
  val maxidx_of: Proof.context -> int
wenzelm@20303
    12
  val constraints_of: Proof.context -> typ Vartab.table * sort Vartab.table
wenzelm@20303
    13
  val is_declared: Proof.context -> string -> bool
wenzelm@42494
    14
  val check_name: binding -> string
wenzelm@20303
    15
  val default_type: Proof.context -> string -> typ option
wenzelm@20303
    16
  val def_type: Proof.context -> bool -> indexname -> typ option
wenzelm@20303
    17
  val def_sort: Proof.context -> indexname -> sort option
wenzelm@59646
    18
  val declare_maxidx: int -> Proof.context -> Proof.context
wenzelm@27280
    19
  val declare_names: term -> Proof.context -> Proof.context
wenzelm@20303
    20
  val declare_constraints: term -> Proof.context -> Proof.context
wenzelm@62012
    21
  val declare_internal: term -> Proof.context -> Proof.context
wenzelm@20303
    22
  val declare_term: term -> Proof.context -> Proof.context
wenzelm@27280
    23
  val declare_typ: typ -> Proof.context -> Proof.context
wenzelm@20303
    24
  val declare_prf: Proofterm.proof -> Proof.context -> Proof.context
wenzelm@20303
    25
  val declare_thm: thm -> Proof.context -> Proof.context
wenzelm@20303
    26
  val variant_frees: Proof.context -> term list -> (string * 'a) list -> (string * 'a) list
wenzelm@60401
    27
  val bind_term: indexname * term -> Proof.context -> Proof.context
wenzelm@60401
    28
  val unbind_term: indexname -> Proof.context -> Proof.context
wenzelm@60401
    29
  val maybe_bind_term: indexname * term option -> Proof.context -> Proof.context
wenzelm@20303
    30
  val expand_binds: Proof.context -> term -> term
wenzelm@25325
    31
  val lookup_const: Proof.context -> string -> string option
wenzelm@25316
    32
  val is_const: Proof.context -> string -> bool
wenzelm@25325
    33
  val declare_const: string * string -> Proof.context -> Proof.context
wenzelm@55635
    34
  val next_bound: string * typ -> Proof.context -> term * Proof.context
wenzelm@55014
    35
  val revert_bounds: Proof.context -> term -> term
wenzelm@59798
    36
  val is_body: Proof.context -> bool
wenzelm@59798
    37
  val set_body: bool -> Proof.context -> Proof.context
wenzelm@59798
    38
  val restore_body: Proof.context -> Proof.context -> Proof.context
wenzelm@59790
    39
  val improper_fixes: Proof.context -> Proof.context
wenzelm@59790
    40
  val restore_proper_fixes: Proof.context -> Proof.context -> Proof.context
wenzelm@59790
    41
  val is_improper: Proof.context -> string -> bool
wenzelm@42488
    42
  val is_fixed: Proof.context -> string -> bool
wenzelm@59846
    43
  val is_newly_fixed: Proof.context -> Proof.context -> string -> bool
wenzelm@42493
    44
  val fixed_ord: Proof.context -> string * string -> order
wenzelm@42488
    45
  val intern_fixed: Proof.context -> string -> string
wenzelm@42488
    46
  val lookup_fixed: Proof.context -> string -> string option
wenzelm@42488
    47
  val revert_fixed: Proof.context -> string -> string
wenzelm@62987
    48
  val markup_fixed: Proof.context -> string -> Markup.T
wenzelm@62987
    49
  val markup: Proof.context -> string -> Markup.T
wenzelm@62987
    50
  val markup_entity_def: Proof.context -> string -> Markup.T
wenzelm@62987
    51
  val dest_fixes: Proof.context -> (string * string) list
wenzelm@42482
    52
  val add_fixed_names: Proof.context -> term -> string list -> string list
wenzelm@42482
    53
  val add_fixed: Proof.context -> term -> (string * typ) list -> (string * typ) list
wenzelm@59846
    54
  val add_newly_fixed: Proof.context -> Proof.context ->
wenzelm@59846
    55
    term -> (string * typ) list -> (string * typ) list
wenzelm@42482
    56
  val add_free_names: Proof.context -> term -> string list -> string list
wenzelm@42482
    57
  val add_frees: Proof.context -> term -> (string * typ) list -> (string * typ) list
wenzelm@42488
    58
  val add_fixes_binding: binding list -> Proof.context -> string list * Proof.context
wenzelm@20303
    59
  val add_fixes: string list -> Proof.context -> string list * Proof.context
wenzelm@20303
    60
  val add_fixes_direct: string list -> Proof.context -> Proof.context
wenzelm@59796
    61
  val fix_dummy_patterns: term -> Proof.context -> term * Proof.context
wenzelm@20797
    62
  val variant_fixes: string list -> Proof.context -> string list * Proof.context
wenzelm@60822
    63
  val gen_all: Proof.context -> thm -> thm
wenzelm@20303
    64
  val export_terms: Proof.context -> Proof.context -> term list -> term list
wenzelm@20303
    65
  val exportT_terms: Proof.context -> Proof.context -> term list -> term list
wenzelm@20303
    66
  val exportT: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@20303
    67
  val export_prf: Proof.context -> Proof.context -> Proofterm.proof -> Proofterm.proof
wenzelm@20303
    68
  val export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@21522
    69
  val export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@59796
    70
  val invent_types: sort list -> Proof.context -> (string * sort) list * Proof.context
wenzelm@20303
    71
  val importT_inst: term list -> Proof.context -> ((indexname * sort) * typ) list * Proof.context
wenzelm@20303
    72
  val import_inst: bool -> term list -> Proof.context ->
wenzelm@20303
    73
    (((indexname * sort) * typ) list * ((indexname * typ) * term) list) * Proof.context
wenzelm@20303
    74
  val importT_terms: term list -> Proof.context -> term list * Proof.context
wenzelm@20303
    75
  val import_terms: bool -> term list -> Proof.context -> term list * Proof.context
wenzelm@60642
    76
  val importT: thm list -> Proof.context ->
wenzelm@60642
    77
    (((indexname * sort) * ctyp) list * thm list) * Proof.context
wenzelm@20303
    78
  val import_prf: bool -> Proofterm.proof -> Proof.context -> Proofterm.proof * Proof.context
wenzelm@31794
    79
  val import: bool -> thm list -> Proof.context ->
wenzelm@60642
    80
    ((((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list) * thm list) * Proof.context
wenzelm@70304
    81
  val import_vars: Proof.context -> thm -> thm
wenzelm@21287
    82
  val tradeT: (Proof.context -> thm list -> thm list) -> Proof.context -> thm list -> thm list
wenzelm@21287
    83
  val trade: (Proof.context -> thm list -> thm list) -> Proof.context -> thm list -> thm list
wenzelm@60707
    84
  val is_bound_focus: Proof.context -> bool
wenzelm@60707
    85
  val set_bound_focus: bool -> Proof.context -> Proof.context
wenzelm@60707
    86
  val restore_bound_focus: Proof.context -> Proof.context -> Proof.context
wenzelm@60695
    87
  val focus_params: binding list option -> term -> Proof.context ->
wenzelm@60695
    88
    (string list * (string * typ) list) * Proof.context
wenzelm@60695
    89
  val focus: binding list option -> term -> Proof.context ->
wenzelm@60695
    90
    ((string * (string * typ)) list * term) * Proof.context
wenzelm@60695
    91
  val focus_cterm: binding list option -> cterm -> Proof.context ->
wenzelm@60695
    92
    ((string * cterm) list * cterm) * Proof.context
wenzelm@60695
    93
  val focus_subgoal: binding list option -> int -> thm -> Proof.context ->
wenzelm@60695
    94
    ((string * cterm) list * cterm) * Proof.context
wenzelm@20303
    95
  val warn_extra_tfrees: Proof.context -> Proof.context -> unit
wenzelm@24694
    96
  val polymorphic_types: Proof.context -> term list -> (indexname * sort) list * term list
wenzelm@20303
    97
  val polymorphic: Proof.context -> term list -> term list
wenzelm@19899
    98
end;
wenzelm@19899
    99
wenzelm@19899
   100
structure Variable: VARIABLE =
wenzelm@19899
   101
struct
wenzelm@19899
   102
wenzelm@19899
   103
(** local context data **)
wenzelm@19899
   104
wenzelm@59790
   105
type fixes = (string * bool) Name_Space.table;
wenzelm@50201
   106
val empty_fixes: fixes = Name_Space.empty_table Markup.fixedN;
wenzelm@42488
   107
wenzelm@19899
   108
datatype data = Data of
wenzelm@59798
   109
 {names: Name.context,                  (*type/term variable names*)
wenzelm@25325
   110
  consts: string Symtab.table,          (*consts within the local scope*)
wenzelm@55014
   111
  bounds: int * ((string * typ) * string) list,  (*next index, internal name, type, external name*)
wenzelm@42488
   112
  fixes: fixes,                         (*term fixes -- global name space, intern ~> extern*)
wenzelm@20102
   113
  binds: (typ * term) Vartab.table,     (*term bindings*)
wenzelm@20162
   114
  type_occs: string list Symtab.table,  (*type variables -- possibly within term variables*)
wenzelm@24765
   115
  maxidx: int,                          (*maximum var index*)
wenzelm@20162
   116
  constraints:
wenzelm@20102
   117
    typ Vartab.table *                  (*type constraints*)
wenzelm@20162
   118
    sort Vartab.table};                 (*default sorts*)
wenzelm@19899
   119
wenzelm@61508
   120
fun make_data (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =
wenzelm@59798
   121
  Data {names = names, consts = consts, bounds = bounds, fixes = fixes, binds = binds,
wenzelm@61508
   122
    type_occs = type_occs, maxidx = maxidx, constraints = constraints};
wenzelm@19899
   123
wenzelm@59150
   124
val empty_data =
wenzelm@59798
   125
  make_data (Name.context, Symtab.empty, (0, []), empty_fixes, Vartab.empty,
wenzelm@61508
   126
    Symtab.empty, ~1, (Vartab.empty, Vartab.empty));
wenzelm@59150
   127
wenzelm@33519
   128
structure Data = Proof_Data
wenzelm@19899
   129
(
wenzelm@19899
   130
  type T = data;
wenzelm@59150
   131
  fun init _ = empty_data;
wenzelm@19899
   132
);
wenzelm@19899
   133
wenzelm@19899
   134
fun map_data f =
wenzelm@61508
   135
  Data.map (fn Data {names, consts, bounds, fixes, binds, type_occs, maxidx, constraints} =>
wenzelm@61508
   136
    make_data (f (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints)));
wenzelm@25316
   137
wenzelm@25316
   138
fun map_names f =
wenzelm@61508
   139
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   140
    (f names, consts, bounds, fixes, binds, type_occs, maxidx, constraints));
wenzelm@19899
   141
wenzelm@25325
   142
fun map_consts f =
wenzelm@61508
   143
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   144
    (names, f consts, bounds, fixes, binds, type_occs, maxidx, constraints));
wenzelm@55014
   145
wenzelm@55014
   146
fun map_bounds f =
wenzelm@61508
   147
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   148
    (names, consts, f bounds, fixes, binds, type_occs, maxidx, constraints));
wenzelm@20162
   149
wenzelm@25316
   150
fun map_fixes f =
wenzelm@61508
   151
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   152
    (names, consts, bounds, f fixes, binds, type_occs, maxidx, constraints));
wenzelm@19899
   153
wenzelm@25316
   154
fun map_binds f =
wenzelm@61508
   155
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   156
    (names, consts, bounds, fixes, f binds, type_occs, maxidx, constraints));
wenzelm@24765
   157
wenzelm@25316
   158
fun map_type_occs f =
wenzelm@61508
   159
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   160
    (names, consts, bounds, fixes, binds, f type_occs, maxidx, constraints));
wenzelm@19899
   161
wenzelm@25316
   162
fun map_maxidx f =
wenzelm@61508
   163
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   164
    (names, consts, bounds, fixes, binds, type_occs, f maxidx, constraints));
wenzelm@20102
   165
wenzelm@25316
   166
fun map_constraints f =
wenzelm@61508
   167
  map_data (fn (names, consts, bounds, fixes, binds, type_occs, maxidx, constraints) =>
wenzelm@61508
   168
    (names, consts, bounds, fixes, binds, type_occs, maxidx, f constraints));
wenzelm@19899
   169
wenzelm@45650
   170
fun rep_data ctxt = Data.get ctxt |> (fn Data rep => rep);
wenzelm@19899
   171
wenzelm@20102
   172
val names_of = #names o rep_data;
wenzelm@20102
   173
val fixes_of = #fixes o rep_data;
wenzelm@56025
   174
val fixes_space = Name_Space.space_of_table o fixes_of;
wenzelm@19899
   175
val binds_of = #binds o rep_data;
wenzelm@20162
   176
val type_occs_of = #type_occs o rep_data;
wenzelm@24765
   177
val maxidx_of = #maxidx o rep_data;
wenzelm@20162
   178
val constraints_of = #constraints o rep_data;
wenzelm@19899
   179
wenzelm@20162
   180
val is_declared = Name.is_declared o names_of;
wenzelm@19899
   181
wenzelm@47021
   182
val check_name = Name_Space.base_name o tap Binding.check;
wenzelm@42357
   183
wenzelm@19899
   184
wenzelm@19899
   185
wenzelm@19899
   186
(** declarations **)
wenzelm@19899
   187
wenzelm@19899
   188
(* default sorts and types *)
wenzelm@19899
   189
wenzelm@20162
   190
fun default_type ctxt x = Vartab.lookup (#1 (constraints_of ctxt)) (x, ~1);
wenzelm@19899
   191
wenzelm@19899
   192
fun def_type ctxt pattern xi =
wenzelm@20162
   193
  let val {binds, constraints = (types, _), ...} = rep_data ctxt in
wenzelm@19899
   194
    (case Vartab.lookup types xi of
wenzelm@19899
   195
      NONE =>
wenzelm@19899
   196
        if pattern then NONE
wenzelm@39290
   197
        else Vartab.lookup binds xi |> Option.map (Type.mark_polymorphic o #1)
wenzelm@19899
   198
    | some => some)
wenzelm@19899
   199
  end;
wenzelm@19899
   200
wenzelm@20162
   201
val def_sort = Vartab.lookup o #2 o constraints_of;
wenzelm@20162
   202
wenzelm@20162
   203
wenzelm@59646
   204
(* maxidx *)
wenzelm@59646
   205
wenzelm@59646
   206
val declare_maxidx = map_maxidx o Integer.max;
wenzelm@59646
   207
wenzelm@59646
   208
wenzelm@20162
   209
(* names *)
wenzelm@20162
   210
wenzelm@24765
   211
fun declare_type_names t =
wenzelm@29279
   212
  map_names (fold_types (fold_atyps Term.declare_typ_names) t) #>
wenzelm@24765
   213
  map_maxidx (fold_types Term.maxidx_typ t);
wenzelm@20162
   214
wenzelm@20162
   215
fun declare_names t =
wenzelm@20162
   216
  declare_type_names t #>
wenzelm@29279
   217
  map_names (fold_aterms Term.declare_term_frees t) #>
wenzelm@24765
   218
  map_maxidx (Term.maxidx_term t);
wenzelm@20162
   219
wenzelm@20162
   220
wenzelm@20162
   221
(* type occurrences *)
wenzelm@20162
   222
wenzelm@24719
   223
fun decl_type_occsT T = fold_atyps (fn TFree (a, _) => Symtab.default (a, []) | _ => I) T;
wenzelm@24719
   224
wenzelm@22671
   225
val decl_type_occs = fold_term_types
wenzelm@20162
   226
  (fn Free (x, _) => fold_atyps (fn TFree (a, _) => Symtab.insert_list (op =) (a, x) | _ => I)
wenzelm@24719
   227
    | _ => decl_type_occsT);
wenzelm@19899
   228
wenzelm@24719
   229
val declare_type_occsT = map_type_occs o fold_types decl_type_occsT;
wenzelm@22671
   230
val declare_type_occs = map_type_occs o decl_type_occs;
wenzelm@22671
   231
wenzelm@19899
   232
wenzelm@20162
   233
(* constraints *)
wenzelm@19899
   234
wenzelm@49685
   235
fun constrain_tvar (xi, raw_S) =
wenzelm@49688
   236
  let val S = #2 (Term_Position.decode_positionS raw_S)
wenzelm@49685
   237
  in if S = dummyS then Vartab.delete_safe xi else Vartab.update (xi, S) end;
wenzelm@21355
   238
wenzelm@20162
   239
fun declare_constraints t = map_constraints (fn (types, sorts) =>
wenzelm@20162
   240
  let
wenzelm@20162
   241
    val types' = fold_aterms
wenzelm@62955
   242
      (fn Free (x, T) => Vartab.update ((x, ~1), T)
wenzelm@62955
   243
        | Var v => Vartab.update v
wenzelm@20162
   244
        | _ => I) t types;
wenzelm@45426
   245
    val sorts' = (fold_types o fold_atyps)
wenzelm@21355
   246
      (fn TFree (x, S) => constrain_tvar ((x, ~1), S)
wenzelm@21355
   247
        | TVar v => constrain_tvar v
wenzelm@45426
   248
        | _ => I) t sorts;
wenzelm@20162
   249
  in (types', sorts') end)
wenzelm@24719
   250
  #> declare_type_occsT t
wenzelm@22711
   251
  #> declare_type_names t;
wenzelm@19899
   252
wenzelm@20162
   253
wenzelm@20162
   254
(* common declarations *)
wenzelm@20162
   255
wenzelm@20162
   256
fun declare_internal t =
wenzelm@20162
   257
  declare_names t #>
wenzelm@28625
   258
  declare_type_occs t #>
wenzelm@61508
   259
  Thm.declare_term_sorts t;
wenzelm@19911
   260
wenzelm@20162
   261
fun declare_term t =
wenzelm@20162
   262
  declare_internal t #>
wenzelm@20162
   263
  declare_constraints t;
wenzelm@19899
   264
wenzelm@27280
   265
val declare_typ = declare_term o Logic.mk_type;
wenzelm@27280
   266
wenzelm@20303
   267
val declare_prf = Proofterm.fold_proof_terms declare_internal (declare_internal o Logic.mk_type);
wenzelm@20303
   268
wenzelm@22691
   269
val declare_thm = Thm.fold_terms declare_internal;
wenzelm@19899
   270
wenzelm@19899
   271
wenzelm@19899
   272
(* renaming term/type frees *)
wenzelm@19899
   273
wenzelm@20162
   274
fun variant_frees ctxt ts frees =
wenzelm@19899
   275
  let
wenzelm@20162
   276
    val names = names_of (fold declare_names ts ctxt);
wenzelm@43326
   277
    val xs = fst (fold_map Name.variant (map #1 frees) names);
wenzelm@20084
   278
  in xs ~~ map snd frees end;
wenzelm@19899
   279
wenzelm@19899
   280
wenzelm@19899
   281
wenzelm@20303
   282
(** term bindings **)
wenzelm@20303
   283
wenzelm@60401
   284
fun bind_term ((x, i), t) =
wenzelm@60401
   285
  let
wenzelm@60401
   286
    val u = Term.close_schematic_term t;
wenzelm@60401
   287
    val U = Term.fastype_of u;
wenzelm@60401
   288
  in declare_term u #> map_binds (Vartab.update ((x, i), (U, u))) end;
wenzelm@60401
   289
wenzelm@60401
   290
val unbind_term = map_binds o Vartab.delete_safe;
wenzelm@60401
   291
wenzelm@60401
   292
fun maybe_bind_term (xi, SOME t) = bind_term (xi, t)
wenzelm@60401
   293
  | maybe_bind_term (xi, NONE) = unbind_term xi;
wenzelm@20303
   294
wenzelm@20303
   295
fun expand_binds ctxt =
wenzelm@20303
   296
  let
wenzelm@20303
   297
    val binds = binds_of ctxt;
wenzelm@21799
   298
    val get = fn Var (xi, _) => Vartab.lookup binds xi | _ => NONE;
wenzelm@21799
   299
  in Envir.beta_norm o Envir.expand_term get end;
wenzelm@20303
   300
wenzelm@20303
   301
wenzelm@20303
   302
wenzelm@25325
   303
(** consts **)
wenzelm@25316
   304
wenzelm@25325
   305
val lookup_const = Symtab.lookup o #consts o rep_data;
wenzelm@25325
   306
val is_const = is_some oo lookup_const;
wenzelm@25316
   307
wenzelm@25325
   308
val declare_fixed = map_consts o Symtab.delete_safe;
wenzelm@25325
   309
val declare_const = map_consts o Symtab.update;
wenzelm@25316
   310
wenzelm@25316
   311
wenzelm@25316
   312
wenzelm@55014
   313
(** bounds **)
wenzelm@55014
   314
wenzelm@55014
   315
fun next_bound (a, T) ctxt =
wenzelm@55014
   316
  let
wenzelm@55014
   317
    val b = Name.bound (#1 (#bounds (rep_data ctxt)));
wenzelm@55014
   318
    val ctxt' = ctxt |> map_bounds (fn (next, bounds) => (next + 1, ((b, T), a) :: bounds));
wenzelm@55635
   319
  in (Free (b, T), ctxt') end;
wenzelm@55014
   320
wenzelm@55014
   321
fun revert_bounds ctxt t =
wenzelm@55014
   322
  (case #2 (#bounds (rep_data ctxt)) of
wenzelm@55014
   323
    [] => t
wenzelm@55014
   324
  | bounds =>
wenzelm@55014
   325
      let
wenzelm@55014
   326
        val names = Term.declare_term_names t (names_of ctxt);
wenzelm@55014
   327
        val xs = rev (#1 (fold_map Name.variant (rev (map #2 bounds)) names));
wenzelm@55014
   328
        fun subst ((b, T), _) x' = (Free (b, T), Syntax_Trans.mark_bound_abs (x', T));
wenzelm@55014
   329
      in Term.subst_atomic (map2 subst bounds xs) t end);
wenzelm@55014
   330
wenzelm@55014
   331
wenzelm@55014
   332
wenzelm@19911
   333
(** fixes **)
wenzelm@19911
   334
wenzelm@59798
   335
(* inner body mode *)
wenzelm@59798
   336
wenzelm@69575
   337
val inner_body = Config.declare_bool ("inner_body", \<^here>) (K false);
wenzelm@69576
   338
val is_body = Config.apply inner_body;
wenzelm@59798
   339
val set_body = Config.put inner_body;
wenzelm@69576
   340
val restore_body = set_body o is_body;
wenzelm@59798
   341
wenzelm@59798
   342
wenzelm@59790
   343
(* proper mode *)
wenzelm@59790
   344
wenzelm@69575
   345
val proper_fixes = Config.declare_bool ("proper_fixes", \<^here>) (K true);
wenzelm@59790
   346
val improper_fixes = Config.put proper_fixes false;
wenzelm@69576
   347
val restore_proper_fixes = Config.put proper_fixes o Config.apply proper_fixes;
wenzelm@59790
   348
wenzelm@59790
   349
fun is_improper ctxt x =
wenzelm@59884
   350
  (case Name_Space.lookup (fixes_of ctxt) x of
wenzelm@59884
   351
    SOME (_, proper) => not proper
wenzelm@59790
   352
  | NONE => false);
wenzelm@59790
   353
wenzelm@59790
   354
wenzelm@42488
   355
(* specialized name space *)
wenzelm@42488
   356
wenzelm@59883
   357
val is_fixed = Name_Space.defined o fixes_of;
wenzelm@59846
   358
fun is_newly_fixed inner outer = is_fixed inner andf (not o is_fixed outer);
wenzelm@42488
   359
wenzelm@42493
   360
val fixed_ord = Name_Space.entry_ord o fixes_space;
wenzelm@42493
   361
val intern_fixed = Name_Space.intern o fixes_space;
wenzelm@42488
   362
wenzelm@42488
   363
fun lookup_fixed ctxt x =
wenzelm@42488
   364
  let val x' = intern_fixed ctxt x
wenzelm@42488
   365
  in if is_fixed ctxt x' then SOME x' else NONE end;
wenzelm@42488
   366
wenzelm@42488
   367
fun revert_fixed ctxt x =
wenzelm@59884
   368
  (case Name_Space.lookup (fixes_of ctxt) x of
wenzelm@59884
   369
    SOME (x', _) => if intern_fixed ctxt x' = x then x' else x
wenzelm@42488
   370
  | NONE => x);
wenzelm@42488
   371
wenzelm@45472
   372
fun markup_fixed ctxt x =
wenzelm@45472
   373
  Name_Space.markup (fixes_space ctxt) x
wenzelm@45472
   374
  |> Markup.name (revert_fixed ctxt x);
wenzelm@45472
   375
wenzelm@62987
   376
fun markup ctxt x =
wenzelm@62987
   377
  if is_improper ctxt x then Markup.improper
wenzelm@62987
   378
  else if Name.is_skolem x then Markup.skolem
wenzelm@62987
   379
  else Markup.free;
wenzelm@62987
   380
wenzelm@62987
   381
val markup_entity_def = Name_Space.markup_def o fixes_space;
wenzelm@62987
   382
wenzelm@42488
   383
fun dest_fixes ctxt =
wenzelm@59790
   384
  Name_Space.fold_table (fn (x, (y, _)) => cons (y, x)) (fixes_of ctxt) []
wenzelm@59058
   385
  |> sort (Name_Space.entry_ord (fixes_space ctxt) o apply2 #2);
wenzelm@42488
   386
wenzelm@42488
   387
wenzelm@42482
   388
(* collect variables *)
wenzelm@42482
   389
wenzelm@42482
   390
fun add_free_names ctxt =
wenzelm@42482
   391
  fold_aterms (fn Free (x, _) => not (is_fixed ctxt x) ? insert (op =) x | _ => I);
wenzelm@42482
   392
wenzelm@42482
   393
fun add_frees ctxt =
wenzelm@42482
   394
  fold_aterms (fn Free (x, T) => not (is_fixed ctxt x) ? insert (op =) (x, T) | _ => I);
wenzelm@42482
   395
wenzelm@42482
   396
fun add_fixed_names ctxt =
wenzelm@42482
   397
  fold_aterms (fn Free (x, _) => is_fixed ctxt x ? insert (op =) x | _ => I);
wenzelm@42482
   398
wenzelm@42482
   399
fun add_fixed ctxt =
wenzelm@42482
   400
  fold_aterms (fn Free (x, T) => is_fixed ctxt x ? insert (op =) (x, T) | _ => I);
wenzelm@42482
   401
wenzelm@59846
   402
fun add_newly_fixed ctxt' ctxt =
wenzelm@59846
   403
  fold_aterms (fn Free (x, T) => is_newly_fixed ctxt' ctxt x ? insert (op =) (x, T) | _ => I);
wenzelm@59846
   404
wenzelm@42482
   405
wenzelm@42482
   406
(* declarations *)
wenzelm@42482
   407
wenzelm@20084
   408
local
wenzelm@20084
   409
wenzelm@42488
   410
fun err_dups dups =
wenzelm@42488
   411
  error ("Duplicate fixed variable(s): " ^ commas (map Binding.print dups));
wenzelm@19911
   412
wenzelm@42488
   413
fun new_fixed ((x, x'), pos) ctxt =
wenzelm@42488
   414
  if is_some (lookup_fixed ctxt x') then err_dups [Binding.make (x, pos)]
wenzelm@42488
   415
  else
wenzelm@59790
   416
    let
wenzelm@59790
   417
      val proper = Config.get ctxt proper_fixes;
wenzelm@61949
   418
      val context = Context.Proof ctxt
wenzelm@61949
   419
        |> Name_Space.map_naming (K Name_Space.global_naming)
wenzelm@61949
   420
        |> Context_Position.set_visible_generic false;
wenzelm@59790
   421
    in
wenzelm@47005
   422
      ctxt
wenzelm@47005
   423
      |> map_fixes
wenzelm@59790
   424
        (Name_Space.define context true (Binding.make (x', pos), (x, proper)) #> snd #>
wenzelm@58668
   425
          Name_Space.alias_table Name_Space.global_naming (Binding.make (x, pos)) x')
wenzelm@47005
   426
      |> declare_fixed x
wenzelm@47005
   427
      |> declare_constraints (Syntax.free x')
wenzelm@47005
   428
  end;
wenzelm@42488
   429
wenzelm@42488
   430
fun new_fixes names' xs xs' ps =
wenzelm@20102
   431
  map_names (K names') #>
wenzelm@42488
   432
  fold new_fixed ((xs ~~ xs') ~~ ps) #>
wenzelm@20084
   433
  pair xs';
wenzelm@20084
   434
wenzelm@20084
   435
in
wenzelm@20084
   436
wenzelm@42488
   437
fun add_fixes_binding bs ctxt =
wenzelm@19911
   438
  let
wenzelm@19911
   439
    val _ =
wenzelm@55948
   440
      (case filter (Name.is_skolem o Binding.name_of) bs of
wenzelm@42488
   441
        [] => ()
wenzelm@42488
   442
      | bads => error ("Illegal internal Skolem constant(s): " ^ commas (map Binding.print bads)));
wenzelm@42488
   443
    val _ =
wenzelm@59058
   444
      (case duplicates (op = o apply2 Binding.name_of) bs of
wenzelm@42488
   445
        [] => ()
wenzelm@42488
   446
      | dups => err_dups dups);
wenzelm@42488
   447
wenzelm@42494
   448
    val xs = map check_name bs;
wenzelm@20102
   449
    val names = names_of ctxt;
wenzelm@20084
   450
    val (xs', names') =
wenzelm@43326
   451
      if is_body ctxt then fold_map Name.variant xs names |>> map Name.skolem
wenzelm@42488
   452
      else (xs, fold Name.declare xs names);
wenzelm@42488
   453
  in ctxt |> new_fixes names' xs xs' (map Binding.pos_of bs) end;
wenzelm@19911
   454
wenzelm@60707
   455
fun bound_fixes xs ctxt =
wenzelm@60707
   456
  let
wenzelm@60707
   457
    val (xs', ctxt') = fold_map next_bound xs ctxt;
wenzelm@60707
   458
    val no_ps = replicate (length xs) Position.none;
wenzelm@60707
   459
  in ctxt' |> new_fixes (names_of ctxt') (map #1 xs) (map (#1 o dest_Free) xs') no_ps end;
wenzelm@60707
   460
wenzelm@20797
   461
fun variant_fixes raw_xs ctxt =
wenzelm@20084
   462
  let
wenzelm@20102
   463
    val names = names_of ctxt;
wenzelm@55948
   464
    val xs = map (fn x => Name.clean x |> Name.is_internal x ? Name.internal) raw_xs;
wenzelm@43326
   465
    val (xs', names') = fold_map Name.variant xs names |>> (is_body ctxt ? map Name.skolem);
wenzelm@60707
   466
    val no_ps = replicate (length xs) Position.none;
wenzelm@60707
   467
  in ctxt |> new_fixes names' xs xs' no_ps end;
wenzelm@20084
   468
wenzelm@20084
   469
end;
wenzelm@19911
   470
wenzelm@42488
   471
val add_fixes = add_fixes_binding o map Binding.name;
wenzelm@20251
   472
wenzelm@20251
   473
fun add_fixes_direct xs ctxt = ctxt
wenzelm@20251
   474
  |> set_body false
wenzelm@20251
   475
  |> (snd o add_fixes xs)
wenzelm@20251
   476
  |> restore_body ctxt;
wenzelm@20251
   477
wenzelm@59796
   478
wenzelm@59796
   479
(* dummy patterns *)
wenzelm@59796
   480
wenzelm@59796
   481
fun fix_dummy_patterns (Const ("Pure.dummy_pattern", T)) ctxt =
wenzelm@61923
   482
      let val ([x], ctxt') = ctxt |> set_body true |> add_fixes [Name.uu_] ||> restore_body ctxt
wenzelm@59796
   483
      in (Free (x, T), ctxt') end
wenzelm@59796
   484
  | fix_dummy_patterns (Abs (x, T, b)) ctxt =
wenzelm@59796
   485
      let val (b', ctxt') = fix_dummy_patterns b ctxt
wenzelm@59796
   486
      in (Abs (x, T, b'), ctxt') end
wenzelm@59796
   487
  | fix_dummy_patterns (t $ u) ctxt =
wenzelm@59796
   488
      let
wenzelm@59796
   489
        val (t', ctxt') = fix_dummy_patterns t ctxt;
wenzelm@59796
   490
        val (u', ctxt'') = fix_dummy_patterns u ctxt';
wenzelm@59796
   491
      in (t' $ u', ctxt'') end
wenzelm@59796
   492
  | fix_dummy_patterns a ctxt = (a, ctxt);
wenzelm@19911
   493
wenzelm@19911
   494
wenzelm@19911
   495
wenzelm@22671
   496
(** export -- generalize type/term variables (beware of closure sizes) **)
wenzelm@19911
   497
wenzelm@60823
   498
fun gen_all ctxt th =
wenzelm@60823
   499
  let
wenzelm@60823
   500
    val i = Thm.maxidx_thm th (maxidx_of ctxt) + 1;
wenzelm@60823
   501
    fun gen (x, T) = Thm.forall_elim (Thm.cterm_of ctxt (Var ((x, i), T)));
wenzelm@60823
   502
  in fold gen (Drule.outer_params (Thm.prop_of th)) th end;
wenzelm@60822
   503
wenzelm@19911
   504
fun export_inst inner outer =
wenzelm@19911
   505
  let
wenzelm@20162
   506
    val declared_outer = is_declared outer;
wenzelm@59846
   507
    val still_fixed = not o is_newly_fixed inner outer;
wenzelm@19911
   508
wenzelm@42488
   509
    val gen_fixes =
wenzelm@56025
   510
      Name_Space.fold_table (fn (y, _) => not (is_fixed outer y) ? cons y)
wenzelm@56025
   511
        (fixes_of inner) [];
wenzelm@22671
   512
wenzelm@22671
   513
    val type_occs_inner = type_occs_of inner;
wenzelm@22671
   514
    fun gen_fixesT ts =
wenzelm@19911
   515
      Symtab.fold (fn (a, xs) =>
wenzelm@20162
   516
        if declared_outer a orelse exists still_fixed xs
wenzelm@22671
   517
        then I else cons a) (fold decl_type_occs ts type_occs_inner) [];
wenzelm@19911
   518
  in (gen_fixesT, gen_fixes) end;
wenzelm@19911
   519
wenzelm@19911
   520
fun exportT_inst inner outer = #1 (export_inst inner outer);
wenzelm@19911
   521
wenzelm@22671
   522
fun exportT_terms inner outer =
wenzelm@59645
   523
  let
wenzelm@59645
   524
    val mk_tfrees = exportT_inst inner outer;
wenzelm@59645
   525
    val maxidx = maxidx_of outer;
wenzelm@59645
   526
  in
wenzelm@22671
   527
    fn ts => ts |> map
wenzelm@31977
   528
      (Term_Subst.generalize (mk_tfrees ts, [])
wenzelm@59645
   529
        (fold (Term.fold_types Term.maxidx_typ) ts maxidx + 1))
wenzelm@22671
   530
  end;
wenzelm@19911
   531
wenzelm@22671
   532
fun export_terms inner outer =
wenzelm@59645
   533
  let
wenzelm@59645
   534
    val (mk_tfrees, tfrees) = export_inst inner outer;
wenzelm@59645
   535
    val maxidx = maxidx_of outer;
wenzelm@59645
   536
  in
wenzelm@22671
   537
    fn ts => ts |> map
wenzelm@31977
   538
      (Term_Subst.generalize (mk_tfrees ts, tfrees)
wenzelm@59645
   539
        (fold Term.maxidx_term ts maxidx + 1))
wenzelm@22671
   540
  end;
wenzelm@19911
   541
wenzelm@20303
   542
fun export_prf inner outer prf =
wenzelm@20303
   543
  let
wenzelm@22671
   544
    val (mk_tfrees, frees) = export_inst (declare_prf prf inner) outer;
wenzelm@22671
   545
    val tfrees = mk_tfrees [];
wenzelm@59645
   546
    val maxidx = maxidx_of outer;
wenzelm@59645
   547
    val idx = Proofterm.maxidx_proof prf maxidx + 1;
wenzelm@36620
   548
    val gen_term = Term_Subst.generalize_same (tfrees, frees) idx;
wenzelm@36620
   549
    val gen_typ = Term_Subst.generalizeT_same tfrees idx;
wenzelm@36620
   550
  in Same.commit (Proofterm.map_proof_terms_same gen_term gen_typ) prf end;
wenzelm@20303
   551
wenzelm@22671
   552
wenzelm@59645
   553
fun gen_export (mk_tfrees, frees) maxidx ths =
wenzelm@19911
   554
  let
wenzelm@22671
   555
    val tfrees = mk_tfrees (map Thm.full_prop_of ths);
wenzelm@59645
   556
    val idx = fold Thm.maxidx_thm ths maxidx + 1;
wenzelm@59645
   557
  in map (Thm.generalize (tfrees, frees) idx) ths end;
wenzelm@19911
   558
wenzelm@59645
   559
fun exportT inner outer = gen_export (exportT_inst inner outer, []) (maxidx_of outer);
wenzelm@59645
   560
fun export inner outer = gen_export (export_inst inner outer) (maxidx_of outer);
wenzelm@19911
   561
wenzelm@21522
   562
fun export_morphism inner outer =
wenzelm@21522
   563
  let
wenzelm@21522
   564
    val fact = export inner outer;
wenzelm@21522
   565
    val term = singleton (export_terms inner outer);
wenzelm@21522
   566
    val typ = Logic.type_map term;
wenzelm@54740
   567
  in
wenzelm@70311
   568
    Morphism.transfer_morphism' inner $>
wenzelm@70311
   569
    Morphism.transfer_morphism' outer $>
wenzelm@54740
   570
    Morphism.morphism "Variable.export" {binding = [], typ = [typ], term = [term], fact = [fact]}
wenzelm@54740
   571
  end;
wenzelm@19911
   572
wenzelm@19911
   573
wenzelm@24765
   574
wenzelm@19911
   575
(** import -- fix schematic type/term variables **)
wenzelm@19911
   576
wenzelm@59796
   577
fun invent_types Ss ctxt =
wenzelm@59796
   578
  let
wenzelm@59796
   579
    val tfrees = Name.invent (names_of ctxt) Name.aT (length Ss) ~~ Ss;
wenzelm@59796
   580
    val ctxt' = fold (declare_constraints o Logic.mk_type o TFree) tfrees ctxt;
wenzelm@59796
   581
  in (tfrees, ctxt') end;
wenzelm@59796
   582
wenzelm@19911
   583
fun importT_inst ts ctxt =
wenzelm@19911
   584
  let
wenzelm@19911
   585
    val tvars = rev (fold Term.add_tvars ts []);
wenzelm@19911
   586
    val (tfrees, ctxt') = invent_types (map #2 tvars) ctxt;
wenzelm@19911
   587
  in (tvars ~~ map TFree tfrees, ctxt') end;
wenzelm@19911
   588
wenzelm@19911
   589
fun import_inst is_open ts ctxt =
wenzelm@19911
   590
  let
wenzelm@26714
   591
    val ren = Name.clean #> (if is_open then I else Name.internal);
wenzelm@19911
   592
    val (instT, ctxt') = importT_inst ts ctxt;
wenzelm@31977
   593
    val vars = map (apsnd (Term_Subst.instantiateT instT)) (rev (fold Term.add_vars ts []));
wenzelm@20797
   594
    val (xs, ctxt'') = variant_fixes (map (ren o #1 o #1) vars) ctxt';
wenzelm@19911
   595
    val inst = vars ~~ map Free (xs ~~ map #2 vars);
wenzelm@19911
   596
  in ((instT, inst), ctxt'') end;
wenzelm@19911
   597
wenzelm@19911
   598
fun importT_terms ts ctxt =
wenzelm@19911
   599
  let val (instT, ctxt') = importT_inst ts ctxt
wenzelm@31977
   600
  in (map (Term_Subst.instantiate (instT, [])) ts, ctxt') end;
wenzelm@19911
   601
wenzelm@19911
   602
fun import_terms is_open ts ctxt =
wenzelm@19911
   603
  let val (inst, ctxt') = import_inst is_open ts ctxt
wenzelm@31977
   604
  in (map (Term_Subst.instantiate inst) ts, ctxt') end;
wenzelm@19911
   605
wenzelm@31794
   606
fun importT ths ctxt =
wenzelm@19911
   607
  let
wenzelm@19911
   608
    val (instT, ctxt') = importT_inst (map Thm.full_prop_of ths) ctxt;
wenzelm@60805
   609
    val instT' = map (apsnd (Thm.ctyp_of ctxt')) instT;
wenzelm@60805
   610
    val ths' = map (Thm.instantiate (instT', [])) ths;
wenzelm@32280
   611
  in ((instT', ths'), ctxt') end;
wenzelm@19911
   612
wenzelm@20303
   613
fun import_prf is_open prf ctxt =
wenzelm@20303
   614
  let
wenzelm@20303
   615
    val ts = rev (Proofterm.fold_proof_terms cons (cons o Logic.mk_type) prf []);
wenzelm@20303
   616
    val (insts, ctxt') = import_inst is_open ts ctxt;
wenzelm@20303
   617
  in (Proofterm.instantiate insts prf, ctxt') end;
wenzelm@20303
   618
wenzelm@31794
   619
fun import is_open ths ctxt =
wenzelm@19911
   620
  let
wenzelm@60805
   621
    val ((instT, inst), ctxt') = import_inst is_open (map Thm.full_prop_of ths) ctxt;
wenzelm@60805
   622
    val insts' =
wenzelm@60805
   623
     (map (apsnd (Thm.ctyp_of ctxt')) instT,
wenzelm@60805
   624
      map (apsnd (Thm.cterm_of ctxt')) inst);
wenzelm@32280
   625
    val ths' = map (Thm.instantiate insts') ths;
wenzelm@32280
   626
  in ((insts', ths'), ctxt') end;
wenzelm@19911
   627
wenzelm@70304
   628
fun import_vars ctxt th =
wenzelm@70304
   629
  let val ((_, [th']), _) = ctxt |> set_body false |> import true [th];
wenzelm@70304
   630
  in th' end;
wenzelm@70304
   631
wenzelm@19911
   632
wenzelm@19926
   633
(* import/export *)
wenzelm@19926
   634
wenzelm@21287
   635
fun gen_trade imp exp f ctxt ths =
wenzelm@20220
   636
  let val ((_, ths'), ctxt') = imp ths ctxt
wenzelm@21287
   637
  in exp ctxt' ctxt (f ctxt' ths') end;
wenzelm@19926
   638
wenzelm@31794
   639
val tradeT = gen_trade importT exportT;
wenzelm@31794
   640
val trade = gen_trade (import true) export;
wenzelm@19926
   641
wenzelm@19926
   642
wenzelm@67721
   643
(* focus on outermost parameters: \<And>x y z. B *)
wenzelm@42495
   644
wenzelm@69575
   645
val bound_focus = Config.declare_bool ("bound_focus", \<^here>) (K false);
wenzelm@69576
   646
val is_bound_focus = Config.apply bound_focus;
wenzelm@60707
   647
val set_bound_focus = Config.put bound_focus;
wenzelm@69576
   648
val restore_bound_focus = set_bound_focus o is_bound_focus;
wenzelm@60707
   649
wenzelm@60695
   650
fun focus_params bindings t ctxt =
wenzelm@42495
   651
  let
wenzelm@60707
   652
    val ps = Term.variant_frees t (Term.strip_all_vars t);  (*as they are printed :-*)
wenzelm@60707
   653
    val (xs, Ts) = split_list ps;
wenzelm@60695
   654
    val (xs', ctxt') =
wenzelm@60695
   655
      (case bindings of
wenzelm@60695
   656
        SOME bs => ctxt |> set_body true |> add_fixes_binding bs ||> restore_body ctxt
wenzelm@60707
   657
      | NONE => if is_bound_focus ctxt then bound_fixes ps ctxt else variant_fixes xs ctxt);
wenzelm@60707
   658
    val ps' = xs' ~~ Ts;
wenzelm@60707
   659
    val ctxt'' = ctxt' |> fold (declare_constraints o Free) ps';
wenzelm@60707
   660
  in ((xs, ps'), ctxt'') end;
wenzelm@42495
   661
wenzelm@60695
   662
fun focus bindings t ctxt =
wenzelm@42495
   663
  let
wenzelm@60695
   664
    val ((xs, ps), ctxt') = focus_params bindings t ctxt;
wenzelm@42495
   665
    val t' = Term.subst_bounds (rev (map Free ps), Term.strip_all_body t);
wenzelm@42495
   666
  in (((xs ~~ ps), t'), ctxt') end;
wenzelm@20149
   667
wenzelm@20149
   668
fun forall_elim_prop t prop =
wenzelm@46497
   669
  Thm.beta_conversion false (Thm.apply (Thm.dest_arg prop) t)
wenzelm@20579
   670
  |> Thm.cprop_of |> Thm.dest_arg;
wenzelm@20149
   671
wenzelm@60695
   672
fun focus_cterm bindings goal ctxt =
wenzelm@20149
   673
  let
wenzelm@60695
   674
    val ((xs, ps), ctxt') = focus_params bindings (Thm.term_of goal) ctxt;
wenzelm@59623
   675
    val ps' = map (Thm.cterm_of ctxt' o Free) ps;
wenzelm@20220
   676
    val goal' = fold forall_elim_prop ps' goal;
wenzelm@42495
   677
  in ((xs ~~ ps', goal'), ctxt') end;
wenzelm@20149
   678
wenzelm@60695
   679
fun focus_subgoal bindings i st =
wenzelm@20303
   680
  let
wenzelm@22691
   681
    val all_vars = Thm.fold_terms Term.add_vars st [];
wenzelm@20303
   682
  in
wenzelm@60401
   683
    fold (unbind_term o #1) all_vars #>
wenzelm@20303
   684
    fold (declare_constraints o Var) all_vars #>
wenzelm@60695
   685
    focus_cterm bindings (Thm.cprem_of st i)
wenzelm@20303
   686
  end;
wenzelm@20303
   687
wenzelm@20303
   688
wenzelm@19911
   689
wenzelm@19911
   690
(** implicit polymorphism **)
wenzelm@19899
   691
wenzelm@19899
   692
(* warn_extra_tfrees *)
wenzelm@19899
   693
wenzelm@19899
   694
fun warn_extra_tfrees ctxt1 ctxt2 =
wenzelm@19899
   695
  let
wenzelm@19911
   696
    fun occs_typ a = Term.exists_subtype (fn TFree (b, _) => a = b | _ => false);
wenzelm@20162
   697
    fun occs_free a x =
wenzelm@20162
   698
      (case def_type ctxt1 false (x, ~1) of
wenzelm@20162
   699
        SOME T => if occs_typ a T then I else cons (a, x)
wenzelm@20162
   700
      | NONE => cons (a, x));
wenzelm@19899
   701
wenzelm@20162
   702
    val occs1 = type_occs_of ctxt1;
wenzelm@20162
   703
    val occs2 = type_occs_of ctxt2;
wenzelm@19911
   704
    val extras = Symtab.fold (fn (a, xs) =>
wenzelm@19911
   705
      if Symtab.defined occs1 a then I else fold (occs_free a) xs) occs2 [];
wenzelm@19899
   706
    val tfrees = map #1 extras |> sort_distinct string_ord;
wenzelm@19899
   707
    val frees = map #2 extras |> sort_distinct string_ord;
wenzelm@19899
   708
  in
wenzelm@38831
   709
    if null extras orelse not (Context_Position.is_visible ctxt2) then ()
wenzelm@19899
   710
    else warning ("Introduced fixed type variable(s): " ^ commas tfrees ^ " in " ^
wenzelm@19899
   711
      space_implode " or " (map quote frees))
wenzelm@19899
   712
  end;
wenzelm@19899
   713
wenzelm@19899
   714
wenzelm@20149
   715
(* polymorphic terms *)
wenzelm@19899
   716
wenzelm@24694
   717
fun polymorphic_types ctxt ts =
wenzelm@20003
   718
  let
wenzelm@20003
   719
    val ctxt' = fold declare_term ts ctxt;
wenzelm@20162
   720
    val occs = type_occs_of ctxt;
wenzelm@20162
   721
    val occs' = type_occs_of ctxt';
wenzelm@20162
   722
    val types = Symtab.fold (fn (a, _) => if Symtab.defined occs a then I else cons a) occs' [];
wenzelm@24765
   723
    val idx = maxidx_of ctxt' + 1;
wenzelm@24694
   724
    val Ts' = (fold o fold_types o fold_atyps)
wenzelm@24694
   725
      (fn T as TFree _ =>
wenzelm@31977
   726
          (case Term_Subst.generalizeT types idx T of TVar v => insert (op =) v | _ => I)
wenzelm@24694
   727
        | _ => I) ts [];
wenzelm@31977
   728
    val ts' = map (Term_Subst.generalize (types, []) idx) ts;
wenzelm@24694
   729
  in (rev Ts', ts') end;
wenzelm@24694
   730
wenzelm@24694
   731
fun polymorphic ctxt ts = snd (polymorphic_types ctxt ts);
wenzelm@20003
   732
wenzelm@19899
   733
end;