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