src/Pure/consts.ML
author wenzelm
Thu Dec 07 17:58:44 2006 +0100 (2006-12-07 ago)
changeset 21694 9f65fecb6e10
parent 21680 5d2230ad1261
child 21720 059e6b8cee8e
permissions -rw-r--r--
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 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 -> ((string * typ) * 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 |
    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         val args' = map cert args;
   149         fun comb head' = Term.list_comb (head', args');
   150       in
   151         (case head of
   152           Abs (x, T, t) => comb (Abs (x, T, cert t))
   153         | Const (c, T) =>
   154             let
   155               val T' = Type.cert_typ tsig T;
   156               val (U, kind) = #1 (#1 (the_const consts c));
   157             in
   158               if not (Type.raw_instance (T', U)) then
   159                 err "Illegal type for constant" (c, T)
   160               else
   161                 (case (kind, expand_abbrevs) of
   162                   (Abbreviation u, true) =>
   163                     Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   164                       err "Illegal type for abbreviation" (c, T), args')
   165                 | _ => comb head)
   166             end
   167         | _ => comb head)
   168       end;
   169   in cert end;
   170 
   171 
   172 (* typargs -- view actual const type as instance of declaration *)
   173 
   174 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   175   | subscript T [] = T
   176   | subscript T _ = raise Subscript;
   177 
   178 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   179 
   180 fun instance consts (c, Ts) =
   181   let
   182     val declT = declaration consts c;
   183     val vars = map Term.dest_TVar (typargs consts (c, declT));
   184   in declT |> TermSubst.instantiateT (vars ~~ Ts) end;
   185 
   186 
   187 
   188 (** build consts **)
   189 
   190 fun err_dup_consts cs =
   191   error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
   192 
   193 fun err_inconsistent_constraints cs =
   194   error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
   195 
   196 fun extend_decls naming decl tab = NameSpace.extend_table naming (tab, [decl])
   197   handle Symtab.DUPS cs => err_dup_consts cs;
   198 
   199 
   200 (* name space *)
   201 
   202 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
   203   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, expand_abbrevs));
   204 
   205 
   206 (* declarations *)
   207 
   208 fun declare naming ((c, declT), authentic) =
   209     map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
   210   let
   211     fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   212       | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   213       | args_of (TFree _) _ = I
   214     and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   215       | args_of_list [] _ _ = I;
   216     val decl =
   217       (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), authentic), serial ());
   218   in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, expand_abbrevs) end);
   219 
   220 
   221 (* constraints *)
   222 
   223 fun constrain (c, C) consts =
   224   consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
   225     (the_const consts c handle TYPE (msg, _, _) => error msg;
   226       (decls,
   227         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   228         rev_abbrevs, expand_abbrevs)));
   229 
   230 
   231 (* abbreviations *)
   232 
   233 fun expand_abbrevs b = map_consts (fn (decls, constraints, rev_abbrevs, _) =>
   234   (decls, constraints, rev_abbrevs, b));
   235 
   236 local
   237 
   238 fun strip_abss tm = ([], tm) ::
   239   (case tm of
   240     Abs (a, T, t) =>
   241       if Term.loose_bvar1 (t, 0) then
   242         strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
   243       else []
   244   | _ => []);
   245 
   246 fun rev_abbrev const rhs =
   247   let
   248     fun abbrev (xs, body) =
   249       let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
   250       in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end;
   251   in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
   252 
   253 in
   254 
   255 fun abbreviate pp tsig naming mode ((c, raw_rhs), authentic) consts =
   256   let
   257     val full_c = NameSpace.full naming c;
   258     val rhs = raw_rhs
   259       |> Term.map_types (Type.cert_typ tsig)
   260       |> certify pp tsig (consts |> expand_abbrevs false);
   261     val rhs' = rhs
   262       |> certify pp tsig (consts |> expand_abbrevs true);
   263     val T = Term.fastype_of rhs;
   264 
   265     fun err msg = error (msg ^ " on rhs of abbreviation:\n" ^
   266       Pretty.string_of_term pp (Logic.mk_equals (Const (full_c, T), rhs)));
   267     val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables"
   268     val _ = null (Term.hidden_polymorphism rhs T) orelse err "Extra type variables";
   269   in
   270     consts |> map_consts (fn (decls, constraints, rev_abbrevs, expand_abbrevs) =>
   271       let
   272         val decls' = decls
   273           |> extend_decls naming (c, (((T, Abbreviation rhs'), authentic), serial ()));
   274         val rev_abbrevs' = rev_abbrevs
   275           |> fold (curry Symtab.update_list mode) (rev_abbrev (full_c, T) rhs);
   276       in (decls', constraints, rev_abbrevs', expand_abbrevs) end)
   277     |> pair ((full_c, T), rhs)
   278   end;
   279 
   280 end;
   281 
   282 
   283 (* empty and merge *)
   284 
   285 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty, true);
   286 
   287 fun merge
   288    (Consts ({decls = decls1, constraints = constraints1,
   289       rev_abbrevs = rev_abbrevs1, expand_abbrevs = expand_abbrevs1}, _),
   290     Consts ({decls = decls2, constraints = constraints2,
   291       rev_abbrevs = rev_abbrevs2, expand_abbrevs = expand_abbrevs2}, _)) =
   292   let
   293     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   294       handle Symtab.DUPS cs => err_dup_consts cs;
   295     val constraints' = Symtab.merge (op =) (constraints1, constraints2)
   296       handle Symtab.DUPS cs => err_inconsistent_constraints cs;
   297     val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
   298       (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
   299     val expand_abbrevs' = expand_abbrevs1 orelse expand_abbrevs2;
   300   in make_consts (decls', constraints', rev_abbrevs', expand_abbrevs') end;
   301 
   302 end;