src/Pure/variable.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (22 months ago)
changeset 66695 91500c024c7f
parent 64556 851ae0e7b09c
child 67721 5348bea4accd
permissions -rw-r--r--
tuned;
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@21369
    61
  val auto_fixes: term -> Proof.context -> Proof.context
wenzelm@59796
    62
  val fix_dummy_patterns: term -> Proof.context -> term * Proof.context
wenzelm@20797
    63
  val variant_fixes: string list -> Proof.context -> string list * Proof.context
wenzelm@60822
    64
  val gen_all: Proof.context -> thm -> thm
wenzelm@20303
    65
  val export_terms: Proof.context -> Proof.context -> term list -> term list
wenzelm@20303
    66
  val exportT_terms: Proof.context -> Proof.context -> term list -> term list
wenzelm@20303
    67
  val exportT: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@20303
    68
  val export_prf: Proof.context -> Proof.context -> Proofterm.proof -> Proofterm.proof
wenzelm@20303
    69
  val export: Proof.context -> Proof.context -> thm list -> thm list
wenzelm@21522
    70
  val export_morphism: Proof.context -> Proof.context -> morphism
wenzelm@59796
    71
  val invent_types: sort list -> Proof.context -> (string * sort) list * Proof.context
wenzelm@20303
    72
  val importT_inst: term list -> Proof.context -> ((indexname * sort) * typ) list * Proof.context
wenzelm@20303
    73
  val import_inst: bool -> term list -> Proof.context ->
wenzelm@20303
    74
    (((indexname * sort) * typ) list * ((indexname * typ) * term) list) * Proof.context
wenzelm@20303
    75
  val importT_terms: term list -> Proof.context -> term list * Proof.context
wenzelm@20303
    76
  val import_terms: bool -> term list -> Proof.context -> term list * Proof.context
wenzelm@60642
    77
  val importT: thm list -> Proof.context ->
wenzelm@60642
    78
    (((indexname * sort) * ctyp) list * thm list) * Proof.context
wenzelm@20303
    79
  val import_prf: bool -> Proofterm.proof -> Proof.context -> Proofterm.proof * Proof.context
wenzelm@31794
    80
  val import: bool -> thm list -> Proof.context ->
