src/Pure/consts.ML
author wenzelm
Wed Jan 21 22:26:49 2009 +0100 (2009-01-21)
changeset 29605 f2924219125e
parent 29581 b3b33e0298eb
child 29606 fedb8be05f24
permissions -rw-r--r--
eliminated obsolete var morphism;
     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 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 -> Properties.T                    (*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 -> Properties.T -> (binding * typ) -> T -> T
    34   val constrain: string * typ option -> T -> T
    35   val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Properties.T ->
    36     binding * 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: Properties.T, authentic: bool};
    51 type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
    52 
    53 datatype T = Consts of
    54  {decls: ((decl * abbrev option) * serial) NameSpace.table,
    55   constraints: typ Symtab.table,
    56   rev_abbrevs: (term * term) list Symtab.table} * stamp;
    57 
    58 fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
    59 
    60 fun make_consts (decls, constraints, rev_abbrevs) =
    61   Consts ({decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs}, stamp ());
    62 
    63 fun map_consts f (Consts ({decls, constraints, rev_abbrevs}, _)) =
    64   make_consts (f (decls, constraints, rev_abbrevs));
    65 
    66 fun abbrevs_of (Consts ({rev_abbrevs, ...}, _)) modes =
    67   maps (Symtab.lookup_list rev_abbrevs) modes;
    68 
    69 
    70 (* dest consts *)
    71 
    72 fun dest (Consts ({decls = (space, decls), constraints, ...}, _)) =
    73  {constants = (space,
    74     Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
    75       Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
    76   constraints = (space, constraints)};
    77 
    78 
    79 (* lookup consts *)
    80 
    81 fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
    82   (case Symtab.lookup tab c of
    83     SOME (decl, _) => decl
    84   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
    85 
    86 fun the_type consts c =
    87   (case the_const consts c of
    88     ({T, ...}, NONE) => T
    89   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
    90 
    91 fun the_abbreviation consts c =
    92   (case the_const consts c of
    93     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
    94   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
    95 
    96 val the_decl = #1 oo the_const;
    97 val type_scheme = #T oo the_decl;
    98 val type_arguments = #typargs oo the_decl;
    99 val the_tags = #tags oo the_decl;
   100 
   101 val is_monomorphic = null oo type_arguments;
   102 
   103 fun the_constraint (consts as Consts ({constraints, ...}, _)) c =
   104   (case Symtab.lookup constraints c of
   105     SOME T => T
   106   | NONE => type_scheme consts c);
   107 
   108 
   109 (* name space and syntax *)
   110 
   111 fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
   112 
   113 val intern = NameSpace.intern o space_of;
   114 val extern = NameSpace.extern o space_of;
   115 
   116 fun extern_early consts c =
   117   (case try (the_const consts) c of
   118     SOME ({authentic = true, ...}, _) => Syntax.constN ^ c
   119   | _ => extern consts c);
   120 
   121 fun syntax consts (c, mx) =
   122   let
   123     val ({T, authentic, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
   124     val c' = if authentic then Syntax.constN ^ c else NameSpace.base c;
   125   in (c', T, mx) end;
   126 
   127 fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
   128 
   129 
   130 (* read_const *)
   131 
   132 fun read_const consts raw_c =
   133   let
   134     val c = intern consts raw_c;
   135     val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
   136   in Const (c, T) end;
   137 
   138 
   139 (* certify *)
   140 
   141 fun certify pp tsig do_expand consts =
   142   let
   143     fun err msg (c, T) =
   144       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
   145     val certT = Type.cert_typ tsig;
   146     fun cert tm =
   147       let
   148         val (head, args) = Term.strip_comb tm;
   149         val args' = map cert args;
   150         fun comb head' = Term.list_comb (head', args');
   151       in
   152         (case head of
   153           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   154         | Const (c, T) =>
   155             let
   156               val T' = certT T;
   157               val ({T = U, ...}, abbr) = the_const consts c;
   158               fun expand u =
   159                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   160                   err "Illegal type for abbreviation" (c, T), args');
   161             in
   162               if not (Type.raw_instance (T', U)) then
   163                 err "Illegal type for constant" (c, T)
   164               else
   165                 (case abbr of
   166                   SOME {rhs, normal_rhs, force_expand} =>
   167                     if do_expand then expand normal_rhs
   168                     else if force_expand then expand rhs
   169                     else comb head
   170                 | _ => comb head)
   171             end
   172         | _ => comb head)
   173       end;
   174   in cert end;
   175 
   176 
   177 (* typargs -- view actual const type as instance of declaration *)
   178 
   179 local
   180 
   181 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   182   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   183   | args_of (TFree _) _ = I
   184 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   185   | args_of_list [] _ _ = I;
   186 
   187 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   188   | subscript T [] = T
   189   | subscript T _ = raise Subscript;
   190 
   191 in
   192 
   193 fun typargs_of T = map #2 (rev (args_of T [] []));
   194 
   195 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   196 
   197 end;
   198 
   199 fun instance consts (c, Ts) =
   200   let
   201     val declT = type_scheme consts c;
   202     val vars = map Term.dest_TVar (typargs consts (c, declT));
   203     val inst = vars ~~ Ts handle UnequalLengths =>
   204       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   205   in declT |> TermSubst.instantiateT inst end;
   206 
   207 
   208 
   209 (** build consts **)
   210 
   211 fun err_dup_const c =
   212   error ("Duplicate declaration of constant " ^ quote c);
   213 
   214 fun extend_decls naming decl tab = NameSpace.bind naming decl tab
   215   handle Symtab.DUP c => err_dup_const c;
   216 
   217 
   218 (* name space *)
   219 
   220 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   221   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
   222 
   223 
   224 (* declarations *)
   225 
   226 fun declare authentic naming tags (b, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
   227   let
   228     val tags' = tags |> Position.default_properties (Position.thread_data ());
   229     val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
   230     val (_, decls') = decls |> extend_decls naming (b, ((decl, NONE), serial ()));
   231   in (decls', constraints, rev_abbrevs) end);
   232 
   233 
   234 (* constraints *)
   235 
   236 fun constrain (c, C) consts =
   237   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   238     (the_const consts c handle TYPE (msg, _, _) => error msg;
   239       (decls,
   240         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   241         rev_abbrevs)));
   242 
   243 
   244 (* abbreviations *)
   245 
   246 local
   247 
   248 fun strip_abss tm = ([], tm) ::
   249   (case tm of
   250     Abs (a, T, t) =>
   251       if Term.loose_bvar1 (t, 0) then
   252         strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
   253       else []
   254   | _ => []);
   255 
   256 fun rev_abbrev lhs rhs =
   257   let
   258     fun abbrev (xs, body) =
   259       let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
   260       in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   261   in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
   262 
   263 in
   264 
   265 fun abbreviate pp tsig naming mode tags (b, raw_rhs) consts =
   266   let
   267     val cert_term = certify pp tsig false consts;
   268     val expand_term = certify pp tsig true consts;
   269     val force_expand = mode = PrintMode.internal;
   270 
   271     val rhs = raw_rhs
   272       |> Term.map_types (Type.cert_typ tsig)
   273       |> cert_term;
   274     val normal_rhs = expand_term rhs;
   275     val T = Term.fastype_of rhs;
   276     val lhs = Const (NameSpace.full_name naming b, T);
   277 
   278     fun err msg = (warning (* FIXME should be error *) (msg ^ " on rhs of abbreviation:\n" ^
   279       Pretty.string_of_term pp (Logic.mk_equals (lhs, rhs))); true);
   280     val _ = Term.exists_subterm Term.is_Var rhs andalso err "Illegal schematic variables";
   281     val _ = null (Term.hidden_polymorphism rhs) orelse err "Extra type variables";
   282   in
   283     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   284       let
   285         val tags' = tags |> Position.default_properties (Position.thread_data ());
   286         val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
   287         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   288         val (_, decls') = decls
   289           |> extend_decls naming (b, ((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;