18060
|
1 |
(* Title: Pure/consts.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
18935
|
5 |
Polymorphic constants: declarations, abbreviations, additional type
|
|
6 |
constraints.
|
18060
|
7 |
*)
|
|
8 |
|
|
9 |
signature CONSTS =
|
|
10 |
sig
|
|
11 |
type T
|
19027
|
12 |
val eq_consts: T * T -> bool
|
18935
|
13 |
val dest: T ->
|
|
14 |
{constants: (typ * term option) NameSpace.table,
|
|
15 |
constraints: typ NameSpace.table}
|
18965
|
16 |
val space_of: T -> NameSpace.T
|
|
17 |
val abbrevs_of: T -> (term * term) list
|
18935
|
18 |
val declaration: T -> string -> typ (*exception TYPE*)
|
|
19 |
val monomorphic: T -> string -> bool (*exception TYPE*)
|
|
20 |
val constraint: T -> string -> typ (*exception TYPE*)
|
|
21 |
val certify: Pretty.pp -> Type.tsig -> T -> term -> term (*exception TYPE*)
|
18965
|
22 |
val read_const: T -> string -> term
|
18146
|
23 |
val typargs: T -> string * typ -> typ list
|
18163
|
24 |
val instance: T -> string * typ list -> typ
|
18060
|
25 |
val declare: NameSpace.naming -> bstring * typ -> T -> T
|
18992
|
26 |
val abbreviate: Pretty.pp -> Type.tsig -> NameSpace.naming -> bool -> bstring * term -> T -> T
|
18060
|
27 |
val constrain: string * typ -> T -> T
|
|
28 |
val hide: bool -> string -> T -> T
|
|
29 |
val empty: T
|
|
30 |
val merge: T * T -> T
|
|
31 |
end
|
|
32 |
|
|
33 |
structure Consts: CONSTS =
|
|
34 |
struct
|
|
35 |
|
|
36 |
(** datatype T **)
|
|
37 |
|
18935
|
38 |
datatype decl =
|
|
39 |
LogicalConst of typ * int list list |
|
|
40 |
Abbreviation of typ * term;
|
|
41 |
|
18060
|
42 |
datatype T = Consts of
|
18935
|
43 |
{decls: (decl * serial) NameSpace.table,
|
18965
|
44 |
abbrevs: (term * term) list,
|
19027
|
45 |
constraints: typ Symtab.table} * stamp;
|
|
46 |
|
|
47 |
fun eq_consts (Consts (_, s1), Consts (_, s2)) = s1 = s2;
|
18060
|
48 |
|
18965
|
49 |
fun make_consts (decls, abbrevs, constraints) =
|
19027
|
50 |
Consts ({decls = decls, abbrevs = abbrevs, constraints = constraints}, stamp ());
|
18060
|
51 |
|
19027
|
52 |
fun map_consts f (Consts ({decls, abbrevs, constraints}, _)) =
|
18965
|
53 |
make_consts (f (decls, abbrevs, constraints));
|
|
54 |
|
19027
|
55 |
fun dest (Consts ({decls = (space, decls), abbrevs = _, constraints}, _)) =
|
18935
|
56 |
{constants = (space, Symtab.fold
|
|
57 |
(fn (c, (LogicalConst (T, _), _)) => Symtab.update (c, (T, NONE))
|
|
58 |
| (c, (Abbreviation (T, t), _)) => Symtab.update (c, (T, SOME t))) decls Symtab.empty),
|
18060
|
59 |
constraints = (space, constraints)};
|
|
60 |
|
19027
|
61 |
fun space_of (Consts ({decls = (space, _), ...}, _)) = space;
|
|
62 |
fun abbrevs_of (Consts ({abbrevs, ...}, _)) = abbrevs;
|
18060
|
63 |
|
|
64 |
|
|
65 |
(* lookup consts *)
|
|
66 |
|
18935
|
67 |
fun err_undeclared c = raise TYPE ("Undeclared constant: " ^ quote c, [], []);
|
|
68 |
|
19027
|
69 |
fun the_const (Consts ({decls = (_, tab), ...}, _)) c =
|
18935
|
70 |
(case Symtab.lookup tab c of SOME (decl, _) => decl | NONE => err_undeclared c);
|
|
71 |
|
|
72 |
fun logical_const consts c =
|
|
73 |
(case the_const consts c of LogicalConst d => d | _ => err_undeclared c);
|
18060
|
74 |
|
18935
|
75 |
fun declaration consts c = #1 (logical_const consts c);
|
|
76 |
fun monomorphic consts c = null (#2 (logical_const consts c));
|
|
77 |
|
19027
|
78 |
fun constraint (consts as Consts ({constraints, ...}, _)) c =
|
18060
|
79 |
(case Symtab.lookup constraints c of
|
|
80 |
SOME T => T
|
18935
|
81 |
| NONE => (case the_const consts c of LogicalConst (T, _) => T | Abbreviation (T, _) => T));
|
|
82 |
|
|
83 |
|
18965
|
84 |
(* certify: check/expand consts *)
|
18060
|
85 |
|
18965
|
86 |
fun certify pp tsig consts =
|
|
87 |
let
|
|
88 |
fun err msg (c, T) =
|
|
89 |
raise TYPE (msg ^ " " ^ quote c ^ " :: " ^ Pretty.string_of_typ pp T, [], []);
|
|
90 |
fun cert tm =
|
|
91 |
let
|
|
92 |
val (head, args) = Term.strip_comb tm;
|
|
93 |
val args' = map cert args;
|
|
94 |
in
|
|
95 |
(case head of
|
|
96 |
Const (c, T) =>
|
|
97 |
(case the_const consts c of
|
|
98 |
LogicalConst (U, _) =>
|
|
99 |
if Type.typ_instance tsig (T, U) then Term.list_comb (head, args')
|
|
100 |
else err "Illegal type for constant" (c, T)
|
|
101 |
| Abbreviation (U, u) =>
|
|
102 |
Term.betapplys (Envir.expand_atom tsig T (U, u) handle TYPE _ =>
|
|
103 |
err "Illegal type for abbreviation" (c, T), args'))
|
|
104 |
| Abs (x, T, t) => Term.list_comb (Abs (x, T, cert t), args')
|
|
105 |
| _ => Term.list_comb (head, args'))
|
|
106 |
end;
|
|
107 |
in cert end;
|
|
108 |
|
|
109 |
|
|
110 |
(* read_const *)
|
|
111 |
|
|
112 |
fun read_const consts raw_c =
|
|
113 |
let
|
|
114 |
val c = NameSpace.intern (space_of consts) raw_c;
|
|
115 |
val _ = the_const consts c handle TYPE (msg, _, _) => error msg;
|
|
116 |
in Const (c, dummyT) end;
|
18060
|
117 |
|
|
118 |
|
|
119 |
(* typargs -- view actual const type as instance of declaration *)
|
|
120 |
|
|
121 |
fun sub (Type (_, Ts)) (i :: is) = sub (nth Ts i) is
|
|
122 |
| sub T [] = T
|
|
123 |
| sub T _ = raise Subscript;
|
|
124 |
|
18935
|
125 |
fun typargs consts (c, T) = map (sub T) (#2 (logical_const consts c));
|
18060
|
126 |
|
18163
|
127 |
fun instance consts (c, Ts) =
|
|
128 |
let
|
|
129 |
val declT = declaration consts c;
|
|
130 |
val vars = map Term.dest_TVar (typargs consts (c, declT));
|
|
131 |
in declT |> Term.instantiateT (vars ~~ Ts) end;
|
|
132 |
|
18060
|
133 |
|
|
134 |
|
18935
|
135 |
(** declare consts **)
|
18060
|
136 |
|
|
137 |
fun err_dup_consts cs =
|
|
138 |
error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
|
|
139 |
|
|
140 |
fun err_inconsistent_constraints cs =
|
|
141 |
error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
|
|
142 |
|
18935
|
143 |
fun extend_decls naming decl tab = NameSpace.extend_table naming (tab, [decl])
|
|
144 |
handle Symtab.DUPS cs => err_dup_consts cs;
|
18060
|
145 |
|
18935
|
146 |
|
|
147 |
(* name space *)
|
18060
|
148 |
|
18965
|
149 |
fun hide fully c = map_consts (fn (decls, abbrevs, constraints) =>
|
|
150 |
(apfst (NameSpace.hide fully c) decls, abbrevs, constraints));
|
18935
|
151 |
|
|
152 |
|
|
153 |
(* declarations *)
|
|
154 |
|
18965
|
155 |
fun declare naming (c, declT) = map_consts (fn (decls, abbrevs, constraints) =>
|
18060
|
156 |
let
|
18935
|
157 |
fun args_of (Type (_, Ts)) pos = args_of_list Ts 0 pos
|
|
158 |
| args_of (TVar v) pos = insert (eq_fst op =) (v, rev pos)
|
|
159 |
| args_of (TFree _) _ = I
|
|
160 |
and args_of_list (T :: Ts) i is = args_of T (i :: is) #> args_of_list Ts (i + 1) is
|
|
161 |
| args_of_list [] _ _ = I;
|
|
162 |
val decl = (c, (LogicalConst (declT, map #2 (rev (args_of declT [] []))), serial ()));
|
18965
|
163 |
in (extend_decls naming decl decls, abbrevs, constraints) end);
|
18060
|
164 |
|
18935
|
165 |
|
|
166 |
(* abbreviations *)
|
|
167 |
|
19027
|
168 |
local
|
|
169 |
|
|
170 |
fun strip_abss (Abs (a, T, t)) =
|
|
171 |
([], t) :: map (fn (xs, b) => ((a, T) :: xs, b)) (strip_abss t)
|
|
172 |
| strip_abss t = [([], t)];
|
|
173 |
|
18965
|
174 |
fun revert_abbrev const rhs =
|
18060
|
175 |
let
|
19027
|
176 |
fun abbrev (xs, body) =
|
|
177 |
let val vars = fold (fn (x, T) => cons (Var ((x, 0), T))) (Term.rename_wrt_term body xs) []
|
|
178 |
in (Term.subst_bounds (rev vars, body), Term.list_comb (Const const, vars)) end;
|
|
179 |
in map abbrev (rev (strip_abss (Envir.beta_eta_contract rhs))) end;
|
|
180 |
|
|
181 |
in
|
18965
|
182 |
|
|
183 |
fun abbreviate pp tsig naming revert (c, raw_rhs) consts =
|
|
184 |
consts |> map_consts (fn (decls, abbrevs, constraints) =>
|
|
185 |
let
|
|
186 |
val rhs = certify pp tsig consts raw_rhs;
|
|
187 |
val T = Term.fastype_of rhs;
|
|
188 |
val decls' = decls |> extend_decls naming (c, (Abbreviation (T, rhs), serial ()));
|
|
189 |
val abbrevs' =
|
19027
|
190 |
if revert then revert_abbrev (NameSpace.full naming c, T) rhs @ abbrevs else abbrevs;
|
18965
|
191 |
in (decls', abbrevs', constraints) end);
|
18935
|
192 |
|
19027
|
193 |
end;
|
|
194 |
|
18060
|
195 |
|
18935
|
196 |
(* constraints *)
|
18060
|
197 |
|
18965
|
198 |
fun constrain (c, T) = map_consts (fn (decls, abbrevs, constraints) =>
|
|
199 |
(decls, abbrevs, Symtab.update (c, T) constraints));
|
18060
|
200 |
|
|
201 |
|
|
202 |
(* empty and merge *)
|
|
203 |
|
18965
|
204 |
val empty = make_consts (NameSpace.empty_table, [], Symtab.empty);
|
18060
|
205 |
|
|
206 |
fun merge
|
19027
|
207 |
(Consts ({decls = decls1, abbrevs = abbrevs1, constraints = constraints1}, _),
|
|
208 |
Consts ({decls = decls2, abbrevs = abbrevs2, constraints = constraints2}, _)) =
|
18060
|
209 |
let
|
18935
|
210 |
val decls' = NameSpace.merge_tables (eq_snd (op =)) (decls1, decls2)
|
18060
|
211 |
handle Symtab.DUPS cs => err_dup_consts cs;
|
18965
|
212 |
val abbrevs' =
|
|
213 |
Library.merge (fn ((t, u), (t', u')) => t aconv t' andalso u aconv u') (abbrevs1, abbrevs2);
|
18935
|
214 |
val constraints' = Symtab.merge (op =) (constraints1, constraints2)
|
18060
|
215 |
handle Symtab.DUPS cs => err_inconsistent_constraints cs;
|
18965
|
216 |
in make_consts (decls', abbrevs', constraints') end;
|
18060
|
217 |
|
|
218 |
end;
|