src/Pure/consts.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 29606 fedb8be05f24
child 30280 eb98b49ef835
permissions -rw-r--r--
use long names for old-style fold combinators;
     1 (*  Title:      Pure/consts.ML
     2     Author:     Makarius
     3 
     4 Polymorphic constants: declarations, abbreviations, additional type
     5 constraints.
     6 *)
     7 
     8 signature CONSTS =
     9 sig
    10   type T
    11   val eq_consts: T * T -> bool
    12   val abbrevs_of: T -> string list -> (term * term) list
    13   val dest: T ->
    14    {constants: (typ * term option) NameSpace.table,
    15     constraints: typ NameSpace.table}
    16   val the_type: T -> string -> typ                             (*exception TYPE*)
    17   val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
    18   val type_scheme: T -> string -> typ                          (*exception TYPE*)
    19   val the_tags: T -> string -> Properties.T                    (*exception TYPE*)
    20   val is_monomorphic: T -> string -> bool                      (*exception TYPE*)
    21   val the_constraint: T -> string -> typ                       (*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 -> Properties.T -> (binding * typ) -> T -> T
    33   val constrain: string * typ option -> T -> T
    34   val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Properties.T ->
    35     binding * term -> T -> (term * term) * T
    36   val revert_abbrev: string -> string -> T -> T
    37   val hide: bool -> string -> T -> T
    38   val empty: T
    39   val merge: T * T -> T
    40 end;
    41 
    42 structure Consts: CONSTS =
    43 struct
    44 
    45 (** consts type **)
    46 
    47 (* datatype T *)
    48 
    49 type decl = {T: typ, typargs: int list list, tags: Properties.T, authentic: bool};
    50 type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
    51 
    52 datatype T = Consts of
    53  {decls: ((decl * abbrev option) * serial) NameSpace.table,
    54   constraints: typ Symtab.table,
    55   rev_abbrevs: (term * term) list Symtab.table} * stamp;
    56 
    57 fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
    58 
    59 fun make_consts (decls, constraints, rev_abbrevs) =
    60   Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
    61 
    62 fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
    63   make_consts (f (decls, constraints, rev_abbrevs));
    64 
    65 fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
    66   maps (Symtab.lookup_list rev_abbrevs) modes;
    67 
    68 
    69 (* dest consts *)
    70 
    71 fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
    72  {constants = (space,
    73     Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
    74       Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
    75   constraints = (space, constraints)};
    76 
    77 
    78 (* lookup consts *)
    79 
    80 fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
    81   (case Symtab.lookup tab c of
    82     SOME (decl, _) => decl
    83   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
    84 
    85 fun the_type consts c =
    86   (case the_const consts c of
    87     ({T, ...}, NONE) => T
    88   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
    89 
    90 fun the_abbreviation consts c =
    91   (case the_const consts c of
    92     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
    93   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
    94 
    95 val the_decl = #1 oo the_const;
    96 val type_scheme = #T oo the_decl;
    97 val type_arguments = #typargs oo the_decl;
    98 val the_tags = #tags oo the_decl;
    99 
   100 val is_monomorphic = null oo type_arguments;
   101 
   102 fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
   103   (case Symtab.lookup constraints c of
   104     SOME T => T
   105   | NONE => type_scheme consts c);
   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 = type_scheme 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               fun expand u =
   158                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   159                   err "Illegal type for abbreviation" (c, T), args');
   160             in
   161               if not (Type.raw_instance (T', U)) then
   162                 err "Illegal type for constant" (c, T)
   163               else
   164                 (case abbr of
   165                   SOME {rhs, normal_rhs, force_expand} =>
   166                     if do_expand then expand normal_rhs
   167                     else if force_expand then expand rhs
   168                     else comb head
   169                 | _ => comb head)
   170             end
   171         | _ => comb head)
   172       end;
   173   in cert end;
   174 
   175 
   176 (* typargs -- view actual const type as instance of declaration *)
   177 
   178 local
   179 
   180 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   181   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   182   | args_of (TFree _) _ = I
   183 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   184   | args_of_list [] _ _ = I;
   185 
   186 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   187   | subscript T [] = T
   188   | subscript T _ = raise Subscript;
   189 
   190 in
   191 
   192 fun typargs_of T = map #2 (rev (args_of T [] []));
   193 
   194 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   195 
   196 end;
   197 
   198 fun instance consts (c, Ts) =
   199   let
   200     val declT = type_scheme consts c;
   201     val vars = map Term.dest_TVar (typargs consts (c, declT));
   202     val inst = vars ~~ Ts handle UnequalLengths =>
   203       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   204   in declT |> TermSubst.instantiateT inst end;
   205 
   206 
   207 
   208 (** build consts **)
   209 
   210 fun err_dup_const c =
   211   error ("Duplicate declaration of constant " ^ quote c);
   212 
   213 fun extend_decls naming decl tab = NameSpace.bind naming decl tab
   214   handle Symtab.DUP c => err_dup_const c;
   215 
   216 
   217 (* name space *)
   218 
   219 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   220   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
   221 
   222 
   223 (* declarations *)
   224 
   225 fun declare authentic naming tags (b, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
   226   let
   227     val tags' = tags |> Position.default_properties (Position.thread_data ());
   228     val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
   229     val (_, decls') = decls |> extend_decls naming (b, ((decl, NONE), serial ()));
   230   in (decls', constraints, rev_abbrevs) end);
   231 
   232 
   233 (* constraints *)
   234 
   235 fun constrain (c, C) consts =
   236   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   237     (the_const consts c handle TYPE (msg, _, _) => error msg;
   238       (decls,
   239         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   240         rev_abbrevs)));
   241 
   242 
   243 (* abbreviations *)
   244 
   245 local
   246 
   247 fun strip_abss tm = ([], tm) ::
   248   (case tm of
   249     Abs (a, T, t) =>
   250       if Term.loose_bvar1 (t, 0) then
   251         strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
   252       else []
   253   | _ => []);
   254 
   255 fun rev_abbrev lhs rhs =
   256   let
   257     fun abbrev (xs, body) =
   258       let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
   259       in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   260   in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
   261 
   262 in
   263 
   264 fun abbreviate pp tsig naming mode tags (b, raw_rhs) consts =
   265   let
   266     val cert_term = certify pp tsig false consts;
   267     val expand_term = certify pp tsig true consts;
   268     val force_expand = mode = PrintMode.internal;
   269 
   270     val rhs = raw_rhs
   271       |> Term.map_types (Type.cert_typ tsig)
   272       |> cert_term;
   273     val normal_rhs = expand_term rhs;
   274     val T = Term.fastype_of rhs;
   275     val lhs = Const (NameSpace.full_name naming b, T);
   276 
   277     fun err msg = (warning (* FIXME should be error *) (msg ^ " on rhs of abbreviation:\n" ^
   278       Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs))); true);
   279     val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables";
   280     val _ = null (Term.hidden_polymorphism rhs) orelse err "Extra type variables";
   281   in
   282     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   283       let
   284         val tags' = tags |> Position.default_properties (Position.thread_data ());
   285         val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
   286         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   287         val (_, decls') = decls
   288           |> extend_decls naming (b, ((decl, SOME abbr), serial ()));
   289         val rev_abbrevs' = rev_abbrevs
   290           |> fold (curry Symtab.cons_list mode) (rev_abbrev lhs rhs);
   291       in (decls', constraints, rev_abbrevs') end)
   292     |> pair (lhs, rhs)
   293   end;
   294 
   295 fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   296   let
   297     val (T, rhs) = the_abbreviation consts c;
   298     val rev_abbrevs' = rev_abbrevs
   299       |> fold (curry Symtab.cons_list mode) (rev_abbrev (Const (c, T)) rhs);
   300   in (decls, constraints, rev_abbrevs') end);
   301 
   302 end;
   303 
   304 
   305 (* empty and merge *)
   306 
   307 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
   308 
   309 fun merge
   310    (Consts ({decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1}, _),
   311     Consts ({decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}, _)) =
   312   let
   313     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   314       handle Symtab.DUP c => err_dup_const c;
   315     val constraints' = Symtab.join (K fst) (constraints1, constraints2);
   316     val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
   317       (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
   318   in make_consts (decls', constraints', rev_abbrevs') end;
   319 
   320 end;