author | blanchet |
Fri, 04 Dec 2009 17:19:59 +0100 | |
changeset 33982 | 1ae222745c4a |
parent 33705 | 947184dc75c9 |
child 34124 | c4628a1dcf75 |
permissions | -rw-r--r-- |
33982 | 1 |
(* Title: HOL/Tools/Nitpick/nitpick_peephole.ML |
33192 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
Copyright 2008, 2009 |
|
4 |
||
5 |
Peephole optimizer for Nitpick. |
|
6 |
*) |
|
7 |
||
8 |
signature NITPICK_PEEPHOLE = |
|
9 |
sig |
|
10 |
type formula = Kodkod.formula |
|
11 |
type int_expr = Kodkod.int_expr |
|
12 |
type rel_expr = Kodkod.rel_expr |
|
13 |
type decl = Kodkod.decl |
|
14 |
type expr_assign = Kodkod.expr_assign |
|
15 |
||
16 |
type name_pool = { |
|
17 |
rels: Kodkod.n_ary_index list, |
|
18 |
vars: Kodkod.n_ary_index list, |
|
19 |
formula_reg: int, |
|
20 |
rel_reg: int} |
|
21 |
||
22 |
val initial_pool : name_pool |
|
23 |
val not3_rel : rel_expr |
|
24 |
val suc_rel : rel_expr |
|
25 |
val nat_add_rel : rel_expr |
|
26 |
val int_add_rel : rel_expr |
|
27 |
val nat_subtract_rel : rel_expr |
|
28 |
val int_subtract_rel : rel_expr |
|
29 |
val nat_multiply_rel : rel_expr |
|
30 |
val int_multiply_rel : rel_expr |
|
31 |
val nat_divide_rel : rel_expr |
|
32 |
val int_divide_rel : rel_expr |
|
33 |
val nat_modulo_rel : rel_expr |
|
34 |
val int_modulo_rel : rel_expr |
|
35 |
val nat_less_rel : rel_expr |
|
36 |
val int_less_rel : rel_expr |
|
37 |
val gcd_rel : rel_expr |
|
38 |
val lcm_rel : rel_expr |
|
39 |
val norm_frac_rel : rel_expr |
|
40 |
val atom_for_bool : int -> bool -> rel_expr |
|
41 |
val formula_for_bool : bool -> formula |
|
42 |
val atom_for_nat : int * int -> int -> int |
|
43 |
val min_int_for_card : int -> int |
|
44 |
val max_int_for_card : int -> int |
|
45 |
val int_for_atom : int * int -> int -> int |
|
46 |
val atom_for_int : int * int -> int -> int |
|
47 |
val inline_rel_expr : rel_expr -> bool |
|
48 |
val empty_n_ary_rel : int -> rel_expr |
|
49 |
val num_seq : int -> int -> int_expr list |
|
50 |
val s_and : formula -> formula -> formula |
|
51 |
||
52 |
type kodkod_constrs = { |
|
53 |
kk_all: decl list -> formula -> formula, |
|
54 |
kk_exist: decl list -> formula -> formula, |
|
55 |
kk_formula_let: expr_assign list -> formula -> formula, |
|
56 |
kk_formula_if: formula -> formula -> formula -> formula, |
|
57 |
kk_or: formula -> formula -> formula, |
|
58 |
kk_not: formula -> formula, |
|
59 |
kk_iff: formula -> formula -> formula, |
|
60 |
kk_implies: formula -> formula -> formula, |
|
61 |
kk_and: formula -> formula -> formula, |
|
62 |
kk_subset: rel_expr -> rel_expr -> formula, |
|
63 |
kk_rel_eq: rel_expr -> rel_expr -> formula, |
|
64 |
kk_no: rel_expr -> formula, |
|
65 |
kk_lone: rel_expr -> formula, |
|
66 |
kk_one: rel_expr -> formula, |
|
67 |
kk_some: rel_expr -> formula, |
|
68 |
kk_rel_let: expr_assign list -> rel_expr -> rel_expr, |
|
69 |
kk_rel_if: formula -> rel_expr -> rel_expr -> rel_expr, |
|
70 |
kk_union: rel_expr -> rel_expr -> rel_expr, |
|
71 |
kk_difference: rel_expr -> rel_expr -> rel_expr, |
|
72 |
kk_override: rel_expr -> rel_expr -> rel_expr, |
|
73 |
kk_intersect: rel_expr -> rel_expr -> rel_expr, |
|
74 |
kk_product: rel_expr -> rel_expr -> rel_expr, |
|
75 |
kk_join: rel_expr -> rel_expr -> rel_expr, |
|
76 |
kk_closure: rel_expr -> rel_expr, |
|
77 |
kk_reflexive_closure: rel_expr -> rel_expr, |
|
78 |
kk_comprehension: decl list -> formula -> rel_expr, |
|
79 |
kk_project: rel_expr -> int_expr list -> rel_expr, |
|
80 |
kk_project_seq: rel_expr -> int -> int -> rel_expr, |
|
81 |
kk_not3: rel_expr -> rel_expr, |
|
82 |
kk_nat_less: rel_expr -> rel_expr -> rel_expr, |
|
83 |
kk_int_less: rel_expr -> rel_expr -> rel_expr |
|
84 |
} |
|
85 |
||
86 |
val kodkod_constrs : bool -> int -> int -> int -> kodkod_constrs |
|
87 |
end; |
|
88 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
89 |
structure Nitpick_Peephole : NITPICK_PEEPHOLE = |
33192 | 90 |
struct |
91 |
||
92 |
open Kodkod |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
93 |
open Nitpick_Util |
33192 | 94 |
|
95 |
type name_pool = { |
|
96 |
rels: n_ary_index list, |
|
97 |
vars: n_ary_index list, |
|
98 |
formula_reg: int, |
|
99 |
rel_reg: int} |
|
100 |
||
101 |
(* If you add new built-in relations, make sure to increment the counters here |
|
102 |
as well to avoid name clashes (which fortunately would be detected by |
|
103 |
Kodkodi). *) |
|
104 |
val initial_pool = |
|
105 |
{rels = [(2, 10), (3, 20), (4, 10)], vars = [], formula_reg = 10, |
|
106 |
rel_reg = 10} |
|
107 |
||
108 |
val not3_rel = Rel (2, 0) |
|
109 |
val suc_rel = Rel (2, 1) |
|
110 |
val nat_add_rel = Rel (3, 0) |
|
111 |
val int_add_rel = Rel (3, 1) |
|
112 |
val nat_subtract_rel = Rel (3, 2) |
|
113 |
val int_subtract_rel = Rel (3, 3) |
|
114 |
val nat_multiply_rel = Rel (3, 4) |
|
115 |
val int_multiply_rel = Rel (3, 5) |
|
116 |
val nat_divide_rel = Rel (3, 6) |
|
117 |
val int_divide_rel = Rel (3, 7) |
|
118 |
val nat_modulo_rel = Rel (3, 8) |
|
119 |
val int_modulo_rel = Rel (3, 9) |
|
120 |
val nat_less_rel = Rel (3, 10) |
|
121 |
val int_less_rel = Rel (3, 11) |
|
122 |
val gcd_rel = Rel (3, 12) |
|
123 |
val lcm_rel = Rel (3, 13) |
|
124 |
val norm_frac_rel = Rel (4, 0) |
|
125 |
||
126 |
(* int -> bool -> rel_expr *) |
|
127 |
fun atom_for_bool j0 = Atom o Integer.add j0 o int_for_bool |
|
128 |
(* bool -> formula *) |
|
129 |
fun formula_for_bool b = if b then True else False |
|
130 |
||
131 |
(* int * int -> int -> int *) |
|
132 |
fun atom_for_nat (k, j0) n = if n < 0 orelse n >= k then ~1 else n + j0 |
|
133 |
(* int -> int *) |
|
134 |
fun min_int_for_card k = ~k div 2 + 1 |
|
135 |
fun max_int_for_card k = k div 2 |
|
136 |
(* int * int -> int -> int *) |
|
137 |
fun int_for_atom (k, j0) j = |
|
138 |
let val j = j - j0 in if j <= max_int_for_card k then j else j - k end |
|
139 |
fun atom_for_int (k, j0) n = |
|
140 |
if n < min_int_for_card k orelse n > max_int_for_card k then ~1 |
|
141 |
else if n < 0 then n + k + j0 |
|
142 |
else n + j0 |
|
143 |
||
144 |
(* rel_expr -> bool *) |
|
145 |
fun is_none_product (Product (r1, r2)) = |
|
146 |
is_none_product r1 orelse is_none_product r2 |
|
147 |
| is_none_product None = true |
|
148 |
| is_none_product _ = false |
|
149 |
||
150 |
(* rel_expr -> bool *) |
|
151 |
fun is_one_rel_expr (Atom _) = true |
|
152 |
| is_one_rel_expr (AtomSeq (1, _)) = true |
|
153 |
| is_one_rel_expr (Var _) = true |
|
154 |
| is_one_rel_expr _ = false |
|
155 |
||
156 |
(* rel_expr -> bool *) |
|
157 |
fun inline_rel_expr (Product (r1, r2)) = |
|
158 |
inline_rel_expr r1 andalso inline_rel_expr r2 |
|
159 |
| inline_rel_expr Iden = true |
|
160 |
| inline_rel_expr Ints = true |
|
161 |
| inline_rel_expr None = true |
|
162 |
| inline_rel_expr Univ = true |
|
163 |
| inline_rel_expr (Atom _) = true |
|
164 |
| inline_rel_expr (AtomSeq _) = true |
|
165 |
| inline_rel_expr (Rel _) = true |
|
166 |
| inline_rel_expr (Var _) = true |
|
167 |
| inline_rel_expr (RelReg _) = true |
|
168 |
| inline_rel_expr _ = false |
|
169 |
||
170 |
(* rel_expr -> rel_expr -> bool option *) |
|
171 |
fun rel_expr_equal None (Atom _) = SOME false |
|
172 |
| rel_expr_equal None (AtomSeq (k, _)) = SOME (k = 0) |
|
173 |
| rel_expr_equal (Atom _) None = SOME false |
|
174 |
| rel_expr_equal (AtomSeq (k, _)) None = SOME (k = 0) |
|
175 |
| rel_expr_equal (Atom j1) (Atom j2) = SOME (j1 = j2) |
|
176 |
| rel_expr_equal (Atom j) (AtomSeq (k, j0)) = SOME (j = j0 andalso k = 1) |
|
177 |
| rel_expr_equal (AtomSeq (k, j0)) (Atom j) = SOME (j = j0 andalso k = 1) |
|
178 |
| rel_expr_equal (AtomSeq x1) (AtomSeq x2) = SOME (x1 = x2) |
|
179 |
| rel_expr_equal r1 r2 = if r1 = r2 then SOME true else NONE |
|
180 |
||
181 |
(* rel_expr -> rel_expr -> bool option *) |
|
182 |
fun rel_expr_intersects (Atom j1) (Atom j2) = SOME (j1 = j2) |
|
183 |
| rel_expr_intersects (Atom j) (AtomSeq (k, j0)) = SOME (j < j0 + k) |
|
184 |
| rel_expr_intersects (AtomSeq (k, j0)) (Atom j) = SOME (j < j0 + k) |
|
185 |
| rel_expr_intersects (AtomSeq (k1, j01)) (AtomSeq (k2, j02)) = |
|
186 |
SOME (k1 > 0 andalso k2 > 0 andalso j01 + k1 > j02 andalso j02 + k2 > j01) |
|
187 |
| rel_expr_intersects r1 r2 = |
|
188 |
if is_none_product r1 orelse is_none_product r2 then SOME false else NONE |
|
189 |
||
190 |
(* int -> rel_expr *) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
191 |
fun empty_n_ary_rel 0 = raise ARG ("Nitpick_Peephole.empty_n_ary_rel", "0") |
33192 | 192 |
| empty_n_ary_rel n = funpow (n - 1) (curry Product None) None |
193 |
||
194 |
(* decl -> rel_expr *) |
|
195 |
fun decl_one_set (DeclOne (_, r)) = r |
|
196 |
| decl_one_set _ = |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
197 |
raise ARG ("Nitpick_Peephole.decl_one_set", "not \"DeclOne\"") |
33192 | 198 |
|
199 |
(* int_expr -> bool *) |
|
200 |
fun is_Num (Num _) = true |
|
201 |
| is_Num _ = false |
|
202 |
(* int_expr -> int *) |
|
203 |
fun dest_Num (Num k) = k |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
204 |
| dest_Num _ = raise ARG ("Nitpick_Peephole.dest_Num", "not \"Num\"") |
33192 | 205 |
(* int -> int -> int_expr list *) |
206 |
fun num_seq j0 n = map Num (index_seq j0 n) |
|
207 |
||
208 |
(* rel_expr -> rel_expr -> bool *) |
|
209 |
fun occurs_in_union r (Union (r1, r2)) = |
|
210 |
occurs_in_union r r1 orelse occurs_in_union r r2 |
|
211 |
| occurs_in_union r r' = (r = r') |
|
212 |
||
213 |
(* rel_expr -> rel_expr -> rel_expr *) |
|
214 |
fun s_and True f2 = f2 |
|
215 |
| s_and False _ = False |
|
216 |
| s_and f1 True = f1 |
|
217 |
| s_and _ False = False |
|
218 |
| s_and f1 f2 = And (f1, f2) |
|
219 |
||
220 |
type kodkod_constrs = { |
|
221 |
kk_all: decl list -> formula -> formula, |
|
222 |
kk_exist: decl list -> formula -> formula, |
|
223 |
kk_formula_let: expr_assign list -> formula -> formula, |
|
224 |
kk_formula_if: formula -> formula -> formula -> formula, |
|
225 |
kk_or: formula -> formula -> formula, |
|
226 |
kk_not: formula -> formula, |
|
227 |
kk_iff: formula -> formula -> formula, |
|
228 |
kk_implies: formula -> formula -> formula, |
|
229 |
kk_and: formula -> formula -> formula, |
|
230 |
kk_subset: rel_expr -> rel_expr -> formula, |
|
231 |
kk_rel_eq: rel_expr -> rel_expr -> formula, |
|
232 |
kk_no: rel_expr -> formula, |
|
233 |
kk_lone: rel_expr -> formula, |
|
234 |
kk_one: rel_expr -> formula, |
|
235 |
kk_some: rel_expr -> formula, |
|
236 |
kk_rel_let: expr_assign list -> rel_expr -> rel_expr, |
|
237 |
kk_rel_if: formula -> rel_expr -> rel_expr -> rel_expr, |
|
238 |
kk_union: rel_expr -> rel_expr -> rel_expr, |
|
239 |
kk_difference: rel_expr -> rel_expr -> rel_expr, |
|
240 |
kk_override: rel_expr -> rel_expr -> rel_expr, |
|
241 |
kk_intersect: rel_expr -> rel_expr -> rel_expr, |
|
242 |
kk_product: rel_expr -> rel_expr -> rel_expr, |
|
243 |
kk_join: rel_expr -> rel_expr -> rel_expr, |
|
244 |
kk_closure: rel_expr -> rel_expr, |
|
245 |
kk_reflexive_closure: rel_expr -> rel_expr, |
|
246 |
kk_comprehension: decl list -> formula -> rel_expr, |
|
247 |
kk_project: rel_expr -> int_expr list -> rel_expr, |
|
248 |
kk_project_seq: rel_expr -> int -> int -> rel_expr, |
|
249 |
kk_not3: rel_expr -> rel_expr, |
|
250 |
kk_nat_less: rel_expr -> rel_expr -> rel_expr, |
|
251 |
kk_int_less: rel_expr -> rel_expr -> rel_expr |
|
252 |
} |
|
253 |
||
254 |
(* We assume throughout that Kodkod variables have a "one" constraint. This is |
|
255 |
always the case if Kodkod's skolemization is disabled. *) |
|
256 |
(* bool -> int -> int -> int -> kodkod_constrs *) |
|
257 |
fun kodkod_constrs optim nat_card int_card main_j0 = |
|
258 |
let |
|
259 |
val false_atom = Atom main_j0 |
|
260 |
val true_atom = Atom (main_j0 + 1) |
|
261 |
||
262 |
(* bool -> int *) |
|
263 |
val from_bool = atom_for_bool main_j0 |
|
264 |
(* int -> Kodkod.rel_expr *) |
|
265 |
fun from_nat n = Atom (n + main_j0) |
|
266 |
val from_int = Atom o atom_for_int (int_card, main_j0) |
|
267 |
(* int -> int *) |
|
268 |
fun to_nat j = j - main_j0 |
|
269 |
val to_int = int_for_atom (int_card, main_j0) |
|
270 |
||
271 |
(* decl list -> formula -> formula *) |
|
272 |
fun s_all _ True = True |
|
273 |
| s_all _ False = False |
|
274 |
| s_all [] f = f |
|
275 |
| s_all ds (All (ds', f)) = All (ds @ ds', f) |
|
276 |
| s_all ds f = All (ds, f) |
|
277 |
fun s_exist _ True = True |
|
278 |
| s_exist _ False = False |
|
279 |
| s_exist [] f = f |
|
280 |
| s_exist ds (Exist (ds', f)) = Exist (ds @ ds', f) |
|
281 |
| s_exist ds f = Exist (ds, f) |
|
282 |
||
283 |
(* expr_assign list -> formula -> formula *) |
|
284 |
fun s_formula_let _ True = True |
|
285 |
| s_formula_let _ False = False |
|
286 |
| s_formula_let assigns f = FormulaLet (assigns, f) |
|
287 |
||
288 |
(* formula -> formula *) |
|
289 |
fun s_not True = False |
|
290 |
| s_not False = True |
|
291 |
| s_not (All (ds, f)) = Exist (ds, s_not f) |
|
292 |
| s_not (Exist (ds, f)) = All (ds, s_not f) |
|
293 |
| s_not (Or (f1, f2)) = And (s_not f1, s_not f2) |
|
294 |
| s_not (Implies (f1, f2)) = And (f1, s_not f2) |
|
295 |
| s_not (And (f1, f2)) = Or (s_not f1, s_not f2) |
|
296 |
| s_not (Not f) = f |
|
297 |
| s_not (No r) = Some r |
|
298 |
| s_not (Some r) = No r |
|
299 |
| s_not f = Not f |
|
300 |
||
301 |
(* formula -> formula -> formula *) |
|
302 |
fun s_or True _ = True |
|
303 |
| s_or False f2 = f2 |
|
304 |
| s_or _ True = True |
|
305 |
| s_or f1 False = f1 |
|
306 |
| s_or f1 f2 = if f1 = f2 then f1 else Or (f1, f2) |
|
307 |
fun s_iff True f2 = f2 |
|
308 |
| s_iff False f2 = s_not f2 |
|
309 |
| s_iff f1 True = f1 |
|
310 |
| s_iff f1 False = s_not f1 |
|
311 |
| s_iff f1 f2 = if f1 = f2 then True else Iff (f1, f2) |
|
312 |
fun s_implies True f2 = f2 |
|
313 |
| s_implies False _ = True |
|
314 |
| s_implies _ True = True |
|
315 |
| s_implies f1 False = s_not f1 |
|
316 |
| s_implies f1 f2 = if f1 = f2 then True else Implies (f1, f2) |
|
317 |
||
318 |
(* formula -> formula -> formula -> formula *) |
|
319 |
fun s_formula_if True f2 _ = f2 |
|
320 |
| s_formula_if False _ f3 = f3 |
|
321 |
| s_formula_if f1 True f3 = s_or f1 f3 |
|
322 |
| s_formula_if f1 False f3 = s_and (s_not f1) f3 |
|
323 |
| s_formula_if f1 f2 True = s_implies f1 f2 |
|
324 |
| s_formula_if f1 f2 False = s_and f1 f2 |
|
325 |
| s_formula_if f f1 f2 = FormulaIf (f, f1, f2) |
|
326 |
||
327 |
(* rel_expr -> int_expr list -> rel_expr *) |
|
328 |
fun s_project r is = |
|
329 |
(case r of |
|
330 |
Project (r1, is') => |
|
331 |
if forall is_Num is then |
|
332 |
s_project r1 (map (nth is' o dest_Num) is) |
|
333 |
else |
|
334 |
raise SAME () |
|
335 |
| _ => raise SAME ()) |
|
336 |
handle SAME () => |
|
337 |
let val n = length is in |
|
338 |
if arity_of_rel_expr r = n andalso is = num_seq 0 n then r |
|
339 |
else Project (r, is) |
|
340 |
end |
|
341 |
||
342 |
(* rel_expr -> formula *) |
|
343 |
fun s_no None = True |
|
344 |
| s_no (Product (r1, r2)) = s_or (s_no r1) (s_no r2) |
|
345 |
| s_no (Intersect (Closure (Kodkod.Rel x), Kodkod.Iden)) = Acyclic x |
|
346 |
| s_no r = if is_one_rel_expr r then False else No r |
|
347 |
fun s_lone None = True |
|
348 |
| s_lone r = if is_one_rel_expr r then True else Lone r |
|
349 |
fun s_one None = False |
|
350 |
| s_one r = |
|
351 |
if is_one_rel_expr r then |
|
352 |
True |
|
353 |
else if inline_rel_expr r then |
|
354 |
case arity_of_rel_expr r of |
|
355 |
1 => One r |
|
356 |
| arity => foldl1 And (map (One o s_project r o single o Num) |
|
357 |
(index_seq 0 arity)) |
|
358 |
else |
|
359 |
One r |
|
360 |
fun s_some None = False |
|
361 |
| s_some (Atom _) = True |
|
362 |
| s_some (Product (r1, r2)) = s_and (s_some r1) (s_some r2) |
|
363 |
| s_some r = if is_one_rel_expr r then True else Some r |
|
364 |
||
365 |
(* rel_expr -> rel_expr *) |
|
366 |
fun s_not3 (Atom j) = Atom (if j = main_j0 then j + 1 else j - 1) |
|
367 |
| s_not3 (r as Join (r1, r2)) = |
|
368 |
if r2 = not3_rel then r1 else Join (r, not3_rel) |
|
369 |
| s_not3 r = Join (r, not3_rel) |
|
370 |
||
371 |
(* rel_expr -> rel_expr -> formula *) |
|
372 |
fun s_rel_eq r1 r2 = |
|
373 |
(case (r1, r2) of |
|
374 |
(Join (r11, r12), _) => |
|
375 |
if r12 = not3_rel then s_rel_eq r11 (s_not3 r2) else raise SAME () |
|
376 |
| (_, Join (r21, r22)) => |
|
377 |
if r22 = not3_rel then s_rel_eq r21 (s_not3 r1) else raise SAME () |
|
378 |
| _ => raise SAME ()) |
|
379 |
handle SAME () => |
|
380 |
case rel_expr_equal r1 r2 of |
|
381 |
SOME true => True |
|
382 |
| SOME false => False |
|
383 |
| NONE => |
|
384 |
case (r1, r2) of |
|
385 |
(_, RelIf (f, r21, r22)) => |
|
386 |
if inline_rel_expr r1 then |
|
387 |
s_formula_if f (s_rel_eq r1 r21) (s_rel_eq r1 r22) |
|
388 |
else |
|
389 |
RelEq (r1, r2) |
|
390 |
| (RelIf (f, r11, r12), _) => |
|
391 |
if inline_rel_expr r2 then |
|
392 |
s_formula_if f (s_rel_eq r11 r2) (s_rel_eq r12 r2) |
|
393 |
else |
|
394 |
RelEq (r1, r2) |
|
395 |
| (_, Kodkod.None) => s_no r1 |
|
396 |
| (Kodkod.None, _) => s_no r2 |
|
397 |
| _ => RelEq (r1, r2) |
|
398 |
fun s_subset (Atom j1) (Atom j2) = formula_for_bool (j1 = j2) |
|
399 |
| s_subset (Atom j) (AtomSeq (k, j0)) = |
|
400 |
formula_for_bool (j >= j0 andalso j < j0 + k) |
|
401 |
| s_subset (r1 as Union (r11, r12)) r2 = |
|
402 |
s_and (s_subset r11 r2) (s_subset r12 r2) |
|
403 |
| s_subset r1 (r2 as Union (r21, r22)) = |
|
404 |
if is_one_rel_expr r1 then |
|
405 |
s_or (s_subset r1 r21) (s_subset r1 r22) |
|
406 |
else |
|
407 |
if s_subset r1 r21 = True orelse s_subset r1 r22 = True |
|
408 |
orelse r1 = r2 then |
|
409 |
True |
|
410 |
else |
|
411 |
Subset (r1, r2) |
|
412 |
| s_subset r1 r2 = |
|
413 |
if r1 = r2 orelse is_none_product r1 then True |
|
414 |
else if is_none_product r2 then s_no r1 |
|
415 |
else if forall is_one_rel_expr [r1, r2] then s_rel_eq r1 r2 |
|
416 |
else Subset (r1, r2) |
|
417 |
||
418 |
(* expr_assign list -> rel_expr -> rel_expr *) |
|
419 |
fun s_rel_let [b as AssignRelReg (x', r')] (r as RelReg x) = |
|
420 |
if x = x' then r' else RelLet ([b], r) |
|
421 |
| s_rel_let bs r = RelLet (bs, r) |
|
422 |
||
423 |
(* formula -> rel_expr -> rel_expr -> rel_expr *) |
|
424 |
fun s_rel_if f r1 r2 = |
|
425 |
(case (f, r1, r2) of |
|
426 |
(True, _, _) => r1 |
|
427 |
| (False, _, _) => r2 |
|
428 |
| (No r1', None, RelIf (One r2', r3', r4')) => |
|
429 |
if r1' = r2' andalso r2' = r3' then s_rel_if (Lone r1') r1' r4' |
|
430 |
else raise SAME () |
|
431 |
| _ => raise SAME ()) |
|
432 |
handle SAME () => if r1 = r2 then r1 else RelIf (f, r1, r2) |
|
433 |
||
434 |
(* rel_expr -> rel_expr -> rel_expr *) |
|
435 |
fun s_union r1 (Union (r21, r22)) = s_union (s_union r1 r21) r22 |
|
436 |
| s_union r1 r2 = |
|
437 |
if is_none_product r1 then r2 |
|
438 |
else if is_none_product r2 then r1 |
|
439 |
else if r1 = r2 then r1 |
|
440 |
else if occurs_in_union r2 r1 then r1 |
|
441 |
else Union (r1, r2) |
|
442 |
fun s_difference r1 r2 = |
|
443 |
if is_none_product r1 orelse is_none_product r2 then r1 |
|
444 |
else if r1 = r2 then empty_n_ary_rel (arity_of_rel_expr r1) |
|
445 |
else Difference (r1, r2) |
|
446 |
fun s_override r1 r2 = |
|
447 |
if is_none_product r2 then r1 |
|
448 |
else if is_none_product r1 then r2 |
|
449 |
else Override (r1, r2) |
|
450 |
fun s_intersect r1 r2 = |
|
451 |
case rel_expr_intersects r1 r2 of |
|
452 |
SOME true => if r1 = r2 then r1 else Intersect (r1, r2) |
|
453 |
| SOME false => empty_n_ary_rel (arity_of_rel_expr r1) |
|
454 |
| NONE => if is_none_product r1 then r1 |
|
455 |
else if is_none_product r2 then r2 |
|
456 |
else Intersect (r1, r2) |
|
457 |
fun s_product r1 r2 = |
|
458 |
if is_none_product r1 then |
|
459 |
Product (r1, empty_n_ary_rel (arity_of_rel_expr r2)) |
|
460 |
else if is_none_product r2 then |
|
461 |
Product (empty_n_ary_rel (arity_of_rel_expr r1), r2) |
|
462 |
else |
|
463 |
Product (r1, r2) |
|
464 |
fun s_join r1 (Product (Product (r211, r212), r22)) = |
|
465 |
Product (s_join r1 (Product (r211, r212)), r22) |
|
466 |
| s_join (Product (r11, Product (r121, r122))) r2 = |
|
467 |
Product (r11, s_join (Product (r121, r122)) r2) |
|
468 |
| s_join None r = empty_n_ary_rel (arity_of_rel_expr r - 1) |
|
469 |
| s_join r None = empty_n_ary_rel (arity_of_rel_expr r - 1) |
|
470 |
| s_join (Product (None, None)) r = empty_n_ary_rel (arity_of_rel_expr r) |
|
471 |
| s_join r (Product (None, None)) = empty_n_ary_rel (arity_of_rel_expr r) |
|
472 |
| s_join Iden r2 = r2 |
|
473 |
| s_join r1 Iden = r1 |
|
474 |
| s_join (Product (r1, r2)) Univ = |
|
475 |
if arity_of_rel_expr r2 = 1 then r1 |
|
476 |
else Product (r1, s_join r2 Univ) |
|
477 |
| s_join Univ (Product (r1, r2)) = |
|
478 |
if arity_of_rel_expr r1 = 1 then r2 |
|
479 |
else Product (s_join Univ r1, r2) |
|
480 |
| s_join r1 (r2 as Product (r21, r22)) = |
|
481 |
if arity_of_rel_expr r1 = 1 then |
|
482 |
case rel_expr_intersects r1 r21 of |
|
483 |
SOME true => r22 |
|
484 |
| SOME false => empty_n_ary_rel (arity_of_rel_expr r2 - 1) |
|
485 |
| NONE => Join (r1, r2) |
|
486 |
else |
|
487 |
Join (r1, r2) |
|
488 |
| s_join (r1 as Product (r11, r12)) r2 = |
|
489 |
if arity_of_rel_expr r2 = 1 then |
|
490 |
case rel_expr_intersects r2 r12 of |
|
491 |
SOME true => r11 |
|
492 |
| SOME false => empty_n_ary_rel (arity_of_rel_expr r1 - 1) |
|
493 |
| NONE => Join (r1, r2) |
|
494 |
else |
|
495 |
Join (r1, r2) |
|
496 |
| s_join r1 (r2 as RelIf (f, r21, r22)) = |
|
497 |
if inline_rel_expr r1 then s_rel_if f (s_join r1 r21) (s_join r1 r22) |
|
498 |
else Join (r1, r2) |
|
499 |
| s_join (r1 as RelIf (f, r11, r12)) r2 = |
|
500 |
if inline_rel_expr r2 then s_rel_if f (s_join r11 r2) (s_join r12 r2) |
|
501 |
else Join (r1, r2) |
|
502 |
| s_join (r1 as Atom j1) (r2 as Rel (2, j2)) = |
|
503 |
if r2 = suc_rel then |
|
504 |
let val n = to_nat j1 + 1 in |
|
505 |
if n < nat_card then from_nat n else None |
|
506 |
end |
|
507 |
else |
|
508 |
Join (r1, r2) |
|
509 |
| s_join r1 (r2 as Project (r21, Num k :: is)) = |
|
510 |
if k = arity_of_rel_expr r21 - 1 andalso arity_of_rel_expr r1 = 1 then |
|
511 |
s_project (s_join r21 r1) is |
|
512 |
else |
|
513 |
Join (r1, r2) |
|
514 |
| s_join r1 (Join (r21, r22 as Rel (3, j22))) = |
|
515 |
((if r22 = nat_add_rel then |
|
516 |
case (r21, r1) of |
|
517 |
(Atom j1, Atom j2) => |
|
518 |
let val n = to_nat j1 + to_nat j2 in |
|
519 |
if n < nat_card then from_nat n else None |
|
520 |
end |
|
521 |
| (Atom j, r) => |
|
522 |
(case to_nat j of |
|
523 |
0 => r |
|
524 |
| 1 => s_join r suc_rel |
|
525 |
| _ => raise SAME ()) |
|
526 |
| (r, Atom j) => |
|
527 |
(case to_nat j of |
|
528 |
0 => r |
|
529 |
| 1 => s_join r suc_rel |
|
530 |
| _ => raise SAME ()) |
|
531 |
| _ => raise SAME () |
|
532 |
else if r22 = nat_subtract_rel then |
|
533 |
case (r21, r1) of |
|
33705
947184dc75c9
removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents:
33232
diff
changeset
|
534 |
(Atom j1, Atom j2) => from_nat (nat_minus (to_nat j1) (to_nat j2)) |
33192 | 535 |
| _ => raise SAME () |
536 |
else if r22 = nat_multiply_rel then |
|
537 |
case (r21, r1) of |
|
538 |
(Atom j1, Atom j2) => |
|
539 |
let val n = to_nat j1 * to_nat j2 in |
|
540 |
if n < nat_card then from_nat n else None |
|
541 |
end |
|
542 |
| (Atom j, r) => |
|
543 |
(case to_nat j of 0 => Atom j | 1 => r | _ => raise SAME ()) |
|
544 |
| (r, Atom j) => |
|
545 |
(case to_nat j of 0 => Atom j | 1 => r | _ => raise SAME ()) |
|
546 |
| _ => raise SAME () |
|
547 |
else |
|
548 |
raise SAME ()) |
|
549 |
handle SAME () => List.foldr Join r22 [r1, r21]) |
|
550 |
| s_join r1 r2 = Join (r1, r2) |
|
551 |
||
552 |
(* rel_expr -> rel_expr *) |
|
553 |
fun s_closure Iden = Iden |
|
554 |
| s_closure r = if is_none_product r then r else Closure r |
|
555 |
fun s_reflexive_closure Iden = Iden |
|
556 |
| s_reflexive_closure r = |
|
557 |
if is_none_product r then Iden else ReflexiveClosure r |
|
558 |
||
559 |
(* decl list -> formula -> rel_expr *) |
|
560 |
fun s_comprehension ds False = empty_n_ary_rel (length ds) |
|
561 |
| s_comprehension ds True = fold1 s_product (map decl_one_set ds) |
|
562 |
| s_comprehension [d as DeclOne ((1, j1), r)] |
|
563 |
(f as RelEq (Var (1, j2), Atom j)) = |
|
564 |
if j1 = j2 andalso rel_expr_intersects (Atom j) r = SOME true then |
|
565 |
Atom j |
|
566 |
else |
|
567 |
Comprehension ([d], f) |
|
568 |
| s_comprehension ds f = Comprehension (ds, f) |
|
569 |
||
570 |
(* rel_expr -> int -> int -> rel_expr *) |
|
571 |
fun s_project_seq r = |
|
572 |
let |
|
573 |
(* int -> rel_expr -> int -> int -> rel_expr *) |
|
574 |
fun aux arity r j0 n = |
|
575 |
if j0 = 0 andalso arity = n then |
|
576 |
r |
|
577 |
else case r of |
|
578 |
RelIf (f, r1, r2) => |
|
579 |
s_rel_if f (aux arity r1 j0 n) (aux arity r2 j0 n) |
|
580 |
| Product (r1, r2) => |
|
581 |
let |
|
582 |
val arity2 = arity_of_rel_expr r2 |
|
583 |
val arity1 = arity - arity2 |
|
33705
947184dc75c9
removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents:
33232
diff
changeset
|
584 |
val n1 = Int.min (nat_minus arity1 j0, n) |
33192 | 585 |
val n2 = n - n1 |
586 |
(* unit -> rel_expr *) |
|
587 |
fun one () = aux arity1 r1 j0 n1 |
|
33705
947184dc75c9
removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents:
33232
diff
changeset
|
588 |
fun two () = aux arity2 r2 (nat_minus j0 arity1) n2 |
33192 | 589 |
in |
590 |
case (n1, n2) of |
|
591 |
(0, _) => s_rel_if (s_some r1) (two ()) (empty_n_ary_rel n2) |
|
592 |
| (_, 0) => s_rel_if (s_some r2) (one ()) (empty_n_ary_rel n1) |
|
593 |
| _ => s_product (one ()) (two ()) |
|
594 |
end |
|
595 |
| _ => s_project r (num_seq j0 n) |
|
596 |
in aux (arity_of_rel_expr r) r end |
|
597 |
||
598 |
(* rel_expr -> rel_expr -> rel_expr *) |
|
599 |
fun s_nat_subtract r1 r2 = fold s_join [r1, r2] nat_subtract_rel |
|
600 |
fun s_nat_less (Atom j1) (Atom j2) = from_bool (j1 < j2) |
|
601 |
| s_nat_less r1 r2 = fold s_join [r1, r2] nat_less_rel |
|
602 |
fun s_int_less (Atom j1) (Atom j2) = from_bool (to_int j1 < to_int j2) |
|
603 |
| s_int_less r1 r2 = fold s_join [r1, r2] int_less_rel |
|
604 |
||
605 |
(* rel_expr -> int -> int -> rel_expr *) |
|
606 |
fun d_project_seq r j0 n = Project (r, num_seq j0 n) |
|
607 |
(* rel_expr -> rel_expr *) |
|
608 |
fun d_not3 r = Join (r, not3_rel) |
|
609 |
(* rel_expr -> rel_expr -> rel_expr *) |
|
610 |
fun d_nat_subtract r1 r2 = List.foldl Join nat_subtract_rel [r1, r2] |
|
611 |
fun d_nat_less r1 r2 = List.foldl Join nat_less_rel [r1, r2] |
|
612 |
fun d_int_less r1 r2 = List.foldl Join int_less_rel [r1, r2] |
|
613 |
in |
|
614 |
if optim then |
|
615 |
{kk_all = s_all, kk_exist = s_exist, kk_formula_let = s_formula_let, |
|
616 |
kk_formula_if = s_formula_if, kk_or = s_or, kk_not = s_not, |
|
617 |
kk_iff = s_iff, kk_implies = s_implies, kk_and = s_and, |
|
618 |
kk_subset = s_subset, kk_rel_eq = s_rel_eq, kk_no = s_no, |
|
619 |
kk_lone = s_lone, kk_one = s_one, kk_some = s_some, |
|
620 |
kk_rel_let = s_rel_let, kk_rel_if = s_rel_if, kk_union = s_union, |
|
621 |
kk_difference = s_difference, kk_override = s_override, |
|
622 |
kk_intersect = s_intersect, kk_product = s_product, kk_join = s_join, |
|
623 |
kk_closure = s_closure, kk_reflexive_closure = s_reflexive_closure, |
|
624 |
kk_comprehension = s_comprehension, kk_project = s_project, |
|
625 |
kk_project_seq = s_project_seq, kk_not3 = s_not3, |
|
626 |
kk_nat_less = s_nat_less, kk_int_less = s_int_less} |
|
627 |
else |
|
628 |
{kk_all = curry All, kk_exist = curry Exist, |
|
629 |
kk_formula_let = curry FormulaLet, kk_formula_if = curry3 FormulaIf, |
|
630 |
kk_or = curry Or,kk_not = Not, kk_iff = curry Iff, kk_implies = curry |
|
631 |
Implies, kk_and = curry And, kk_subset = curry Subset, kk_rel_eq = curry |
|
632 |
RelEq, kk_no = No, kk_lone = Lone, kk_one = One, kk_some = Some, |
|
633 |
kk_rel_let = curry RelLet, kk_rel_if = curry3 RelIf, kk_union = curry |
|
634 |
Union, kk_difference = curry Difference, kk_override = curry Override, |
|
635 |
kk_intersect = curry Intersect, kk_product = curry Product, |
|
636 |
kk_join = curry Join, kk_closure = Closure, |
|
637 |
kk_reflexive_closure = ReflexiveClosure, kk_comprehension = curry |
|
638 |
Comprehension, kk_project = curry Project, |
|
639 |
kk_project_seq = d_project_seq, kk_not3 = d_not3, |
|
640 |
kk_nat_less = d_nat_less, kk_int_less = d_int_less} |
|
641 |
end |
|
642 |
||
643 |
end; |