18060
|
1 |
(* Title: Pure/consts.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Polymorphic constants.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature CONSTS =
|
|
9 |
sig
|
|
10 |
type T
|
|
11 |
val dest: T -> {declarations: typ NameSpace.table, constraints: typ NameSpace.table}
|
|
12 |
val space: T -> NameSpace.T
|
|
13 |
val declaration: T -> string -> typ (*exception TYPE*)
|
|
14 |
val constraint: T -> string -> typ (*exception TYPE*)
|
|
15 |
val monomorphic: T -> string -> bool
|
|
16 |
val typargs: T -> string -> typ -> typ list
|
|
17 |
val declare: NameSpace.naming -> bstring * typ -> T -> T
|
|
18 |
val constrain: string * typ -> T -> T
|
|
19 |
val hide: bool -> string -> T -> T
|
|
20 |
val empty: T
|
|
21 |
val merge: T * T -> T
|
|
22 |
end
|
|
23 |
|
|
24 |
structure Consts: CONSTS =
|
|
25 |
struct
|
|
26 |
|
|
27 |
|
|
28 |
(** datatype T **)
|
|
29 |
|
|
30 |
type arg = (indexname * sort) * int list; (*type variable with first occurrence*)
|
|
31 |
|
|
32 |
datatype T = Consts of
|
|
33 |
{declarations: ((typ * arg list) * serial) NameSpace.table,
|
|
34 |
constraints: typ Symtab.table};
|
|
35 |
|
|
36 |
fun make_consts (declarations, constraints) =
|
|
37 |
Consts {declarations = declarations, constraints = constraints};
|
|
38 |
|
|
39 |
fun map_consts f (Consts {declarations, constraints}) =
|
|
40 |
make_consts (f (declarations, constraints));
|
|
41 |
|
|
42 |
fun dest (Consts {declarations = (space, decls), constraints}) =
|
|
43 |
{declarations = (space, Symtab.map (#1 o #1) decls),
|
|
44 |
constraints = (space, constraints)};
|
|
45 |
|
|
46 |
fun space (Consts {declarations = (space, _), ...}) = space;
|
|
47 |
|
|
48 |
|
|
49 |
(* lookup consts *)
|
|
50 |
|
|
51 |
fun the_const (Consts {declarations = (_, decls), ...}) c =
|
|
52 |
(case Symtab.lookup decls c of SOME (decl, _) => decl
|
|
53 |
| NONE => raise TYPE ("Undeclared constant " ^ quote c, [], []));
|
|
54 |
|
|
55 |
fun declaration consts c = #1 (the_const consts c);
|
|
56 |
|
|
57 |
fun constraint (consts as Consts {constraints, ...}) c =
|
|
58 |
(case Symtab.lookup constraints c of
|
|
59 |
SOME T => T
|
|
60 |
| NONE => declaration consts c);
|
|
61 |
|
|
62 |
fun monomorphic consts c = null (#2 (the_const consts c));
|
|
63 |
|
|
64 |
|
|
65 |
(* typargs -- view actual const type as instance of declaration *)
|
|
66 |
|
|
67 |
fun sub (Type (_, Ts)) (i :: is) = sub (nth Ts i) is
|
|
68 |
| sub T [] = T
|
|
69 |
| sub T _ = raise Subscript;
|
|
70 |
|
|
71 |
fun typargs consts c =
|
|
72 |
let val (_, args) = the_const consts c
|
|
73 |
in fn T => map (sub T o #2) args end;
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
(** build consts **)
|
|
78 |
|
|
79 |
fun err_dup_consts cs =
|
|
80 |
error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
|
|
81 |
|
|
82 |
fun err_inconsistent_constraints cs =
|
|
83 |
error ("Inconsistent type constraints for constant(s) " ^ commas_quote cs);
|
|
84 |
|
|
85 |
|
|
86 |
(* declarations etc. *)
|
|
87 |
|
|
88 |
fun args_of declT =
|
|
89 |
let
|
|
90 |
fun args (Type (_, Ts)) pos = args_list Ts 0 pos
|
|
91 |
| args (TVar v) pos = insert (eq_fst op =) (v, rev pos)
|
|
92 |
| args (TFree _) _ = I
|
|
93 |
and args_list (T :: Ts) i is = args T (i :: is) #> args_list Ts (i + 1) is
|
|
94 |
| args_list [] _ _ = I;
|
|
95 |
in rev (args declT [] []) end;
|
|
96 |
|
|
97 |
fun declare naming (c, T) = map_consts (apfst (fn declarations =>
|
|
98 |
let
|
|
99 |
val decl = (c, ((T, args_of T), serial ()));
|
|
100 |
val declarations' = NameSpace.extend_table naming (declarations, [decl])
|
|
101 |
handle Symtab.DUPS cs => err_dup_consts cs;
|
|
102 |
in declarations' end));
|
|
103 |
|
|
104 |
val constrain = map_consts o apsnd o Symtab.update;
|
|
105 |
|
|
106 |
fun hide fully c = map_consts (apfst (apfst (NameSpace.hide fully c)));
|
|
107 |
|
|
108 |
|
|
109 |
(* empty and merge *)
|
|
110 |
|
|
111 |
val empty = make_consts (NameSpace.empty_table, Symtab.empty);
|
|
112 |
|
|
113 |
fun merge
|
|
114 |
(Consts {declarations = declarations1, constraints = constraints1},
|
|
115 |
Consts {declarations = declarations2, constraints = constraints2}) =
|
|
116 |
let
|
|
117 |
val declarations = NameSpace.merge_tables (eq_snd (op =)) (declarations1, declarations2)
|
|
118 |
handle Symtab.DUPS cs => err_dup_consts cs;
|
|
119 |
val constraints = Symtab.merge (op =) (constraints1, constraints2)
|
|
120 |
handle Symtab.DUPS cs => err_inconsistent_constraints cs;
|
|
121 |
in make_consts (declarations, constraints) end;
|
|
122 |
|
|
123 |
end;
|