src/Pure/consts.ML
author wenzelm
Thu Aug 30 15:04:42 2007 +0200 (2007-08-30 ago)
changeset 24484 013b98b57b86
parent 23655 d2d1138e0ddc
child 24673 62b75508eb66
permissions -rw-r--r--
maintain mode in context (get/set/restore_mode);
     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   handle UnequalLengths => raise TYPE ("const_instance", Ts, [Const(c,dummyT)]);
   194 
   195 
   196 
   197 (** build consts **)
   198 
   199 fun err_dup_const c =
   200   error ("Duplicate declaration of constant " ^ quote c);
   201 
   202 fun err_inconsistent_constraints c =
   203   error ("Inconsistent type constraints for constant " ^ quote c);
   204 
   205 fun extend_decls naming decl tab = NameSpace.extend_table naming [decl] tab
   206   handle Symtab.DUP c => err_dup_const c;
   207 
   208 
   209 (* name space *)
   210 
   211 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs, do_expand) =>
   212   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs, do_expand));
   213 
   214 
   215 (* declarations *)
   216 
   217 fun declare naming ((c, declT), authentic) =
   218     map_consts (fn (decls, constraints, rev_abbrevs, do_expand) =>
   219   let
   220     fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   221       | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   222       | args_of (TFree _) _ = I
   223     and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   224       | args_of_list [] _ _ = I;
   225     val decl =
   226       (((declT, LogicalConst (map #2 (rev (args_of declT [] [])))), authentic), serial ());
   227   in (extend_decls naming (c, decl) decls, constraints, rev_abbrevs, do_expand) end);
   228 
   229 
   230 (* constraints *)
   231 
   232 fun constrain (c, C) consts =
   233   consts |> map_consts (fn (decls, constraints, rev_abbrevs, do_expand) =>
   234     (the_const consts c handle TYPE (msg, _, _) => error msg;
   235       (decls,
   236         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   237         rev_abbrevs, do_expand)));
   238 
   239 
   240 (* abbreviations *)
   241 
   242 fun set_expand b = map_consts (fn (decls, constraints, rev_abbrevs, _) =>
   243   (decls, constraints, rev_abbrevs, b));
   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 (c, raw_rhs) consts =
   265   let
   266     val cert_term = certify pp tsig (consts |> set_expand false);
   267     val expand_term = certify pp tsig (consts |> set_expand true);
   268     val force_expand = mode = Syntax.internalM;
   269 
   270     val rhs = raw_rhs
   271       |> Term.map_types (Type.cert_typ tsig)
   272       |> cert_term;
   273     val T = Term.fastype_of rhs;
   274     val lhs = Const (NameSpace.full naming c, T);
   275     val rhs' = expand_term rhs;
   276 
   277     fun err msg = error (msg ^ " on rhs of abbreviation:\n" ^
   278       Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs)));
   279     val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables"
   280     val _ = null (Term.hidden_polymorphism rhs T) orelse err "Extra type variables";
   281   in
   282     consts |> map_consts (fn (decls, constraints, rev_abbrevs, do_expand) =>
   283       let
   284         val decls' = decls |> extend_decls naming
   285           (c, (((T, Abbreviation (rhs, rhs', force_expand)), true), serial ()));
   286         val rev_abbrevs' = rev_abbrevs
   287           |> fold (curry Symtab.update_list mode) (rev_abbrev lhs rhs);
   288       in (decls', constraints, rev_abbrevs', do_expand) end)
   289     |> pair (lhs, rhs)
   290   end;
   291 
   292 end;
   293 
   294 
   295 (* empty and merge *)
   296 
   297 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty, true);
   298 
   299 fun merge
   300    (Consts ({decls = decls1, constraints = constraints1,
   301       rev_abbrevs = rev_abbrevs1, do_expand = do_expand1}, _),
   302     Consts ({decls = decls2, constraints = constraints2,
   303       rev_abbrevs = rev_abbrevs2, do_expand = do_expand2}, _)) =
   304   let
   305     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   306       handle Symtab.DUP c => err_dup_const c;
   307     val constraints' = Symtab.merge (op =) (constraints1, constraints2)
   308       handle Symtab.DUP c => err_inconsistent_constraints c;
   309     val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
   310       (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
   311     val do_expand' = do_expand1 orelse do_expand2;
   312   in make_consts (decls', constraints', rev_abbrevs', do_expand') end;
   313 
   314 end;