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