src/Pure/consts.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32784 1a5dde5079ac
child 33092 c859019d3ac5
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
     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 retrieve_abbrevs: T -> string list -> term -> (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) Item_Net.T Symtab.table};
    56 
    57 fun eq_consts
    58    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
    59     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
    60   pointer_eq (decls1, decls2) andalso
    61   pointer_eq (constraints1, constraints2) andalso
    62   pointer_eq (rev_abbrevs1, rev_abbrevs2);
    63 
    64 fun make_consts (decls, constraints, rev_abbrevs) =
    65   Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
    66 
    67 fun map_consts f (Consts {decls, constraints, rev_abbrevs}) =
    68   make_consts (f (decls, constraints, rev_abbrevs));
    69 
    70 
    71 (* reverted abbrevs *)
    72 
    73 val empty_abbrevs = Item_Net.init (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') #1;
    74 
    75 fun insert_abbrevs mode abbrs =
    76   Symtab.map_default (mode, empty_abbrevs) (Item_Net.insert abbrs);
    77 
    78 fun retrieve_abbrevs (Consts {rev_abbrevs, ...}) modes =
    79   let val nets = map_filter (Symtab.lookup rev_abbrevs) modes
    80   in fn t => maps (fn net => Item_Net.retrieve net t) nets end;
    81 
    82 
    83 (* dest consts *)
    84 
    85 fun dest (Consts {decls = (space, decls), constraints, ...}) =
    86  {constants = (space,
    87     Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
    88       Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
    89   constraints = (space, constraints)};
    90 
    91 
    92 (* lookup consts *)
    93 
    94 fun the_const (Consts {decls = (_, tab), ...}) c =
    95   (case Symtab.lookup tab c of
    96     SOME (decl, _) => decl
    97   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
    98 
    99 fun the_type consts c =
   100   (case the_const consts c of
   101     ({T, ...}, NONE) => T
   102   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
   103 
   104 fun the_abbreviation consts c =
   105   (case the_const consts c of
   106     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
   107   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
   108 
   109 val the_decl = #1 oo the_const;
   110 val type_scheme = #T oo the_decl;
   111 val type_arguments = #typargs oo the_decl;
   112 val the_tags = #tags oo the_decl;
   113 
   114 val is_monomorphic = null oo type_arguments;
   115 
   116 fun the_constraint (consts as Consts {constraints, ...}) c =
   117   (case Symtab.lookup constraints c of
   118     SOME T => T
   119   | NONE => type_scheme consts c);
   120 
   121 
   122 (* name space and syntax *)
   123 
   124 fun space_of (Consts {decls = (space, _), ...}) = space;
   125 
   126 val intern = NameSpace.intern o space_of;
   127 val extern = NameSpace.extern o space_of;
   128 
   129 fun extern_early consts c =
   130   (case try (the_const consts) c of
   131     SOME ({authentic = true, ...}, _) => Syntax.constN ^ c
   132   | _ => extern consts c);
   133 
   134 fun syntax consts (c, mx) =
   135   let
   136     val ({T, authentic, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
   137     val c' = if authentic then Syntax.constN ^ c else Long_Name.base_name c;
   138   in (c', T, mx) end;
   139 
   140 fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
   141 
   142 
   143 (* read_const *)
   144 
   145 fun read_const consts raw_c =
   146   let
   147     val c = intern consts raw_c;
   148     val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
   149   in Const (c, T) end;
   150 
   151 
   152 (* certify *)
   153 
   154 fun certify pp tsig do_expand consts =
   155   let
   156     fun err msg (c, T) =
   157       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
   158     val certT = Type.cert_typ tsig;
   159     fun cert tm =
   160       let
   161         val (head, args) = Term.strip_comb tm;
   162         val args' = map cert args;
   163         fun comb head' = Term.list_comb (head', args');
   164       in
   165         (case head of
   166           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   167         | Const (c, T) =>
   168             let
   169               val T' = certT T;
   170               val ({T = U, ...}, abbr) = the_const consts c;
   171               fun expand u =
   172                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   173                   err "Illegal type for abbreviation" (c, T), args');
   174             in
   175               if not (Type.raw_instance (T', U)) then
   176                 err "Illegal type for constant" (c, T)
   177               else
   178                 (case abbr of
   179                   SOME {rhs, normal_rhs, force_expand} =>
   180                     if do_expand then expand normal_rhs
   181                     else if force_expand then expand rhs
   182                     else comb head
   183                 | _ => comb head)
   184             end
   185         | _ => comb head)
   186       end;
   187   in cert end;
   188 
   189 
   190 (* typargs -- view actual const type as instance of declaration *)
   191 
   192 local
   193 
   194 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   195   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   196   | args_of (TFree _) _ = I
   197 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   198   | args_of_list [] _ _ = I;
   199 
   200 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   201   | subscript T [] = T
   202   | subscript _ _ = raise Subscript;
   203 
   204 in
   205 
   206 fun typargs_of T = map #2 (rev (args_of T [] []));
   207 
   208 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   209 
   210 end;
   211 
   212 fun instance consts (c, Ts) =
   213   let
   214     val declT = type_scheme consts c;
   215     val vars = map Term.dest_TVar (typargs consts (c, declT));
   216     val inst = vars ~~ Ts handle UnequalLengths =>
   217       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   218   in declT |> Term_Subst.instantiateT inst end;
   219 
   220 
   221 
   222 (** build consts **)
   223 
   224 fun err_dup_const c =
   225   error ("Duplicate declaration of constant " ^ quote c);
   226 
   227 fun extend_decls naming decl tab = NameSpace.define naming decl tab
   228   handle Symtab.DUP c => err_dup_const c;
   229 
   230 
   231 (* name space *)
   232 
   233 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   234   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
   235 
   236 
   237 (* declarations *)
   238 
   239 fun declare authentic naming tags (b, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
   240   let
   241     val tags' = tags |> Position.default_properties (Position.thread_data ());
   242     val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
   243     val (_, decls') = decls |> extend_decls naming (b, ((decl, NONE), serial ()));
   244   in (decls', constraints, rev_abbrevs) end);
   245 
   246 
   247 (* constraints *)
   248 
   249 fun constrain (c, C) consts =
   250   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   251     (the_const consts c handle TYPE (msg, _, _) => error msg;
   252       (decls,
   253         constraints |> (case C of SOME T => Symtab.update (c, T) | NONE => Symtab.delete_safe c),
   254         rev_abbrevs)));
   255 
   256 
   257 (* abbreviations *)
   258 
   259 local
   260 
   261 fun strip_abss (t as Abs (x, T, b)) =
   262       if Term.loose_bvar1 (b, 0) then strip_abss b |>> cons (x, T)
   263       else ([], t)
   264   | strip_abss t = ([], t);
   265 
   266 fun rev_abbrev lhs rhs =
   267   let
   268     val (xs, body) = strip_abss (Envir.beta_eta_contract rhs);
   269     val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) [];
   270   in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   271 
   272 in
   273 
   274 fun abbreviate pp tsig naming mode tags (b, raw_rhs) consts =
   275   let
   276     val cert_term = certify pp tsig false consts;
   277     val expand_term = certify pp tsig true consts;
   278     val force_expand = mode = PrintMode.internal;
   279 
   280     val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
   281       error ("Illegal schematic variables on rhs of abbreviation: " ^ Binding.str_of b);
   282 
   283     val rhs = raw_rhs
   284       |> Term.map_types (Type.cert_typ tsig)
   285       |> cert_term
   286       |> Term.close_schematic_term;
   287     val normal_rhs = expand_term rhs;
   288     val T = Term.fastype_of rhs;
   289     val lhs = Const (NameSpace.full_name naming b, T);
   290   in
   291     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   292       let
   293         val tags' = tags |> Position.default_properties (Position.thread_data ());
   294         val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
   295         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   296         val (_, decls') = decls
   297           |> extend_decls naming (b, ((decl, SOME abbr), serial ()));
   298         val rev_abbrevs' = rev_abbrevs
   299           |> insert_abbrevs mode (rev_abbrev lhs rhs);
   300       in (decls', constraints, rev_abbrevs') end)
   301     |> pair (lhs, rhs)
   302   end;
   303 
   304 fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   305   let
   306     val (T, rhs) = the_abbreviation consts c;
   307     val rev_abbrevs' = rev_abbrevs
   308       |> insert_abbrevs mode (rev_abbrev (Const (c, T)) rhs);
   309   in (decls, constraints, rev_abbrevs') end);
   310 
   311 end;
   312 
   313 
   314 (* empty and merge *)
   315 
   316 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
   317 
   318 fun merge
   319    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
   320     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
   321   let
   322     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   323       handle Symtab.DUP c => err_dup_const c;
   324     val constraints' = Symtab.join (K fst) (constraints1, constraints2);
   325     val rev_abbrevs' = Symtab.join (K Item_Net.merge) (rev_abbrevs1, rev_abbrevs2);
   326   in make_consts (decls', constraints', rev_abbrevs') end;
   327 
   328 end;