src/Pure/consts.ML
author blanchet
Mon May 19 23:43:53 2014 +0200 (2014-05-19)
changeset 57008 10f68b83b474
parent 56139 b7add947a6ef
child 61262 7bd1eb4b056e
permissions -rw-r--r--
use E 1.8's auto scheduler option
     1 (*  Title:      Pure/consts.ML
     2     Author:     Makarius
     3 
     4 Polymorphic constants: declarations, abbreviations, additional type
     5 constraints.
     6 *)
     7 
     8 signature CONSTS =
     9 sig
    10   type T
    11   val eq_consts: T * T -> bool
    12   val change_base: bool -> T -> T
    13   val change_ignore: T -> T
    14   val retrieve_abbrevs: T -> string list -> term -> (term * term) list
    15   val dest: T ->
    16    {const_space: Name_Space.T,
    17     constants: (string * (typ * term option)) list,
    18     constraints: (string * typ) list}
    19   val the_const: T -> string -> string * typ                   (*exception TYPE*)
    20   val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
    21   val type_scheme: T -> string -> typ                          (*exception TYPE*)
    22   val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
    23   val the_constraint: T -> string -> typ                       (*exception TYPE*)
    24   val space_of: T -> Name_Space.T
    25   val alias: Name_Space.naming -> binding -> string -> T -> T
    26   val is_concealed: T -> string -> bool
    27   val intern: T -> xstring -> string
    28   val intern_syntax: T -> xstring -> string
    29   val check_const: Context.generic -> T -> xstring * Position.T list -> term * Position.report list
    30   val certify: Context.pretty -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
    31   val typargs: T -> string * typ -> typ list
    32   val instance: T -> string * typ list -> typ
    33   val declare: Context.generic -> binding * typ -> T -> T
    34   val constrain: string * typ option -> T -> T
    35   val abbreviate: Context.generic -> Type.tsig -> string -> binding * term -> T -> (term * term) * T
    36   val revert_abbrev: string -> string -> T -> T
    37   val hide: bool -> string -> T -> T
    38   val empty: T
    39   val merge: T * T -> T
    40 end;
    41 
    42 structure Consts: CONSTS =
    43 struct
    44 
    45 (** consts type **)
    46 
    47 (* datatype T *)
    48 
    49 type decl = {T: typ, typargs: int list list};
    50 type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
    51 
    52 datatype T = Consts of
    53  {decls: (decl * abbrev option) Name_Space.table,
    54   constraints: typ Symtab.table,
    55   rev_abbrevs: (term * term) Item_Net.T Symtab.table};
    56 
    57 fun eq_consts
    58    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
    59     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
    60   pointer_eq (decls1, decls2) andalso
    61   pointer_eq (constraints1, constraints2) andalso
    62   pointer_eq (rev_abbrevs1, rev_abbrevs2);
    63 
    64 fun make_consts (decls, constraints, rev_abbrevs) =
    65   Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
    66 
    67 fun map_consts f (Consts {decls, constraints, rev_abbrevs}) =
    68   make_consts (f (decls, constraints, rev_abbrevs));
    69 
    70 fun change_base begin = map_consts (fn (decls, constraints, rev_abbrevs) =>
    71   (Name_Space.change_base begin decls, constraints, rev_abbrevs));
    72 
    73 val change_ignore = map_consts (fn (decls, constraints, rev_abbrevs) =>
    74   (Name_Space.change_ignore decls, constraints, rev_abbrevs));
    75 
    76 
    77 (* reverted abbrevs *)
    78 
    79 val empty_abbrevs =
    80   Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1);
    81 
    82 fun update_abbrevs mode abbrs =
    83   Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs);
    84 
    85 fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
    86   let val nets = map_filter (Symtab.lookup rev_abbrevs) modes
    87   in fn t => maps (fn net => Item_Net.retrieve net t) nets end;
    88 
    89 
    90 (* dest consts *)
    91 
    92 fun dest (Consts {decls, constraints, ...}) =
    93  {const_space = Name_Space.space_of_table decls,
    94   constants =
    95     Name_Space.fold_table (fn (c, ({T, ...}, abbr)) =>
    96       cons (c, (T, Option.map #rhs abbr))) decls [],
    97   constraints = Symtab.dest constraints};
    98 
    99 
   100 (* lookup consts *)
   101 
   102 fun the_entry (Consts {decls, ...}) c =
   103   (case Name_Space.lookup_key decls c of
   104     SOME entry => entry
   105   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
   106 
   107 fun the_const consts c =
   108   (case the_entry consts c of
   109     (c', ({T, ...}, NONE)) => (c', T)
   110   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
   111 
   112 fun the_abbreviation consts c =
   113   (case the_entry consts c of
   114     (_, ({T, ...}, SOME {rhs, ...})) => (T, rhs)
   115   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
   116 
   117 fun the_decl consts = #1 o #2 o the_entry consts;
   118 val type_scheme = #T oo the_decl;
   119 val type_arguments = #typargs oo the_decl;
   120 
   121 val is_monomorphic = null oo type_arguments;
   122 
   123 fun the_constraint (consts as Consts {constraints, ...}) c =
   124   (case Symtab.lookup constraints c of
   125     SOME T => T
   126   | NONE => type_scheme consts c);
   127 
   128 
   129 (* name space and syntax *)
   130 
   131 fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls;
   132 
   133 fun alias naming binding name = map_consts (fn (decls, constraints, rev_abbrevs) =>
   134   ((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs));
   135 
   136 val is_concealed = Name_Space.is_concealed o space_of;
   137 
   138 val intern = Name_Space.intern o space_of;
   139 
   140 fun intern_syntax consts s =
   141   (case try Lexicon.unmark_const s of
   142     SOME c => c
   143   | NONE => intern consts s);
   144 
   145 
   146 (* check_const *)
   147 
   148 fun check_const context consts (xname, ps) =
   149   let
   150     val Consts {decls, ...} = consts;
   151     val ((c, reports), _) = Name_Space.check_reports context decls (xname, ps);
   152     val T = type_scheme consts c handle TYPE (msg, _, _) => error (msg ^ Position.here_list ps);
   153   in (Const (c, T), reports) end;
   154 
   155 
   156 (* certify *)
   157 
   158 fun certify pp tsig do_expand consts =
   159   let
   160     fun err msg (c, T) =
   161       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^
   162         Syntax.string_of_typ (Syntax.init_pretty pp) T, [], []);
   163     val certT = Type.cert_typ tsig;
   164     fun cert tm =
   165       let
   166         val (head, args) = Term.strip_comb tm;
   167         val args' = map cert args;
   168         fun comb head' = Term.list_comb (head', args');
   169       in
   170         (case head of
   171           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   172         | Const (c, T) =>
   173             let
   174               val T' = certT T;
   175               val (_, ({T = U, ...}, abbr)) = the_entry consts c;
   176               fun expand u =
   177                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   178                   err "Illegal type for abbreviation" (c, T), args');
   179             in
   180               if not (Type.raw_instance (T', U)) then
   181                 err "Illegal type for constant" (c, T)
   182               else
   183                 (case abbr of
   184                   SOME {rhs, normal_rhs, force_expand} =>
   185                     if do_expand then expand normal_rhs
   186                     else if force_expand then expand rhs
   187                     else comb head
   188                 | _ => comb head)
   189             end
   190         | _ => comb head)
   191       end;
   192   in cert end;
   193 
   194 
   195 (* typargs -- view actual const type as instance of declaration *)
   196 
   197 local
   198 
   199 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   200   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   201   | args_of (TFree _) _ = I
   202 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   203   | args_of_list [] _ _ = I;
   204 
   205 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   206   | subscript T [] = T
   207   | subscript _ _ = raise Subscript;
   208 
   209 in
   210 
   211 fun typargs_of T = map #2 (rev (args_of T [] []));
   212 
   213 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   214 
   215 end;
   216 
   217 fun instance consts (c, Ts) =
   218   let
   219     val declT = type_scheme consts c;
   220     val vars = map Term.dest_TVar (typargs consts (c, declT));
   221     val inst = vars ~~ Ts handle ListPair.UnequalLengths =>
   222       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   223   in declT |> Term_Subst.instantiateT inst end;
   224 
   225 
   226 
   227 (** build consts **)
   228 
   229 (* name space *)
   230 
   231 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   232   (Name_Space.hide_table fully c decls, constraints, rev_abbrevs));
   233 
   234 
   235 (* declarations *)
   236 
   237 fun declare context (b, declT) =
   238   map_consts (fn (decls, constraints, rev_abbrevs) =>
   239     let
   240       val decl = {T = declT, typargs = typargs_of declT};
   241       val _ = Binding.check b;
   242       val (_, decls') = decls |> Name_Space.define context true (b, (decl, NONE));
   243     in (decls', constraints, rev_abbrevs) end);
   244 
   245 
   246 (* constraints *)
   247 
   248 fun constrain (c, C) consts =
   249   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   250     (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg;
   251       (decls,
   252         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   253         rev_abbrevs)));
   254 
   255 
   256 (* abbreviations *)
   257 
   258 local
   259 
   260 fun strip_abss (t as Abs (x, T, b)) =
   261       if Term.is_dependent b then strip_abss b |>> cons (x, T)  (* FIXME decr!? *)
   262       else ([], t)
   263   | strip_abss t = ([], t);
   264 
   265 fun rev_abbrev lhs rhs =
   266   let
   267     val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
   268     val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
   269   in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   270 
   271 in
   272 
   273 fun abbreviate context tsig mode (b, raw_rhs) consts =
   274   let
   275     val pp = Context.pretty_generic context;
   276     val cert_term = certify pp tsig false consts;
   277     val expand_term = certify pp tsig true consts;
   278     val force_expand = mode = Print_Mode.internal;
   279 
   280     val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
   281       error ("Illegal schematic variables on rhs of abbreviation " ^ Binding.print b);
   282 
   283     val rhs = raw_rhs
   284       |> Term.map_types (Type.cert_typ tsig)
   285       |> cert_term
   286       |> Term.close_schematic_term;
   287     val normal_rhs = expand_term rhs;
   288     val T = Term.fastype_of rhs;
   289     val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T);
   290   in
   291     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   292       let
   293         val decl = {T = T, typargs = typargs_of T};
   294         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   295         val _ = Binding.check b;
   296         val (_, decls') = decls
   297           |> Name_Space.define context true (b, (decl, SOME abbr));
   298         val rev_abbrevs' = rev_abbrevs
   299           |> update_abbrevs mode (rev_abbrev lhs rhs);
   300       in (decls', constraints, rev_abbrevs') end)
   301     |> pair (lhs, rhs)
   302   end;
   303 
   304 fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   305   let
   306     val (T, rhs) = the_abbreviation consts c;
   307     val rev_abbrevs' = rev_abbrevs
   308       |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
   309   in (decls, constraints, rev_abbrevs') end);
   310 
   311 end;
   312 
   313 
   314 (* empty and merge *)
   315 
   316 val empty =
   317   make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty);
   318 
   319 fun merge
   320    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
   321     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
   322   let
   323     val decls' = Name_Space.merge_tables (decls1, decls2);
   324     val constraints' = Symtab.merge (K true) (constraints1, constraints2);
   325     val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
   326   in make_consts (decls', constraints', rev_abbrevs') end;
   327 
   328 end;