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