src/Pure/consts.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 63632 a59d9b81be24
permissions -rw-r--r--
more robust sorted_entries;
     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.generic -> 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 in
    87     fn t =>
    88       let
    89         val retrieve =
    90           if Term.could_beta_eta_contract t
    91           then Item_Net.retrieve
    92           else Item_Net.retrieve_matching
    93       in maps (fn net => retrieve net t) nets end
    94   end;
    95 
    96 
    97 (* dest consts *)
    98 
    99 fun dest (Consts {decls, constraints, ...}) =
   100  {const_space = Name_Space.space_of_table decls,
   101   constants =
   102     Name_Space.fold_table (fn (c, ({T, ...}, abbr)) =>
   103       cons (c, (T, Option.map #rhs abbr))) decls [],
   104   constraints = Symtab.dest constraints};
   105 
   106 
   107 (* lookup consts *)
   108 
   109 fun the_entry (Consts {decls, ...}) c =
   110   (case Name_Space.lookup_key decls c of
   111     SOME entry => entry
   112   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
   113 
   114 fun the_const consts c =
   115   (case the_entry consts c of
   116     (c', ({T, ...}, NONE)) => (c', T)
   117   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
   118 
   119 fun the_abbreviation consts c =
   120   (case the_entry consts c of
   121     (_, ({T, ...}, SOME {rhs, ...})) => (T, rhs)
   122   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
   123 
   124 fun the_decl consts = #1 o #2 o the_entry consts;
   125 val type_scheme = #T oo the_decl;
   126 val type_arguments = #typargs oo the_decl;
   127 
   128 val is_monomorphic = null oo type_arguments;
   129 
   130 fun the_constraint (consts as Consts {constraints, ...}) c =
   131   (case Symtab.lookup constraints c of
   132     SOME T => T
   133   | NONE => type_scheme consts c);
   134 
   135 
   136 (* name space and syntax *)
   137 
   138 fun space_of (Consts {decls, ...}) = Name_Space.space_of_table decls;
   139 
   140 fun alias naming binding name = map_consts (fn (decls, constraints, rev_abbrevs) =>
   141   ((Name_Space.alias_table naming binding name decls), constraints, rev_abbrevs));
   142 
   143 val is_concealed = Name_Space.is_concealed o space_of;
   144 
   145 val intern = Name_Space.intern o space_of;
   146 
   147 fun intern_syntax consts s =
   148   (case try Lexicon.unmark_const s of
   149     SOME c => c
   150   | NONE => intern consts s);
   151 
   152 
   153 (* check_const *)
   154 
   155 fun check_const context consts (xname, ps) =
   156   let
   157     val Consts {decls, ...} = consts;
   158     val ((c, reports), _) = Name_Space.check_reports context decls (xname, ps);
   159     val T = type_scheme consts c handle TYPE (msg, _, _) => error (msg ^ Position.here_list ps);
   160   in (Const (c, T), reports) end;
   161 
   162 
   163 (* certify *)
   164 
   165 fun certify context tsig do_expand consts =
   166   let
   167     fun err msg (c, T) =
   168       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^
   169         Syntax.string_of_typ (Syntax.init_pretty context) T, [], []);
   170     val certT = Type.cert_typ tsig;
   171     fun cert tm =
   172       let
   173         val (head, args) = Term.strip_comb tm;
   174         val args' = map cert args;
   175         fun comb head' = Term.list_comb (head', args');
   176       in
   177         (case head of
   178           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   179         | Const (c, T) =>
   180             let
   181               val T' = certT T;
   182               val (_, ({T = U, ...}, abbr)) = the_entry consts c;
   183               fun expand u =
   184                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   185                   err "Illegal type for abbreviation" (c, T), args');
   186             in
   187               if not (Type.raw_instance (T', U)) then
   188                 err "Illegal type for constant" (c, T)
   189               else
   190                 (case abbr of
   191                   SOME {rhs, normal_rhs, force_expand} =>
   192                     if do_expand then expand normal_rhs
   193                     else if force_expand then expand rhs
   194                     else comb head
   195                 | _ => comb head)
   196             end
   197         | _ => comb head)
   198       end;
   199   in cert end;
   200 
   201 
   202 (* typargs -- view actual const type as instance of declaration *)
   203 
   204 local
   205 
   206 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   207   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   208   | args_of (TFree _) _ = I
   209 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   210   | args_of_list [] _ _ = I;
   211 
   212 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   213   | subscript T [] = T
   214   | subscript _ _ = raise Subscript;
   215 
   216 in
   217 
   218 fun typargs_of T = map #2 (rev (args_of T [] []));
   219 
   220 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   221 
   222 end;
   223 
   224 fun instance consts (c, Ts) =
   225   let
   226     val declT = type_scheme consts c;
   227     val vars = map Term.dest_TVar (typargs consts (c, declT));
   228     val inst = vars ~~ Ts handle ListPair.UnequalLengths =>
   229       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   230   in declT |> Term_Subst.instantiateT inst end;
   231 
   232 
   233 
   234 (** build consts **)
   235 
   236 (* name space *)
   237 
   238 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   239   (Name_Space.hide_table fully c decls, constraints, rev_abbrevs));
   240 
   241 
   242 (* declarations *)
   243 
   244 fun declare context (b, declT) =
   245   map_consts (fn (decls, constraints, rev_abbrevs) =>
   246     let
   247       val decl = {T = declT, typargs = typargs_of declT};
   248       val _ = Binding.check b;
   249       val (_, decls') = decls |> Name_Space.define context true (b, (decl, NONE));
   250     in (decls', constraints, rev_abbrevs) end);
   251 
   252 
   253 (* constraints *)
   254 
   255 fun constrain (c, C) consts =
   256   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   257     (#2 (the_entry consts c) handle TYPE (msg, _, _) => error msg;
   258       (decls,
   259         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   260         rev_abbrevs)));
   261 
   262 
   263 (* abbreviations *)
   264 
   265 local
   266 
   267 fun strip_abss (t as Abs (x, T, b)) =
   268       if Term.is_dependent b then strip_abss b |>> cons (x, T)  (* FIXME decr!? *)
   269       else ([], t)
   270   | strip_abss t = ([], t);
   271 
   272 fun rev_abbrev lhs rhs =
   273   let
   274     val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
   275     val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
   276   in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   277 
   278 in
   279 
   280 fun abbreviate context tsig mode (b, raw_rhs) consts =
   281   let
   282     val cert_term = certify context tsig false consts;
   283     val expand_term = certify context tsig true consts;
   284     val force_expand = mode = Print_Mode.internal;
   285 
   286     val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
   287       error ("Illegal schematic variables on rhs of abbreviation " ^ Binding.print b);
   288 
   289     val rhs = raw_rhs
   290       |> Term.map_types (Type.cert_typ tsig)
   291       |> cert_term
   292       |> Term.close_schematic_term;
   293     val normal_rhs = expand_term rhs;
   294     val T = Term.fastype_of rhs;
   295     val lhs = Const (Name_Space.full_name (Name_Space.naming_of context) b, T);
   296   in
   297     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   298       let
   299         val decl = {T = T, typargs = typargs_of T};
   300         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   301         val _ = Binding.check b;
   302         val (_, decls') = decls
   303           |> Name_Space.define context true (b, (decl, SOME abbr));
   304         val rev_abbrevs' = rev_abbrevs
   305           |> update_abbrevs mode (rev_abbrev lhs rhs);
   306       in (decls', constraints, rev_abbrevs') end)
   307     |> pair (lhs, rhs)
   308   end;
   309 
   310 fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   311   let
   312     val (T, rhs) = the_abbreviation consts c;
   313     val rev_abbrevs' = rev_abbrevs
   314       |> update_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
   315   in (decls, constraints, rev_abbrevs') end);
   316 
   317 end;
   318 
   319 
   320 (* empty and merge *)
   321 
   322 val empty =
   323   make_consts (Name_Space.empty_table Markup.constantN, Symtab.empty, Symtab.empty);
   324 
   325 fun merge
   326    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
   327     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
   328   let
   329     val decls' = Name_Space.merge_tables (decls1, decls2);
   330     val constraints' = Symtab.merge (K true) (constraints1, constraints2);
   331     val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
   332   in make_consts (decls', constraints', rev_abbrevs') end;
   333 
   334 end;