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