src/Pure/consts.ML
author wenzelm
Fri May 28 20:41:23 2010 +0200 (2010-05-28)
changeset 37174 6feaab4fc27d
parent 37146 f652333bbf8e
child 40722 441260986b63
permissions -rw-r--r--
assume given SCALA_HOME, e.g. from component settings or external setup;
     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 retrieve_abbrevs: T -> string list -> term -> (term * term) list
    13   val dest: T ->
    14    {constants: (typ * term option) Name_Space.table,
    15     constraints: typ Name_Space.table}
    16   val the_type: T -> string -> typ                             (*exception TYPE*)
    17   val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
    18   val type_scheme: T -> string -> typ                          (*exception TYPE*)
    19   val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
    20   val the_constraint: T -> string -> typ                       (*exception TYPE*)
    21   val space_of: T -> Name_Space.T
    22   val alias: Name_Space.naming -> binding -> string -> T -> T
    23   val is_concealed: T -> string -> bool
    24   val intern: T -> xstring -> string
    25   val extern: T -> string -> xstring
    26   val intern_syntax: T -> xstring -> string
    27   val extern_syntax: T -> string -> xstring
    28   val read_const: T -> string -> term
    29   val certify: Pretty.pp -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
    30   val typargs: T -> string * typ -> typ list
    31   val instance: T -> string * typ list -> typ
    32   val declare: Name_Space.naming -> binding * typ -> T -> T
    33   val constrain: string * typ option -> T -> T
    34   val abbreviate: Pretty.pp -> Type.tsig -> Name_Space.naming -> string ->
    35     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 
    71 (* reverted abbrevs *)
    72 
    73 val empty_abbrevs =
    74   Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (single o #1);
    75 
    76 fun update_abbrevs mode abbrs =
    77   Symtab.map_default (mode, empty_abbrevs) (Item_Net.update abbrs);
    78 
    79 fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
    80   let val nets = map_filter (Symtab.lookup rev_abbrevs) modes
    81   in fn t => maps (fn net => Item_Net.retrieve net t) nets end;
    82 
    83 
    84 (* dest consts *)
    85 
    86 fun dest (Consts {decls = (space, decls), constraints, ...}) =
    87  {constants = (space,
    88     Symtab.fold (fn (c, ({T, ...}, abbr)) =>
    89       Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
    90   constraints = (space, constraints)};
    91 
    92 
    93 (* lookup consts *)
    94 
    95 fun the_const (Consts {decls = (_, tab), ...}) c =
    96   (case Symtab.lookup tab c of
    97     SOME decl => decl
    98   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
    99 
   100 fun the_type consts c =
   101   (case the_const consts c of
   102     ({T, ...}, NONE) => T
   103   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
   104 
   105 fun the_abbreviation consts c =
   106   (case the_const consts c of
   107     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
   108   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
   109 
   110 val the_decl = #1 oo the_const;
   111 val type_scheme = #T oo the_decl;
   112 val type_arguments = #typargs oo the_decl;
   113 
   114 val is_monomorphic = null oo type_arguments;
   115 
   116 fun the_constraint (consts as Consts {constraints, ...}) c =
   117   (case Symtab.lookup constraints c of
   118     SOME T => T
   119   | NONE => type_scheme consts c);
   120 
   121 
   122 (* name space and syntax *)
   123 
   124 fun space_of (Consts {decls = (space, _), ...}) = space;
   125 
   126 fun alias naming binding name = map_consts (fn ((space, decls), constraints, rev_abbrevs) =>
   127   ((Name_Space.alias naming binding name space, decls), constraints, rev_abbrevs));
   128 
   129 val is_concealed = Name_Space.is_concealed o space_of;
   130 
   131 val intern = Name_Space.intern o space_of;
   132 val extern = Name_Space.extern o space_of;
   133 
   134 fun intern_syntax consts s =
   135   (case try Syntax.unmark_const s of
   136     SOME c => c
   137   | NONE => intern consts s);
   138 
   139 fun extern_syntax consts s =
   140   (case try Syntax.unmark_const s of
   141     SOME c => extern consts c
   142   | NONE => s);
   143 
   144 
   145 (* read_const *)
   146 
   147 fun read_const consts raw_c =
   148   let
   149     val c = intern consts raw_c;
   150     val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
   151   in Const (c, T) end;
   152 
   153 
   154 (* certify *)
   155 
   156 fun certify pp tsig do_expand consts =
   157   let
   158     fun err msg (c, T) =
   159       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
   160     val certT = Type.cert_typ tsig;
   161     fun cert tm =
   162       let
   163         val (head, args) = Term.strip_comb tm;
   164         val args' = map cert args;
   165         fun comb head' = Term.list_comb (head', args');
   166       in
   167         (case head of
   168           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   169         | Const (c, T) =>
   170             let
   171               val T' = certT T;
   172               val ({T = U, ...}, abbr) = the_const consts c;
   173               fun expand u =
   174                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   175                   err "Illegal type for abbreviation" (c, T), args');
   176             in
   177               if not (Type.raw_instance (T', U)) then
   178                 err "Illegal type for constant" (c, T)
   179               else
   180                 (case abbr of
   181                   SOME {rhs, normal_rhs, force_expand} =>
   182                     if do_expand then expand normal_rhs
   183                     else if force_expand then expand rhs
   184                     else comb head
   185                 | _ => comb head)
   186             end
   187         | _ => comb head)
   188       end;
   189   in cert end;
   190 
   191 
   192 (* typargs -- view actual const type as instance of declaration *)
   193 
   194 local
   195 
   196 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   197   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   198   | args_of (TFree _) _ = I
   199 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   200   | args_of_list [] _ _ = I;
   201 
   202 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   203   | subscript T [] = T
   204   | subscript _ _ = raise Subscript;
   205 
   206 in
   207 
   208 fun typargs_of T = map #2 (rev (args_of T [] []));
   209 
   210 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   211 
   212 end;
   213 
   214 fun instance consts (c, Ts) =
   215   let
   216     val declT = type_scheme consts c;
   217     val vars = map Term.dest_TVar (typargs consts (c, declT));
   218     val inst = vars ~~ Ts handle UnequalLengths =>
   219       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   220   in declT |> Term_Subst.instantiateT inst end;
   221 
   222 
   223 
   224 (** build consts **)
   225 
   226 (* name space *)
   227 
   228 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   229   (apfst (Name_Space.hide fully c) decls, constraints, rev_abbrevs));
   230 
   231 
   232 (* declarations *)
   233 
   234 fun declare naming (b, declT) =
   235   map_consts (fn (decls, constraints, rev_abbrevs) =>
   236     let
   237       val decl = {T = declT, typargs = typargs_of declT};
   238       val (_, decls') = decls |> Name_Space.define true naming (b, (decl, NONE));
   239     in (decls', constraints, rev_abbrevs) end);
   240 
   241 
   242 (* constraints *)
   243 
   244 fun constrain (c, C) consts =
   245   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   246     (the_const consts c handle TYPE (msg, _, _) => error msg;
   247       (decls,
   248         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   249         rev_abbrevs)));
   250 
   251 
   252 (* abbreviations *)
   253 
   254 local
   255 
   256 fun strip_abss (t as Abs (x, T, b)) =
   257       if Term.loose_bvar1 (b, 0) then strip_abss b |>> cons (x, T)
   258       else ([], t)
   259   | strip_abss t = ([], t);
   260 
   261 fun rev_abbrev lhs rhs =
   262   let
   263     val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
   264     val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
   265   in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   266 
   267 in
   268 
   269 fun abbreviate pp tsig naming mode (b, raw_rhs) consts =
   270   let
   271     val cert_term = certify pp tsig false consts;
   272     val expand_term = certify pp tsig true consts;
   273     val force_expand = mode = Print_Mode.internal;
   274 
   275     val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
   276       error ("Illegal schematic variables on rhs of abbreviation " ^ quote (Binding.str_of b));
   277 
   278     val rhs = raw_rhs
   279       |> Term.map_types (Type.cert_typ tsig)
   280       |> cert_term
   281       |> Term.close_schematic_term;
   282     val normal_rhs = expand_term rhs;
   283     val T = Term.fastype_of rhs;
   284     val lhs = Const (Name_Space.full_name naming b, T);
   285   in
   286     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   287       let
   288         val decl = {T = T, typargs = typargs_of T};
   289         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   290         val (_, decls') = decls
   291           |> Name_Space.define true naming (b, (decl, SOME abbr));
   292         val rev_abbrevs' = rev_abbrevs
   293           |> update_abbrevs mode (rev_abbrev lhs rhs);
   294       in (decls', constraints, rev_abbrevs') end)
   295     |> pair (lhs, rhs)
   296   end;
   297 
   298 fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   299   let
   300     val (T, rhs) = the_abbreviation consts c;
   301     val rev_abbrevs' = rev_abbrevs
   302       |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
   303   in (decls, constraints, rev_abbrevs') end);
   304 
   305 end;
   306 
   307 
   308 (* empty and merge *)
   309 
   310 val empty = make_consts (Name_Space.empty_table "constant", Symtab.empty, Symtab.empty);
   311 
   312 fun merge
   313    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
   314     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
   315   let
   316     val decls' = Name_Space.merge_tables (decls1, decls2);
   317     val constraints' = Symtab.join (K fst) (constraints1, constraints2);
   318     val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
   319   in make_consts (decls', constraints', rev_abbrevs') end;
   320 
   321 end;