src/Pure/consts.ML
changeset 18935 f22be3d61ed5
parent 18163 9b729737bf14
child 18965 3b76383e3ab3
equal deleted inserted replaced
18934:0342b7c21388 18935:f22be3d61ed5
     1 (*  Title:      Pure/consts.ML
     1 (*  Title:      Pure/consts.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Makarius
     3     Author:     Makarius
     4 
     4 
     5 Polymorphic constants.
     5 Polymorphic constants: declarations, abbreviations, additional type
       
     6 constraints.
     6 *)
     7 *)
     7 
     8 
     8 signature CONSTS =
     9 signature CONSTS =
     9 sig
    10 sig
    10   type T
    11   type T
    11   val dest: T -> {declarations: typ NameSpace.table, constraints: typ NameSpace.table}
    12   val dest: T ->
       
    13    {constants: (typ * term option) NameSpace.table,
       
    14     constraints: typ NameSpace.table}
    12   val space: T -> NameSpace.T
    15   val space: T -> NameSpace.T
    13   val declaration: T -> string -> typ  (*exception TYPE*)
    16   val declaration: T -> string -> typ                           (*exception TYPE*)
    14   val constraint: T -> string -> typ    (*exception TYPE*)
    17   val monomorphic: T -> string -> bool                          (*exception TYPE*)
    15   val monomorphic: T -> string -> bool
    18   val constraint: T -> string -> typ                            (*exception TYPE*)
       
    19   val expansion: Type.tsig -> T -> string * typ -> term option  (*exception TYPE*)
       
    20   val certify: Pretty.pp -> Type.tsig -> T -> term -> term      (*exception TYPE*)
    16   val typargs: T -> string * typ -> typ list
    21   val typargs: T -> string * typ -> typ list
    17   val instance: T -> string * typ list -> typ
    22   val instance: T -> string * typ list -> typ
    18   val declare: NameSpace.naming -> bstring * typ -> T -> T
    23   val declare: NameSpace.naming -> bstring * typ -> T -> T
       
    24   val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> string * term -> T -> T
    19   val constrain: string * typ -> T -> T
    25   val constrain: string * typ -> T -> T
    20   val hide: bool -> string -> T -> T
    26   val hide: bool -> string -> T -> T
    21   val empty: T
    27   val empty: T
    22   val merge: T * T -> T
    28   val merge: T * T -> T
    23 end
    29 end
    24 
    30 
    25 structure Consts: CONSTS =
    31 structure Consts: CONSTS =
    26 struct
    32 struct
    27 
    33 
    28 
       
    29 (** datatype T **)
    34 (** datatype T **)
    30 
    35 
       
    36 datatype decl =
       
    37   LogicalConst of typ * int list list |
       
    38   Abbreviation of typ * term;
       
    39 
    31 datatype T = Consts of
    40 datatype T = Consts of
    32  {declarations: ((typ * int list list) * serial) NameSpace.table,
    41  {decls: (decl * serial) NameSpace.table,
    33   constraints: typ Symtab.table};
    42   constraints: typ Symtab.table};
    34 
    43 
    35 fun make_consts (declarations, constraints) =
    44 fun make_consts (decls, constraints) = Consts {decls = decls, constraints = constraints};
    36   Consts {declarations = declarations, constraints = constraints};
    45 fun map_consts f (Consts {decls, constraints}) = make_consts (f (decls, constraints));
    37 
    46 
    38 fun map_consts f (Consts {declarations, constraints}) =
    47 fun dest (Consts {decls = (space, decls), constraints}) =
    39   make_consts (f (declarations, constraints));
    48  {constants = (space, Symtab.fold
    40 
    49     (fn (c, (LogicalConst (T, _), _)) => Symtab.update (c, (T, NONE))
    41 fun dest (Consts {declarations = (space, decls), constraints}) =
    50       | (c, (Abbreviation (T, t), _)) => Symtab.update (c, (T, SOME t))) decls Symtab.empty),
    42  {declarations = (space, Symtab.map (#1 o #1) decls),
       
    43   constraints = (space, constraints)};
    51   constraints = (space, constraints)};
    44 
    52 
    45 fun space (Consts {declarations = (space, _), ...}) = space;
    53 fun space (Consts {decls, ...}) = #1 decls;
    46 
    54 
    47 
    55 
    48 (* lookup consts *)
    56 (* lookup consts *)
    49 
    57 
    50 fun the_const (Consts {declarations = (_, decls), ...}) c =
    58 fun err_undeclared c = raise TYPE ("Undeclared constant: " ^ quote c, [], []);
    51   (case Symtab.lookup decls c of SOME (decl, _) => decl
       
    52   | NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
       
    53 
    59 
    54 fun declaration consts c = #1 (the_const consts c);
    60 fun the_const (Consts {decls = (_, tab), ...}) c =
       
    61   (case Symtab.lookup tab c of SOME (decl, _) => decl | NONE => err_undeclared c);
       
    62 
       
    63 fun logical_const consts c =
       
    64   (case the_const consts c of LogicalConst d => d | _ => err_undeclared c);
       
    65 
       
    66 fun declaration consts c = #1 (logical_const consts c);
       
    67 fun monomorphic consts c = null (#2 (logical_const consts c));
       
    68 
       
    69 fun expansion tsig consts (c, T) =
       
    70   (case the_const consts c of
       
    71     Abbreviation a => SOME (Envir.expand_atom tsig T a)
       
    72   | _ => NONE);
    55 
    73 
    56 fun constraint (consts as Consts {constraints, ...}) c =
    74 fun constraint (consts as Consts {constraints, ...}) c =
    57   (case Symtab.lookup constraints c of
    75   (case Symtab.lookup constraints c of
    58     SOME T => T
    76     SOME T => T
    59   | NONE => declaration consts c);
    77   | NONE => (case the_const consts c of LogicalConst (T, _) => T | Abbreviation (T, _) => T));
    60 
    78 
    61 fun monomorphic consts c = null (#2 (the_const consts c));
    79 
       
    80 (* certify: check/expand consts -- without beta normalization *)
       
    81 
       
    82 fun certify pp tsig consts tm =
       
    83   let
       
    84     fun err msg = raise TYPE (msg, [], [tm]);
       
    85     fun show_const (c, T) = quote c ^ " :: " ^ Pretty.string_of_typ pp T;
       
    86 
       
    87     fun cert (t as Const (c, T)) =
       
    88           (case the_const consts c of
       
    89             LogicalConst (U, _) =>
       
    90               if Type.typ_instance tsig (T, U) then t
       
    91               else err ("Illegal type for constant " ^ show_const (c, T))
       
    92           | Abbreviation (U, u) =>
       
    93               Envir.expand_atom tsig T (U, u) handle TYPE _ =>
       
    94                 err ("Illegal type for abbreviation " ^ show_const (c, T)))
       
    95       | cert (t $ u) = cert t $ cert u
       
    96       | cert (Abs (x, T, t)) = Abs (x, T, cert t)
       
    97       | cert a = a;
       
    98   in cert tm end;
    62 
    99 
    63 
   100 
    64 (* typargs -- view actual const type as instance of declaration *)
   101 (* typargs -- view actual const type as instance of declaration *)
    65 
   102 
    66 fun sub (Type (_, Ts)) (i :: is) = sub (nth Ts i) is
   103 fun sub (Type (_, Ts)) (i :: is) = sub (nth Ts i) is
    67   | sub T [] = T
   104   | sub T [] = T
    68   | sub T _ = raise Subscript;
   105   | sub T _ = raise Subscript;
    69 
   106 
    70 fun typargs consts (c, T) = map (sub T) (#2 (the_const consts c));
   107 fun typargs consts (c, T) = map (sub T) (#2 (logical_const consts c));
    71 
   108 
    72 fun instance consts (c, Ts) =
   109 fun instance consts (c, Ts) =
    73   let
   110   let
    74     val declT = declaration consts c;
   111     val declT = declaration consts c;
    75     val vars = map Term.dest_TVar (typargs consts (c, declT));
   112     val vars = map Term.dest_TVar (typargs consts (c, declT));
    76   in declT |> Term.instantiateT (vars ~~ Ts) end;
   113   in declT |> Term.instantiateT (vars ~~ Ts) end;
    77 
   114 
    78 
   115 
    79 
   116 
    80 (** build consts **)
   117 (** declare consts **)
    81 
   118 
    82 fun err_dup_consts cs =
   119 fun err_dup_consts cs =
    83   error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
   120   error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
    84 
   121 
    85 fun err_inconsistent_constraints cs =
   122 fun err_inconsistent_constraints cs =
    86   error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
   123   error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
    87 
   124 
       
   125 fun extend_decls naming decl tab = NameSpace.extend_table naming (tab, [decl])
       
   126   handle Symtab.DUPS cs => err_dup_consts cs;
    88 
   127 
    89 (* declarations etc. *)
       
    90 
   128 
    91 fun args_of declT =
   129 (* name space *)
       
   130 
       
   131 fun hide fully c = map_consts (fn (decls, constraints) =>
       
   132   (apfst (NameSpace.hide fully c) decls, constraints));
       
   133 
       
   134 
       
   135 (* declarations *)
       
   136 
       
   137 fun declare naming (c, declT) = map_consts (fn (decls, constraints) =>
    92   let
   138   let
    93     fun args (Type (_, Ts)) pos = args_list Ts 0 pos
   139     fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
    94       | args (TVar v) pos = insert (eq_fst op =) (v, rev pos)
   140       | args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
    95       | args (TFree _) _ = I
   141       | args_of (TFree _) _ = I
    96     and args_list (T :: Ts) i is = args T (i :: is) #> args_list Ts (i + 1) is
   142     and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
    97       | args_list [] _ _ = I;
   143       | args_of_list [] _ _ = I;
    98   in map #2 (rev (args declT [] [])) end;
   144     val decl = (c, (LogicalConst (declT, map #2 (rev (args_of declT [] []))), serial ()));
       
   145   in (extend_decls naming decl decls, constraints) end);
    99 
   146 
   100 fun declare naming (c, T) = map_consts (apfst (fn declarations =>
   147 
       
   148 (* abbreviations *)
       
   149 
       
   150 fun abbreviate pp tsig naming (c, rhs) consts =
       
   151     consts |> map_consts (fn (decls, constraints) =>
   101   let
   152   let
   102     val decl = (c, ((T, args_of T), serial ()));
   153     val rhs' = Envir.beta_norm (certify pp tsig consts rhs);
   103     val declarations' = NameSpace.extend_table naming (declarations, [decl])
   154     val decl = (c, (Abbreviation (Term.fastype_of rhs', rhs'), serial ()));
   104       handle Symtab.DUPS cs => err_dup_consts cs;
   155   in (extend_decls naming decl decls, constraints) end);
   105   in declarations' end));
       
   106 
   156 
   107 val constrain = map_consts o apsnd o Symtab.update;
       
   108 
   157 
   109 fun hide fully c = map_consts (apfst (apfst (NameSpace.hide fully c)));
   158 (* constraints *)
       
   159 
       
   160 fun constrain (c, T) =
       
   161   map_consts (fn (decls, constraints) => (decls, Symtab.update (c, T) constraints));
   110 
   162 
   111 
   163 
   112 (* empty and merge *)
   164 (* empty and merge *)
   113 
   165 
   114 val empty = make_consts (NameSpace.empty_table, Symtab.empty);
   166 val empty = make_consts (NameSpace.empty_table, Symtab.empty);
   115 
   167 
   116 fun merge
   168 fun merge
   117   (Consts {declarations = declarations1, constraints = constraints1},
   169   (Consts {decls = decls1, constraints = constraints1},
   118    Consts {declarations = declarations2, constraints = constraints2}) =
   170    Consts {decls = decls2, constraints = constraints2}) =
   119   let
   171   let
   120     val declarations = NameSpace.merge_tables (eq_snd (op =)) (declarations1, declarations2)
   172     val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
   121       handle Symtab.DUPS cs => err_dup_consts cs;
   173       handle Symtab.DUPS cs => err_dup_consts cs;
   122     val constraints = Symtab.merge (op =) (constraints1, constraints2)
   174     val constraints' = Symtab.merge (op =) (constraints1, constraints2)
   123       handle Symtab.DUPS cs => err_inconsistent_constraints cs;
   175       handle Symtab.DUPS cs => err_inconsistent_constraints cs;
   124   in make_consts (declarations, constraints) end;
   176   in make_consts (decls', constraints') end;
   125 
   177 
   126 end;
   178 end;