src/Pure/consts.ML
author wenzelm
Sun Mar 08 17:26:14 2009 +0100 (2009-03-08)
changeset 30364 577edc39b501
parent 30286 cf89a03ee308
child 30424 692279df7cc2
permissions -rw-r--r--
moved basic algebra of long names from structure NameSpace to Long_Name;
     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 abbrevs_of: T -> string list -> (term * term) list
    12   val dest: T ->
    13    {constants: (typ * term option) NameSpace.table,
    14     constraints: typ NameSpace.table}
    15   val the_type: T -> string -> typ                             (*exception TYPE*)
    16   val the_abbreviation: T -> string -> typ * term              (*exception TYPE*)
    17   val type_scheme: T -> string -> typ                          (*exception TYPE*)
    18   val the_tags: T -> string -> Properties.T                    (*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 -> bool -> T -> term -> term  (*exception TYPE*)
    29   val typargs: T -> string * typ -> typ list
    30   val instance: T -> string * typ list -> typ
    31   val declare: bool -> NameSpace.naming -> Properties.T -> (binding * typ) -> T -> T
    32   val constrain: string * typ option -> T -> T
    33   val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string -> Properties.T ->
    34     binding * term -> T -> (term * term) * T
    35   val revert_abbrev: string -> string -> T -> 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 (** consts type **)
    45 
    46 (* datatype T *)
    47 
    48 type decl = {T: typ, typargs: int list list, tags: Properties.T, authentic: bool};
    49 type abbrev = {rhs: term, normal_rhs: term, force_expand: bool};
    50 
    51 datatype T = Consts of
    52  {decls: ((decl * abbrev option) * serial) NameSpace.table,
    53   constraints: typ Symtab.table,
    54   rev_abbrevs: (term * term) list Symtab.table};
    55 
    56 fun make_consts (decls, constraints, rev_abbrevs) =
    57   Consts {decls = decls, constraints = constraints, rev_abbrevs = rev_abbrevs};
    58 
    59 fun map_consts f (Consts {decls, constraints, rev_abbrevs}) =
    60   make_consts (f (decls, constraints, rev_abbrevs));
    61 
    62 fun abbrevs_of (Consts {rev_abbrevs, ...}) modes =
    63   maps (Symtab.lookup_list rev_abbrevs) modes;
    64 
    65 
    66 (* dest consts *)
    67 
    68 fun dest (Consts {decls = (space, decls), constraints, ...}) =
    69  {constants = (space,
    70     Symtab.fold (fn (c, (({T, ...}, abbr), _)) =>
    71       Symtab.update (c, (T, Option.map #rhs abbr))) decls Symtab.empty),
    72   constraints = (space, constraints)};
    73 
    74 
    75 (* lookup consts *)
    76 
    77 fun the_const (Consts {decls = (_, tab), ...}) c =
    78   (case Symtab.lookup tab c of
    79     SOME (decl, _) => decl
    80   | NONE => raise TYPE ("Unknown constant: " ^ quote c, [], []));
    81 
    82 fun the_type consts c =
    83   (case the_const consts c of
    84     ({T, ...}, NONE) => T
    85   | _ => raise TYPE ("Not a logical constant: " ^ quote c, [], []));
    86 
    87 fun the_abbreviation consts c =
    88   (case the_const consts c of
    89     ({T, ...}, SOME {rhs, ...}) => (T, rhs)
    90   | _ => raise TYPE ("Not an abbreviated constant: " ^ quote c, [], []));
    91 
    92 val the_decl = #1 oo the_const;
    93 val type_scheme = #T oo the_decl;
    94 val type_arguments = #typargs oo the_decl;
    95 val the_tags = #tags oo the_decl;
    96 
    97 val is_monomorphic = null oo type_arguments;
    98 
    99 fun the_constraint (consts as Consts {constraints, ...}) c =
   100   (case Symtab.lookup constraints c of
   101     SOME T => T
   102   | NONE => type_scheme consts c);
   103 
   104 
   105 (* name space and syntax *)
   106 
   107 fun space_of (Consts {decls = (space, _), ...}) = space;
   108 
   109 val intern = NameSpace.intern o space_of;
   110 val extern = NameSpace.extern o space_of;
   111 
   112 fun extern_early consts c =
   113   (case try (the_const consts) c of
   114     SOME ({authentic = true, ...}, _) => Syntax.constN ^ c
   115   | _ => extern consts c);
   116 
   117 fun syntax consts (c, mx) =
   118   let
   119     val ({T, authentic, ...}, _) = the_const consts c handle TYPE (msg, _, _) => error msg;
   120     val c' = if authentic then Syntax.constN ^ c else Long_Name.base_name c;
   121   in (c', T, mx) end;
   122 
   123 fun syntax_name consts c = #1 (syntax consts (c, NoSyn));
   124 
   125 
   126 (* read_const *)
   127 
   128 fun read_const consts raw_c =
   129   let
   130     val c = intern consts raw_c;
   131     val T = type_scheme consts c handle TYPE (msg, _, _) => error msg;
   132   in Const (c, T) end;
   133 
   134 
   135 (* certify *)
   136 
   137 fun certify pp tsig do_expand consts =
   138   let
   139     fun err msg (c, T) =
   140       raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
   141     val certT = Type.cert_typ tsig;
   142     fun cert tm =
   143       let
   144         val (head, args) = Term.strip_comb tm;
   145         val args' = map cert args;
   146         fun comb head' = Term.list_comb (head', args');
   147       in
   148         (case head of
   149           Abs (x, T, t) => comb (Abs (x, certT T, cert t))
   150         | Const (c, T) =>
   151             let
   152               val T' = certT T;
   153               val ({T = U, ...}, abbr) = the_const consts c;
   154               fun expand u =
   155                 Term.betapplys (Envir.expand_atom T' (U, u) handle TYPE _ =>
   156                   err "Illegal type for abbreviation" (c, T), args');
   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 {rhs, normal_rhs, force_expand} =>
   163                     if do_expand then expand normal_rhs
   164                     else if force_expand then expand rhs
   165                     else comb head
   166                 | _ => comb head)
   167             end
   168         | _ => comb head)
   169       end;
   170   in cert end;
   171 
   172 
   173 (* typargs -- view actual const type as instance of declaration *)
   174 
   175 local
   176 
   177 fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
   178   | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   179   | args_of (TFree _) _ = I
   180 and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
   181   | args_of_list [] _ _ = I;
   182 
   183 fun subscript (Type (_, Ts)) (i :: is) = subscript (nth Ts i) is
   184   | subscript T [] = T
   185   | subscript T _ = raise Subscript;
   186 
   187 in
   188 
   189 fun typargs_of T = map #2 (rev (args_of T [] []));
   190 
   191 fun typargs consts (c, T) = map (subscript T) (type_arguments consts c);
   192 
   193 end;
   194 
   195 fun instance consts (c, Ts) =
   196   let
   197     val declT = type_scheme consts c;
   198     val vars = map Term.dest_TVar (typargs consts (c, declT));
   199     val inst = vars ~~ Ts handle UnequalLengths =>
   200       raise TYPE ("Consts.instance", Ts, [Const (c, dummyT)]);
   201   in declT |> TermSubst.instantiateT inst end;
   202 
   203 
   204 
   205 (** build consts **)
   206 
   207 fun err_dup_const c =
   208   error ("Duplicate declaration of constant " ^ quote c);
   209 
   210 fun extend_decls naming decl tab = NameSpace.bind naming decl tab
   211   handle Symtab.DUP c => err_dup_const c;
   212 
   213 
   214 (* name space *)
   215 
   216 fun hide fully c = map_consts (fn (decls, constraints, rev_abbrevs) =>
   217   (apfst (NameSpace.hide fully c) decls, constraints, rev_abbrevs));
   218 
   219 
   220 (* declarations *)
   221 
   222 fun declare authentic naming tags (b, declT) = map_consts (fn (decls, constraints, rev_abbrevs) =>
   223   let
   224     val tags' = tags |> Position.default_properties (Position.thread_data ());
   225     val decl = {T = declT, typargs = typargs_of declT, tags = tags', authentic = authentic};
   226     val (_, decls') = decls |> extend_decls naming (b, ((decl, NONE), serial ()));
   227   in (decls', constraints, rev_abbrevs) end);
   228 
   229 
   230 (* constraints *)
   231 
   232 fun constrain (c, C) consts =
   233   consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   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)));
   238 
   239 
   240 (* abbreviations *)
   241 
   242 local
   243 
   244 fun strip_abss tm = ([], tm) ::
   245   (case tm of
   246     Abs (a, T, t) =>
   247       if Term.loose_bvar1 (t, 0) then
   248         strip_abss t |> map (fn (xs, b) => ((a, T) :: xs, b))
   249       else []
   250   | _ => []);
   251 
   252 fun rev_abbrev lhs rhs =
   253   let
   254     fun abbrev (xs, body) =
   255       let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
   256       in (Term.subst_bounds (rev vars, body), Term.list_comb (lhs, vars)) end;
   257   in map abbrev (strip_abss (Envir.beta_eta_contract rhs)) end;
   258 
   259 in
   260 
   261 fun abbreviate pp tsig naming mode tags (b, raw_rhs) consts =
   262   let
   263     val cert_term = certify pp tsig false consts;
   264     val expand_term = certify pp tsig true consts;
   265     val force_expand = mode = PrintMode.internal;
   266 
   267     val _ = Term.exists_subterm Term.is_Var raw_rhs andalso
   268       error ("Illegal schematic variables on rhs of abbreviation: " ^ Binding.str_of b);
   269 
   270     val rhs = raw_rhs
   271       |> Term.map_types (Type.cert_typ tsig)
   272       |> cert_term
   273       |> Term.close_schematic_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   in
   278     consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   279       let
   280         val tags' = tags |> Position.default_properties (Position.thread_data ());
   281         val decl = {T = T, typargs = typargs_of T, tags = tags', authentic = true};
   282         val abbr = {rhs = rhs, normal_rhs = normal_rhs, force_expand = force_expand};
   283         val (_, decls') = decls
   284           |> extend_decls naming (b, ((decl, SOME abbr), serial ()));
   285         val rev_abbrevs' = rev_abbrevs
   286           |> fold (curry Symtab.cons_list mode) (rev_abbrev lhs rhs);
   287       in (decls', constraints, rev_abbrevs') end)
   288     |> pair (lhs, rhs)
   289   end;
   290 
   291 fun revert_abbrev mode c consts = consts |> map_consts (fn (decls, constraints, rev_abbrevs) =>
   292   let
   293     val (T, rhs) = the_abbreviation consts c;
   294     val rev_abbrevs' = rev_abbrevs
   295       |> fold (curry Symtab.cons_list mode) (rev_abbrev (Const (c, T)) rhs);
   296   in (decls, constraints, rev_abbrevs') end);
   297 
   298 end;
   299 
   300 
   301 (* empty and merge *)
   302 
   303 val empty = make_consts (NameSpace.empty_table, Symtab.empty, Symtab.empty);
   304 
   305 fun merge
   306    (Consts {decls = decls1, constraints = constraints1, rev_abbrevs = rev_abbrevs1},
   307     Consts {decls = decls2, constraints = constraints2, rev_abbrevs = rev_abbrevs2}) =
   308   let
   309     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   310       handle Symtab.DUP c => err_dup_const c;
   311     val constraints' = Symtab.join (K fst) (constraints1, constraints2);
   312     val rev_abbrevs' = (rev_abbrevs1, rev_abbrevs2) |> Symtab.join
   313       (K (Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u')));
   314   in make_consts (decls', constraints', rev_abbrevs') end;
   315 
   316 end;