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