src/Pure/consts.ML
author wenzelm
Sat Sep 22 17:45:55 2007 +0200 (2007-09-22 ago)
changeset 24673 62b75508eb66
parent 23655 d2d1138e0ddc
child 24772 2b838dfeca1e
permissions -rw-r--r--
certify: do_expand as explicit argument, actually certify type of abstractions;
tuned signature;
tuned;
     1 (*  Title:      Pure/consts.ML
     2     ID:         $Id$
     3     Author:     Makarius
     4 
     5 Polymorphic constants: declarations, abbreviations, additional type
     6 constraints.
     7 *)
     8 
     9 signature CONSTS =
    10 sig
    11   type T
    12   val eq_consts: T * T -> bool
    13   val abbrevs_of: T -> string list -> (term * term) list
    14   val dest: T ->
    15    {constants: (typ * (term * term) option) NameSpace.table,
    16     constraints: typ NameSpace.table}
    17   val the_abbreviation: T -> string -> typ * (term * term)          (*exception TYPE*)
    18   val the_declaration: 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 -> NameSpace.T
    22   val intern: T -> xstring -> string
    23   val extern: T -> string -> xstring
    24   val extern_early: T -> string -> xstring
    25   val syntax: T -> string * mixfix -> string * typ * mixfix
    26   val syntax_name: T -> string -> string
    27   val read_const: T -> string -> term
    28   val certify: Pretty.pp -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
    29   val typargs: T -> string * typ -> typ list
    30   val instance: T -> string * typ list -> typ
    31   val declare: NameSpace.naming -> (bstring * typ) * bool -> T -> T
    32   val constrain: string * typ option -> T -> T
    33   val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string ->
    34     bstring * term -> T -> (term * term) * T
    35   val hide: bool -> string -> T -> T
    36   val empty: T
    37   val merge: T * T -> T
    38 end;
    39 
    40 structure Consts: CONSTS =
    41 struct
    42 
    43 
    44 (** consts type **)
    45 
    46 (* datatype T *)
    47 
    48 datatype kind =
    49   LogicalConst of int list list |      (*typargs positions*)
    50   Abbreviation of term * term * bool   (*rhs, normal rhs, force_expand*);
    51 
    52 type decl =
    53   (typ * kind) *
    54   bool; (*authentic syntax*)
    55 
    56 datatype T = Consts of
    57  {decls: (decl * serial) NameSpace.table,
    58   constraints: typ Symtab.table,
    59   rev_abbrevs: (term * term) list Symtab.table} * stamp;
    60 
    61 fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
    62 
    63 fun make_consts (decls, constraints, rev_abbrevs) =
    64   Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
    65 
    66 fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
    67   make_consts (f (decls, constraints, rev_abbrevs));
    68 
    69 fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
    70   maps (Symtab.lookup_list rev_abbrevs) modes;
    71 
    72 
    73 (* dest consts *)
    74 
    75 fun dest_kind (LogicalConst _) = NONE
    76   | dest_kind (Abbreviation (t, t', _)) = SOME (t, t');
    77 
    78 fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
    79  {constants = (space,
    80     Symtab.fold (fn (c, (((T, kind), _), _)) =>
    81       Symtab.update (c, (T, dest_kind kind))) decls Symtab.empty),
    82   constraints = (space, constraints)};
    83 
    84 
    85 (* lookup consts *)
    86 
    87 fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
    88   (case Symtab.lookup tab c of
    89     SOME decl => decl
    90   | NONE => raise TYPE ("Undeclared constant: " ^ quote c, [], []));
    91 
    92 fun logical_const consts c =
    93   (case #1 (#1 (the_const consts c)) of
    94     (T, LogicalConst ps) => (T, ps)
    95   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
    96 
    97 fun the_abbreviation consts c =
    98   (case #1 (#1 (the_const consts c)) of
    99     (T, Abbreviation (t, t', _)) => (T, (t, t'))
   100   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
   101 
   102 val the_declaration = #1 oo logical_const;
   103 val type_arguments = #2 oo logical_const;
   104 val is_monomorphic = null oo type_arguments;
   105 
   106 fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
   107   (case Symtab.lookup constraints c of
   108     SOME T => T
   109   | NONE => #1 (#1 (#1 (the_const consts c))));
   110 
   111 
   112 (* name space and syntax *)
   113 
   114 fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
   115 
   116 val intern = NameSpace.intern o space_of;
   117 val extern = NameSpace.extern o space_of;
   118 
   119 fun extern_early consts c =
   120   (case try (the_const consts) c of
   121     SOME ((_, true), _) => Syntax.constN ^ c
   122   | _ => extern consts c);
   123 
   124 fun syntax consts (c, mx) =
   125   let
   126     val ((T, _), authentic) = #1 (the_const consts c) handle TYPE (msg, _, _) => error msg;
   127     val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
   128   in (c', T, mx) end;
   129 
   130 fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
   131 
   132 
   133 (* read_const *)
   134 
   135 fun read_const consts raw_c =
   136   let
   137     val c = intern consts raw_c;
   138     val (((T, _), _), _) = the_const consts c handle TYPE (msg, _, _) => error msg;
   139   in Const (c, T) end;
   140 
   141 
   142 (* certify *)
   143 
   144 fun certify pp tsig do_expand consts =
   145   let
   146     fun err msg (c, T) =
   147       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
   148     val certT = Type.cert_typ tsig;
   149     fun cert tm =
   150       let
   151         val (head, args) = Term.strip_comb tm;
   152         val args' = map cert args;
   153         fun comb head' = Term.list_comb (head', args');
   154       in
   155         (case head of
   156           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   157         | Const (c, T) =>
   158             let
   159               val T' = certT T;
   160               val (U, kind) = #1 (#1 (the_const consts c));
   161             in
   162               if not (Type.raw_instance (T', U)) then
   163                 err "Illegal type for constant" (c, T)
   164               else
   165                 (case kind of
   166                   Abbreviation (_, u, force_expand) =>
   167                     if do_expand orelse force_expand then
   168                       Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   169                         err "Illegal type for abbreviation" (c, T), args')
   170                     else comb head
   171                 | _ => comb head)
   172             end
   173         | _ => comb head)
   174       end;
   175   in cert end;
   176 
   177 
   178 (* typargs -- view actual const type as instance of declaration *)
   179 
   180 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   181   | subscript T [] = T
   182   | subscript T _ = raise Subscript;
   183 
   184 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   185 
   186 fun instance consts (c, Ts) =
   187   let
   188     val declT = the_declaration consts c;
   189     val vars = map Term.dest_TVar (typargs consts (c, declT));
   190     val inst = vars ~~ Ts handle UnequalLengths =>
   191       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   192   in declT |> TermSubst.instantiateT inst end;
   193 
   194 
   195 
   196 (** build consts **)
   197 
   198 fun err_dup_const c =
   199   error ("Duplicate declaration of constant " ^ quote c);
   200 
   201 fun err_inconsistent_constraints c =
   202   error ("Inconsistent type constraints for constant " ^ quote c);
   203 
   204 fun extend_decls naming decl tab = NameSpace.extend_table naming [decl] tab
   205   handle Symtab.DUP c => err_dup_const c;
   206 
   207 
   208 (* name space *)
   209 
   210 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   211   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
   212 
   213 
   214 (* declarations *)
   215 
   216 fun declare naming ((c, declT), authentic) = map_consts (fn (decls, constraints, rev_abbrevs) =>
   217   let
   218     fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   219       | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   220       | args_of (TFree _) _ = I
   221     and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   222       | args_of_list [] _ _ = I;
   223     val decl =
   224       (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), authentic), serial ());
   225   in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs) end);
   226 
   227 
   228 (* constraints *)
   229 
   230 fun constrain (c, C) consts =
   231   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   232     (the_const consts c handle TYPE (msg, _, _) => error msg;
   233       (decls,
   234         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   235         rev_abbrevs)));
   236 
   237 
   238 (* abbreviations *)
   239 
   240 local
   241 
   242 fun strip_abss tm = ([], tm) ::
   243   (case tm of
   244     Abs (a, T, t) =>
   245       if Term.loose_bvar1 (t, 0) then
   246         strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
   247       else []
   248   | _ => []);
   249 
   250 fun rev_abbrev lhs rhs =
   251   let
   252     fun abbrev (xs, body) =
   253       let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
   254       in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   255   in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
   256 
   257 in
   258 
   259 fun abbreviate pp tsig naming mode (c, raw_rhs) consts =
   260   let
   261     val cert_term = certify pp tsig false consts;
   262     val expand_term = certify pp tsig true consts;
   263     val force_expand = mode = Syntax.internalM;
   264 
   265     val rhs = raw_rhs
   266       |> Term.map_types (Type.cert_typ tsig)
   267       |> cert_term;
   268     val T = Term.fastype_of rhs;
   269     val lhs = Const (NameSpace.full naming c, T);
   270     val rhs' = expand_term rhs;
   271 
   272     fun err msg = error (msg ^ " on rhs of abbreviation:\n" ^
   273       Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs)));
   274     val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables"
   275     val _ = null (Term.hidden_polymorphism rhs T) orelse err "Extra type variables";
   276   in
   277     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   278       let
   279         val decls' = decls |> extend_decls naming
   280           (c, (((T, Abbreviation (rhs, rhs', force_expand)), true), serial ()));
   281         val rev_abbrevs' = rev_abbrevs
   282           |> fold (curry Symtab.update_list mode) (rev_abbrev lhs rhs);
   283       in (decls', constraints, rev_abbrevs') end)
   284     |> pair (lhs, rhs)
   285   end;
   286 
   287 end;
   288 
   289 
   290 (* empty and merge *)
   291 
   292 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
   293 
   294 fun merge
   295    (Consts ({decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, _),
   296     Consts ({decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}, _)) =
   297   let
   298     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   299       handle Symtab.DUP c => err_dup_const c;
   300     val constraints' = Symtab.merge (op =) (constraints1, constraints2)
   301       handle Symtab.DUP c => err_inconsistent_constraints c;
   302     val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
   303       (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
   304   in make_consts (decls', constraints', rev_abbrevs') end;
   305 
   306 end;