src/Pure/variable.ML
author wenzelm
Tue Jul 11 23:00:39 2006 +0200 (2006-07-11)
changeset 20102 6676a17dfc88
parent 20084 aa320957f00c
child 20123 88fa41273824
permissions -rw-r--r--
separate names filed (covers fixes/defaults);
     1 (*  Title:      Pure/variable.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Fixed type/term variables and polymorphic term abbreviations.
     6 *)
     7 
     8 signature VARIABLE =
     9 sig
    10   val is_body: Context.proof -> bool
    11   val set_body: bool -> Context.proof -> Context.proof
    12   val restore_body: Context.proof -> Context.proof -> Context.proof
    13   val fixes_of: Context.proof -> (string * string) list
    14   val binds_of: Context.proof -> (typ * term) Vartab.table
    15   val defaults_of: Context.proof ->
    16     typ Vartab.table * sort Vartab.table * string list * string list Symtab.table
    17   val used_types: Context.proof -> string list
    18   val is_declared: Context.proof -> string -> bool
    19   val is_fixed: Context.proof -> string -> bool
    20   val def_sort: Context.proof -> indexname -> sort option
    21   val def_type: Context.proof -> bool -> indexname -> typ option
    22   val default_type: Context.proof -> string -> typ option
    23   val declare_type: typ -> Context.proof -> Context.proof
    24   val declare_syntax: term -> Context.proof -> Context.proof
    25   val declare_term: term -> Context.proof -> Context.proof
    26   val declare_thm: thm -> Context.proof -> Context.proof
    27   val thm_context: thm -> Context.proof
    28   val rename_wrt: Context.proof -> term list -> (string * 'a) list -> (string * 'a) list
    29   val add_fixes: string list -> Context.proof -> string list * Context.proof
    30   val invent_fixes: string list -> Context.proof -> string list * Context.proof
    31   val invent_types: sort list -> Context.proof -> (string * sort) list * Context.proof
    32   val export_inst: Context.proof -> Context.proof -> string list * string list
    33   val exportT_inst: Context.proof -> Context.proof -> string list
    34   val export_terms: Context.proof -> Context.proof -> term list -> term list
    35   val exportT_terms: Context.proof -> Context.proof -> term list -> term list
    36   val exportT: Context.proof -> Context.proof -> thm list -> thm list
    37   val export: Context.proof -> Context.proof -> thm list -> thm list
    38   val importT_inst: term list -> Context.proof -> ((indexname * sort) * typ) list * Context.proof
    39   val import_inst: bool -> term list -> Context.proof ->
    40     (((indexname * sort) * typ) list * ((indexname * typ) * term) list) * Context.proof
    41   val importT_terms: term list -> Context.proof -> term list * Context.proof
    42   val import_terms: bool -> term list -> Context.proof -> term list * Context.proof
    43   val importT: thm list -> Context.proof -> thm list * Context.proof
    44   val import: bool -> thm list -> Context.proof -> thm list * Context.proof
    45   val tradeT: Context.proof -> (thm list -> thm list) -> thm list -> thm list
    46   val trade: Context.proof -> (thm list -> thm list) -> thm list -> thm list
    47   val warn_extra_tfrees: Context.proof -> Context.proof -> unit
    48   val monomorphic: Context.proof -> term list -> term list
    49   val polymorphic: Context.proof -> term list -> term list
    50   val hidden_polymorphism: term -> typ -> (indexname * sort) list
    51   val add_binds: (indexname * term option) list -> Context.proof -> Context.proof
    52   val expand_binds: Context.proof -> term -> term
    53 end;
    54 
    55 structure Variable: VARIABLE =
    56 struct
    57 
    58 
    59 (** local context data **)
    60 
    61 datatype data = Data of
    62  {is_body: bool,                        (*inner body mode*)
    63   names: Name.context,        		(*used type/term variable names*)
    64   fixes: (string * string) list, 	(*term fixes -- extern/intern*)
    65   binds: (typ * term) Vartab.table,     (*term bindings*)
    66   defaults:
    67     typ Vartab.table *                  (*type constraints*)
    68     sort Vartab.table *                 (*default sorts*)
    69     string list *                       (*used type variables*)
    70     string list Symtab.table};          (*occurrences of type variables in term variables*)
    71 
    72 fun make_data (is_body, names, fixes, binds, defaults) =
    73   Data {is_body = is_body, names = names, fixes = fixes, binds = binds, defaults = defaults};
    74 
    75 structure Data = ProofDataFun
    76 (
    77   val name = "Pure/variable";
    78   type T = data;
    79   fun init thy =
    80     make_data (false, Name.make_context ["", "'"], [], Vartab.empty,
    81       (Vartab.empty, Vartab.empty, [], Symtab.empty));
    82   fun print _ _ = ();
    83 );
    84 
    85 val _ = Context.add_setup Data.init;
    86 
    87 fun map_data f =
    88   Data.map (fn Data {is_body, names, fixes, binds, defaults} =>
    89     make_data (f (is_body, names, fixes, binds, defaults)));
    90 
    91 fun map_names f = map_data (fn (is_body, names, fixes, binds, defaults) =>
    92   (is_body, f names, fixes, binds, defaults));
    93 
    94 fun map_fixes f = map_data (fn (is_body, names, fixes, binds, defaults) =>
    95   (is_body, names, f fixes, binds, defaults));
    96 
    97 fun map_binds f = map_data (fn (is_body, names, fixes, binds, defaults) =>
    98   (is_body, names, fixes, f binds, defaults));
    99 
   100 fun map_defaults f = map_data (fn (is_body, names, fixes, binds, defaults) =>
   101   (is_body, names, fixes, binds, f defaults));
   102 
   103 fun rep_data ctxt = Data.get ctxt |> (fn Data args => args);
   104 
   105 val is_body = #is_body o rep_data;
   106 fun set_body b = map_data (fn (_, names, fixes, binds, defaults) =>
   107   (b, names, fixes, binds, defaults));
   108 fun restore_body ctxt = set_body (is_body ctxt);
   109 
   110 val names_of = #names o rep_data;
   111 val fixes_of = #fixes o rep_data;
   112 val binds_of = #binds o rep_data;
   113 
   114 val defaults_of = #defaults o rep_data;
   115 val used_types = #3 o defaults_of;
   116 val type_occs_of = #4 o defaults_of;
   117 
   118 fun is_declared ctxt x = Vartab.defined (#1 (defaults_of ctxt)) (x, ~1);
   119 fun is_fixed ctxt x = exists (fn (_, y) => x = y) (fixes_of ctxt);
   120 
   121 
   122 
   123 (** declarations **)
   124 
   125 (* default sorts and types *)
   126 
   127 val def_sort = Vartab.lookup o #2 o defaults_of;
   128 
   129 fun def_type ctxt pattern xi =
   130   let val {binds, defaults = (types, _, _, _), ...} = rep_data ctxt in
   131     (case Vartab.lookup types xi of
   132       NONE =>
   133         if pattern then NONE
   134         else Vartab.lookup binds xi |> Option.map (TypeInfer.polymorphicT o #1)
   135     | some => some)
   136   end;
   137 
   138 fun default_type ctxt x = Vartab.lookup (#1 (defaults_of ctxt)) (x, ~1);
   139 
   140 
   141 (* declare types/terms *)
   142 
   143 local
   144 
   145 val ins_types = fold_aterms
   146   (fn Free (x, T) => Vartab.update ((x, ~1), T)
   147     | Var v => Vartab.update v
   148     | _ => I);
   149 
   150 val ins_sorts = fold_atyps
   151   (fn TFree (x, S) => Vartab.update ((x, ~1), S)
   152     | TVar v => Vartab.update v
   153     | _ => I);
   154 
   155 val ins_used = fold_atyps
   156   (fn TFree (x, _) => insert (op =) x | _ => I);
   157 
   158 val ins_occs = fold_term_types (fn t =>
   159   let val x = case t of Free (x, _) => x | _ => ""
   160   in fold_atyps (fn TFree (a, _) => Symtab.insert_list (op =) (a, x) | _ => I) end);
   161 
   162 fun ins_skolem def_ty = fold_rev (fn (x, x') =>
   163   (case def_ty x' of
   164     SOME T => Vartab.update ((x, ~1), T)
   165   | NONE => I));
   166 
   167 val ins_namesT = fold_atyps
   168   (fn TFree (x, _) => Name.declare x | _ => I);
   169 
   170 fun ins_names t =
   171   fold_types ins_namesT t #>
   172   fold_aterms (fn Free (x, _) => Name.declare x | _ => I) t;
   173 
   174 in
   175 
   176 fun declare_type T = map_defaults (fn (types, sorts, used, occ) =>
   177   (types,
   178     ins_sorts T sorts,
   179     ins_used T used,
   180     occ)) #>
   181   map_names (ins_namesT T);
   182 
   183 fun declare_syntax t = map_defaults (fn (types, sorts, used, occ) =>
   184   (ins_types t types,
   185     fold_types ins_sorts t sorts,
   186     fold_types ins_used t used,
   187     occ)) #>
   188   map_names (ins_names t);
   189 
   190 fun declare_occs t = map_defaults (fn (types, sorts, used, occ) =>
   191   (types, sorts, used, ins_occs t occ));
   192 
   193 fun declare_term t ctxt =
   194   ctxt
   195   |> declare_syntax t
   196   |> map_defaults (fn (types, sorts, used, occ) =>
   197      (ins_skolem (fn x => Vartab.lookup types (x, ~1)) (fixes_of ctxt) types,
   198       sorts,
   199       used,
   200       ins_occs t occ))
   201   |> map_names (ins_names t);
   202 
   203 fun declare_thm th = fold declare_term (Thm.full_prop_of th :: Thm.hyps_of th);
   204 fun thm_context th = Context.init_proof (Thm.theory_of_thm th) |> declare_thm th;
   205 
   206 end;
   207 
   208 
   209 (* renaming term/type frees *)
   210 
   211 fun rename_wrt ctxt ts frees =
   212   let
   213     val names = names_of (ctxt |> fold declare_syntax ts);
   214     val xs = fst (Name.variants (map #1 frees) names);
   215   in xs ~~ map snd frees end;
   216 
   217 
   218 
   219 (** fixes **)
   220 
   221 local
   222 
   223 fun no_dups [] = ()
   224   | no_dups dups = error ("Duplicate fixed variable(s): " ^ commas_quote dups);
   225 
   226 fun new_fixes names' xs xs' =
   227   map_names (K names') #>
   228   map_fixes (fn fixes => (rev (xs ~~ xs') @ fixes)) #>
   229   fold (declare_syntax o Syntax.free) xs' #>
   230   pair xs';
   231 
   232 in
   233 
   234 fun add_fixes xs ctxt =
   235   let
   236     val _ =
   237       (case filter (can Name.dest_skolem) xs of [] => ()
   238       | bads => error ("Illegal internal Skolem constant(s): " ^ commas_quote bads));
   239     val _ = no_dups (duplicates (op =) xs);
   240     val (ys, zs) = split_list (fixes_of ctxt);
   241     val names = names_of ctxt;
   242     val (xs', names') =
   243       if is_body ctxt then Name.variants (map Name.skolem xs) names
   244       else (no_dups (xs inter_string ys); no_dups (xs inter_string zs);
   245         (xs, fold Name.declare xs names));
   246   in ctxt |> new_fixes names' xs xs' end;
   247 
   248 fun invent_fixes raw_xs ctxt =
   249   let
   250     val names = names_of ctxt;
   251     val (xs, names') = Name.variants (map Name.clean raw_xs) names;
   252     val xs' = map Name.skolem xs;
   253   in ctxt |> new_fixes names' xs xs' end;
   254 
   255 end;
   256 
   257 fun invent_types Ss ctxt =
   258   let
   259     val tfrees = Name.invents (names_of ctxt) "'a" (length Ss) ~~ Ss;
   260     val ctxt' = fold (declare_type o TFree) tfrees ctxt;
   261   in (tfrees, ctxt') end;
   262 
   263 
   264 
   265 (** export -- generalize type/term variables **)
   266 
   267 fun export_inst inner outer =
   268   let
   269     val types_outer = used_types outer;
   270     val fixes_inner = fixes_of inner;
   271     val fixes_outer = fixes_of outer;
   272 
   273     val gen_fixes = map #2 (Library.take (length fixes_inner - length fixes_outer, fixes_inner));
   274     val still_fixed = not o member (op =) ("" :: gen_fixes);
   275     val gen_fixesT =
   276       Symtab.fold (fn (a, xs) =>
   277         if member (op =) types_outer a orelse exists still_fixed xs
   278         then I else cons a) (type_occs_of inner) [];
   279   in (gen_fixesT, gen_fixes) end;
   280 
   281 fun exportT_inst inner outer = #1 (export_inst inner outer);
   282 
   283 fun exportT_terms inner outer ts =
   284   map (Term.generalize (exportT_inst (fold declare_occs ts inner) outer, [])
   285     (fold (Term.fold_types Term.maxidx_typ) ts ~1 + 1)) ts;
   286 
   287 fun export_terms inner outer ts =
   288   map (Term.generalize (export_inst (fold declare_occs ts inner) outer)
   289     (fold Term.maxidx_term ts ~1 + 1)) ts;
   290 
   291 fun gen_export inst inner outer ths =
   292   let
   293     val ths' = map Thm.adjust_maxidx_thm ths;
   294     val inner' = fold (declare_occs o Thm.full_prop_of) ths' inner;
   295   in map (Thm.generalize (inst inner' outer) (fold Thm.maxidx_thm ths' ~1 + 1)) ths' end;
   296 
   297 val exportT = gen_export (rpair [] oo exportT_inst);
   298 val export = gen_export export_inst;
   299 
   300 
   301 
   302 (** import -- fix schematic type/term variables **)
   303 
   304 fun importT_inst ts ctxt =
   305   let
   306     val tvars = rev (fold Term.add_tvars ts []);
   307     val (tfrees, ctxt') = invent_types (map #2 tvars) ctxt;
   308   in (tvars ~~ map TFree tfrees, ctxt') end;
   309 
   310 fun import_inst is_open ts ctxt =
   311   let
   312     val (instT, ctxt') = importT_inst ts ctxt;
   313     val vars = map (apsnd (Term.instantiateT instT)) (rev (fold Term.add_vars ts []));
   314     val ren = if is_open then I else Name.internal;
   315     val (xs, ctxt'') = invent_fixes (map (ren o #1 o #1) vars) ctxt';
   316     val inst = vars ~~ map Free (xs ~~ map #2 vars);
   317   in ((instT, inst), ctxt'') end;
   318 
   319 fun importT_terms ts ctxt =
   320   let val (instT, ctxt') = importT_inst ts ctxt
   321   in (map (Term.instantiate (instT, [])) ts, ctxt') end;
   322 
   323 fun import_terms is_open ts ctxt =
   324   let val (inst, ctxt') = import_inst is_open ts ctxt
   325   in (map (Term.instantiate inst) ts, ctxt') end;
   326 
   327 fun importT ths ctxt =
   328   let
   329     val thy = Context.theory_of_proof ctxt;
   330     val certT = Thm.ctyp_of thy;
   331     val (instT, ctxt') = importT_inst (map Thm.full_prop_of ths) ctxt;
   332     val instT' = map (fn (v, T) => (certT (TVar v), certT T)) instT;
   333     val ths' = map (Thm.instantiate (instT', [])) ths;
   334   in (ths', ctxt') end;
   335 
   336 fun import is_open ths ctxt =
   337   let
   338     val thy = Context.theory_of_proof ctxt;
   339     val cert = Thm.cterm_of thy;
   340     val certT = Thm.ctyp_of thy;
   341     val ((instT, inst), ctxt') = import_inst is_open (map Thm.full_prop_of ths) ctxt;
   342     val instT' = map (fn (v, T) => (certT (TVar v), certT T)) instT;
   343     val inst' = map (fn (v, t) => (cert (Var v), cert t)) inst;
   344     val ths' = map (Thm.instantiate (instT', inst')) ths;
   345   in (ths', ctxt') end;
   346 
   347 
   348 (* import/export *)
   349 
   350 fun gen_trade imp exp ctxt f ths =
   351   let val (ths', ctxt') = imp ths ctxt
   352   in exp ctxt' ctxt (f ths') end;
   353 
   354 val tradeT = gen_trade importT exportT;
   355 val trade = gen_trade (import true) export;
   356 
   357 
   358 
   359 (** implicit polymorphism **)
   360 
   361 (* warn_extra_tfrees *)
   362 
   363 fun warn_extra_tfrees ctxt1 ctxt2 =
   364   let
   365     fun occs_typ a = Term.exists_subtype (fn TFree (b, _) => a = b | _ => false);
   366     fun occs_free _ "" = I
   367       | occs_free a x =
   368           (case def_type ctxt1 false (x, ~1) of
   369             SOME T => if occs_typ a T then I else cons (a, x)
   370           | NONE => cons (a, x));
   371 
   372     val occs1 = type_occs_of ctxt1 and occs2 = type_occs_of ctxt2;
   373     val extras = Symtab.fold (fn (a, xs) =>
   374       if Symtab.defined occs1 a then I else fold (occs_free a) xs) occs2 [];
   375     val tfrees = map #1 extras |> sort_distinct string_ord;
   376     val frees = map #2 extras |> sort_distinct string_ord;
   377   in
   378     if null extras then ()
   379     else warning ("Introduced fixed type variable(s): " ^ commas tfrees ^ " in " ^
   380       space_implode " or " (map quote frees))
   381   end;
   382 
   383 
   384 (* monomorphic vs. polymorphic terms *)
   385 
   386 fun monomorphic ctxt ts =
   387   #1 (importT_terms ts (fold declare_term ts ctxt));
   388 
   389 fun polymorphic ctxt ts =
   390   let
   391     val ctxt' = fold declare_term ts ctxt;
   392     val types = subtract (op =) (used_types ctxt) (used_types ctxt');
   393     val idx = fold (Term.fold_types Term.maxidx_typ) ts ~1 + 1;
   394   in map (Term.generalize (types, []) idx) ts end;
   395 
   396 
   397 
   398 (** term bindings **)
   399 
   400 fun hidden_polymorphism t T =
   401   let
   402     val tvarsT = Term.add_tvarsT T [];
   403     val extra_tvars = Term.fold_types (Term.fold_atyps
   404       (fn TVar v => if member (op =) tvarsT v then I else insert (op =) v | _ => I)) t [];
   405   in extra_tvars end;
   406 
   407 fun add_bind (xi, NONE) = map_binds (Vartab.delete_safe xi)
   408   | add_bind ((x, i), SOME t) =
   409       let
   410         val T = Term.fastype_of t;
   411         val t' =
   412           if null (hidden_polymorphism t T) then t
   413           else Var ((x ^ "_has_extra_type_vars_on_rhs", i), T);
   414       in declare_term t' #> map_binds (Vartab.update ((x, i), (T, t'))) end;
   415 
   416 val add_binds = fold add_bind;
   417 
   418 fun expand_binds ctxt =
   419   let
   420     val binds = binds_of ctxt;
   421     fun expand (t as Var (xi, T)) =
   422           (case Vartab.lookup binds xi of
   423             SOME u => Envir.expand_atom T u
   424           | NONE => t)
   425       | expand t = t;
   426   in Envir.beta_norm o Term.map_aterms expand end;
   427 
   428 end;