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