wenzelm@60642
    81
    ((((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list) * thm list) * Proof.context
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@59798
   337
val inner_body =
wenzelm@64556
   338
  Config.bool (Config.declare ("inner_body", \<^here>) (K (Config.Bool false)));
wenzelm@59798
   339
wenzelm@59798
   340
fun is_body ctxt = Config.get ctxt inner_body;
wenzelm@59798
   341
val set_body = Config.put inner_body;
wenzelm@59798
   342
fun restore_body ctxt = set_body (is_body ctxt);
wenzelm@59798
   343
wenzelm@59798
   344
wenzelm@59790
   345
(* proper mode *)
wenzelm@59790
   346
wenzelm@59790
   347
val proper_fixes =
wenzelm@64556
   348
  Config.bool (Config.declare ("proper_fixes", \<^here>) (K (Config.Bool true)));
wenzelm@59790
   349
wenzelm@59790
   350
val improper_fixes = Config.put proper_fixes false;
wenzelm@59790
   351
fun restore_proper_fixes ctxt = Config.put proper_fixes (Config.get ctxt proper_fixes);
wenzelm@59790
   352
wenzelm@59790
   353
fun is_improper ctxt x =
wenzelm@59884
   354
  (case Name_Space.lookup (fixes_of ctxt) x of
wenzelm@59884
   355
    SOME (_, proper) => not proper
wenzelm@59790
   356
  | NONE => false);
wenzelm@59790
   357
wenzelm@59790
   358
wenzelm@42488
   359
(* specialized name space *)
wenzelm@42488
   360
wenzelm@59883
   361
val is_fixed = Name_Space.defined o fixes_of;
wenzelm@59846
   362
fun is_newly_fixed inner outer = is_fixed inner andf (not o is_fixed outer);
wenzelm@42488
   363
wenzelm@42493
   364
val fixed_ord = Name_Space.entry_ord o fixes_space;
wenzelm@42493
   365
val intern_fixed = Name_Space.intern o fixes_space;
wenzelm@42488
   366
wenzelm@42488
   367
fun lookup_fixed ctxt x =
wenzelm@42488
   368
  let val x' = intern_fixed ctxt x
wenzelm@42488
   369
  in if is_fixed ctxt x' then SOME x' else NONE end;
wenzelm@42488
   370
wenzelm@42488
   371
fun revert_fixed ctxt x =
wenzelm@59884
   372
  (case Name_Space.lookup (fixes_of ctxt) x of
wenzelm@59884
   373
    SOME (x', _) => if intern_fixed ctxt x' = x then x' else x
wenzelm@42488
   374
  | NONE => x);
wenzelm@42488
   375
wenzelm@45472
   376
fun markup_fixed ctxt x =
wenzelm@45472
   377
  Name_Space.markup (fixes_space ctxt) x
wenzelm@45472
   378
  |> Markup.name (revert_fixed ctxt x);
wenzelm@45472
   379
wenzelm@62987
   380
fun markup ctxt x =
wenzelm@62987
   381
  if is_improper ctxt x then Markup.improper
wenzelm@62987
   382
  else if Name.is_skolem x then Markup.skolem
wenzelm@62987
   383
  else Markup.free;
wenzelm@62987
   384
wenzelm@62987
   385
val markup_entity_def = Name_Space.markup_def o fixes_space;
wenzelm@62987
   386
wenzelm@42488
   387
fun dest_fixes ctxt =
wenzelm@59790
   388
  Name_Space.fold_table (fn (x, (y, _)) => cons (y, x)) (fixes_of ctxt) []
wenzelm@59058
   389
  |> sort (Name_Space.entry_ord (fixes_space ctxt) o apply2 #2);
wenzelm@42488
   390
wenzelm@42488
   391
wenzelm@42482
   392
(* collect variables *)
wenzelm@42482
   393
wenzelm@42482
   394
fun add_free_names ctxt =
wenzelm@42482
   395
  fold_aterms (fn Free (x, _) => not (is_fixed ctxt x) ? insert (op =) x | _ => I);
wenzelm@42482
   396
wenzelm@42482
   397
fun add_frees ctxt =
wenzelm@42482
   398
  fold_aterms (fn Free (x, T) => not (is_fixed ctxt x) ? insert (op =) (x, T) | _ => I);
wenzelm@42482
   399
wenzelm@42482
   400
fun add_fixed_names ctxt =
wenzelm@42482
   401
  fold_aterms (fn Free (x, _) => is_fixed ctxt x ? insert (op =) x | _ => I);
wenzelm@42482
   402
wenzelm@42482
   403
fun add_fixed ctxt =
wenzelm@42482
   404
  fold_aterms (fn Free (x, T) => is_fixed ctxt x ? insert (op =) (x, T) | _ => I);
wenzelm@42482
   405
wenzelm@59846
   406
fun add_newly_fixed ctxt' ctxt =
wenzelm@59846
   407
  fold_aterms (fn Free (x, T) => is_newly_fixed ctxt' ctxt x ? insert (op =) (x, T) | _ => I);
wenzelm@59846
   408
wenzelm@42482
   409
wenzelm@42482
   410
(* declarations *)
wenzelm@42482
   411
wenzelm@20084
   412
local
wenzelm@20084
   413
wenzelm@42488
   414
fun err_dups dups =
wenzelm@42488
   415
  error ("Duplicate fixed variable(s): " ^ commas (map Binding.print dups));
wenzelm@19911
   416
wenzelm@42488
   417
fun new_fixed ((x, x'), pos) ctxt =
wenzelm@42488
   418
  if is_some (lookup_fixed ctxt x') then err_dups [Binding.make (x, pos)]
wenzelm@42488
   419
  else
wenzelm@59790
   420
    let
wenzelm@59790
   421
      val proper = Config.get ctxt proper_fixes;
wenzelm@61949
   422
      val context = Context.Proof ctxt
wenzelm@61949
   423
        |> Name_Space.map_naming (K Name_Space.global_naming)
wenzelm@61949
   424
        |> Context_Position.set_visible_generic false;
wenzelm@59790
   425
    in
wenzelm@47005
   426
      ctxt
wenzelm@47005
   427
      |> map_fixes
wenzelm@59790
   428
        (Name_Space.define context true (Binding.make (x', pos), (x, proper)) #> snd #>
wenzelm@58668
   429
          Name_Space.alias_table Name_Space.global_naming (Binding.make (x, pos)) x')
wenzelm@47005
   430
      |> declare_fixed x
wenzelm@47005
   431
      |> declare_constraints (Syntax.free x')
wenzelm@47005
   432
  end;
wenzelm@42488
   433
wenzelm@42488
   434
fun new_fixes names' xs xs' ps =
wenzelm@20102
   435
  map_names (K names') #>
wenzelm@42488
   436
  fold new_fixed ((xs ~~ xs') ~~ ps) #>
wenzelm@20084
   437
  pair xs';
wenzelm@20084
   438
wenzelm@20084
   439
in
wenzelm@20084
   440
wenzelm@42488
   441
fun add_fixes_binding bs ctxt =
wenzelm@19911
   442
  let
wenzelm@19911
   443
    val _ =
wenzelm@55948
   444
      (case filter (Name.is_skolem o Binding.name_of) bs of
wenzelm@42488
   445
        [] => ()
wenzelm@42488
   446
      | bads => error ("Illegal internal Skolem constant(s): " ^ commas (map Binding.print bads)));
wenzelm@42488
   447
    val _ =
wenzelm@59058
   448
      (case duplicates (op = o apply2 Binding.name_of) bs of
wenzelm@42488
   449
        [] => ()
wenzelm@42488
   450
      | dups => err_dups dups);
wenzelm@42488
   451
wenzelm@42494
   452
    val xs = map check_name bs;
wenzelm@20102
   453
    val names = names_of ctxt;
wenzelm@20084
   454
    val (xs', names') =
wenzelm@43326
   455
      if is_body ctxt then fold_map Name.variant xs names |>> map Name.skolem
wenzelm@42488
   456
      else (xs, fold Name.declare xs names);
wenzelm@42488
   457
  in ctxt |> new_fixes names' xs xs' (map Binding.pos_of bs) end;
wenzelm@19911
   458
wenzelm@60707
   459
fun bound_fixes xs ctxt =
wenzelm@60707
   460
  let
wenzelm@60707
   461
    val (xs', ctxt') = fold_map next_bound xs ctxt;
wenzelm@60707
   462
    val no_ps = replicate (length xs) Position.none;
wenzelm@60707
   463
  in ctxt' |> new_fixes (names_of ctxt') (map #1 xs) (map (#1 o dest_Free) xs') no_ps end;
wenzelm@60707
   464
wenzelm@20797
   465
fun variant_fixes raw_xs ctxt =
wenzelm@20084
   466
  let
wenzelm@20102
   467
    val names = names_of ctxt;
wenzelm@55948
   468
    val xs = map (fn x => Name.clean x |> Name.is_internal x ? Name.internal) raw_xs;
wenzelm@43326
   469
    val (xs', names') = fold_map Name.variant xs names |>> (is_body ctxt ? map Name.skolem);
wenzelm@60707
   470
    val no_ps = replicate (length xs) Position.none;
wenzelm@60707
   471
  in ctxt |> new_fixes names' xs xs' no_ps end;
wenzelm@20084
   472
wenzelm@20084
   473
end;
wenzelm@19911
   474
wenzelm@42488
   475
val add_fixes = add_fixes_binding o map Binding.name;
wenzelm@20251
   476
wenzelm@20251
   477
fun add_fixes_direct xs ctxt = ctxt
wenzelm@20251
   478
  |> set_body false
wenzelm@20251
   479
  |> (snd o add_fixes xs)
wenzelm@20251
   480
  |> restore_body ctxt;
wenzelm@20251
   481
wenzelm@42482
   482
fun auto_fixes t ctxt = ctxt
wenzelm@42482
   483
  |> not (is_body ctxt) ? add_fixes_direct (rev (add_free_names ctxt t []))
wenzelm@20251
   484
  |> declare_term t;
wenzelm@20251
   485
wenzelm@59796
   486
wenzelm@59796
   487
(* dummy patterns *)
wenzelm@59796
   488
wenzelm@59796
   489
fun fix_dummy_patterns (Const ("Pure.dummy_pattern", T)) ctxt =
wenzelm@61923
   490
      let val ([x], ctxt') = ctxt |> set_body true |> add_fixes [Name.uu_] ||> restore_body ctxt
wenzelm@59796
   491
      in (Free (x, T), ctxt') end
wenzelm@59796
   492
  | fix_dummy_patterns (Abs (x, T, b)) ctxt =
wenzelm@59796
   493
      let val (b', ctxt') = fix_dummy_patterns b ctxt
wenzelm@59796
   494
      in (Abs (x, T, b'), ctxt') end
wenzelm@59796
   495
  | fix_dummy_patterns (t $ u) ctxt =
wenzelm@59796
   496
      let
wenzelm@59796
   497
        val (t', ctxt') = fix_dummy_patterns t ctxt;
wenzelm@59796
   498
        val (u', ctxt'') = fix_dummy_patterns u ctxt';
wenzelm@59796
   499
      in (t' $ u', ctxt'') end
wenzelm@59796
   500
  | fix_dummy_patterns a ctxt = (a, ctxt);
wenzelm@19911
   501
wenzelm@19911
   502
wenzelm@19911
   503
wenzelm@22671
   504
(** export -- generalize type/term variables (beware of closure sizes) **)
wenzelm@19911
   505
wenzelm@60823
   506
fun gen_all ctxt th =
wenzelm@60823
   507
  let
wenzelm@60823
   508
    val i = Thm.maxidx_thm th (maxidx_of ctxt) + 1;
wenzelm@60823
   509
    fun gen (x, T) = Thm.forall_elim (Thm.cterm_of ctxt (Var ((x, i), T)));
wenzelm@60823
   510
  in fold gen (Drule.outer_params (Thm.prop_of th)) th end;
wenzelm@60822
   511
wenzelm@19911
   512
fun export_inst inner outer =
wenzelm@19911
   513
  let
wenzelm@20162
   514
    val declared_outer = is_declared outer;
wenzelm@59846
   515
    val still_fixed = not o is_newly_fixed inner outer;
wenzelm@19911
   516
wenzelm@42488
   517
    val gen_fixes =
wenzelm@56025
   518
      Name_Space.fold_table (fn (y, _) => not (is_fixed outer y) ? cons y)
wenzelm@56025
   519
        (fixes_of inner) [];
wenzelm@22671
   520
wenzelm@22671
   521
    val type_occs_inner = type_occs_of inner;
wenzelm@22671
   522
    fun gen_fixesT ts =
wenzelm@19911
   523
      Symtab.fold (fn (a, xs) =>
wenzelm@20162
   524
        if declared_outer a orelse exists still_fixed xs
wenzelm@22671
   525
        then I else cons a) (fold decl_type_occs ts type_occs_inner) [];
wenzelm@19911
   526
  in (gen_fixesT, gen_fixes) end;
wenzelm@19911
   527
wenzelm@19911
   528
fun exportT_inst inner outer = #1 (export_inst inner outer);
wenzelm@19911
   529
wenzelm@22671
   530
fun exportT_terms inner outer =
wenzelm@59645
   531
  let
wenzelm@59645
   532
    val mk_tfrees = exportT_inst inner outer;
wenzelm@59645
   533
    val maxidx = maxidx_of outer;
wenzelm@59645
   534
  in
wenzelm@22671
   535
    fn ts => ts |> map
wenzelm@31977
   536
      (Term_Subst.generalize (mk_tfrees ts, [])
wenzelm@59645
   537
        (fold (Term.fold_types Term.maxidx_typ) ts maxidx + 1))
wenzelm@22671
   538
  end;
wenzelm@19911
   539
wenzelm@22671
   540
fun export_terms inner outer =
wenzelm@59645
   541
  let
wenzelm@59645
   542
    val (mk_tfrees, tfrees) = export_inst inner outer;
wenzelm@59645
   543
    val maxidx = maxidx_of outer;
wenzelm@59645
   544
  in
wenzelm@22671
   545
    fn ts => ts |> map
wenzelm@31977
   546
      (Term_Subst.generalize (mk_tfrees ts, tfrees)
wenzelm@59645
   547
        (fold Term.maxidx_term ts maxidx + 1))
wenzelm@22671
   548
  end;
wenzelm@19911
   549
wenzelm@20303
   550
fun export_prf inner outer prf =
wenzelm@20303
   551
  let
wenzelm@22671
   552
    val (mk_tfrees, frees) = export_inst (declare_prf prf inner) outer;
wenzelm@22671
   553
    val tfrees = mk_tfrees [];
wenzelm@59645
   554
    val maxidx = maxidx_of outer;
wenzelm@59645
   555
    val idx = Proofterm.maxidx_proof prf maxidx + 1;
wenzelm@36620
   556
    val gen_term = Term_Subst.generalize_same (tfrees, frees) idx;
wenzelm@36620
   557
    val gen_typ = Term_Subst.generalizeT_same tfrees idx;
wenzelm@36620
   558
  in Same.commit (Proofterm.map_proof_terms_same gen_term gen_typ) prf end;
wenzelm@20303
   559
wenzelm@22671
   560
wenzelm@59645
   561
fun gen_export (mk_tfrees, frees) maxidx ths =
wenzelm@19911
   562
  let
wenzelm@22671
   563
    val tfrees = mk_tfrees (map Thm.full_prop_of ths);
wenzelm@59645
   564
    val idx = fold Thm.maxidx_thm ths maxidx + 1;
wenzelm@59645
   565
  in map (Thm.generalize (tfrees, frees) idx) ths end;
wenzelm@19911
   566
wenzelm@59645
   567
fun exportT inner outer = gen_export (exportT_inst inner outer, []) (maxidx_of outer);
wenzelm@59645
   568
fun export inner outer = gen_export (export_inst inner outer) (maxidx_of outer);
wenzelm@19911
   569
wenzelm@21522
   570
fun export_morphism inner outer =
wenzelm@21522
   571
  let
wenzelm@21522
   572
    val fact = export inner outer;
wenzelm@21522
   573
    val term = singleton (export_terms inner outer);
wenzelm@21522
   574
    val typ = Logic.type_map term;
wenzelm@54740
   575
  in
wenzelm@54740
   576
    Morphism.morphism "Variable.export" {binding = [], typ = [typ], term = [term], fact = [fact]}
wenzelm@54740
   577
  end;
wenzelm@19911
   578
wenzelm@19911
   579
wenzelm@24765
   580
wenzelm@19911
   581
(** import -- fix schematic type/term variables **)
wenzelm@19911
   582
wenzelm@59796
   583
fun invent_types Ss ctxt =
wenzelm@59796
   584
  let
wenzelm@59796
   585
    val tfrees = Name.invent (names_of ctxt) Name.aT (length Ss) ~~ Ss;
wenzelm@59796
   586
    val ctxt' = fold (declare_constraints o Logic.mk_type o TFree) tfrees ctxt;
wenzelm@59796
   587
  in (tfrees, ctxt') end;
wenzelm@59796
   588
wenzelm@19911
   589
fun importT_inst ts ctxt =
wenzelm@19911
   590
  let
wenzelm@19911
   591
    val tvars = rev (fold Term.add_tvars ts []);
wenzelm@19911
   592
    val (tfrees, ctxt') = invent_types (map #2 tvars) ctxt;
wenzelm@19911
   593
  in (tvars ~~ map TFree tfrees, ctxt') end;
wenzelm@19911
   594
wenzelm@19911
   595
fun import_inst is_open ts ctxt =
wenzelm@19911
   596
  let
wenzelm@26714
   597
    val ren = Name.clean #> (if is_open then I else Name.internal);
wenzelm@19911
   598
    val (instT, ctxt') = importT_inst ts ctxt;
wenzelm@31977
   599
    val vars = map (apsnd (Term_Subst.instantiateT instT)) (rev (fold Term.add_vars ts []));
wenzelm@20797
   600
    val (xs, ctxt'') = variant_fixes (map (ren o #1 o #1) vars) ctxt';
wenzelm@19911
   601
    val inst = vars ~~ map Free (xs ~~ map #2 vars);
wenzelm@19911
   602
  in ((instT, inst), ctxt'') end;
wenzelm@19911
   603
wenzelm@19911
   604
fun importT_terms ts ctxt =
wenzelm@19911
   605
  let val (instT, ctxt') = importT_inst ts ctxt
wenzelm@31977
   606
  in (map (Term_Subst.instantiate (instT, [])) ts, ctxt') end;
wenzelm@19911
   607
wenzelm@19911
   608
fun import_terms is_open ts ctxt =
wenzelm@19911
   609
  let val (inst, ctxt') = import_inst is_open ts ctxt
wenzelm@31977
   610
  in (map (Term_Subst.instantiate inst) ts, ctxt') end;
wenzelm@19911
   611
wenzelm@31794
   612
fun importT ths ctxt =
wenzelm@19911
   613
  let
wenzelm@19911
   614
    val (instT, ctxt') = importT_inst (map Thm.full_prop_of ths) ctxt;
wenzelm@60805
   615
    val instT' = map (apsnd (Thm.ctyp_of ctxt')) instT;
wenzelm@60805
   616
    val ths' = map (Thm.instantiate (instT', [])) ths;
wenzelm@32280
   617
  in ((instT', ths'), ctxt') end;
wenzelm@19911
   618
wenzelm@20303
   619
fun import_prf is_open prf ctxt =
wenzelm@20303
   620
  let
wenzelm@20303
   621
    val ts = rev (Proofterm.fold_proof_terms cons (cons o Logic.mk_type) prf []);
wenzelm@20303
   622
    val (insts, ctxt') = import_inst is_open ts ctxt;
wenzelm@20303
   623
  in (Proofterm.instantiate insts prf, ctxt') end;
wenzelm@20303
   624
wenzelm@31794
   625
fun import is_open ths ctxt =
wenzelm@19911
   626
  let
wenzelm@60805
   627
    val ((instT, inst), ctxt') = import_inst is_open (map Thm.full_prop_of ths) ctxt;
wenzelm@60805
   628
    val insts' =
wenzelm@60805
   629
     (map (apsnd (Thm.ctyp_of ctxt')) instT,
wenzelm@60805
   630
      map (apsnd (Thm.cterm_of ctxt')) inst);
wenzelm@32280
   631
    val ths' = map (Thm.instantiate insts') ths;
wenzelm@32280
   632
  in ((insts', ths'), ctxt') end;
wenzelm@19911
   633
wenzelm@19911
   634
wenzelm@19926
   635
(* import/export *)
wenzelm@19926
   636
wenzelm@21287
   637
fun gen_trade imp exp f ctxt ths =
wenzelm@20220
   638
  let val ((_, ths'), ctxt') = imp ths ctxt
wenzelm@21287
   639
  in exp ctxt' ctxt (f ctxt' ths') end;
wenzelm@19926
   640
wenzelm@31794
   641
val tradeT = gen_trade importT exportT;
wenzelm@31794
   642
val trade = gen_trade (import true) export;
wenzelm@19926
   643
wenzelm@19926
   644
wenzelm@42495
   645
(* focus on outermost parameters: !!x y z. B *)
wenzelm@42495
   646
wenzelm@60707
   647
val bound_focus =
wenzelm@64556
   648
  Config.bool (Config.declare ("bound_focus", \<^here>) (K (Config.Bool false)));
wenzelm@60707
   649
wenzelm@60707
   650
fun is_bound_focus ctxt = Config.get ctxt bound_focus;
wenzelm@60707
   651
val set_bound_focus = Config.put bound_focus;
wenzelm@60707
   652
fun restore_bound_focus ctxt = set_bound_focus (is_bound_focus ctxt);
wenzelm@60707
   653
wenzelm@60695
   654
fun focus_params bindings t ctxt =
wenzelm@42495
   655
  let
wenzelm@60707
   656
    val ps = Term.variant_frees t (Term.strip_all_vars t);  (*as they are printed :-*)
wenzelm@60707
   657
    val (xs, Ts) = split_list ps;
wenzelm@60695
   658
    val (xs', ctxt') =
wenzelm@60695
   659
      (case bindings of
wenzelm@60695
   660
        SOME bs => ctxt |> set_body true |> add_fixes_binding bs ||> restore_body ctxt
wenzelm@60707
   661
      | NONE => if is_bound_focus ctxt then bound_fixes ps ctxt else variant_fixes xs ctxt);
wenzelm@60707
   662
    val ps' = xs' ~~ Ts;
wenzelm@60707
   663
    val ctxt'' = ctxt' |> fold (declare_constraints o Free) ps';
wenzelm@60707
   664
  in ((xs, ps'), ctxt'') end;
wenzelm@42495
   665
wenzelm@60695
   666
fun focus bindings t ctxt =
wenzelm@42495
   667
  let
wenzelm@60695
   668
    val ((xs, ps), ctxt') = focus_params bindings t ctxt;
wenzelm@42495
   669
    val t' = Term.subst_bounds (rev (map Free ps), Term.strip_all_body t);
wenzelm@42495
   670
  in (((xs ~~ ps), t'), ctxt') end;
wenzelm@20149
   671
wenzelm@20149
   672
fun forall_elim_prop t prop =
wenzelm@46497
   673
  Thm.beta_conversion false (Thm.apply (Thm.dest_arg prop) t)
wenzelm@20579
   674
  |> Thm.cprop_of |> Thm.dest_arg;
wenzelm@20149
   675
wenzelm@60695
   676
fun focus_cterm bindings goal ctxt =
wenzelm@20149
   677
  let
wenzelm@60695
   678
    val ((xs, ps), ctxt') = focus_params bindings (Thm.term_of goal) ctxt;
wenzelm@59623
   679
    val ps' = map (Thm.cterm_of ctxt' o Free) ps;
wenzelm@20220
   680
    val goal' = fold forall_elim_prop ps' goal;
wenzelm@42495
   681
  in ((xs ~~ ps', goal'), ctxt') end;
wenzelm@20149
   682
wenzelm@60695
   683
fun focus_subgoal bindings i st =
wenzelm@20303
   684
  let
wenzelm@22691
   685
    val all_vars = Thm.fold_terms Term.add_vars st [];
wenzelm@20303
   686
  in
wenzelm@60401
   687
    fold (unbind_term o #1) all_vars #>
wenzelm@20303
   688
    fold (declare_constraints o Var) all_vars #>
wenzelm@60695
   689
    focus_cterm bindings (Thm.cprem_of st i)
wenzelm@20303
   690
  end;
wenzelm@20303
   691
wenzelm@20303
   692
wenzelm@19911
   693
wenzelm@19911
   694
(** implicit polymorphism **)
wenzelm@19899
   695
wenzelm@19899
   696
(* warn_extra_tfrees *)
wenzelm@19899
   697
wenzelm@19899
   698
fun warn_extra_tfrees ctxt1 ctxt2 =
wenzelm@19899
   699
  let
wenzelm@19911
   700
    fun occs_typ a = Term.exists_subtype (fn TFree (b, _) => a = b | _ => false);
wenzelm@20162
   701
    fun occs_free a x =
wenzelm@20162
   702
      (case def_type ctxt1 false (x, ~1) of
wenzelm@20162
   703
        SOME T => if occs_typ a T then I else cons (a, x)
wenzelm@20162
   704
      | NONE => cons (a, x));
wenzelm@19899
   705
wenzelm@20162
   706
    val occs1 = type_occs_of ctxt1;
wenzelm@20162
   707
    val occs2 = type_occs_of ctxt2;
wenzelm@19911
   708
    val extras = Symtab.fold (fn (a, xs) =>
wenzelm@19911
   709
      if Symtab.defined occs1 a then I else fold (occs_free a) xs) occs2 [];
wenzelm@19899
   710
    val tfrees = map #1 extras |> sort_distinct string_ord;
wenzelm@19899
   711
    val frees = map #2 extras |> sort_distinct string_ord;
wenzelm@19899
   712
  in
wenzelm@38831
   713
    if null extras orelse not (Context_Position.is_visible ctxt2) then ()
wenzelm@19899
   714
    else warning ("Introduced fixed type variable(s): " ^ commas tfrees ^ " in " ^
wenzelm@19899
   715
      space_implode " or " (map quote frees))
wenzelm@19899
   716
  end;
wenzelm@19899
   717
wenzelm@19899
   718
wenzelm@20149
   719
(* polymorphic terms *)
wenzelm@19899
   720
wenzelm@24694
   721
fun polymorphic_types ctxt ts =
wenzelm@20003
   722
  let
wenzelm@20003
   723
    val ctxt' = fold declare_term ts ctxt;
wenzelm@20162
   724
    val occs = type_occs_of ctxt;
wenzelm@20162
   725
    val occs' = type_occs_of ctxt';
wenzelm@20162
   726
    val types = Symtab.fold (fn (a, _) => if Symtab.defined occs a then I else cons a) occs' [];
wenzelm@24765
   727
    val idx = maxidx_of ctxt' + 1;
wenzelm@24694
   728
    val Ts' = (fold o fold_types o fold_atyps)
wenzelm@24694
   729
      (fn T as TFree _ =>
wenzelm@31977
   730
          (case Term_Subst.generalizeT types idx T of TVar v => insert (op =) v | _ => I)
wenzelm@24694
   731
        | _ => I) ts [];
wenzelm@31977
   732
    val ts' = map (Term_Subst.generalize (types, []) idx) ts;
wenzelm@24694
   733
  in (rev Ts', ts') end;
wenzelm@24694
   734
wenzelm@24694
   735
fun polymorphic ctxt ts = snd (polymorphic_types ctxt ts);
wenzelm@20003
   736
wenzelm@19899
   737
end;