author | wenzelm |
Mon, 01 Aug 2005 19:20:36 +0200 | |
changeset 16982 | 4600e74aeb0d |
parent 16936 | 93772bd33871 |
child 17223 | 430edc6b7826 |
permissions | -rw-r--r-- |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
1 |
(* Title: Pure/General/defs.ML |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
2 |
ID: $Id$ |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
3 |
Author: Steven Obua, TU Muenchen |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
4 |
|
16982 | 5 |
Checks if definitions preserve consistency of logic by enforcing that |
6 |
there are no cyclic definitions. The algorithm is described in "An |
|
7 |
Algorithm for Determining Definitional Cycles in Higher-Order Logic |
|
8 |
with Overloading", Steven Obua, technical report, to be written :-) |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
9 |
*) |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
10 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
11 |
signature DEFS = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
12 |
sig |
16982 | 13 |
(*true: record the full chain of definitions that lead to a circularity*) |
14 |
val chain_history: bool ref |
|
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
15 |
type graph |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
16 |
val empty: graph |
16982 | 17 |
val declare: theory -> string * typ -> graph -> graph |
18 |
val define: theory -> string * typ -> string -> (string * typ) list -> graph -> graph |
|
19 |
val finalize: theory -> string * typ -> graph -> graph |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
20 |
val merge: Pretty.pp -> graph -> graph -> graph |
16982 | 21 |
val finals: graph -> typ list Symtab.table |
16743
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
22 |
datatype overloadingstate = Open | Closed | Final |
16982 | 23 |
val overloading_info: graph -> string -> (typ * (string*typ) list * overloadingstate) option |
24 |
val monomorphic: graph -> string -> bool |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
25 |
end |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
26 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
27 |
structure Defs :> DEFS = struct |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
28 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
29 |
type tyenv = Type.tyenv |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
30 |
type edgelabel = (int * typ * typ * (typ * string * string) list) |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
31 |
|
16743
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
32 |
datatype overloadingstate = Open | Closed | Final |
16361 | 33 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
34 |
datatype node = Node of |
16308 | 35 |
typ (* most general type of constant *) |
16361 | 36 |
* defnode Symtab.table |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
37 |
(* a table of defnodes, each corresponding to 1 definition of the |
16308 | 38 |
constant for a particular type, indexed by axiom name *) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
39 |
* (unit Symtab.table) Symtab.table |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
40 |
(* a table of all back referencing defnodes to this node, |
16308 | 41 |
indexed by node name of the defnodes *) |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
42 |
* typ list (* a list of all finalized types *) |
16743
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
43 |
* overloadingstate |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
44 |
|
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
45 |
and defnode = Defnode of |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
46 |
typ (* type of the constant in this particular definition *) |
16308 | 47 |
* (edgelabel list) Symtab.table (* The edges, grouped by nodes. *) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
48 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
49 |
fun getnode graph noderef = the (Symtab.lookup (graph, noderef)) |
16361 | 50 |
fun get_nodedefs (Node (_, defs, _, _, _)) = defs |
51 |
fun get_defnode (Node (_, defs, _, _, _)) defname = Symtab.lookup (defs, defname) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
52 |
fun get_defnode' graph noderef defname = |
16308 | 53 |
Symtab.lookup (get_nodedefs (the (Symtab.lookup (graph, noderef))), defname) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
54 |
|
16361 | 55 |
fun table_size table = Symtab.foldl (fn (x, _) => x+1) (0, table) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
56 |
|
16982 | 57 |
datatype graphaction = |
58 |
Declare of string * typ |
|
59 |
| Define of string * typ * string * string * (string * typ) list |
|
60 |
| Finalize of string * typ |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
61 |
|
16982 | 62 |
type graph = int * string Symtab.table * graphaction list * node Symtab.table |
16308 | 63 |
|
16982 | 64 |
val chain_history = ref true |
16308 | 65 |
|
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
66 |
val empty = (0, Symtab.empty, [], Symtab.empty) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
67 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
68 |
exception DEFS of string; |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
69 |
exception CIRCULAR of (typ * string * string) list; |
16113
692fe6595755
Infinite chains in definitions are now detected, too.
obua
parents:
16108
diff
changeset
|
70 |
exception INFINITE_CHAIN of (typ * string * string) list; |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
71 |
exception CLASH of string * string * string; |
16158
2c3565b74b7a
Removed final_consts from theory data. Now const_deps deals with final
obua
parents:
16113
diff
changeset
|
72 |
exception FINAL of string * typ; |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
73 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
74 |
fun def_err s = raise (DEFS s) |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
75 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
76 |
fun no_forwards defs = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
77 |
Symtab.foldl |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
78 |
(fn (closed, (_, Defnode (_, edges))) => |
16361 | 79 |
if not closed then false else Symtab.is_empty edges) |
80 |
(true, defs) |
|
81 |
||
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
82 |
fun checkT' (Type (a, Ts)) = Type (a, map checkT' Ts) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
83 |
| checkT' (TFree (a, _)) = TVar ((a, 0), []) (* FIXME !? *) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
84 |
| checkT' (TVar ((a, 0), _)) = TVar ((a, 0), []) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
85 |
| checkT' (T as TVar _) = raise TYPE ("Illegal schematic type variable encountered", [T], []); |
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
86 |
|
16982 | 87 |
fun checkT thy = Compress.typ thy o checkT'; |
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
88 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
89 |
fun rename ty1 ty2 = Logic.incr_tvar ((maxidx_of_typ ty1)+1) ty2; |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
90 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
91 |
fun subst_incr_tvar inc t = |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
92 |
if (inc > 0) then |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
93 |
let |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
94 |
val tv = typ_tvars t |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
95 |
val t' = Logic.incr_tvar inc t |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
96 |
fun update_subst (((n,i), _), s) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
97 |
Vartab.update (((n, i), ([], TVar ((n, i+inc), []))), s) |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
98 |
in |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
99 |
(t',List.foldl update_subst Vartab.empty tv) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
100 |
end |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
101 |
else |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
102 |
(t, Vartab.empty) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
103 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
104 |
fun subst s ty = Envir.norm_type s ty |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
105 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
106 |
fun subst_history s history = map (fn (ty, cn, dn) => (subst s ty, cn, dn)) history |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
107 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
108 |
fun is_instance instance_ty general_ty = |
16936
93772bd33871
Type.raw_instance, Type.raw_unify, Term.zero_var_indexesT;
wenzelm
parents:
16877
diff
changeset
|
109 |
Type.raw_instance (instance_ty, general_ty) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
110 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
111 |
fun is_instance_r instance_ty general_ty = |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
112 |
is_instance instance_ty (rename instance_ty general_ty) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
113 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
114 |
fun unify ty1 ty2 = |
16936
93772bd33871
Type.raw_instance, Type.raw_unify, Term.zero_var_indexesT;
wenzelm
parents:
16877
diff
changeset
|
115 |
SOME (Type.raw_unify (ty1, ty2) Vartab.empty) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
116 |
handle Type.TUNIFY => NONE |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
117 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
118 |
(* |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
119 |
Unifies ty1 and ty2, renaming ty1 and ty2 so that they have greater indices than max and |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
120 |
so that they are different. All indices in ty1 and ty2 are supposed to be less than or |
16308 | 121 |
equal to max. |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
122 |
Returns SOME (max', s1, s2), so that s1(ty1) = s2(ty2) and max' is greater or equal than |
16308 | 123 |
all indices in s1, s2, ty1, ty2. |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
124 |
*) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
125 |
fun unify_r max ty1 ty2 = |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
126 |
let |
16308 | 127 |
val max = Int.max(max, 0) |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
128 |
val max1 = max (* >= maxidx_of_typ ty1 *) |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
129 |
val max2 = max (* >= maxidx_of_typ ty2 *) |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
130 |
val max = Int.max(max, Int.max (max1, max2)) |
16308 | 131 |
val (ty1, s1) = subst_incr_tvar (max + 1) ty1 |
132 |
val (ty2, s2) = subst_incr_tvar (max + max1 + 2) ty2 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
133 |
val max = max + max1 + max2 + 2 |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
134 |
fun merge a b = Vartab.merge (fn _ => false) (a, b) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
135 |
in |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
136 |
case unify ty1 ty2 of |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
137 |
NONE => NONE |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
138 |
| SOME s => SOME (max, merge s1 s, merge s2 s) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
139 |
end |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
140 |
|
16982 | 141 |
fun can_be_unified_r ty1 ty2 = is_some (unify ty1 (rename ty1 ty2)) |
142 |
fun can_be_unified ty1 ty2 = is_some (unify ty1 ty2) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
143 |
|
16308 | 144 |
fun normalize_edge_idx (edge as (maxidx, u1, v1, history)) = |
145 |
if maxidx <= 1000000 then edge else |
|
146 |
let |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
147 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
148 |
fun idxlist idx extract_ty inject_ty (tab, max) ts = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
149 |
foldr |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
150 |
(fn (e, ((tab, max), ts)) => |
16308 | 151 |
let |
152 |
val ((tab, max), ty) = idx (tab, max) (extract_ty e) |
|
153 |
val e = inject_ty (ty, e) |
|
154 |
in |
|
155 |
((tab, max), e::ts) |
|
156 |
end) |
|
157 |
((tab,max), []) ts |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
158 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
159 |
fun idx (tab,max) (TVar ((a,i),_)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
160 |
(case Inttab.lookup (tab, i) of |
16308 | 161 |
SOME j => ((tab, max), TVar ((a,j),[])) |
162 |
| NONE => ((Inttab.update ((i, max), tab), max+1), TVar ((a,max),[]))) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
163 |
| idx (tab,max) (Type (t, ts)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
164 |
let |
16308 | 165 |
val ((tab, max), ts) = idxlist idx I fst (tab, max) ts |
166 |
in |
|
167 |
((tab,max), Type (t, ts)) |
|
168 |
end |
|
169 |
| idx (tab, max) ty = ((tab, max), ty) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
170 |
|
16308 | 171 |
val ((tab,max), u1) = idx (Inttab.empty, 0) u1 |
172 |
val ((tab,max), v1) = idx (tab, max) v1 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
173 |
val ((tab,max), history) = |
16308 | 174 |
idxlist idx |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
175 |
(fn (ty,_,_) => ty) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
176 |
(fn (ty, (_, s1, s2)) => (ty, s1, s2)) |
16308 | 177 |
(tab, max) history |
178 |
in |
|
179 |
(max, u1, v1, history) |
|
180 |
end |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
181 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
182 |
fun compare_edges (e1 as (maxidx1, u1, v1, history1)) (e2 as (maxidx2, u2, v2, history2)) = |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
183 |
let |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
184 |
val t1 = u1 --> v1 |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
185 |
val t2 = Logic.incr_tvar (maxidx1+1) (u2 --> v2) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
186 |
in |
16308 | 187 |
if (is_instance t1 t2) then |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
188 |
(if is_instance t2 t1 then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
189 |
SOME (int_ord (length history2, length history1)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
190 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
191 |
SOME LESS) |
16308 | 192 |
else if (is_instance t2 t1) then |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
193 |
SOME GREATER |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
194 |
else |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
195 |
NONE |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
196 |
end |
16308 | 197 |
|
198 |
fun merge_edges_1 (x, []) = [x] |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
199 |
| merge_edges_1 (x, (y::ys)) = |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
200 |
(case compare_edges x y of |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
201 |
SOME LESS => (y::ys) |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
202 |
| SOME EQUAL => (y::ys) |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
203 |
| SOME GREATER => merge_edges_1 (x, ys) |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
204 |
| NONE => y::(merge_edges_1 (x, ys))) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
205 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
206 |
fun merge_edges xs ys = foldl merge_edges_1 xs ys |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
207 |
|
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
208 |
fun declare' (g as (cost, axmap, actions, graph)) (cty as (name, ty)) = |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
209 |
(cost, axmap, (Declare cty)::actions, |
16361 | 210 |
Symtab.update_new ((name, Node (ty, Symtab.empty, Symtab.empty, [], Open)), graph)) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
211 |
handle Symtab.DUP _ => |
16361 | 212 |
let |
213 |
val (Node (gty, _, _, _, _)) = the (Symtab.lookup(graph, name)) |
|
214 |
in |
|
215 |
if is_instance_r ty gty andalso is_instance_r gty ty then |
|
216 |
g |
|
217 |
else |
|
218 |
def_err "constant is already declared with different type" |
|
219 |
end |
|
220 |
||
16982 | 221 |
fun declare'' thy g (name, ty) = declare' g (name, checkT thy ty) |
16361 | 222 |
|
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
223 |
val axcounter = ref (IntInf.fromInt 0) |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
224 |
fun newaxname axmap axname = |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
225 |
let |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
226 |
val c = !axcounter |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
227 |
val _ = axcounter := c+1 |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
228 |
val axname' = axname^"_"^(IntInf.toString c) |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
229 |
in |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
230 |
(Symtab.update ((axname', axname), axmap), axname') |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
231 |
end |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
232 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
233 |
fun translate_ex axmap x = |
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
234 |
let |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
235 |
fun translate (ty, nodename, axname) = |
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
236 |
(ty, nodename, the (Symtab.lookup (axmap, axname))) |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
237 |
in |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
238 |
case x of |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
239 |
INFINITE_CHAIN chain => raise (INFINITE_CHAIN (map translate chain)) |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
240 |
| CIRCULAR cycle => raise (CIRCULAR (map translate cycle)) |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
241 |
| _ => raise x |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
242 |
end |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
243 |
|
16826 | 244 |
fun define' (cost, axmap, actions, graph) (mainref, ty) axname orig_axname body = |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
245 |
let |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
246 |
val mainnode = (case Symtab.lookup (graph, mainref) of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
247 |
NONE => def_err ("constant "^mainref^" is not declared") |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
248 |
| SOME n => n) |
16361 | 249 |
val (Node (gty, defs, backs, finals, _)) = mainnode |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
250 |
val _ = (if is_instance_r ty gty then () |
16308 | 251 |
else def_err "type of constant does not match declared type") |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
252 |
fun check_def (s, Defnode (ty', _)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
253 |
(if can_be_unified_r ty ty' then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
254 |
raise (CLASH (mainref, axname, s)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
255 |
else if s = axname then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
256 |
def_err "name of axiom is already used for another definition of this constant" |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
257 |
else false) |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
258 |
val _ = Symtab.exists check_def defs |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
259 |
fun check_final finalty = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
260 |
(if can_be_unified_r finalty ty then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
261 |
raise (FINAL (mainref, finalty)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
262 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
263 |
true) |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
264 |
val _ = forall check_final finals |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
265 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
266 |
(* now we know that the only thing that can prevent acceptance of the definition |
16308 | 267 |
is a cyclic dependency *) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
268 |
|
16308 | 269 |
fun insert_edges edges (nodename, links) = |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
270 |
(if links = [] then |
16308 | 271 |
edges |
272 |
else |
|
273 |
let |
|
274 |
val links = map normalize_edge_idx links |
|
275 |
in |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
276 |
Symtab.update ((nodename, |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
277 |
case Symtab.lookup (edges, nodename) of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
278 |
NONE => links |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
279 |
| SOME links' => merge_edges links' links), |
16308 | 280 |
edges) |
281 |
end) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
282 |
|
16308 | 283 |
fun make_edges ((bodyn, bodyty), edges) = |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
284 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
285 |
val bnode = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
286 |
(case Symtab.lookup (graph, bodyn) of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
287 |
NONE => def_err "body of constant definition references undeclared constant" |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
288 |
| SOME x => x) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
289 |
val (Node (general_btyp, bdefs, bbacks, bfinals, closed)) = bnode |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
290 |
in |
16361 | 291 |
if closed = Final then edges else |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
292 |
case unify_r 0 bodyty general_btyp of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
293 |
NONE => edges |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
294 |
| SOME (maxidx, sigma1, sigma2) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
295 |
if exists (is_instance_r bodyty) bfinals then |
16308 | 296 |
edges |
297 |
else |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
298 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
299 |
fun insert_trans_edges ((step1, edges), (nodename, links)) = |
16308 | 300 |
let |
301 |
val (maxidx1, alpha1, beta1, defname) = step1 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
302 |
fun connect (maxidx2, alpha2, beta2, history) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
303 |
case unify_r (Int.max (maxidx1, maxidx2)) beta1 alpha2 of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
304 |
NONE => NONE |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
305 |
| SOME (max, sleft, sright) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
306 |
SOME (max, subst sleft alpha1, subst sright beta2, |
16982 | 307 |
if !chain_history then |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
308 |
((subst sleft beta1, bodyn, defname):: |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
309 |
(subst_history sright history)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
310 |
else []) |
16308 | 311 |
val links' = List.mapPartial connect links |
312 |
in |
|
313 |
(step1, insert_edges edges (nodename, links')) |
|
314 |
end |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
315 |
|
16308 | 316 |
fun make_edges' ((swallowed, edges), |
317 |
(def_name, Defnode (def_ty, def_edges))) = |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
318 |
if swallowed then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
319 |
(swallowed, edges) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
320 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
321 |
(case unify_r 0 bodyty def_ty of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
322 |
NONE => (swallowed, edges) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
323 |
| SOME (maxidx, sigma1, sigma2) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
324 |
(is_instance_r bodyty def_ty, |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
325 |
snd (Symtab.foldl insert_trans_edges |
16308 | 326 |
(((maxidx, subst sigma1 ty, subst sigma2 def_ty, def_name), |
327 |
edges), def_edges)))) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
328 |
val (swallowed, edges) = Symtab.foldl make_edges' ((false, edges), bdefs) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
329 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
330 |
if swallowed then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
331 |
edges |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
332 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
333 |
insert_edges edges |
16308 | 334 |
(bodyn, [(maxidx, subst sigma1 ty, subst sigma2 general_btyp,[])]) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
335 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
336 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
337 |
|
16308 | 338 |
val edges = foldl make_edges Symtab.empty body |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
339 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
340 |
(* We also have to add the backreferences that this new defnode induces. *) |
16308 | 341 |
fun install_backrefs (graph, (noderef, links)) = |
342 |
if links <> [] then |
|
343 |
let |
|
16361 | 344 |
val (Node (ty, defs, backs, finals, closed)) = getnode graph noderef |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
345 |
val _ = if closed = Final then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
346 |
sys_error ("install_backrefs: closed node cannot be updated") |
16361 | 347 |
else () |
16308 | 348 |
val defnames = |
349 |
(case Symtab.lookup (backs, mainref) of |
|
350 |
NONE => Symtab.empty |
|
351 |
| SOME s => s) |
|
352 |
val defnames' = Symtab.update_new ((axname, ()), defnames) |
|
353 |
val backs' = Symtab.update ((mainref,defnames'), backs) |
|
354 |
in |
|
16361 | 355 |
Symtab.update ((noderef, Node (ty, defs, backs', finals, closed)), graph) |
16308 | 356 |
end |
357 |
else |
|
358 |
graph |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
359 |
|
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
360 |
val graph = Symtab.foldl install_backrefs (graph, edges) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
361 |
|
16361 | 362 |
val (Node (_, _, backs, _, closed)) = getnode graph mainref |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
363 |
val closed = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
364 |
if closed = Final then sys_error "define: closed node" |
16361 | 365 |
else if closed = Open andalso is_instance_r gty ty then Closed else closed |
366 |
||
16308 | 367 |
val thisDefnode = Defnode (ty, edges) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
368 |
val graph = Symtab.update ((mainref, Node (gty, Symtab.update_new |
16361 | 369 |
((axname, thisDefnode), defs), backs, finals, closed)), graph) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
370 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
371 |
(* Now we have to check all backreferences to this node and inform them about |
16308 | 372 |
the new defnode. In this section we also check for circularity. *) |
373 |
fun update_backrefs ((backs, graph), (noderef, defnames)) = |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
374 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
375 |
fun update_defs ((defnames, graph),(defname, _)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
376 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
377 |
val (Node (nodety, nodedefs, nodebacks, nodefinals, closed)) = |
16361 | 378 |
getnode graph noderef |
379 |
val _ = if closed = Final then sys_error "update_defs: closed node" else () |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
380 |
val (Defnode (def_ty, defnode_edges)) = |
16308 | 381 |
the (Symtab.lookup (nodedefs, defname)) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
382 |
val edges = the (Symtab.lookup (defnode_edges, mainref)) |
16361 | 383 |
val refclosed = ref false |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
384 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
385 |
(* the type of thisDefnode is ty *) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
386 |
fun update (e as (max, alpha, beta, history), (changed, edges)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
387 |
case unify_r max beta ty of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
388 |
NONE => (changed, e::edges) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
389 |
| SOME (max', s_beta, s_ty) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
390 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
391 |
val alpha' = subst s_beta alpha |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
392 |
val ty' = subst s_ty ty |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
393 |
val _ = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
394 |
if noderef = mainref andalso defname = axname then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
395 |
(case unify alpha' ty' of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
396 |
NONE => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
397 |
if (is_instance_r ty' alpha') then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
398 |
raise (INFINITE_CHAIN ( |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
399 |
(alpha', mainref, axname):: |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
400 |
(subst_history s_beta history)@ |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
401 |
[(ty', mainref, axname)])) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
402 |
else () |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
403 |
| SOME s => |
16308 | 404 |
raise (CIRCULAR ( |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
405 |
(subst s alpha', mainref, axname):: |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
406 |
(subst_history s (subst_history s_beta history))@ |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
407 |
[(subst s ty', mainref, axname)]))) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
408 |
else () |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
409 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
410 |
if is_instance_r beta ty then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
411 |
(true, edges) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
412 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
413 |
(changed, e::edges) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
414 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
415 |
|
16308 | 416 |
val (changed, edges') = foldl update (false, []) edges |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
417 |
val defnames' = if edges' = [] then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
418 |
defnames |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
419 |
else |
16308 | 420 |
Symtab.update ((defname, ()), defnames) |
421 |
in |
|
422 |
if changed then |
|
423 |
let |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
424 |
val defnode_edges' = |
16308 | 425 |
if edges' = [] then |
426 |
Symtab.delete mainref defnode_edges |
|
427 |
else |
|
428 |
Symtab.update ((mainref, edges'), defnode_edges) |
|
429 |
val defnode' = Defnode (def_ty, defnode_edges') |
|
430 |
val nodedefs' = Symtab.update ((defname, defnode'), nodedefs) |
|
16361 | 431 |
val closed = if closed = Closed andalso Symtab.is_empty defnode_edges' |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
432 |
andalso no_forwards nodedefs' |
16361 | 433 |
then Final else closed |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
434 |
val graph' = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
435 |
Symtab.update |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
436 |
((noderef, |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
437 |
Node (nodety, nodedefs', nodebacks, nodefinals, closed)),graph) |
16308 | 438 |
in |
439 |
(defnames', graph') |
|
440 |
end |
|
441 |
else |
|
442 |
(defnames', graph) |
|
443 |
end |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
444 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
445 |
val (defnames', graph') = Symtab.foldl update_defs |
16308 | 446 |
((Symtab.empty, graph), defnames) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
447 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
448 |
if Symtab.is_empty defnames' then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
449 |
(backs, graph') |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
450 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
451 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
452 |
val backs' = Symtab.update_new ((noderef, defnames'), backs) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
453 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
454 |
(backs', graph') |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
455 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
456 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
457 |
|
16308 | 458 |
val (backs, graph) = Symtab.foldl update_backrefs ((Symtab.empty, graph), backs) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
459 |
|
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
460 |
(* If a Circular exception is thrown then we never reach this point. *) |
16361 | 461 |
val (Node (gty, defs, _, finals, closed)) = getnode graph mainref |
462 |
val closed = if closed = Closed andalso no_forwards defs then Final else closed |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
463 |
val graph = Symtab.update ((mainref, Node (gty, defs, backs, finals, closed)), graph) |
16826 | 464 |
val actions' = (Define (mainref, ty, axname, orig_axname, body))::actions |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
465 |
in |
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
466 |
(cost+3, axmap, actions', graph) |
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
467 |
end handle ex => translate_ex axmap ex |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
468 |
|
16982 | 469 |
fun define'' thy (g as (cost, axmap, actions, graph)) (mainref, ty) orig_axname body = |
16308 | 470 |
let |
16982 | 471 |
val ty = checkT thy ty |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
472 |
fun checkbody (n, t) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
473 |
let |
16361 | 474 |
val (Node (_, _, _,_, closed)) = getnode graph n |
475 |
in |
|
476 |
case closed of |
|
477 |
Final => NONE |
|
16982 | 478 |
| _ => SOME (n, checkT thy t) |
16361 | 479 |
end |
480 |
val body = distinct (List.mapPartial checkbody body) |
|
16826 | 481 |
val (axmap, axname) = newaxname axmap orig_axname |
16308 | 482 |
in |
16826 | 483 |
define' (cost, axmap, actions, graph) (mainref, ty) axname orig_axname body |
16308 | 484 |
end |
485 |
||
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
486 |
fun finalize' (cost, axmap, history, graph) (noderef, ty) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
487 |
case Symtab.lookup (graph, noderef) of |
16308 | 488 |
NONE => def_err ("cannot finalize constant "^noderef^"; it is not declared") |
16361 | 489 |
| SOME (Node (nodety, defs, backs, finals, closed)) => |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
490 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
491 |
val _ = |
16308 | 492 |
if (not (is_instance_r ty nodety)) then |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
493 |
def_err ("only type instances of the declared constant "^ |
16308 | 494 |
noderef^" can be finalized") |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
495 |
else () |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
496 |
val _ = Symtab.exists |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
497 |
(fn (def_name, Defnode (def_ty, _)) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
498 |
if can_be_unified_r ty def_ty then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
499 |
def_err ("cannot finalize constant "^noderef^ |
16308 | 500 |
"; clash with definition "^def_name) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
501 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
502 |
false) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
503 |
defs |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
504 |
|
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
505 |
fun update_finals [] = SOME [ty] |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
506 |
| update_finals (final_ty::finals) = |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
507 |
(if is_instance_r ty final_ty then NONE |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
508 |
else |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
509 |
case update_finals finals of |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
510 |
NONE => NONE |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
511 |
| (r as SOME finals) => |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
512 |
if (is_instance_r final_ty ty) then |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
513 |
r |
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
514 |
else |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
515 |
SOME (final_ty :: finals)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
516 |
in |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
517 |
case update_finals finals of |
16384
90c51c932154
internalize axiom names in Defs.define; compress types via Term.compress_type
obua
parents:
16361
diff
changeset
|
518 |
NONE => (cost, axmap, history, graph) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
519 |
| SOME finals => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
520 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
521 |
val closed = if closed = Open andalso is_instance_r nodety ty then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
522 |
Closed else |
16361 | 523 |
closed |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
524 |
val graph = Symtab.update ((noderef, Node(nodety, defs, backs, finals, closed)), |
16308 | 525 |
graph) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
526 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
527 |
fun update_backref ((graph, backs), (backrefname, backdefnames)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
528 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
529 |
fun update_backdef ((graph, defnames), (backdefname, _)) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
530 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
531 |
val (backnode as Node (backty, backdefs, backbacks, |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
532 |
backfinals, backclosed)) = |
16308 | 533 |
getnode graph backrefname |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
534 |
val (Defnode (def_ty, all_edges)) = |
16308 | 535 |
the (get_defnode backnode backdefname) |
536 |
||
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
537 |
val (defnames', all_edges') = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
538 |
case Symtab.lookup (all_edges, noderef) of |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
539 |
NONE => sys_error "finalize: corrupt backref" |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
540 |
| SOME edges => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
541 |
let |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
542 |
val edges' = List.filter (fn (_, _, beta, _) => |
16308 | 543 |
not (is_instance_r beta ty)) edges |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
544 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
545 |
if edges' = [] then |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
546 |
(defnames, Symtab.delete noderef all_edges) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
547 |
else |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
548 |
(Symtab.update ((backdefname, ()), defnames), |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
549 |
Symtab.update ((noderef, edges'), all_edges)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
550 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
551 |
val defnode' = Defnode (def_ty, all_edges') |
16361 | 552 |
val backdefs' = Symtab.update ((backdefname, defnode'), backdefs) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
553 |
val backclosed' = if backclosed = Closed andalso |
16361 | 554 |
Symtab.is_empty all_edges' |
555 |
andalso no_forwards backdefs' |
|
556 |
then Final else backclosed |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
557 |
val backnode' = |
16361 | 558 |
Node (backty, backdefs', backbacks, backfinals, backclosed') |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
559 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
560 |
(Symtab.update ((backrefname, backnode'), graph), defnames') |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
561 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
562 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
563 |
val (graph', defnames') = |
16308 | 564 |
Symtab.foldl update_backdef ((graph, Symtab.empty), backdefnames) |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
565 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
566 |
(graph', if Symtab.is_empty defnames' then backs |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
567 |
else Symtab.update ((backrefname, defnames'), backs)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
568 |
end |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
569 |
val (graph', backs') = Symtab.foldl update_backref ((graph, Symtab.empty), backs) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
570 |
val Node ( _, defs, _, _, closed) = getnode graph' noderef |
16361 | 571 |
val closed = if closed = Closed andalso no_forwards defs then Final else closed |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
572 |
val graph' = Symtab.update ((noderef, Node (nodety, defs, backs', |
16361 | 573 |
finals, closed)), graph') |
574 |
val history' = (Finalize (noderef, ty)) :: history |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
575 |
in |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
576 |
(cost+1, axmap, history', graph') |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
577 |
end |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
578 |
end |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
579 |
|
16982 | 580 |
fun finalize'' thy g (noderef, ty) = finalize' g (noderef, checkT thy ty) |
16308 | 581 |
|
16826 | 582 |
fun update_axname ax orig_ax (cost, axmap, history, graph) = |
583 |
(cost, Symtab.update ((ax, orig_ax), axmap), history, graph) |
|
584 |
||
16361 | 585 |
fun merge' (Declare cty, g) = declare' g cty |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
586 |
| merge' (Define (name, ty, axname, orig_axname, body), g as (cost, axmap, history, graph)) = |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
587 |
(case Symtab.lookup (graph, name) of |
16826 | 588 |
NONE => define' (update_axname axname orig_axname g) (name, ty) axname orig_axname body |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
589 |
| SOME (Node (_, defs, _, _, _)) => |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
590 |
(case Symtab.lookup (defs, axname) of |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
591 |
NONE => define' (update_axname axname orig_axname g) (name, ty) axname orig_axname body |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
592 |
| SOME _ => g)) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
593 |
| merge' (Finalize finals, g) = finalize' g finals |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
594 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
595 |
fun merge'' (g1 as (cost1, _, actions1, _)) (g2 as (cost2, _, actions2, _)) = |
16308 | 596 |
if cost1 < cost2 then |
597 |
foldr merge' g2 actions1 |
|
598 |
else |
|
599 |
foldr merge' g1 actions2 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
600 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
601 |
fun finals (_, _, history, graph) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
602 |
Symtab.foldl |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
603 |
(fn (finals, (name, Node(_, _, _, ftys, _))) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
604 |
Symtab.update_new ((name, ftys), finals)) |
16198
cfd070a2cc4d
Integrates cycle detection in definitions with finalconsts
obua
parents:
16177
diff
changeset
|
605 |
(Symtab.empty, graph) |
16158
2c3565b74b7a
Removed final_consts from theory data. Now const_deps deals with final
obua
parents:
16113
diff
changeset
|
606 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
607 |
fun overloading_info (_, axmap, _, graph) c = |
16743
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
608 |
let |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
609 |
fun translate (ax, Defnode (ty, _)) = (the (Symtab.lookup (axmap, ax)), ty) |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
610 |
in |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
611 |
case Symtab.lookup (graph, c) of |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
612 |
NONE => NONE |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
613 |
| SOME (Node (ty, defnodes, _, _, state)) => |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
614 |
SOME (ty, map translate (Symtab.dest defnodes), state) |
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
615 |
end |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
616 |
|
16982 | 617 |
|
618 |
(* monomorphic consts -- neither parametric nor ad-hoc polymorphism *) |
|
619 |
||
620 |
fun monomorphicT (Type (_, Ts)) = forall monomorphicT Ts |
|
621 |
| monomorphicT _ = false |
|
622 |
||
623 |
fun monomorphic (_, _, _, graph) c = |
|
624 |
(case Symtab.lookup (graph, c) of |
|
625 |
NONE => true |
|
626 |
| SOME (Node (ty, defnodes, _, _, _)) => |
|
627 |
Symtab.min_key defnodes = Symtab.max_key defnodes andalso |
|
628 |
monomorphicT ty); |
|
16743
21dbff595bf6
1) all theorems in Orderings can now be given as a parameter
obua
parents:
16384
diff
changeset
|
629 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
630 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
631 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
632 |
(** diagnostics **) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
633 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
634 |
fun pretty_const pp (c, T) = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
635 |
[Pretty.str c, Pretty.str " ::", Pretty.brk 1, |
16936
93772bd33871
Type.raw_instance, Type.raw_unify, Term.zero_var_indexesT;
wenzelm
parents:
16877
diff
changeset
|
636 |
Pretty.quote (Pretty.typ pp (Type.freeze_type (Term.zero_var_indexesT T)))]; |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
637 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
638 |
fun pretty_path pp path = fold_rev (fn (T, c, def) => |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
639 |
fn [] => [Pretty.block (pretty_const pp (c, T))] |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
640 |
| prts => Pretty.block (pretty_const pp (c, T) @ |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
641 |
[Pretty.brk 1, Pretty.str ("depends via " ^ quote def ^ " on")]) :: prts) path []; |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
642 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
643 |
fun defs_circular pp path = |
16982 | 644 |
Pretty.str "Cyclic dependency of definitions: " :: pretty_path pp path |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
645 |
|> Pretty.chunks |> Pretty.string_of; |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
646 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
647 |
fun defs_infinite_chain pp path = |
16982 | 648 |
Pretty.str "Infinite chain of definitions: " :: pretty_path pp path |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
649 |
|> Pretty.chunks |> Pretty.string_of; |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
650 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
651 |
fun defs_clash def1 def2 = "Type clash in definitions " ^ quote def1 ^ " and " ^ quote def2; |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
652 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
653 |
fun defs_final pp const = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
654 |
(Pretty.str "Attempt to define final constant" :: Pretty.brk 1 :: pretty_const pp const) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
655 |
|> Pretty.block |> Pretty.string_of; |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
656 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
657 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
658 |
(* external interfaces *) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
659 |
|
16982 | 660 |
fun declare thy const defs = |
661 |
if_none (try (declare'' thy defs) const) defs; |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
662 |
|
16982 | 663 |
fun define thy const name rhs defs = |
664 |
define'' thy defs const name rhs |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
665 |
handle DEFS msg => sys_error msg |
16982 | 666 |
| CIRCULAR path => error (defs_circular (Sign.pp thy) path) |
667 |
| INFINITE_CHAIN path => error (defs_infinite_chain (Sign.pp thy) path) |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
668 |
| CLASH (_, def1, def2) => error (defs_clash def1 def2) |
16982 | 669 |
| FINAL const => error (defs_final (Sign.pp thy) const); |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
670 |
|
16982 | 671 |
fun finalize thy const defs = |
672 |
finalize'' thy defs const handle DEFS msg => sys_error msg; |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
673 |
|
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
674 |
fun merge pp defs1 defs2 = |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
675 |
merge'' defs1 defs2 |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
676 |
handle CIRCULAR namess => error (defs_circular pp namess) |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
677 |
| INFINITE_CHAIN namess => error (defs_infinite_chain pp namess); |
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
678 |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
679 |
end; |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
680 |
|
16308 | 681 |
(* |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
682 |
|
16308 | 683 |
fun tvar name = TVar ((name, 0), []) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
684 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
685 |
val bool = Type ("bool", []) |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
686 |
val int = Type ("int", []) |
16308 | 687 |
val lam = Type("lam", []) |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
688 |
val alpha = tvar "'a" |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
689 |
val beta = tvar "'b" |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
690 |
val gamma = tvar "'c" |
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
691 |
fun pair a b = Type ("pair", [a,b]) |
16308 | 692 |
fun prm a = Type ("prm", [a]) |
693 |
val name = Type ("name", []) |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
694 |
|
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
695 |
val _ = print "make empty" |
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
696 |
val g = Defs.empty |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
697 |
|
16308 | 698 |
val _ = print "declare perm" |
699 |
val g = Defs.declare g ("perm", prm alpha --> beta --> beta) |
|
700 |
||
701 |
val _ = print "declare permF" |
|
702 |
val g = Defs.declare g ("permF", prm alpha --> lam --> lam) |
|
703 |
||
704 |
val _ = print "define perm (1)" |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
705 |
val g = Defs.define g ("perm", prm alpha --> (beta --> gamma) --> (beta --> gamma)) "perm_fun" |
16308 | 706 |
[("perm", prm alpha --> gamma --> gamma), ("perm", prm alpha --> beta --> beta)] |
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
707 |
|
16308 | 708 |
val _ = print "define permF (1)" |
709 |
val g = Defs.define g ("permF", prm alpha --> lam --> lam) "permF_app" |
|
710 |
([("perm", prm alpha --> lam --> lam), |
|
711 |
("perm", prm alpha --> lam --> lam), |
|
712 |
("perm", prm alpha --> lam --> lam), |
|
713 |
("perm", prm alpha --> name --> name)]) |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
714 |
|
16308 | 715 |
val _ = print "define perm (2)" |
716 |
val g = Defs.define g ("perm", prm alpha --> lam --> lam) "perm_lam" |
|
717 |
[("permF", (prm alpha --> lam --> lam))] |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
diff
changeset
|
718 |
|
16877
e92cba1d4842
tuned interfaces declare, define, finalize, merge:
wenzelm
parents:
16838
diff
changeset
|
719 |
*) |