src/Pure/consts.ML
author haftmann
Fri Oct 12 14:42:29 2007 +0200 (2007-10-12 ago)
changeset 25001 7982fe02a50e
parent 24909 afae05eb1f1c
child 25037 d6a3dec2375d
permissions -rw-r--r--
(intermediate quickfix)
     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 the_tags: T -> string -> Markup.property list                 (*exception TYPE*)
    22   val space_of: T -> NameSpace.T
    23   val intern: T -> xstring -> string
    24   val extern: T -> string -> xstring
    25   val extern_early: T -> string -> xstring
    26   val syntax: T -> string * mixfix -> string * typ * mixfix
    27   val syntax_name: T -> string -> string
    28   val read_const: T -> string -> term
    29   val certify: Pretty.pp -> Type.tsig -> bool -> T -> term -> term  (*exception TYPE*)
    30   val typargs: T -> string * typ -> typ list
    31   val instance: T -> string * typ list -> typ
    32   val declare: bool -> NameSpace.naming -> Markup.property list -> (bstring * typ) -> T -> T
    33   val constrain: string * typ option -> T -> T
    34   val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Markup.property list ->
    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 type decl = {T: typ, typargs: int list list, tags: Markup.property list, authentic: bool};
    50 
    51 type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
    52 fun dest_abbrev ({rhs, normal_rhs, ...}: abbrev) = (rhs, normal_rhs);
    53 
    54 datatype T = Consts of
    55  {decls: ((decl * abbrev option) * serial) NameSpace.table,
    56   constraints: typ Symtab.table,
    57   rev_abbrevs: (term * term) list Symtab.table} * stamp;
    58 
    59 fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
    60 
    61 fun make_consts (decls, constraints, rev_abbrevs) =
    62   Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
    63 
    64 fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
    65   make_consts (f (decls, constraints, rev_abbrevs));
    66 
    67 fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
    68   maps (Symtab.lookup_list rev_abbrevs) modes;
    69 
    70 
    71 (* dest consts *)
    72 
    73 fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
    74  {constants = (space,
    75     Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
    76       Symtab.update (c, (T, Option.map dest_abbrev abbr))) decls Symtab.empty),
    77   constraints = (space, constraints)};
    78 
    79 
    80 (* lookup consts *)
    81 
    82 fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
    83   (case Symtab.lookup tab c of
    84     SOME (decl, _) => decl
    85   | NONE => raise TYPE ("Undeclared constant: " ^ quote c, [], []));
    86 
    87 fun the_declaration consts c =
    88   (case the_const consts c of
    89     ({T, ...}, NONE) => T
    90   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
    91 
    92 fun the_abbreviation consts c =
    93   (case the_const consts c of
    94     ({T, ...}, SOME abbr) => (T, dest_abbrev abbr)
    95   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
    96 
    97 val type_arguments = (#typargs o #1) oo the_const;
    98 val is_monomorphic = null oo type_arguments;
    99 
   100 fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
   101   (case Symtab.lookup constraints c of
   102     SOME T => T
   103   | NONE => #T (#1 (the_const consts c)));
   104 
   105 val the_tags = (#tags o #1) oo the_const;
   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 ({authentic = true, ...}, _) => Syntax.constN ^ c
   118   | _ => extern consts c);
   119 
   120 fun syntax consts (c, mx) =
   121   let
   122     val ({T, authentic, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
   123     val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
   124   in (c', T, mx) end;
   125 
   126 fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
   127 
   128 
   129 (* read_const *)
   130 
   131 fun read_const consts raw_c =
   132   let
   133     val c = intern consts raw_c;
   134     val ({T, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
   135   in Const (c, T) end;
   136 
   137 
   138 (* certify *)
   139 
   140 fun certify pp tsig do_expand consts =
   141   let
   142     fun err msg (c, T) =
   143       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
   144     val certT = Type.cert_typ tsig;
   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, certT T, cert t))
   153         | Const (c, T) =>
   154             let
   155               val T' = certT T;
   156               val ({T = U, ...}, abbr) = 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 abbr of
   162                   SOME {normal_rhs = u, force_expand, ...} =>
   163                     if do_expand orelse force_expand then
   164                       Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   165                         err "Illegal type for abbreviation" (c, T), args')
   166                     else comb head
   167                 | _ => comb head)
   168             end
   169         | _ => comb head)
   170       end;
   171   in cert end;
   172 
   173 
   174 (* typargs -- view actual const type as instance of declaration *)
   175 
   176 local
   177 
   178 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   179   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   180   | args_of (TFree _) _ = I
   181 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   182   | args_of_list [] _ _ = I;
   183 
   184 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   185   | subscript T [] = T
   186   | subscript T _ = raise Subscript;
   187 
   188 in
   189 
   190 fun typargs_of T = map #2 (rev (args_of T [] []));
   191 
   192 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   193 
   194 end;
   195 
   196 fun instance consts (c, Ts) =
   197   let
   198     val ({ T = declT, ... }, _) = the_const consts c;
   199     val vars = map Term.dest_TVar (typargs consts (c, declT));
   200     val inst = vars ~~ Ts handle UnequalLengths =>
   201       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   202   in declT |> TermSubst.instantiateT inst end;
   203 
   204 
   205 
   206 (** build consts **)
   207 
   208 fun err_dup_const c =
   209   error ("Duplicate declaration of constant " ^ quote c);
   210 
   211 fun err_inconsistent_constraints (constr1 : typ Symtab.table, constr2 : typ Symtab.table) c =
   212   error ("Inconsistent type constraints for constant " ^ quote c ^ "\n"
   213     ^ (setmp show_sorts true makestring o the o Symtab.lookup constr1) c ^ "\n"
   214     ^ (setmp show_sorts true makestring o the o Symtab.lookup constr2) c); (*FIXME*)
   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 = Syntax.internalM;
   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 T) 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.update_list mode) (rev_abbrev lhs rhs);
   292       in (decls', constraints, rev_abbrevs') end)
   293     |> pair (lhs, rhs)
   294   end;
   295 
   296 end;
   297 
   298 
   299 (* empty and merge *)
   300 
   301 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
   302 
   303 fun merge
   304    (Consts ({decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, _),
   305     Consts ({decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}, _)) =
   306   let
   307     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   308       handle Symtab.DUP c => err_dup_const c;
   309     val constraints' = (*Symtab.merge (op =)*)Symtab.join (K snd)(*FIXME*) (constraints1, constraints2)
   310       handle Symtab.DUP c => err_inconsistent_constraints (constraints1, constraints2) c;
   311     val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
   312       (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
   313   in make_consts (decls', constraints', rev_abbrevs') end;
   314 
   315 end;