author | blanchet |
Mon, 21 Jun 2010 11:15:21 +0200 | |
changeset 37476 | 0681e46b4022 |
parent 36385 | ff5f88702590 |
child 38121 | a9d96531c2ee |
permissions | -rw-r--r-- |
33982 | 1 |
(* Title: HOL/Tools/Nitpick/nitpick_kodkod.ML |
33192 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
34982
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset
|
3 |
Copyright 2008, 2009, 2010 |
33192 | 4 |
|
5 |
Kodkod problem generator part of Kodkod. |
|
6 |
*) |
|
7 |
||
8 |
signature NITPICK_KODKOD = |
|
9 |
sig |
|
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
10 |
type hol_context = Nitpick_HOL.hol_context |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
11 |
type dtype_spec = Nitpick_Scope.dtype_spec |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
12 |
type kodkod_constrs = Nitpick_Peephole.kodkod_constrs |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
13 |
type nut = Nitpick_Nut.nut |
33192 | 14 |
type nfa_transition = Kodkod.rel_expr * typ |
15 |
type nfa_entry = typ * nfa_transition list |
|
16 |
type nfa_table = nfa_entry list |
|
17 |
||
18 |
structure NameTable : TABLE |
|
19 |
||
20 |
val univ_card : |
|
21 |
int -> int -> int -> Kodkod.bound list -> Kodkod.formula -> int |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
22 |
val check_bits : int -> Kodkod.formula -> unit |
33192 | 23 |
val check_arity : int -> int -> unit |
24 |
val kk_tuple : bool -> int -> int list -> Kodkod.tuple |
|
25 |
val tuple_set_from_atom_schema : (int * int) list -> Kodkod.tuple_set |
|
26 |
val sequential_int_bounds : int -> Kodkod.int_bound list |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
27 |
val pow_of_two_int_bounds : int -> int -> Kodkod.int_bound list |
33192 | 28 |
val bounds_for_built_in_rels_in_formula : |
29 |
bool -> int -> int -> int -> int -> Kodkod.formula -> Kodkod.bound list |
|
30 |
val bound_for_plain_rel : Proof.context -> bool -> nut -> Kodkod.bound |
|
31 |
val bound_for_sel_rel : |
|
32 |
Proof.context -> bool -> dtype_spec list -> nut -> Kodkod.bound |
|
33 |
val merge_bounds : Kodkod.bound list -> Kodkod.bound list |
|
34 |
val declarative_axiom_for_plain_rel : kodkod_constrs -> nut -> Kodkod.formula |
|
35 |
val declarative_axioms_for_datatypes : |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
36 |
hol_context -> bool -> int -> int Typtab.table -> kodkod_constrs |
33192 | 37 |
-> nut NameTable.table -> dtype_spec list -> Kodkod.formula list |
38 |
val kodkod_formula_from_nut : |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
39 |
int Typtab.table -> kodkod_constrs -> nut -> Kodkod.formula |
33192 | 40 |
end; |
41 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
42 |
structure Nitpick_Kodkod : NITPICK_KODKOD = |
33192 | 43 |
struct |
44 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
45 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
46 |
open Nitpick_HOL |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
47 |
open Nitpick_Scope |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
48 |
open Nitpick_Peephole |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
49 |
open Nitpick_Rep |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
50 |
open Nitpick_Nut |
33192 | 51 |
|
34126 | 52 |
structure KK = Kodkod |
53 |
||
54 |
type nfa_transition = KK.rel_expr * typ |
|
33192 | 55 |
type nfa_entry = typ * nfa_transition list |
56 |
type nfa_table = nfa_entry list |
|
57 |
||
35407 | 58 |
structure NfaGraph = Typ_Graph |
33192 | 59 |
|
34126 | 60 |
fun flip_nums n = index_seq 1 n @ [0] |> map KK.Num |
33192 | 61 |
|
62 |
fun univ_card nat_card int_card main_j0 bounds formula = |
|
63 |
let |
|
64 |
fun rel_expr_func r k = |
|
65 |
Int.max (k, case r of |
|
34126 | 66 |
KK.Atom j => j + 1 |
67 |
| KK.AtomSeq (k', j0) => j0 + k' |
|
33192 | 68 |
| _ => 0) |
69 |
fun tuple_func t k = |
|
70 |
case t of |
|
34126 | 71 |
KK.Tuple js => fold Integer.max (map (Integer.add 1) js) k |
33192 | 72 |
| _ => k |
73 |
fun tuple_set_func ts k = |
|
34126 | 74 |
Int.max (k, case ts of KK.TupleAtomSeq (k', j0) => j0 + k' | _ => 0) |
33192 | 75 |
val expr_F = {formula_func = K I, rel_expr_func = rel_expr_func, |
76 |
int_expr_func = K I} |
|
77 |
val tuple_F = {tuple_func = tuple_func, tuple_set_func = tuple_set_func} |
|
34126 | 78 |
val card = fold (KK.fold_bound expr_F tuple_F) bounds 1 |
79 |
|> KK.fold_formula expr_F formula |
|
33192 | 80 |
in Int.max (main_j0 + fold Integer.max [2, nat_card, int_card] 0, card) end |
81 |
||
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
82 |
fun check_bits bits formula = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
83 |
let |
34126 | 84 |
fun int_expr_func (KK.Num k) () = |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
85 |
if is_twos_complement_representable bits k then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
86 |
() |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
87 |
else |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
88 |
raise TOO_SMALL ("Nitpick_Kodkod.check_bits", |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
89 |
"\"bits\" value " ^ string_of_int bits ^ |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
90 |
" too small for problem") |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
91 |
| int_expr_func _ () = () |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
92 |
val expr_F = {formula_func = K I, rel_expr_func = K I, |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
93 |
int_expr_func = int_expr_func} |
34126 | 94 |
in KK.fold_formula expr_F formula () end |
33192 | 95 |
|
96 |
fun check_arity univ_card n = |
|
34126 | 97 |
if n > KK.max_arity univ_card then |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
98 |
raise TOO_LARGE ("Nitpick_Kodkod.check_arity", |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
99 |
"arity " ^ string_of_int n ^ " too large for universe of \ |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
100 |
\cardinality " ^ string_of_int univ_card) |
33192 | 101 |
else |
102 |
() |
|
103 |
||
104 |
fun kk_tuple debug univ_card js = |
|
105 |
if debug then |
|
34126 | 106 |
KK.Tuple js |
33192 | 107 |
else |
34126 | 108 |
KK.TupleIndex (length js, |
109 |
fold (fn j => fn accum => accum * univ_card + j) js 0) |
|
33192 | 110 |
|
34126 | 111 |
val tuple_set_from_atom_schema = foldl1 KK.TupleProduct o map KK.TupleAtomSeq |
33192 | 112 |
val upper_bound_for_rep = tuple_set_from_atom_schema o atom_schema_of_rep |
113 |
||
34126 | 114 |
val single_atom = KK.TupleSet o single o KK.Tuple o single |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
115 |
fun sequential_int_bounds n = [(NONE, map single_atom (index_seq 0 n))] |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
116 |
fun pow_of_two_int_bounds bits j0 = |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
117 |
let |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
118 |
fun aux 0 _ _ = [] |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
119 |
| aux 1 pow_of_two j = [(SOME (~ pow_of_two), [single_atom j])] |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
120 |
| aux iter pow_of_two j = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
121 |
(SOME pow_of_two, [single_atom j]) :: |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
122 |
aux (iter - 1) (2 * pow_of_two) (j + 1) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
123 |
in aux (bits + 1) 1 j0 end |
33192 | 124 |
|
125 |
fun built_in_rels_in_formula formula = |
|
126 |
let |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
127 |
fun rel_expr_func (KK.Rel (x as (n, j))) = |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
128 |
if x = unsigned_bit_word_sel_rel orelse x = signed_bit_word_sel_rel then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
129 |
I |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
130 |
else |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
131 |
(case AList.lookup (op =) (#rels initial_pool) n of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
132 |
SOME k => j < k ? insert (op =) x |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
133 |
| NONE => I) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
134 |
| rel_expr_func _ = I |
33192 | 135 |
val expr_F = {formula_func = K I, rel_expr_func = rel_expr_func, |
136 |
int_expr_func = K I} |
|
34126 | 137 |
in KK.fold_formula expr_F formula [] end |
33192 | 138 |
|
139 |
val max_table_size = 65536 |
|
140 |
||
141 |
fun check_table_size k = |
|
142 |
if k > max_table_size then |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
143 |
raise TOO_LARGE ("Nitpick_Kodkod.check_table_size", |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
144 |
"precomputed table too large (" ^ string_of_int k ^ ")") |
33192 | 145 |
else |
146 |
() |
|
147 |
||
148 |
fun tabulate_func1 debug univ_card (k, j0) f = |
|
149 |
(check_table_size k; |
|
150 |
map_filter (fn j1 => let val j2 = f j1 in |
|
151 |
if j2 >= 0 then |
|
152 |
SOME (kk_tuple debug univ_card [j1 + j0, j2 + j0]) |
|
153 |
else |
|
154 |
NONE |
|
155 |
end) (index_seq 0 k)) |
|
156 |
fun tabulate_op2 debug univ_card (k, j0) res_j0 f = |
|
157 |
(check_table_size (k * k); |
|
158 |
map_filter (fn j => let |
|
159 |
val j1 = j div k |
|
160 |
val j2 = j - j1 * k |
|
161 |
val j3 = f (j1, j2) |
|
162 |
in |
|
163 |
if j3 >= 0 then |
|
164 |
SOME (kk_tuple debug univ_card |
|
165 |
[j1 + j0, j2 + j0, j3 + res_j0]) |
|
166 |
else |
|
167 |
NONE |
|
168 |
end) (index_seq 0 (k * k))) |
|
169 |
fun tabulate_op2_2 debug univ_card (k, j0) res_j0 f = |
|
170 |
(check_table_size (k * k); |
|
171 |
map_filter (fn j => let |
|
172 |
val j1 = j div k |
|
173 |
val j2 = j - j1 * k |
|
174 |
val (j3, j4) = f (j1, j2) |
|
175 |
in |
|
176 |
if j3 >= 0 andalso j4 >= 0 then |
|
177 |
SOME (kk_tuple debug univ_card |
|
178 |
[j1 + j0, j2 + j0, j3 + res_j0, |
|
179 |
j4 + res_j0]) |
|
180 |
else |
|
181 |
NONE |
|
182 |
end) (index_seq 0 (k * k))) |
|
183 |
fun tabulate_nat_op2 debug univ_card (k, j0) f = |
|
184 |
tabulate_op2 debug univ_card (k, j0) j0 (atom_for_nat (k, 0) o f) |
|
185 |
fun tabulate_int_op2 debug univ_card (k, j0) f = |
|
186 |
tabulate_op2 debug univ_card (k, j0) j0 |
|
187 |
(atom_for_int (k, 0) o f o pairself (int_for_atom (k, 0))) |
|
188 |
fun tabulate_int_op2_2 debug univ_card (k, j0) f = |
|
189 |
tabulate_op2_2 debug univ_card (k, j0) j0 |
|
190 |
(pairself (atom_for_int (k, 0)) o f |
|
191 |
o pairself (int_for_atom (k, 0))) |
|
192 |
||
193 |
fun isa_div (m, n) = m div n handle General.Div => 0 |
|
194 |
fun isa_mod (m, n) = m mod n handle General.Div => m |
|
195 |
fun isa_gcd (m, 0) = m |
|
196 |
| isa_gcd (m, n) = isa_gcd (n, isa_mod (m, n)) |
|
197 |
fun isa_lcm (m, n) = isa_div (m * n, isa_gcd (m, n)) |
|
198 |
val isa_zgcd = isa_gcd o pairself abs |
|
199 |
fun isa_norm_frac (m, n) = |
|
200 |
if n < 0 then isa_norm_frac (~m, ~n) |
|
201 |
else if m = 0 orelse n = 0 then (0, 1) |
|
202 |
else let val p = isa_zgcd (m, n) in (isa_div (m, p), isa_div (n, p)) end |
|
203 |
||
204 |
fun tabulate_built_in_rel debug univ_card nat_card int_card j0 (x as (n, _)) = |
|
205 |
(check_arity univ_card n; |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
206 |
if x = not3_rel then |
33192 | 207 |
("not3", tabulate_func1 debug univ_card (2, j0) (curry (op -) 1)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
208 |
else if x = suc_rel then |
33192 | 209 |
("suc", tabulate_func1 debug univ_card (univ_card - j0 - 1, j0) |
210 |
(Integer.add 1)) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
211 |
else if x = nat_add_rel then |
33192 | 212 |
("nat_add", tabulate_nat_op2 debug univ_card (nat_card, j0) (op +)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
213 |
else if x = int_add_rel then |
33192 | 214 |
("int_add", tabulate_int_op2 debug univ_card (int_card, j0) (op +)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
215 |
else if x = nat_subtract_rel then |
33192 | 216 |
("nat_subtract", |
33705
947184dc75c9
removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents:
33631
diff
changeset
|
217 |
tabulate_op2 debug univ_card (nat_card, j0) j0 (uncurry nat_minus)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
218 |
else if x = int_subtract_rel then |
33192 | 219 |
("int_subtract", tabulate_int_op2 debug univ_card (int_card, j0) (op -)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
220 |
else if x = nat_multiply_rel then |
33192 | 221 |
("nat_multiply", tabulate_nat_op2 debug univ_card (nat_card, j0) (op * )) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
222 |
else if x = int_multiply_rel then |
33192 | 223 |
("int_multiply", tabulate_int_op2 debug univ_card (int_card, j0) (op * )) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
224 |
else if x = nat_divide_rel then |
33192 | 225 |
("nat_divide", tabulate_nat_op2 debug univ_card (nat_card, j0) isa_div) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
226 |
else if x = int_divide_rel then |
33192 | 227 |
("int_divide", tabulate_int_op2 debug univ_card (int_card, j0) isa_div) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
228 |
else if x = nat_less_rel then |
33192 | 229 |
("nat_less", tabulate_nat_op2 debug univ_card (nat_card, j0) |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35312
diff
changeset
|
230 |
(int_from_bool o op <)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
231 |
else if x = int_less_rel then |
33192 | 232 |
("int_less", tabulate_int_op2 debug univ_card (int_card, j0) |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35312
diff
changeset
|
233 |
(int_from_bool o op <)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
234 |
else if x = gcd_rel then |
33192 | 235 |
("gcd", tabulate_nat_op2 debug univ_card (nat_card, j0) isa_gcd) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
236 |
else if x = lcm_rel then |
33192 | 237 |
("lcm", tabulate_nat_op2 debug univ_card (nat_card, j0) isa_lcm) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
238 |
else if x = norm_frac_rel then |
33192 | 239 |
("norm_frac", tabulate_int_op2_2 debug univ_card (int_card, j0) |
240 |
isa_norm_frac) |
|
241 |
else |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
242 |
raise ARG ("Nitpick_Kodkod.tabulate_built_in_rel", "unknown relation")) |
33192 | 243 |
|
244 |
fun bound_for_built_in_rel debug univ_card nat_card int_card j0 x = |
|
245 |
let |
|
246 |
val (nick, ts) = tabulate_built_in_rel debug univ_card nat_card int_card |
|
247 |
j0 x |
|
34126 | 248 |
in ([(x, nick)], [KK.TupleSet ts]) end |
33192 | 249 |
|
250 |
fun bounds_for_built_in_rels_in_formula debug univ_card nat_card int_card j0 = |
|
251 |
map (bound_for_built_in_rel debug univ_card nat_card int_card j0) |
|
252 |
o built_in_rels_in_formula |
|
253 |
||
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
254 |
fun bound_comment ctxt debug nick T R = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
255 |
short_name nick ^ |
34982
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset
|
256 |
(if debug then " :: " ^ unyxml (Syntax.string_of_typ ctxt T) else "") ^ |
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset
|
257 |
" : " ^ string_for_rep R |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
258 |
|
33192 | 259 |
fun bound_for_plain_rel ctxt debug (u as FreeRel (x, T, R, nick)) = |
260 |
([(x, bound_comment ctxt debug nick T R)], |
|
261 |
if nick = @{const_name bisim_iterator_max} then |
|
262 |
case R of |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
263 |
Atom (k, j0) => [single_atom (k - 1 + j0)] |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
264 |
| _ => raise NUT ("Nitpick_Kodkod.bound_for_plain_rel", [u]) |
33192 | 265 |
else |
34126 | 266 |
[KK.TupleSet [], upper_bound_for_rep R]) |
33192 | 267 |
| bound_for_plain_rel _ _ u = |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
268 |
raise NUT ("Nitpick_Kodkod.bound_for_plain_rel", [u]) |
33192 | 269 |
|
270 |
fun bound_for_sel_rel ctxt debug dtypes |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
271 |
(FreeRel (x, T as Type (@{type_name fun}, [T1, T2]), |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
272 |
R as Func (Atom (_, j0), R2), nick)) = |
33192 | 273 |
let |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
274 |
val {delta, epsilon, exclusive, explicit_max, ...} = |
33192 | 275 |
constr_spec dtypes (original_name nick, T1) |
276 |
in |
|
277 |
([(x, bound_comment ctxt debug nick T R)], |
|
278 |
if explicit_max = 0 then |
|
34126 | 279 |
[KK.TupleSet []] |
33192 | 280 |
else |
34126 | 281 |
let val ts = KK.TupleAtomSeq (epsilon - delta, delta + j0) in |
33192 | 282 |
if R2 = Formula Neut then |
34126 | 283 |
[ts] |> not exclusive ? cons (KK.TupleSet []) |
33192 | 284 |
else |
35072
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
285 |
[KK.TupleSet [], |
35178 | 286 |
if T1 = T2 andalso epsilon > delta andalso |
35179
4b198af5beb5
redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents:
35178
diff
changeset
|
287 |
(datatype_spec dtypes T1 |> the |> pairf #co #standard) |
4b198af5beb5
redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents:
35178
diff
changeset
|
288 |
= (false, true) then |
35072
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
289 |
index_seq delta (epsilon - delta) |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
290 |
|> map (fn j => |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
291 |
KK.TupleProduct (KK.TupleSet [Kodkod.Tuple [j + j0]], |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
292 |
KK.TupleAtomSeq (j, j0))) |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
293 |
|> foldl1 KK.TupleUnion |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
294 |
else |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
295 |
KK.TupleProduct (ts, upper_bound_for_rep R2)] |
33192 | 296 |
end) |
297 |
end |
|
298 |
| bound_for_sel_rel _ _ _ u = |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
299 |
raise NUT ("Nitpick_Kodkod.bound_for_sel_rel", [u]) |
33192 | 300 |
|
301 |
fun merge_bounds bs = |
|
302 |
let |
|
303 |
fun arity (zs, _) = fst (fst (hd zs)) |
|
304 |
fun add_bound ds b [] = List.revAppend (ds, [b]) |
|
305 |
| add_bound ds b (c :: cs) = |
|
306 |
if arity b = arity c andalso snd b = snd c then |
|
307 |
List.revAppend (ds, (fst c @ fst b, snd c) :: cs) |
|
308 |
else |
|
309 |
add_bound (c :: ds) b cs |
|
310 |
in fold (add_bound []) bs [] end |
|
311 |
||
34126 | 312 |
fun unary_var_seq j0 n = map (curry KK.Var 1) (index_seq j0 n) |
33192 | 313 |
|
34126 | 314 |
val singleton_from_combination = foldl1 KK.Product o map KK.Atom |
33192 | 315 |
fun all_singletons_for_rep R = |
316 |
if is_lone_rep R then |
|
317 |
all_combinations_for_rep R |> map singleton_from_combination |
|
318 |
else |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
319 |
raise REP ("Nitpick_Kodkod.all_singletons_for_rep", [R]) |
33192 | 320 |
|
34126 | 321 |
fun unpack_products (KK.Product (r1, r2)) = |
33192 | 322 |
unpack_products r1 @ unpack_products r2 |
323 |
| unpack_products r = [r] |
|
34126 | 324 |
fun unpack_joins (KK.Join (r1, r2)) = unpack_joins r1 @ unpack_joins r2 |
33192 | 325 |
| unpack_joins r = [r] |
326 |
||
327 |
val empty_rel_for_rep = empty_n_ary_rel o arity_of_rep |
|
328 |
fun full_rel_for_rep R = |
|
329 |
case atom_schema_of_rep R of |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
330 |
[] => raise REP ("Nitpick_Kodkod.full_rel_for_rep", [R]) |
34126 | 331 |
| schema => foldl1 KK.Product (map KK.AtomSeq schema) |
33192 | 332 |
|
333 |
fun decls_for_atom_schema j0 schema = |
|
34126 | 334 |
map2 (fn j => fn x => KK.DeclOne ((1, j), KK.AtomSeq x)) |
33192 | 335 |
(index_seq j0 (length schema)) schema |
336 |
||
337 |
(* The type constraint below is a workaround for a Poly/ML bug. *) |
|
338 |
||
339 |
fun d_n_ary_function ({kk_all, kk_join, kk_lone, kk_one, ...} : kodkod_constrs) |
|
340 |
R r = |
|
341 |
let val body_R = body_rep R in |
|
342 |
if is_lone_rep body_R then |
|
343 |
let |
|
344 |
val binder_schema = atom_schema_of_reps (binder_reps R) |
|
345 |
val body_schema = atom_schema_of_rep body_R |
|
346 |
val one = is_one_rep body_R |
|
34126 | 347 |
val opt_x = case r of KK.Rel x => SOME x | _ => NONE |
33192 | 348 |
in |
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
349 |
if opt_x <> NONE andalso length binder_schema = 1 andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
350 |
length body_schema = 1 then |
34126 | 351 |
(if one then KK.Function else KK.Functional) |
352 |
(the opt_x, KK.AtomSeq (hd binder_schema), |
|
353 |
KK.AtomSeq (hd body_schema)) |
|
33192 | 354 |
else |
355 |
let |
|
356 |
val decls = decls_for_atom_schema ~1 binder_schema |
|
357 |
val vars = unary_var_seq ~1 (length binder_schema) |
|
358 |
val kk_xone = if one then kk_one else kk_lone |
|
359 |
in kk_all decls (kk_xone (fold kk_join vars r)) end |
|
360 |
end |
|
361 |
else |
|
34126 | 362 |
KK.True |
33192 | 363 |
end |
34126 | 364 |
fun kk_n_ary_function kk R (r as KK.Rel x) = |
33192 | 365 |
if not (is_opt_rep R) then |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
366 |
if x = suc_rel then |
34126 | 367 |
KK.False |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
368 |
else if x = nat_add_rel then |
33192 | 369 |
formula_for_bool (card_of_rep (body_rep R) = 1) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
370 |
else if x = nat_multiply_rel then |
33192 | 371 |
formula_for_bool (card_of_rep (body_rep R) <= 2) |
372 |
else |
|
373 |
d_n_ary_function kk R r |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
374 |
else if x = nat_subtract_rel then |
34126 | 375 |
KK.True |
33192 | 376 |
else |
377 |
d_n_ary_function kk R r |
|
378 |
| kk_n_ary_function kk R r = d_n_ary_function kk R r |
|
379 |
||
34126 | 380 |
fun kk_disjoint_sets _ [] = KK.True |
33192 | 381 |
| kk_disjoint_sets (kk as {kk_and, kk_no, kk_intersect, ...} : kodkod_constrs) |
382 |
(r :: rs) = |
|
383 |
fold (kk_and o kk_no o kk_intersect r) rs (kk_disjoint_sets kk rs) |
|
384 |
||
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
385 |
fun basic_rel_rel_let j ({kk_rel_let, ...} : kodkod_constrs) f r = |
33192 | 386 |
if inline_rel_expr r then |
387 |
f r |
|
388 |
else |
|
34126 | 389 |
let val x = (KK.arity_of_rel_expr r, j) in |
390 |
kk_rel_let [KK.AssignRelReg (x, r)] (f (KK.RelReg x)) |
|
33192 | 391 |
end |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
392 |
val single_rel_rel_let = basic_rel_rel_let 0 |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
393 |
fun double_rel_rel_let kk f r1 r2 = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
394 |
single_rel_rel_let kk (fn r1 => basic_rel_rel_let 1 kk (f r1) r2) r1 |
35386
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset
|
395 |
fun triple_rel_rel_let kk f r1 r2 r3 = |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
396 |
double_rel_rel_let kk |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
397 |
(fn r1 => fn r2 => basic_rel_rel_let 2 kk (f r1 r2) r3) r1 r2 |
33192 | 398 |
|
399 |
fun atom_from_formula ({kk_rel_if, ...} : kodkod_constrs) j0 f = |
|
34126 | 400 |
kk_rel_if f (KK.Atom (j0 + 1)) (KK.Atom j0) |
33192 | 401 |
fun rel_expr_from_formula kk R f = |
402 |
case unopt_rep R of |
|
403 |
Atom (2, j0) => atom_from_formula kk j0 f |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
404 |
| _ => raise REP ("Nitpick_Kodkod.rel_expr_from_formula", [R]) |
33192 | 405 |
|
406 |
fun unpack_vect_in_chunks ({kk_project_seq, ...} : kodkod_constrs) chunk_arity |
|
407 |
num_chunks r = |
|
408 |
List.tabulate (num_chunks, fn j => kk_project_seq r (j * chunk_arity) |
|
409 |
chunk_arity) |
|
410 |
||
411 |
fun kk_n_fold_join |
|
412 |
(kk as {kk_intersect, kk_product, kk_join, kk_project_seq, ...}) one R1 |
|
413 |
res_R r1 r2 = |
|
414 |
case arity_of_rep R1 of |
|
415 |
1 => kk_join r1 r2 |
|
416 |
| arity1 => |
|
417 |
let |
|
418 |
val unpacked_rs1 = |
|
419 |
if inline_rel_expr r1 then unpack_vect_in_chunks kk 1 arity1 r1 |
|
420 |
else unpack_products r1 |
|
421 |
in |
|
422 |
if one andalso length unpacked_rs1 = arity1 then |
|
423 |
fold kk_join unpacked_rs1 r2 |
|
424 |
else |
|
425 |
kk_project_seq |
|
426 |
(kk_intersect (kk_product r1 (full_rel_for_rep res_R)) r2) |
|
427 |
arity1 (arity_of_rep res_R) |
|
428 |
end |
|
429 |
||
430 |
fun kk_case_switch (kk as {kk_union, kk_product, ...}) R1 R2 r rs1 rs2 = |
|
431 |
if rs1 = rs2 then r |
|
432 |
else kk_n_fold_join kk true R1 R2 r (fold1 kk_union (map2 kk_product rs1 rs2)) |
|
433 |
||
434 |
val lone_rep_fallback_max_card = 4096 |
|
435 |
val some_j0 = 0 |
|
436 |
||
437 |
fun lone_rep_fallback kk new_R old_R r = |
|
438 |
if old_R = new_R then |
|
439 |
r |
|
440 |
else |
|
441 |
let val card = card_of_rep old_R in |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
442 |
if is_lone_rep old_R andalso is_lone_rep new_R andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
443 |
card = card_of_rep new_R then |
33192 | 444 |
if card >= lone_rep_fallback_max_card then |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
445 |
raise TOO_LARGE ("Nitpick_Kodkod.lone_rep_fallback", |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
446 |
"too high cardinality (" ^ string_of_int card ^ ")") |
33192 | 447 |
else |
448 |
kk_case_switch kk old_R new_R r (all_singletons_for_rep old_R) |
|
449 |
(all_singletons_for_rep new_R) |
|
450 |
else |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
451 |
raise REP ("Nitpick_Kodkod.lone_rep_fallback", [old_R, new_R]) |
33192 | 452 |
end |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
453 |
and atom_from_rel_expr kk x old_R r = |
33192 | 454 |
case old_R of |
455 |
Func (R1, R2) => |
|
456 |
let |
|
457 |
val dom_card = card_of_rep R1 |
|
458 |
val R2' = case R2 of Atom _ => R2 | _ => Atom (card_of_rep R2, some_j0) |
|
459 |
in |
|
460 |
atom_from_rel_expr kk x (Vect (dom_card, R2')) |
|
461 |
(vect_from_rel_expr kk dom_card R2' old_R r) |
|
462 |
end |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
463 |
| Opt _ => raise REP ("Nitpick_Kodkod.atom_from_rel_expr", [old_R]) |
33192 | 464 |
| _ => lone_rep_fallback kk (Atom x) old_R r |
465 |
and struct_from_rel_expr kk Rs old_R r = |
|
466 |
case old_R of |
|
467 |
Atom _ => lone_rep_fallback kk (Struct Rs) old_R r |
|
468 |
| Struct Rs' => |
|
469 |
let |
|
470 |
val Rs = filter (not_equal Unit) Rs |
|
471 |
val Rs' = filter (not_equal Unit) Rs' |
|
472 |
in |
|
473 |
if Rs' = Rs then |
|
474 |
r |
|
475 |
else if map card_of_rep Rs' = map card_of_rep Rs then |
|
476 |
let |
|
477 |
val old_arities = map arity_of_rep Rs' |
|
478 |
val old_offsets = offset_list old_arities |
|
479 |
val old_rs = map2 (#kk_project_seq kk r) old_offsets old_arities |
|
480 |
in |
|
481 |
fold1 (#kk_product kk) |
|
482 |
(map3 (rel_expr_from_rel_expr kk) Rs Rs' old_rs) |
|
483 |
end |
|
484 |
else |
|
485 |
lone_rep_fallback kk (Struct Rs) old_R r |
|
486 |
end |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
487 |
| _ => raise REP ("Nitpick_Kodkod.struct_from_rel_expr", [old_R]) |
33192 | 488 |
and vect_from_rel_expr kk k R old_R r = |
489 |
case old_R of |
|
490 |
Atom _ => lone_rep_fallback kk (Vect (k, R)) old_R r |
|
491 |
| Vect (k', R') => |
|
492 |
if k = k' andalso R = R' then r |
|
493 |
else lone_rep_fallback kk (Vect (k, R)) old_R r |
|
494 |
| Func (R1, Formula Neut) => |
|
495 |
if k = card_of_rep R1 then |
|
496 |
fold1 (#kk_product kk) |
|
497 |
(map (fn arg_r => |
|
498 |
rel_expr_from_formula kk R (#kk_subset kk arg_r r)) |
|
499 |
(all_singletons_for_rep R1)) |
|
500 |
else |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
501 |
raise REP ("Nitpick_Kodkod.vect_from_rel_expr", [old_R]) |
33192 | 502 |
| Func (Unit, R2) => rel_expr_from_rel_expr kk R R2 r |
503 |
| Func (R1, R2) => |
|
504 |
fold1 (#kk_product kk) |
|
505 |
(map (fn arg_r => |
|
506 |
rel_expr_from_rel_expr kk R R2 |
|
507 |
(kk_n_fold_join kk true R1 R2 arg_r r)) |
|
508 |
(all_singletons_for_rep R1)) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
509 |
| _ => raise REP ("Nitpick_Kodkod.vect_from_rel_expr", [old_R]) |
33192 | 510 |
and func_from_no_opt_rel_expr kk R1 R2 (Atom x) r = |
511 |
let |
|
512 |
val dom_card = card_of_rep R1 |
|
513 |
val R2' = case R2 of Atom _ => R2 | _ => Atom (card_of_rep R2, some_j0) |
|
514 |
in |
|
515 |
func_from_no_opt_rel_expr kk R1 R2 (Vect (dom_card, R2')) |
|
516 |
(vect_from_rel_expr kk dom_card R2' (Atom x) r) |
|
517 |
end |
|
518 |
| func_from_no_opt_rel_expr kk Unit R2 old_R r = |
|
519 |
(case old_R of |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
520 |
Vect (_, R') => rel_expr_from_rel_expr kk R2 R' r |
33192 | 521 |
| Func (Unit, R2') => rel_expr_from_rel_expr kk R2 R2' r |
522 |
| Func (Atom (1, _), Formula Neut) => |
|
523 |
(case unopt_rep R2 of |
|
524 |
Atom (2, j0) => atom_from_formula kk j0 (#kk_some kk r) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
525 |
| _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr", |
33192 | 526 |
[old_R, Func (Unit, R2)])) |
527 |
| Func (R1', R2') => |
|
528 |
rel_expr_from_rel_expr kk R2 R2' (#kk_project_seq kk r (arity_of_rep R1') |
|
529 |
(arity_of_rep R2')) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
530 |
| _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr", |
33192 | 531 |
[old_R, Func (Unit, R2)])) |
532 |
| func_from_no_opt_rel_expr kk R1 (Formula Neut) old_R r = |
|
533 |
(case old_R of |
|
534 |
Vect (k, Atom (2, j0)) => |
|
535 |
let |
|
536 |
val args_rs = all_singletons_for_rep R1 |
|
537 |
val vals_rs = unpack_vect_in_chunks kk 1 k r |
|
538 |
fun empty_or_singleton_set_for arg_r val_r = |
|
34126 | 539 |
#kk_join kk val_r (#kk_product kk (KK.Atom (j0 + 1)) arg_r) |
33192 | 540 |
in |
541 |
fold1 (#kk_union kk) (map2 empty_or_singleton_set_for args_rs vals_rs) |
|
542 |
end |
|
543 |
| Func (R1', Formula Neut) => |
|
544 |
if R1 = R1' then |
|
545 |
r |
|
546 |
else |
|
547 |
let |
|
548 |
val schema = atom_schema_of_rep R1 |
|
549 |
val r1 = fold1 (#kk_product kk) (unary_var_seq ~1 (length schema)) |
|
550 |
|> rel_expr_from_rel_expr kk R1' R1 |
|
33582
bdf98e327f0b
fixed soundness bug in Nitpick related to sets of sets;
blanchet
parents:
33571
diff
changeset
|
551 |
val kk_xeq = (if is_one_rep R1' then #kk_subset else #kk_rel_eq) kk |
33192 | 552 |
in |
33582
bdf98e327f0b
fixed soundness bug in Nitpick related to sets of sets;
blanchet
parents:
33571
diff
changeset
|
553 |
#kk_comprehension kk (decls_for_atom_schema ~1 schema) (kk_xeq r1 r) |
33192 | 554 |
end |
555 |
| Func (Unit, (Atom (2, j0))) => |
|
34126 | 556 |
#kk_rel_if kk (#kk_rel_eq kk r (KK.Atom (j0 + 1))) |
33192 | 557 |
(full_rel_for_rep R1) (empty_rel_for_rep R1) |
558 |
| Func (R1', Atom (2, j0)) => |
|
559 |
func_from_no_opt_rel_expr kk R1 (Formula Neut) |
|
34126 | 560 |
(Func (R1', Formula Neut)) (#kk_join kk r (KK.Atom (j0 + 1))) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
561 |
| _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr", |
33192 | 562 |
[old_R, Func (R1, Formula Neut)])) |
563 |
| func_from_no_opt_rel_expr kk R1 R2 old_R r = |
|
564 |
case old_R of |
|
565 |
Vect (k, R) => |
|
566 |
let |
|
567 |
val args_rs = all_singletons_for_rep R1 |
|
568 |
val vals_rs = unpack_vect_in_chunks kk (arity_of_rep R) k r |
|
569 |
|> map (rel_expr_from_rel_expr kk R2 R) |
|
570 |
in fold1 (#kk_union kk) (map2 (#kk_product kk) args_rs vals_rs) end |
|
571 |
| Func (R1', Formula Neut) => |
|
572 |
(case R2 of |
|
573 |
Atom (x as (2, j0)) => |
|
574 |
let val schema = atom_schema_of_rep R1 in |
|
575 |
if length schema = 1 then |
|
34126 | 576 |
#kk_override kk (#kk_product kk (KK.AtomSeq (hd schema)) |
577 |
(KK.Atom j0)) |
|
578 |
(#kk_product kk r (KK.Atom (j0 + 1))) |
|
33192 | 579 |
else |
580 |
let |
|
581 |
val r1 = fold1 (#kk_product kk) (unary_var_seq ~1 (length schema)) |
|
582 |
|> rel_expr_from_rel_expr kk R1' R1 |
|
34126 | 583 |
val r2 = KK.Var (1, ~(length schema) - 1) |
33192 | 584 |
val r3 = atom_from_formula kk j0 (#kk_subset kk r1 r) |
585 |
in |
|
586 |
#kk_comprehension kk (decls_for_atom_schema ~1 (schema @ [x])) |
|
33582
bdf98e327f0b
fixed soundness bug in Nitpick related to sets of sets;
blanchet
parents:
33571
diff
changeset
|
587 |
(#kk_subset kk r2 r3) |
33192 | 588 |
end |
589 |
end |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
590 |
| _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr", |
33192 | 591 |
[old_R, Func (R1, R2)])) |
592 |
| Func (Unit, R2') => |
|
593 |
let val j0 = some_j0 in |
|
594 |
func_from_no_opt_rel_expr kk R1 R2 (Func (Atom (1, j0), R2')) |
|
34126 | 595 |
(#kk_product kk (KK.Atom j0) r) |
33192 | 596 |
end |
597 |
| Func (R1', R2') => |
|
598 |
if R1 = R1' andalso R2 = R2' then |
|
599 |
r |
|
600 |
else |
|
601 |
let |
|
602 |
val dom_schema = atom_schema_of_rep R1 |
|
603 |
val ran_schema = atom_schema_of_rep R2 |
|
604 |
val dom_prod = fold1 (#kk_product kk) |
|
605 |
(unary_var_seq ~1 (length dom_schema)) |
|
606 |
|> rel_expr_from_rel_expr kk R1' R1 |
|
607 |
val ran_prod = fold1 (#kk_product kk) |
|
608 |
(unary_var_seq (~(length dom_schema) - 1) |
|
609 |
(length ran_schema)) |
|
610 |
|> rel_expr_from_rel_expr kk R2' R2 |
|
611 |
val app = kk_n_fold_join kk true R1' R2' dom_prod r |
|
33582
bdf98e327f0b
fixed soundness bug in Nitpick related to sets of sets;
blanchet
parents:
33571
diff
changeset
|
612 |
val kk_xeq = (if is_one_rep R2' then #kk_subset else #kk_rel_eq) kk |
33192 | 613 |
in |
614 |
#kk_comprehension kk (decls_for_atom_schema ~1 |
|
615 |
(dom_schema @ ran_schema)) |
|
33582
bdf98e327f0b
fixed soundness bug in Nitpick related to sets of sets;
blanchet
parents:
33571
diff
changeset
|
616 |
(kk_xeq ran_prod app) |
33192 | 617 |
end |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
618 |
| _ => raise REP ("Nitpick_Kodkod.func_from_no_opt_rel_expr", |
33192 | 619 |
[old_R, Func (R1, R2)]) |
620 |
and rel_expr_from_rel_expr kk new_R old_R r = |
|
621 |
let |
|
622 |
val unopt_old_R = unopt_rep old_R |
|
623 |
val unopt_new_R = unopt_rep new_R |
|
624 |
in |
|
625 |
if unopt_old_R <> old_R andalso unopt_new_R = new_R then |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
626 |
raise REP ("Nitpick_Kodkod.rel_expr_from_rel_expr", [old_R, new_R]) |
33192 | 627 |
else if unopt_new_R = unopt_old_R then |
628 |
r |
|
629 |
else |
|
630 |
(case unopt_new_R of |
|
631 |
Atom x => atom_from_rel_expr kk x |
|
632 |
| Struct Rs => struct_from_rel_expr kk Rs |
|
633 |
| Vect (k, R') => vect_from_rel_expr kk k R' |
|
634 |
| Func (R1, R2) => func_from_no_opt_rel_expr kk R1 R2 |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
635 |
| _ => raise REP ("Nitpick_Kodkod.rel_expr_from_rel_expr", |
33192 | 636 |
[old_R, new_R])) |
637 |
unopt_old_R r |
|
638 |
end |
|
639 |
and rel_expr_to_func kk R1 R2 = rel_expr_from_rel_expr kk (Func (R1, R2)) |
|
640 |
||
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
641 |
fun bit_set_from_atom ({kk_join, ...} : kodkod_constrs) T r = |
34126 | 642 |
kk_join r (KK.Rel (if T = @{typ "unsigned_bit word"} then |
643 |
unsigned_bit_word_sel_rel |
|
644 |
else |
|
645 |
signed_bit_word_sel_rel)) |
|
646 |
val int_expr_from_atom = KK.SetSum ooo bit_set_from_atom |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
647 |
fun atom_from_int_expr (kk as {kk_rel_eq, kk_comprehension, ...} |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
648 |
: kodkod_constrs) T R i = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
649 |
kk_comprehension (decls_for_atom_schema ~1 (atom_schema_of_rep R)) |
34126 | 650 |
(kk_rel_eq (bit_set_from_atom kk T (KK.Var (1, ~1))) |
651 |
(KK.Bits i)) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
652 |
|
33192 | 653 |
fun declarative_axiom_for_plain_rel kk (FreeRel (x, _, R as Func _, nick)) = |
654 |
kk_n_ary_function kk (R |> nick = @{const_name List.set} ? unopt_rep) |
|
34126 | 655 |
(KK.Rel x) |
33192 | 656 |
| declarative_axiom_for_plain_rel ({kk_lone, kk_one, ...} : kodkod_constrs) |
657 |
(FreeRel (x, _, R, _)) = |
|
34126 | 658 |
if is_one_rep R then kk_one (KK.Rel x) |
659 |
else if is_lone_rep R andalso card_of_rep R > 1 then kk_lone (KK.Rel x) |
|
660 |
else KK.True |
|
33192 | 661 |
| declarative_axiom_for_plain_rel _ u = |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
662 |
raise NUT ("Nitpick_Kodkod.declarative_axiom_for_plain_rel", [u]) |
33192 | 663 |
|
664 |
fun const_triple rel_table (x as (s, T)) = |
|
665 |
case the_name rel_table (ConstName (s, T, Any)) of |
|
34126 | 666 |
FreeRel ((n, j), _, R, _) => (KK.Rel (n, j), R, n) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
667 |
| _ => raise TERM ("Nitpick_Kodkod.const_triple", [Const x]) |
33192 | 668 |
|
669 |
fun discr_rel_expr rel_table = #1 o const_triple rel_table o discr_for_constr |
|
670 |
||
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
671 |
fun nfa_transitions_for_sel hol_ctxt binarize |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
672 |
({kk_project, ...} : kodkod_constrs) rel_table |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
673 |
(dtypes : dtype_spec list) constr_x n = |
33192 | 674 |
let |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
675 |
val x as (_, T) = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
676 |
binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize constr_x n |
33192 | 677 |
val (r, R, arity) = const_triple rel_table x |
678 |
val type_schema = type_schema_of_rep T R |
|
679 |
in |
|
680 |
map_filter (fn (j, T) => |
|
681 |
if forall (not_equal T o #typ) dtypes then NONE |
|
34126 | 682 |
else SOME (kk_project r (map KK.Num [0, j]), T)) |
33192 | 683 |
(index_seq 1 (arity - 1) ~~ tl type_schema) |
684 |
end |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
685 |
fun nfa_transitions_for_constr hol_ctxt binarize kk rel_table dtypes |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
686 |
(x as (_, T)) = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
687 |
maps (nfa_transitions_for_sel hol_ctxt binarize kk rel_table dtypes x) |
33192 | 688 |
(index_seq 0 (num_sels_for_constr_type T)) |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
689 |
fun nfa_entry_for_datatype _ _ _ _ _ ({co = true, ...} : dtype_spec) = NONE |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
690 |
| nfa_entry_for_datatype _ _ _ _ _ {standard = false, ...} = NONE |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
691 |
| nfa_entry_for_datatype _ _ _ _ _ {deep = false, ...} = NONE |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
692 |
| nfa_entry_for_datatype hol_ctxt binarize kk rel_table dtypes |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
693 |
{typ, constrs, ...} = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
694 |
SOME (typ, maps (nfa_transitions_for_constr hol_ctxt binarize kk rel_table |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
695 |
dtypes o #const) constrs) |
33192 | 696 |
|
34126 | 697 |
val empty_rel = KK.Product (KK.None, KK.None) |
33192 | 698 |
|
35177 | 699 |
fun direct_path_rel_exprs nfa start_T final_T = |
700 |
case AList.lookup (op =) nfa final_T of |
|
701 |
SOME trans => map fst (filter (curry (op =) start_T o snd) trans) |
|
33192 | 702 |
| NONE => [] |
35177 | 703 |
and any_path_rel_expr ({kk_union, ...} : kodkod_constrs) nfa [] start_T |
704 |
final_T = |
|
705 |
fold kk_union (direct_path_rel_exprs nfa start_T final_T) |
|
706 |
(if start_T = final_T then KK.Iden else empty_rel) |
|
707 |
| any_path_rel_expr (kk as {kk_union, ...}) nfa (T :: Ts) start_T final_T = |
|
708 |
kk_union (any_path_rel_expr kk nfa Ts start_T final_T) |
|
709 |
(knot_path_rel_expr kk nfa Ts start_T T final_T) |
|
710 |
and knot_path_rel_expr (kk as {kk_join, kk_reflexive_closure, ...}) nfa Ts |
|
711 |
start_T knot_T final_T = |
|
712 |
kk_join (kk_join (any_path_rel_expr kk nfa Ts knot_T final_T) |
|
713 |
(kk_reflexive_closure (loop_path_rel_expr kk nfa Ts knot_T))) |
|
714 |
(any_path_rel_expr kk nfa Ts start_T knot_T) |
|
715 |
and loop_path_rel_expr ({kk_union, ...} : kodkod_constrs) nfa [] start_T = |
|
716 |
fold kk_union (direct_path_rel_exprs nfa start_T start_T) empty_rel |
|
717 |
| loop_path_rel_expr (kk as {kk_union, kk_closure, ...}) nfa (T :: Ts) |
|
718 |
start_T = |
|
719 |
if start_T = T then |
|
720 |
kk_closure (loop_path_rel_expr kk nfa Ts start_T) |
|
33192 | 721 |
else |
35177 | 722 |
kk_union (loop_path_rel_expr kk nfa Ts start_T) |
723 |
(knot_path_rel_expr kk nfa Ts start_T T start_T) |
|
33192 | 724 |
|
725 |
fun graph_for_nfa nfa = |
|
726 |
let |
|
35177 | 727 |
fun new_node T = perhaps (try (NfaGraph.new_node (T, ()))) |
33192 | 728 |
fun add_nfa [] = I |
729 |
| add_nfa ((_, []) :: nfa) = add_nfa nfa |
|
35177 | 730 |
| add_nfa ((T, ((_, T') :: transitions)) :: nfa) = |
731 |
add_nfa ((T, transitions) :: nfa) o NfaGraph.add_edge (T, T') o |
|
732 |
new_node T' o new_node T |
|
33192 | 733 |
in add_nfa nfa NfaGraph.empty end |
734 |
||
735 |
fun strongly_connected_sub_nfas nfa = |
|
736 |
nfa |> graph_for_nfa |> NfaGraph.strong_conn |
|
737 |
|> map (fn keys => filter (member (op =) keys o fst) nfa) |
|
738 |
||
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
739 |
fun acyclicity_axiom_for_datatype kk nfa start_T = |
33192 | 740 |
#kk_no kk (#kk_intersect kk |
35177 | 741 |
(loop_path_rel_expr kk nfa (map fst nfa) start_T) KK.Iden) |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
742 |
fun acyclicity_axioms_for_datatypes hol_ctxt binarize kk rel_table dtypes = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
743 |
map_filter (nfa_entry_for_datatype hol_ctxt binarize kk rel_table dtypes) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
744 |
dtypes |
33192 | 745 |
|> strongly_connected_sub_nfas |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
746 |
|> maps (fn nfa => map (acyclicity_axiom_for_datatype kk nfa o fst) nfa) |
33192 | 747 |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
748 |
fun sel_axiom_for_sel hol_ctxt binarize j0 |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
749 |
(kk as {kk_all, kk_formula_if, kk_subset, kk_no, kk_join, ...}) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
750 |
rel_table dom_r ({const, delta, epsilon, exclusive, ...} : constr_spec) |
33192 | 751 |
n = |
752 |
let |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
753 |
val x = binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize const n |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
754 |
val (r, R, _) = const_triple rel_table x |
33192 | 755 |
val R2 = dest_Func R |> snd |
756 |
val z = (epsilon - delta, delta + j0) |
|
757 |
in |
|
758 |
if exclusive then |
|
759 |
kk_n_ary_function kk (Func (Atom z, R2)) r |
|
760 |
else |
|
34126 | 761 |
let val r' = kk_join (KK.Var (1, 0)) r in |
762 |
kk_all [KK.DeclOne ((1, 0), KK.AtomSeq z)] |
|
763 |
(kk_formula_if (kk_subset (KK.Var (1, 0)) dom_r) |
|
764 |
(kk_n_ary_function kk R2 r') (kk_no r')) |
|
33192 | 765 |
end |
766 |
end |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
767 |
fun sel_axioms_for_constr hol_ctxt binarize bits j0 kk rel_table |
33192 | 768 |
(constr as {const, delta, epsilon, explicit_max, ...}) = |
769 |
let |
|
770 |
val honors_explicit_max = |
|
771 |
explicit_max < 0 orelse epsilon - delta <= explicit_max |
|
772 |
in |
|
773 |
if explicit_max = 0 then |
|
774 |
[formula_for_bool honors_explicit_max] |
|
775 |
else |
|
776 |
let |
|
35178 | 777 |
val dom_r = discr_rel_expr rel_table const |
33192 | 778 |
val max_axiom = |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
779 |
if honors_explicit_max then |
34126 | 780 |
KK.True |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
781 |
else if is_twos_complement_representable bits (epsilon - delta) then |
35178 | 782 |
KK.LE (KK.Cardinality dom_r, KK.Num explicit_max) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
783 |
else |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
784 |
raise TOO_SMALL ("Nitpick_Kodkod.sel_axioms_for_constr", |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
785 |
"\"bits\" value " ^ string_of_int bits ^ |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
786 |
" too small for \"max\"") |
33192 | 787 |
in |
788 |
max_axiom :: |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
789 |
map (sel_axiom_for_sel hol_ctxt binarize j0 kk rel_table dom_r constr) |
33192 | 790 |
(index_seq 0 (num_sels_for_constr_type (snd const))) |
791 |
end |
|
792 |
end |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
793 |
fun sel_axioms_for_datatype hol_ctxt binarize bits j0 kk rel_table |
33192 | 794 |
({constrs, ...} : dtype_spec) = |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
795 |
maps (sel_axioms_for_constr hol_ctxt binarize bits j0 kk rel_table) constrs |
33192 | 796 |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
797 |
fun uniqueness_axiom_for_constr hol_ctxt binarize |
33192 | 798 |
({kk_all, kk_implies, kk_and, kk_rel_eq, kk_lone, kk_join, ...} |
799 |
: kodkod_constrs) rel_table ({const, ...} : constr_spec) = |
|
800 |
let |
|
801 |
fun conjunct_for_sel r = |
|
34126 | 802 |
kk_rel_eq (kk_join (KK.Var (1, 0)) r) (kk_join (KK.Var (1, 1)) r) |
33192 | 803 |
val num_sels = num_sels_for_constr_type (snd const) |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
804 |
val triples = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
805 |
map (const_triple rel_table |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
806 |
o binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize const) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
807 |
(~1 upto num_sels - 1) |
33192 | 808 |
val set_r = triples |> hd |> #1 |
809 |
in |
|
810 |
if num_sels = 0 then |
|
811 |
kk_lone set_r |
|
812 |
else |
|
34126 | 813 |
kk_all (map (KK.DeclOne o rpair set_r o pair 1) [0, 1]) |
33192 | 814 |
(kk_implies |
815 |
(fold1 kk_and (map (conjunct_for_sel o #1) (tl triples))) |
|
34126 | 816 |
(kk_rel_eq (KK.Var (1, 0)) (KK.Var (1, 1)))) |
33192 | 817 |
end |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
818 |
fun uniqueness_axioms_for_datatype hol_ctxt binarize kk rel_table |
33192 | 819 |
({constrs, ...} : dtype_spec) = |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
820 |
map (uniqueness_axiom_for_constr hol_ctxt binarize kk rel_table) constrs |
33192 | 821 |
|
822 |
fun effective_constr_max ({delta, epsilon, ...} : constr_spec) = epsilon - delta |
|
823 |
fun partition_axioms_for_datatype j0 (kk as {kk_rel_eq, kk_union, ...}) |
|
824 |
rel_table |
|
825 |
({card, constrs, ...} : dtype_spec) = |
|
826 |
if forall #exclusive constrs then |
|
827 |
[Integer.sum (map effective_constr_max constrs) = card |> formula_for_bool] |
|
828 |
else |
|
829 |
let val rs = map (discr_rel_expr rel_table o #const) constrs in |
|
34126 | 830 |
[kk_rel_eq (fold1 kk_union rs) (KK.AtomSeq (card, j0)), |
33192 | 831 |
kk_disjoint_sets kk rs] |
832 |
end |
|
833 |
||
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
834 |
fun other_axioms_for_datatype _ _ _ _ _ _ {deep = false, ...} = [] |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
835 |
| other_axioms_for_datatype hol_ctxt binarize bits ofs kk rel_table |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
836 |
(dtype as {typ, ...}) = |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
837 |
let val j0 = offset_of_type ofs typ in |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
838 |
sel_axioms_for_datatype hol_ctxt binarize bits j0 kk rel_table dtype @ |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
839 |
uniqueness_axioms_for_datatype hol_ctxt binarize kk rel_table dtype @ |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
840 |
partition_axioms_for_datatype j0 kk rel_table dtype |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
841 |
end |
33192 | 842 |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
843 |
fun declarative_axioms_for_datatypes hol_ctxt binarize bits ofs kk rel_table |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
844 |
dtypes = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
845 |
acyclicity_axioms_for_datatypes hol_ctxt binarize kk rel_table dtypes @ |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
846 |
maps (other_axioms_for_datatype hol_ctxt binarize bits ofs kk rel_table) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
847 |
dtypes |
33192 | 848 |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
849 |
fun kodkod_formula_from_nut ofs |
33192 | 850 |
(kk as {kk_all, kk_exist, kk_formula_let, kk_formula_if, kk_or, kk_not, |
35072
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
851 |
kk_iff, kk_implies, kk_and, kk_subset, kk_rel_eq, kk_no, |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
852 |
kk_lone, kk_one, kk_some, kk_rel_let, kk_rel_if, kk_union, |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
853 |
kk_difference, kk_intersect, kk_product, kk_join, kk_closure, |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
854 |
kk_comprehension, kk_project, kk_project_seq, kk_not3, |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
855 |
kk_nat_less, kk_int_less, ...}) u = |
33192 | 856 |
let |
857 |
val main_j0 = offset_of_type ofs bool_T |
|
858 |
val bool_j0 = main_j0 |
|
859 |
val bool_atom_R = Atom (2, main_j0) |
|
34126 | 860 |
val false_atom = KK.Atom bool_j0 |
861 |
val true_atom = KK.Atom (bool_j0 + 1) |
|
33192 | 862 |
|
863 |
fun formula_from_opt_atom polar j0 r = |
|
864 |
case polar of |
|
34126 | 865 |
Neg => kk_not (kk_rel_eq r (KK.Atom j0)) |
866 |
| _ => kk_rel_eq r (KK.Atom (j0 + 1)) |
|
33192 | 867 |
val formula_from_atom = formula_from_opt_atom Pos |
868 |
||
869 |
val unpack_formulas = |
|
870 |
map (formula_from_atom bool_j0) oo unpack_vect_in_chunks kk 1 |
|
871 |
fun kk_vect_set_bool_op connective k r1 r2 = |
|
872 |
fold1 kk_and (map2 connective (unpack_formulas k r1) |
|
873 |
(unpack_formulas k r2)) |
|
874 |
||
875 |
fun to_f u = |
|
876 |
case rep_of u of |
|
877 |
Formula polar => |
|
878 |
(case u of |
|
34126 | 879 |
Cst (False, _, _) => KK.False |
880 |
| Cst (True, _, _) => KK.True |
|
33854
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
881 |
| Op1 (Not, _, _, u1) => |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
882 |
kk_not (to_f_with_polarity (flip_polarity polar) u1) |
33192 | 883 |
| Op1 (Finite, _, _, u1) => |
884 |
let val opt1 = is_opt_rep (rep_of u1) in |
|
885 |
case polar of |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
886 |
Neut => |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
887 |
if opt1 then raise NUT ("Nitpick_Kodkod.to_f (Finite)", [u]) |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
888 |
else KK.True |
33192 | 889 |
| Pos => formula_for_bool (not opt1) |
34126 | 890 |
| Neg => KK.True |
33192 | 891 |
end |
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
892 |
| Op1 (IsUnknown, _, _, u1) => kk_no (to_r u1) |
33192 | 893 |
| Op1 (Cast, _, _, u1) => to_f_with_polarity polar u1 |
33854
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
894 |
| Op2 (All, _, _, u1, u2) => |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
895 |
kk_all (untuple to_decl u1) (to_f_with_polarity polar u2) |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
896 |
| Op2 (Exist, _, _, u1, u2) => |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
897 |
kk_exist (untuple to_decl u1) (to_f_with_polarity polar u2) |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
898 |
| Op2 (Or, _, _, u1, u2) => |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
899 |
kk_or (to_f_with_polarity polar u1) (to_f_with_polarity polar u2) |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
900 |
| Op2 (And, _, _, u1, u2) => |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
901 |
kk_and (to_f_with_polarity polar u1) (to_f_with_polarity polar u2) |
33192 | 902 |
| Op2 (Less, T, Formula polar, u1, u2) => |
903 |
formula_from_opt_atom polar bool_j0 |
|
904 |
(to_r (Op2 (Less, T, Opt bool_atom_R, u1, u2))) |
|
905 |
| Op2 (Subset, _, _, u1, u2) => |
|
906 |
let |
|
907 |
val dom_T = domain_type (type_of u1) |
|
908 |
val R1 = rep_of u1 |
|
909 |
val R2 = rep_of u2 |
|
910 |
val (dom_R, ran_R) = |
|
911 |
case min_rep R1 R2 of |
|
912 |
Func (Unit, R') => |
|
913 |
(Atom (1, offset_of_type ofs dom_T), R') |
|
914 |
| Func Rp => Rp |
|
915 |
| R => (Atom (card_of_domain_from_rep 2 R, |
|
916 |
offset_of_type ofs dom_T), |
|
917 |
if is_opt_rep R then Opt bool_atom_R else Formula Neut) |
|
918 |
val set_R = Func (dom_R, ran_R) |
|
919 |
in |
|
920 |
if not (is_opt_rep ran_R) then |
|
921 |
to_set_bool_op kk_implies kk_subset u1 u2 |
|
922 |
else if polar = Neut then |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
923 |
raise NUT ("Nitpick_Kodkod.to_f (Subset)", [u]) |
33192 | 924 |
else |
925 |
let |
|
33886
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
926 |
(* FIXME: merge with similar code below *) |
33192 | 927 |
fun set_to_r widen u = |
928 |
if widen then |
|
929 |
kk_difference (full_rel_for_rep dom_R) |
|
930 |
(kk_join (to_rep set_R u) false_atom) |
|
931 |
else |
|
932 |
kk_join (to_rep set_R u) true_atom |
|
933 |
val widen1 = (polar = Pos andalso is_opt_rep R1) |
|
934 |
val widen2 = (polar = Neg andalso is_opt_rep R2) |
|
935 |
in kk_subset (set_to_r widen1 u1) (set_to_r widen2 u2) end |
|
936 |
end |
|
937 |
| Op2 (DefEq, _, _, u1, u2) => |
|
938 |
(case min_rep (rep_of u1) (rep_of u2) of |
|
34126 | 939 |
Unit => KK.True |
33192 | 940 |
| Formula polar => |
941 |
kk_iff (to_f_with_polarity polar u1) (to_f_with_polarity polar u2) |
|
942 |
| min_R => |
|
943 |
let |
|
944 |
fun args (Op2 (Apply, _, _, u1, u2)) = u2 :: args u1 |
|
945 |
| args (Tuple (_, _, us)) = us |
|
946 |
| args _ = [] |
|
947 |
val opt_arg_us = filter (is_opt_rep o rep_of) (args u1) |
|
948 |
in |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
949 |
if null opt_arg_us orelse not (is_Opt min_R) orelse |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
950 |
is_eval_name u1 then |
33192 | 951 |
fold (kk_or o (kk_no o to_r)) opt_arg_us |
952 |
(kk_rel_eq (to_rep min_R u1) (to_rep min_R u2)) |
|
953 |
else |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
954 |
kk_subset (to_rep min_R u1) (to_rep min_R u2) |
33192 | 955 |
end) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
956 |
| Op2 (Eq, _, _, u1, u2) => |
33192 | 957 |
(case min_rep (rep_of u1) (rep_of u2) of |
34126 | 958 |
Unit => KK.True |
33192 | 959 |
| Formula polar => |
960 |
kk_iff (to_f_with_polarity polar u1) (to_f_with_polarity polar u2) |
|
961 |
| min_R => |
|
962 |
if is_opt_rep min_R then |
|
963 |
if polar = Neut then |
|
964 |
(* continuation of hackish optimization *) |
|
965 |
kk_rel_eq (to_rep min_R u1) (to_rep min_R u2) |
|
966 |
else if is_Cst Unrep u1 then |
|
967 |
to_could_be_unrep (polar = Neg) u2 |
|
968 |
else if is_Cst Unrep u2 then |
|
969 |
to_could_be_unrep (polar = Neg) u1 |
|
970 |
else |
|
971 |
let |
|
972 |
val r1 = to_rep min_R u1 |
|
973 |
val r2 = to_rep min_R u2 |
|
974 |
val both_opt = forall (is_opt_rep o rep_of) [u1, u2] |
|
975 |
in |
|
976 |
(if polar = Pos then |
|
977 |
if not both_opt then |
|
978 |
kk_rel_eq r1 r2 |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
979 |
else if is_lone_rep min_R andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
980 |
arity_of_rep min_R = 1 then |
33192 | 981 |
kk_some (kk_intersect r1 r2) |
982 |
else |
|
983 |
raise SAME () |
|
984 |
else |
|
985 |
if is_lone_rep min_R then |
|
986 |
if arity_of_rep min_R = 1 then |
|
35072
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35070
diff
changeset
|
987 |
kk_lone (kk_union r1 r2) |
33192 | 988 |
else if not both_opt then |
989 |
(r1, r2) |> is_opt_rep (rep_of u2) ? swap |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
990 |
|-> kk_subset |
33192 | 991 |
else |
992 |
raise SAME () |
|
993 |
else |
|
994 |
raise SAME ()) |
|
995 |
handle SAME () => |
|
996 |
formula_from_opt_atom polar bool_j0 |
|
997 |
(to_guard [u1, u2] bool_atom_R |
|
998 |
(rel_expr_from_formula kk bool_atom_R |
|
999 |
(kk_rel_eq r1 r2))) |
|
1000 |
end |
|
1001 |
else |
|
1002 |
let |
|
1003 |
val r1 = to_rep min_R u1 |
|
1004 |
val r2 = to_rep min_R u2 |
|
1005 |
in |
|
1006 |
if is_one_rep min_R then |
|
1007 |
let |
|
1008 |
val rs1 = unpack_products r1 |
|
1009 |
val rs2 = unpack_products r2 |
|
1010 |
in |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1011 |
if length rs1 = length rs2 andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1012 |
map KK.arity_of_rel_expr rs1 |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1013 |
= map KK.arity_of_rel_expr rs2 then |
33192 | 1014 |
fold1 kk_and (map2 kk_subset rs1 rs2) |
1015 |
else |
|
1016 |
kk_subset r1 r2 |
|
1017 |
end |
|
1018 |
else |
|
1019 |
kk_rel_eq r1 r2 |
|
1020 |
end) |
|
33744
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33705
diff
changeset
|
1021 |
| Op2 (The, T, _, u1, u2) => |
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33705
diff
changeset
|
1022 |
to_f_with_polarity polar |
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33705
diff
changeset
|
1023 |
(Op2 (The, T, Opt (Atom (2, bool_j0)), u1, u2)) |
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33705
diff
changeset
|
1024 |
| Op2 (Eps, T, _, u1, u2) => |
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33705
diff
changeset
|
1025 |
to_f_with_polarity polar |
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33705
diff
changeset
|
1026 |
(Op2 (Eps, T, Opt (Atom (2, bool_j0)), u1, u2)) |
33192 | 1027 |
| Op2 (Apply, T, _, u1, u2) => |
1028 |
(case (polar, rep_of u1) of |
|
1029 |
(Neg, Func (R, Formula Neut)) => kk_subset (to_opt R u2) (to_r u1) |
|
1030 |
| _ => |
|
1031 |
to_f_with_polarity polar |
|
1032 |
(Op2 (Apply, T, Opt (Atom (2, offset_of_type ofs T)), u1, u2))) |
|
1033 |
| Op3 (Let, _, _, u1, u2, u3) => |
|
33854
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
1034 |
kk_formula_let [to_expr_assign u1 u2] (to_f_with_polarity polar u3) |
33192 | 1035 |
| Op3 (If, _, _, u1, u2, u3) => |
33854
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
1036 |
kk_formula_if (to_f u1) (to_f_with_polarity polar u2) |
26a4cb3a7eae
fixed a Kodkod generation exception in Nitpick, reported by a Karlsruhe user
blanchet
parents:
33744
diff
changeset
|
1037 |
(to_f_with_polarity polar u3) |
34126 | 1038 |
| FormulaReg (j, _, _) => KK.FormulaReg j |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1039 |
| _ => raise NUT ("Nitpick_Kodkod.to_f", [u])) |
33192 | 1040 |
| Atom (2, j0) => formula_from_atom j0 (to_r u) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1041 |
| _ => raise NUT ("Nitpick_Kodkod.to_f", [u]) |
33192 | 1042 |
and to_f_with_polarity polar u = |
1043 |
case rep_of u of |
|
1044 |
Formula _ => to_f u |
|
1045 |
| Atom (2, j0) => formula_from_atom j0 (to_r u) |
|
1046 |
| Opt (Atom (2, j0)) => formula_from_opt_atom polar j0 (to_r u) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1047 |
| _ => raise NUT ("Nitpick_Kodkod.to_f_with_polarity", [u]) |
33192 | 1048 |
and to_r u = |
1049 |
case u of |
|
1050 |
Cst (False, _, Atom _) => false_atom |
|
1051 |
| Cst (True, _, Atom _) => true_atom |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1052 |
| Cst (Iden, _, Func (Struct [R1, R2], Formula Neut)) => |
33192 | 1053 |
if R1 = R2 andalso arity_of_rep R1 = 1 then |
34126 | 1054 |
kk_intersect KK.Iden (kk_product (full_rel_for_rep R1) KK.Univ) |
33192 | 1055 |
else |
1056 |
let |
|
1057 |
val schema1 = atom_schema_of_rep R1 |
|
1058 |
val schema2 = atom_schema_of_rep R2 |
|
1059 |
val arity1 = length schema1 |
|
1060 |
val arity2 = length schema2 |
|
1061 |
val r1 = fold1 kk_product (unary_var_seq 0 arity1) |
|
1062 |
val r2 = fold1 kk_product (unary_var_seq arity1 arity2) |
|
1063 |
val min_R = min_rep R1 R2 |
|
1064 |
in |
|
1065 |
kk_comprehension |
|
1066 |
(decls_for_atom_schema 0 (schema1 @ schema2)) |
|
1067 |
(kk_rel_eq (rel_expr_from_rel_expr kk min_R R1 r1) |
|
1068 |
(rel_expr_from_rel_expr kk min_R R2 r2)) |
|
1069 |
end |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1070 |
| Cst (Iden, _, Func (Atom (1, j0), Formula Neut)) => KK.Atom j0 |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1071 |
| Cst (Iden, T as Type (@{type_name fun}, [T1, _]), R as Func (R1, _)) => |
33192 | 1072 |
to_rep R (Cst (Iden, T, Func (one_rep ofs T1 R1, Formula Neut))) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1073 |
| Cst (Num j, T, R) => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1074 |
if is_word_type T then |
34126 | 1075 |
atom_from_int_expr kk T R (KK.Num j) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1076 |
else if T = int_T then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1077 |
case atom_for_int (card_of_rep R, offset_of_type ofs int_T) j of |
34126 | 1078 |
~1 => if is_opt_rep R then KK.None |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1079 |
else raise NUT ("Nitpick_Kodkod.to_r (Num)", [u]) |
34126 | 1080 |
| j' => KK.Atom j' |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1081 |
else |
34126 | 1082 |
if j < card_of_rep R then KK.Atom (j + offset_of_type ofs T) |
1083 |
else if is_opt_rep R then KK.None |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1084 |
else raise NUT ("Nitpick_Kodkod.to_r (Num)", [u]) |
33192 | 1085 |
| Cst (Unknown, _, R) => empty_rel_for_rep R |
1086 |
| Cst (Unrep, _, R) => empty_rel_for_rep R |
|
34126 | 1087 |
| Cst (Suc, T as @{typ "unsigned_bit word => unsigned_bit word"}, R) => |
1088 |
to_bit_word_unary_op T R (curry KK.Add (KK.Num 1)) |
|
1089 |
| Cst (Suc, @{typ "nat => nat"}, Func (Atom x, _)) => |
|
1090 |
kk_intersect (KK.Rel suc_rel) (kk_product KK.Univ (KK.AtomSeq x)) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1091 |
| Cst (Suc, _, Func (Atom _, _)) => KK.Rel suc_rel |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1092 |
| Cst (Add, Type (_, [@{typ nat}, _]), _) => KK.Rel nat_add_rel |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1093 |
| Cst (Add, Type (_, [@{typ int}, _]), _) => KK.Rel int_add_rel |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1094 |
| Cst (Add, T as Type (_, [@{typ "unsigned_bit word"}, _]), R) => |
34126 | 1095 |
to_bit_word_binary_op T R NONE (SOME (curry KK.Add)) |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1096 |
| Cst (Add, T as Type (_, [@{typ "signed_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1097 |
to_bit_word_binary_op T R |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1098 |
(SOME (fn i1 => fn i2 => fn i3 => |
34126 | 1099 |
kk_implies (KK.LE (KK.Num 0, KK.BitXor (i1, i2))) |
1100 |
(KK.LE (KK.Num 0, KK.BitXor (i2, i3))))) |
|
1101 |
(SOME (curry KK.Add)) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1102 |
| Cst (Subtract, Type (_, [@{typ nat}, _]), _) => |
34126 | 1103 |
KK.Rel nat_subtract_rel |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1104 |
| Cst (Subtract, Type (_, [@{typ int}, _]), _) => |
34126 | 1105 |
KK.Rel int_subtract_rel |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1106 |
| Cst (Subtract, T as Type (_, [@{typ "unsigned_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1107 |
to_bit_word_binary_op T R NONE |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1108 |
(SOME (fn i1 => fn i2 => |
34126 | 1109 |
KK.IntIf (KK.LE (i1, i2), KK.Num 0, KK.Sub (i1, i2)))) |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1110 |
| Cst (Subtract, T as Type (_, [@{typ "signed_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1111 |
to_bit_word_binary_op T R |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1112 |
(SOME (fn i1 => fn i2 => fn i3 => |
34126 | 1113 |
kk_implies (KK.LT (KK.BitXor (i1, i2), KK.Num 0)) |
1114 |
(KK.LT (KK.BitXor (i2, i3), KK.Num 0)))) |
|
1115 |
(SOME (curry KK.Sub)) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1116 |
| Cst (Multiply, Type (_, [@{typ nat}, _]), _) => |
34126 | 1117 |
KK.Rel nat_multiply_rel |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1118 |
| Cst (Multiply, Type (_, [@{typ int}, _]), _) => |
34126 | 1119 |
KK.Rel int_multiply_rel |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1120 |
| Cst (Multiply, |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1121 |
T as Type (_, [Type (@{type_name word}, [bit_T]), _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1122 |
to_bit_word_binary_op T R |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1123 |
(SOME (fn i1 => fn i2 => fn i3 => |
34126 | 1124 |
kk_or (KK.IntEq (i2, KK.Num 0)) |
1125 |
(KK.IntEq (KK.Div (i3, i2), i1) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1126 |
|> bit_T = @{typ signed_bit} |
34126 | 1127 |
? kk_and (KK.LE (KK.Num 0, |
1128 |
foldl1 KK.BitAnd [i1, i2, i3]))))) |
|
1129 |
(SOME (curry KK.Mult)) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1130 |
| Cst (Divide, Type (_, [@{typ nat}, _]), _) => KK.Rel nat_divide_rel |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1131 |
| Cst (Divide, Type (_, [@{typ int}, _]), _) => KK.Rel int_divide_rel |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1132 |
| Cst (Divide, T as Type (_, [@{typ "unsigned_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1133 |
to_bit_word_binary_op T R NONE |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1134 |
(SOME (fn i1 => fn i2 => |
34126 | 1135 |
KK.IntIf (KK.IntEq (i2, KK.Num 0), |
1136 |
KK.Num 0, KK.Div (i1, i2)))) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1137 |
| Cst (Divide, T as Type (_, [@{typ "signed_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1138 |
to_bit_word_binary_op T R |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1139 |
(SOME (fn i1 => fn i2 => fn i3 => |
34126 | 1140 |
KK.LE (KK.Num 0, foldl1 KK.BitAnd [i1, i2, i3]))) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1141 |
(SOME (fn i1 => fn i2 => |
34126 | 1142 |
KK.IntIf (kk_and (KK.LT (i1, KK.Num 0)) |
1143 |
(KK.LT (KK.Num 0, i2)), |
|
1144 |
KK.Sub (KK.Div (KK.Add (i1, KK.Num 1), i2), KK.Num 1), |
|
1145 |
KK.IntIf (kk_and (KK.LT (KK.Num 0, i1)) |
|
1146 |
(KK.LT (i2, KK.Num 0)), |
|
1147 |
KK.Sub (KK.Div (KK.Sub (i1, KK.Num 1), i2), KK.Num 1), |
|
1148 |
KK.IntIf (KK.IntEq (i2, KK.Num 0), |
|
1149 |
KK.Num 0, KK.Div (i1, i2)))))) |
|
1150 |
| Cst (Gcd, _, _) => KK.Rel gcd_rel |
|
1151 |
| Cst (Lcm, _, _) => KK.Rel lcm_rel |
|
1152 |
| Cst (Fracs, _, Func (Atom (1, _), _)) => KK.None |
|
33192 | 1153 |
| Cst (Fracs, _, Func (Struct _, _)) => |
34126 | 1154 |
kk_project_seq (KK.Rel norm_frac_rel) 2 2 |
1155 |
| Cst (NormFrac, _, _) => KK.Rel norm_frac_rel |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1156 |
| Cst (NatToInt, Type (_, [@{typ nat}, _]), Func (Atom _, Atom _)) => |
34126 | 1157 |
KK.Iden |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1158 |
| Cst (NatToInt, Type (_, [@{typ nat}, _]), |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1159 |
Func (Atom (_, nat_j0), Opt (Atom (int_k, int_j0)))) => |
33192 | 1160 |
if nat_j0 = int_j0 then |
34126 | 1161 |
kk_intersect KK.Iden |
1162 |
(kk_product (KK.AtomSeq (max_int_for_card int_k + 1, nat_j0)) |
|
1163 |
KK.Univ) |
|
33192 | 1164 |
else |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1165 |
raise BAD ("Nitpick_Kodkod.to_r (NatToInt)", "\"nat_j0 <> int_j0\"") |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1166 |
| Cst (NatToInt, T as Type (_, [@{typ "unsigned_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1167 |
to_bit_word_unary_op T R I |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1168 |
| Cst (IntToNat, Type (_, [@{typ int}, _]), |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1169 |
Func (Atom (int_k, int_j0), nat_R)) => |
33192 | 1170 |
let |
1171 |
val abs_card = max_int_for_card int_k + 1 |
|
1172 |
val (nat_k, nat_j0) = the_single (atom_schema_of_rep nat_R) |
|
1173 |
val overlap = Int.min (nat_k, abs_card) |
|
1174 |
in |
|
1175 |
if nat_j0 = int_j0 then |
|
34126 | 1176 |
kk_union (kk_product (KK.AtomSeq (int_k - abs_card, |
1177 |
int_j0 + abs_card)) |
|
1178 |
(KK.Atom nat_j0)) |
|
1179 |
(kk_intersect KK.Iden |
|
1180 |
(kk_product (KK.AtomSeq (overlap, int_j0)) KK.Univ)) |
|
33192 | 1181 |
else |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1182 |
raise BAD ("Nitpick_Kodkod.to_r (IntToNat)", "\"nat_j0 <> int_j0\"") |
33192 | 1183 |
end |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35407
diff
changeset
|
1184 |
| Cst (IntToNat, T as Type (_, [@{typ "signed_bit word"}, _]), R) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1185 |
to_bit_word_unary_op T R |
34126 | 1186 |
(fn i => KK.IntIf (KK.LE (i, KK.Num 0), KK.Num 0, i)) |
33192 | 1187 |
| Op1 (Not, _, R, u1) => kk_not3 (to_rep R u1) |
34126 | 1188 |
| Op1 (Finite, _, Opt (Atom _), _) => KK.None |
33192 | 1189 |
| Op1 (Converse, T, R, u1) => |
1190 |
let |
|
1191 |
val (b_T, a_T) = HOLogic.dest_prodT (domain_type T) |
|
1192 |
val (b_R, a_R) = |
|
1193 |
case R of |
|
1194 |
Func (Struct [R1, R2], _) => (R1, R2) |
|
1195 |
| Func (R1, _) => |
|
1196 |
if card_of_rep R1 <> 1 then |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1197 |
raise REP ("Nitpick_Kodkod.to_r (Converse)", [R]) |
33192 | 1198 |
else |
1199 |
pairself (Atom o pair 1 o offset_of_type ofs) (b_T, a_T) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1200 |
| _ => raise REP ("Nitpick_Kodkod.to_r (Converse)", [R]) |
33192 | 1201 |
val body_R = body_rep R |
1202 |
val a_arity = arity_of_rep a_R |
|
1203 |
val b_arity = arity_of_rep b_R |
|
1204 |
val ab_arity = a_arity + b_arity |
|
1205 |
val body_arity = arity_of_rep body_R |
|
1206 |
in |
|
1207 |
kk_project (to_rep (Func (Struct [a_R, b_R], body_R)) u1) |
|
34126 | 1208 |
(map KK.Num (index_seq a_arity b_arity @ |
1209 |
index_seq 0 a_arity @ |
|
1210 |
index_seq ab_arity body_arity)) |
|
33192 | 1211 |
|> rel_expr_from_rel_expr kk R (Func (Struct [b_R, a_R], body_R)) |
1212 |
end |
|
1213 |
| Op1 (Closure, _, R, u1) => |
|
1214 |
if is_opt_rep R then |
|
1215 |
let |
|
1216 |
val T1 = type_of u1 |
|
1217 |
val R' = rep_to_binary_rel_rep ofs T1 (unopt_rep (rep_of u1)) |
|
1218 |
val R'' = opt_rep ofs T1 R' |
|
1219 |
in |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1220 |
single_rel_rel_let kk |
33192 | 1221 |
(fn r => |
1222 |
let |
|
1223 |
val true_r = kk_closure (kk_join r true_atom) |
|
1224 |
val full_r = full_rel_for_rep R' |
|
1225 |
val false_r = kk_difference full_r |
|
1226 |
(kk_closure (kk_difference full_r |
|
1227 |
(kk_join r false_atom))) |
|
1228 |
in |
|
1229 |
rel_expr_from_rel_expr kk R R'' |
|
1230 |
(kk_union (kk_product true_r true_atom) |
|
1231 |
(kk_product false_r false_atom)) |
|
1232 |
end) (to_rep R'' u1) |
|
1233 |
end |
|
1234 |
else |
|
1235 |
let val R' = rep_to_binary_rel_rep ofs (type_of u1) (rep_of u1) in |
|
1236 |
rel_expr_from_rel_expr kk R R' (kk_closure (to_rep R' u1)) |
|
1237 |
end |
|
1238 |
| Op1 (SingletonSet, _, Func (R1, Opt _), Cst (Unrep, _, _)) => |
|
1239 |
(if R1 = Unit then I else kk_product (full_rel_for_rep R1)) false_atom |
|
1240 |
| Op1 (SingletonSet, _, R, u1) => |
|
1241 |
(case R of |
|
1242 |
Func (R1, Formula Neut) => to_rep R1 u1 |
|
1243 |
| Func (Unit, Opt R) => to_guard [u1] R true_atom |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1244 |
| Func (R1, Opt _) => |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1245 |
single_rel_rel_let kk |
33192 | 1246 |
(fn r => kk_rel_if (kk_no r) (empty_rel_for_rep R) |
1247 |
(rel_expr_to_func kk R1 bool_atom_R |
|
1248 |
(Func (R1, Formula Neut)) r)) |
|
1249 |
(to_opt R1 u1) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1250 |
| _ => raise NUT ("Nitpick_Kodkod.to_r (SingletonSet)", [u])) |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
1251 |
| Op1 (SafeThe, _, R, u1) => |
33192 | 1252 |
if is_opt_rep R then |
1253 |
kk_join (to_rep (Func (unopt_rep R, Opt bool_atom_R)) u1) true_atom |
|
1254 |
else |
|
1255 |
to_rep (Func (R, Formula Neut)) u1 |
|
1256 |
| Op1 (First, T, R, u1) => to_nth_pair_sel 0 T R u1 |
|
1257 |
| Op1 (Second, T, R, u1) => to_nth_pair_sel 1 T R u1 |
|
1258 |
| Op1 (Cast, _, R, u1) => |
|
1259 |
((case rep_of u1 of |
|
1260 |
Formula _ => |
|
1261 |
(case unopt_rep R of |
|
1262 |
Atom (2, j0) => atom_from_formula kk j0 (to_f u1) |
|
1263 |
| _ => raise SAME ()) |
|
1264 |
| _ => raise SAME ()) |
|
1265 |
handle SAME () => rel_expr_from_rel_expr kk R (rep_of u1) (to_r u1)) |
|
1266 |
| Op2 (All, T, R as Opt _, u1, u2) => |
|
1267 |
to_r (Op1 (Not, T, R, |
|
1268 |
Op2 (Exist, T, R, u1, Op1 (Not, T, rep_of u2, u2)))) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1269 |
| Op2 (Exist, _, Opt _, u1, u2) => |
33192 | 1270 |
let val rs1 = untuple to_decl u1 in |
1271 |
if not (is_opt_rep (rep_of u2)) then |
|
34126 | 1272 |
kk_rel_if (kk_exist rs1 (to_f u2)) true_atom KK.None |
33192 | 1273 |
else |
1274 |
let val r2 = to_r u2 in |
|
1275 |
kk_union (kk_rel_if (kk_exist rs1 (kk_rel_eq r2 true_atom)) |
|
34126 | 1276 |
true_atom KK.None) |
33192 | 1277 |
(kk_rel_if (kk_all rs1 (kk_rel_eq r2 false_atom)) |
34126 | 1278 |
false_atom KK.None) |
33192 | 1279 |
end |
1280 |
end |
|
1281 |
| Op2 (Or, _, _, u1, u2) => |
|
1282 |
if is_opt_rep (rep_of u1) then kk_rel_if (to_f u2) true_atom (to_r u1) |
|
1283 |
else kk_rel_if (to_f u1) true_atom (to_r u2) |
|
1284 |
| Op2 (And, _, _, u1, u2) => |
|
1285 |
if is_opt_rep (rep_of u1) then kk_rel_if (to_f u2) (to_r u1) false_atom |
|
1286 |
else kk_rel_if (to_f u1) (to_r u2) false_atom |
|
1287 |
| Op2 (Less, _, _, u1, u2) => |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1288 |
(case type_of u1 of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1289 |
@{typ nat} => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1290 |
if is_Cst Unrep u1 then to_compare_with_unrep u2 false_atom |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1291 |
else if is_Cst Unrep u2 then to_compare_with_unrep u1 true_atom |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1292 |
else kk_nat_less (to_integer u1) (to_integer u2) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1293 |
| @{typ int} => kk_int_less (to_integer u1) (to_integer u2) |
36127
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1294 |
| _ => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1295 |
let |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1296 |
val R1 = Opt (Atom (card_of_rep (rep_of u1), |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1297 |
offset_of_type ofs (type_of u1))) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1298 |
in |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1299 |
double_rel_rel_let kk |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1300 |
(fn r1 => fn r2 => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1301 |
kk_rel_if |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1302 |
(fold kk_and (map_filter (fn (u, r) => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1303 |
if is_opt_rep (rep_of u) then SOME (kk_some r) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1304 |
else NONE) [(u1, r1), (u2, r2)]) KK.True) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1305 |
(atom_from_formula kk bool_j0 (KK.LT (pairself |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1306 |
(int_expr_from_atom kk (type_of u1)) (r1, r2)))) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1307 |
KK.None) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1308 |
(to_rep R1 u1) (to_rep R1 u2) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1309 |
end) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1310 |
| Op2 (The, _, R, u1, u2) => |
33192 | 1311 |
if is_opt_rep R then |
1312 |
let val r1 = to_opt (Func (unopt_rep R, bool_atom_R)) u1 in |
|
1313 |
kk_rel_if (kk_one (kk_join r1 true_atom)) (kk_join r1 true_atom) |
|
1314 |
(kk_rel_if (kk_or (kk_some (kk_join r1 true_atom)) |
|
1315 |
(kk_subset (full_rel_for_rep R) |
|
1316 |
(kk_join r1 false_atom))) |
|
33892
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1317 |
(to_rep R u2) (empty_rel_for_rep R)) |
33192 | 1318 |
end |
1319 |
else |
|
1320 |
let val r1 = to_rep (Func (R, Formula Neut)) u1 in |
|
1321 |
kk_rel_if (kk_one r1) r1 (to_rep R u2) |
|
1322 |
end |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1323 |
| Op2 (Eps, _, R, u1, u2) => |
33192 | 1324 |
if is_opt_rep (rep_of u1) then |
1325 |
let |
|
1326 |
val r1 = to_rep (Func (unopt_rep R, Opt bool_atom_R)) u1 |
|
1327 |
val r2 = to_rep R u2 |
|
1328 |
in |
|
1329 |
kk_union (kk_rel_if (kk_one (kk_join r1 true_atom)) |
|
33892
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1330 |
(kk_join r1 true_atom) (empty_rel_for_rep R)) |
33192 | 1331 |
(kk_rel_if (kk_or (kk_subset r2 (kk_join r1 true_atom)) |
1332 |
(kk_subset (full_rel_for_rep R) |
|
1333 |
(kk_join r1 false_atom))) |
|
33892
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1334 |
r2 (empty_rel_for_rep R)) |
33192 | 1335 |
end |
1336 |
else |
|
1337 |
let |
|
1338 |
val r1 = to_rep (Func (unopt_rep R, Formula Neut)) u1 |
|
1339 |
val r2 = to_rep R u2 |
|
1340 |
in |
|
33892
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1341 |
kk_union (kk_rel_if (kk_one r1) r1 (empty_rel_for_rep R)) |
33192 | 1342 |
(kk_rel_if (kk_or (kk_no r1) (kk_subset r2 r1)) |
33892
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1343 |
r2 (empty_rel_for_rep R)) |
33192 | 1344 |
end |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1345 |
| Op2 (Triad, _, Opt (Atom (2, j0)), u1, u2) => |
33192 | 1346 |
let |
1347 |
val f1 = to_f u1 |
|
1348 |
val f2 = to_f u2 |
|
1349 |
in |
|
1350 |
if f1 = f2 then |
|
1351 |
atom_from_formula kk j0 f1 |
|
1352 |
else |
|
34126 | 1353 |
kk_union (kk_rel_if f1 true_atom KK.None) |
1354 |
(kk_rel_if f2 KK.None false_atom) |
|
33192 | 1355 |
end |
1356 |
| Op2 (Composition, _, R, u1, u2) => |
|
1357 |
let |
|
33863
fb13147a3050
generate arguments of relational composition in the right order in Nitpick
blanchet
parents:
33854
diff
changeset
|
1358 |
val (a_T, b_T) = HOLogic.dest_prodT (domain_type (type_of u1)) |
fb13147a3050
generate arguments of relational composition in the right order in Nitpick
blanchet
parents:
33854
diff
changeset
|
1359 |
val (_, c_T) = HOLogic.dest_prodT (domain_type (type_of u2)) |
fb13147a3050
generate arguments of relational composition in the right order in Nitpick
blanchet
parents:
33854
diff
changeset
|
1360 |
val ab_k = card_of_domain_from_rep 2 (rep_of u1) |
fb13147a3050
generate arguments of relational composition in the right order in Nitpick
blanchet
parents:
33854
diff
changeset
|
1361 |
val bc_k = card_of_domain_from_rep 2 (rep_of u2) |
33192 | 1362 |
val ac_k = card_of_domain_from_rep 2 R |
1363 |
val a_k = exact_root 2 (ac_k * ab_k div bc_k) |
|
1364 |
val b_k = exact_root 2 (ab_k * bc_k div ac_k) |
|
1365 |
val c_k = exact_root 2 (bc_k * ac_k div ab_k) |
|
1366 |
val a_R = Atom (a_k, offset_of_type ofs a_T) |
|
1367 |
val b_R = Atom (b_k, offset_of_type ofs b_T) |
|
1368 |
val c_R = Atom (c_k, offset_of_type ofs c_T) |
|
1369 |
val body_R = body_rep R |
|
1370 |
in |
|
1371 |
(case body_R of |
|
1372 |
Formula Neut => |
|
33863
fb13147a3050
generate arguments of relational composition in the right order in Nitpick
blanchet
parents:
33854
diff
changeset
|
1373 |
kk_join (to_rep (Func (Struct [a_R, b_R], Formula Neut)) u1) |
fb13147a3050
generate arguments of relational composition in the right order in Nitpick
blanchet
parents:
33854
diff
changeset
|
1374 |
(to_rep (Func (Struct [b_R, c_R], Formula Neut)) u2) |
33192 | 1375 |
| Opt (Atom (2, _)) => |
1376 |
let |
|
33886
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1377 |
(* FIXME: merge with similar code above *) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1378 |
fun must R1 R2 u = |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1379 |
kk_join (to_rep (Func (Struct [R1, R2], body_R)) u) true_atom |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1380 |
fun may R1 R2 u = |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1381 |
kk_difference |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1382 |
(full_rel_for_rep (Struct [R1, R2])) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1383 |
(kk_join (to_rep (Func (Struct [R1, R2], body_R)) u) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1384 |
false_atom) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1385 |
in |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1386 |
kk_union |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1387 |
(kk_product (kk_join (must a_R b_R u1) (must b_R c_R u2)) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1388 |
true_atom) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1389 |
(kk_product (kk_difference |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1390 |
(full_rel_for_rep (Struct [a_R, c_R])) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1391 |
(kk_join (may a_R b_R u1) (may b_R c_R u2))) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1392 |
false_atom) |
cde73f8dbe4e
fix soundness bug in Nitpick's Kodkod generator for the relational composition case
blanchet
parents:
33863
diff
changeset
|
1393 |
end |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1394 |
| _ => raise NUT ("Nitpick_Kodkod.to_r (Composition)", [u])) |
33192 | 1395 |
|> rel_expr_from_rel_expr kk R (Func (Struct [a_R, c_R], body_R)) |
1396 |
end |
|
1397 |
| Op2 (Product, T, R, u1, u2) => |
|
1398 |
let |
|
1399 |
val (a_T, b_T) = HOLogic.dest_prodT (domain_type T) |
|
1400 |
val a_k = card_of_domain_from_rep 2 (rep_of u1) |
|
1401 |
val b_k = card_of_domain_from_rep 2 (rep_of u2) |
|
1402 |
val a_R = Atom (a_k, offset_of_type ofs a_T) |
|
1403 |
val b_R = Atom (b_k, offset_of_type ofs b_T) |
|
1404 |
val body_R = body_rep R |
|
1405 |
in |
|
1406 |
(case body_R of |
|
1407 |
Formula Neut => |
|
1408 |
kk_product (to_rep (Func (a_R, Formula Neut)) u1) |
|
1409 |
(to_rep (Func (b_R, Formula Neut)) u2) |
|
1410 |
| Opt (Atom (2, _)) => |
|
1411 |
let |
|
1412 |
fun do_nut r R u = kk_join (to_rep (Func (R, body_R)) u) r |
|
1413 |
fun do_term r = |
|
1414 |
kk_product (kk_product (do_nut r a_R u1) (do_nut r b_R u2)) r |
|
1415 |
in kk_union (do_term true_atom) (do_term false_atom) end |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1416 |
| _ => raise NUT ("Nitpick_Kodkod.to_r (Product)", [u])) |
33192 | 1417 |
|> rel_expr_from_rel_expr kk R (Func (Struct [a_R, b_R], body_R)) |
1418 |
end |
|
1419 |
| Op2 (Image, T, R, u1, u2) => |
|
1420 |
(case (rep_of u1, rep_of u2) of |
|
1421 |
(Func (R11, R12), Func (R21, Formula Neut)) => |
|
1422 |
if R21 = R11 andalso is_lone_rep R12 then |
|
1423 |
let |
|
1424 |
fun big_join r = kk_n_fold_join kk false R21 R12 r (to_r u1) |
|
1425 |
val core_r = big_join (to_r u2) |
|
1426 |
val core_R = Func (R12, Formula Neut) |
|
1427 |
in |
|
1428 |
if is_opt_rep R12 then |
|
1429 |
let |
|
1430 |
val schema = atom_schema_of_rep R21 |
|
1431 |
val decls = decls_for_atom_schema ~1 schema |
|
1432 |
val vars = unary_var_seq ~1 (length decls) |
|
1433 |
val f = kk_some (big_join (fold1 kk_product vars)) |
|
1434 |
in |
|
1435 |
kk_rel_if (kk_all decls f) |
|
1436 |
(rel_expr_from_rel_expr kk R core_R core_r) |
|
1437 |
(rel_expr_from_rel_expr kk R (opt_rep ofs T core_R) |
|
1438 |
(kk_product core_r true_atom)) |
|
1439 |
end |
|
1440 |
else |
|
1441 |
rel_expr_from_rel_expr kk R core_R core_r |
|
1442 |
end |
|
1443 |
else |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1444 |
raise NUT ("Nitpick_Kodkod.to_r (Image)", [u1, u2]) |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1445 |
| _ => raise NUT ("Nitpick_Kodkod.to_r (Image)", [u1, u2])) |
33192 | 1446 |
| Op2 (Apply, @{typ nat}, _, |
1447 |
Op2 (Apply, _, _, Cst (Subtract, _, _), u1), u2) => |
|
1448 |
if is_Cst Unrep u2 andalso not (is_opt_rep (rep_of u1)) then |
|
34126 | 1449 |
KK.Atom (offset_of_type ofs nat_T) |
33192 | 1450 |
else |
34126 | 1451 |
fold kk_join (map to_integer [u1, u2]) (KK.Rel nat_subtract_rel) |
35312 | 1452 |
| Op2 (Apply, _, R, u1, u2) => to_apply R u1 u2 |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1453 |
| Op2 (Lambda, _, R as Opt (Atom (1, j0)), u1, u2) => |
34126 | 1454 |
to_guard [u1, u2] R (KK.Atom j0) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1455 |
| Op2 (Lambda, _, Func (_, Formula Neut), u1, u2) => |
33192 | 1456 |
kk_comprehension (untuple to_decl u1) (to_f u2) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1457 |
| Op2 (Lambda, _, Func (_, R2), u1, u2) => |
33192 | 1458 |
let |
1459 |
val dom_decls = untuple to_decl u1 |
|
1460 |
val ran_schema = atom_schema_of_rep R2 |
|
1461 |
val ran_decls = decls_for_atom_schema ~1 ran_schema |
|
1462 |
val ran_vars = unary_var_seq ~1 (length ran_decls) |
|
1463 |
in |
|
1464 |
kk_comprehension (dom_decls @ ran_decls) |
|
1465 |
(kk_subset (fold1 kk_product ran_vars) |
|
1466 |
(to_rep R2 u2)) |
|
1467 |
end |
|
1468 |
| Op3 (Let, _, R, u1, u2, u3) => |
|
1469 |
kk_rel_let [to_expr_assign u1 u2] (to_rep R u3) |
|
1470 |
| Op3 (If, _, R, u1, u2, u3) => |
|
1471 |
if is_opt_rep (rep_of u1) then |
|
35386
45a4e19d3ebd
more work on the new monotonicity stuff in Nitpick
blanchet
parents:
35385
diff
changeset
|
1472 |
triple_rel_rel_let kk |
33192 | 1473 |
(fn r1 => fn r2 => fn r3 => |
1474 |
let val empty_r = empty_rel_for_rep R in |
|
1475 |
fold1 kk_union |
|
1476 |
[kk_rel_if (kk_rel_eq r1 true_atom) r2 empty_r, |
|
1477 |
kk_rel_if (kk_rel_eq r1 false_atom) r3 empty_r, |
|
1478 |
kk_rel_if (kk_rel_eq r2 r3) |
|
1479 |
(if inline_rel_expr r2 then r2 else r3) empty_r] |
|
1480 |
end) |
|
1481 |
(to_r u1) (to_rep R u2) (to_rep R u3) |
|
1482 |
else |
|
1483 |
kk_rel_if (to_f u1) (to_rep R u2) (to_rep R u3) |
|
1484 |
| Tuple (_, R, us) => |
|
1485 |
(case unopt_rep R of |
|
1486 |
Struct Rs => to_product Rs us |
|
1487 |
| Vect (k, R) => to_product (replicate k R) us |
|
1488 |
| Atom (1, j0) => |
|
1489 |
(case filter (not_equal Unit o rep_of) us of |
|
34126 | 1490 |
[] => KK.Atom j0 |
1491 |
| us' => kk_rel_if (kk_some (fold1 kk_product (map to_r us'))) |
|
1492 |
(KK.Atom j0) KK.None) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1493 |
| _ => raise NUT ("Nitpick_Kodkod.to_r (Tuple)", [u])) |
33192 | 1494 |
| Construct ([u'], _, _, []) => to_r u' |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1495 |
| Construct (discr_u :: sel_us, _, _, arg_us) => |
33192 | 1496 |
let |
1497 |
val set_rs = |
|
1498 |
map2 (fn sel_u => fn arg_u => |
|
1499 |
let |
|
1500 |
val (R1, R2) = dest_Func (rep_of sel_u) |
|
1501 |
val sel_r = to_r sel_u |
|
1502 |
val arg_r = to_opt R2 arg_u |
|
1503 |
in |
|
1504 |
if is_one_rep R2 then |
|
1505 |
kk_n_fold_join kk true R2 R1 arg_r |
|
1506 |
(kk_project sel_r (flip_nums (arity_of_rep R2))) |
|
1507 |
else |
|
34288
cf455b5880e1
reduced arity of Nitpick selectors associated with sets by 1, by using "Formula" instead of "Atom 2"
blanchet
parents:
34126
diff
changeset
|
1508 |
kk_comprehension [KK.DeclOne ((1, ~1), to_r discr_u)] |
34126 | 1509 |
(kk_rel_eq (kk_join (KK.Var (1, ~1)) sel_r) arg_r) |
35695 | 1510 |
|> is_opt_rep (rep_of arg_u) ? to_guard [arg_u] R1 |
33192 | 1511 |
end) sel_us arg_us |
1512 |
in fold1 kk_intersect set_rs end |
|
34126 | 1513 |
| BoundRel (x, _, _, _) => KK.Var x |
1514 |
| FreeRel (x, _, _, _) => KK.Rel x |
|
1515 |
| RelReg (j, _, R) => KK.RelReg (arity_of_rep R, j) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1516 |
| u => raise NUT ("Nitpick_Kodkod.to_r", [u]) |
33192 | 1517 |
and to_decl (BoundRel (x, _, R, _)) = |
34126 | 1518 |
KK.DeclOne (x, KK.AtomSeq (the_single (atom_schema_of_rep R))) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1519 |
| to_decl u = raise NUT ("Nitpick_Kodkod.to_decl", [u]) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1520 |
and to_expr_assign (FormulaReg (j, _, _)) u = |
34126 | 1521 |
KK.AssignFormulaReg (j, to_f u) |
33192 | 1522 |
| to_expr_assign (RelReg (j, _, R)) u = |
34126 | 1523 |
KK.AssignRelReg ((arity_of_rep R, j), to_r u) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1524 |
| to_expr_assign u1 _ = raise NUT ("Nitpick_Kodkod.to_expr_assign", [u1]) |
33192 | 1525 |
and to_atom (x as (k, j0)) u = |
1526 |
case rep_of u of |
|
1527 |
Formula _ => atom_from_formula kk j0 (to_f u) |
|
34126 | 1528 |
| Unit => if k = 1 then KK.Atom j0 |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1529 |
else raise NUT ("Nitpick_Kodkod.to_atom", [u]) |
33192 | 1530 |
| R => atom_from_rel_expr kk x R (to_r u) |
1531 |
and to_struct Rs u = |
|
1532 |
case rep_of u of |
|
1533 |
Unit => full_rel_for_rep (Struct Rs) |
|
1534 |
| R' => struct_from_rel_expr kk Rs R' (to_r u) |
|
1535 |
and to_vect k R u = |
|
1536 |
case rep_of u of |
|
1537 |
Unit => full_rel_for_rep (Vect (k, R)) |
|
1538 |
| R' => vect_from_rel_expr kk k R R' (to_r u) |
|
1539 |
and to_func R1 R2 u = |
|
1540 |
case rep_of u of |
|
1541 |
Unit => full_rel_for_rep (Func (R1, R2)) |
|
1542 |
| R' => rel_expr_to_func kk R1 R2 R' (to_r u) |
|
1543 |
and to_opt R u = |
|
1544 |
let val old_R = rep_of u in |
|
1545 |
if is_opt_rep old_R then |
|
1546 |
rel_expr_from_rel_expr kk (Opt R) old_R (to_r u) |
|
1547 |
else |
|
1548 |
to_rep R u |
|
1549 |
end |
|
1550 |
and to_rep (Atom x) u = to_atom x u |
|
1551 |
| to_rep (Struct Rs) u = to_struct Rs u |
|
1552 |
| to_rep (Vect (k, R)) u = to_vect k R u |
|
1553 |
| to_rep (Func (R1, R2)) u = to_func R1 R2 u |
|
1554 |
| to_rep (Opt R) u = to_opt R u |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1555 |
| to_rep R _ = raise REP ("Nitpick_Kodkod.to_rep", [R]) |
33192 | 1556 |
and to_integer u = to_opt (one_rep ofs (type_of u) (rep_of u)) u |
1557 |
and to_guard guard_us R r = |
|
1558 |
let |
|
1559 |
val unpacked_rs = unpack_joins r |
|
1560 |
val plain_guard_rs = |
|
1561 |
map to_r (filter (is_Opt o rep_of) guard_us) |
|
1562 |
|> filter_out (member (op =) unpacked_rs) |
|
1563 |
val func_guard_us = |
|
1564 |
filter ((is_Func andf is_opt_rep) o rep_of) guard_us |
|
1565 |
val func_guard_rs = map to_r func_guard_us |
|
1566 |
val guard_fs = |
|
1567 |
map kk_no plain_guard_rs @ |
|
1568 |
map2 (kk_not oo kk_n_ary_function kk) |
|
1569 |
(map (unopt_rep o rep_of) func_guard_us) func_guard_rs |
|
1570 |
in |
|
35695 | 1571 |
if null guard_fs then r |
1572 |
else kk_rel_if (fold1 kk_or guard_fs) (empty_rel_for_rep R) r |
|
33192 | 1573 |
end |
1574 |
and to_project new_R old_R r j0 = |
|
1575 |
rel_expr_from_rel_expr kk new_R old_R |
|
1576 |
(kk_project_seq r j0 (arity_of_rep old_R)) |
|
1577 |
and to_product Rs us = |
|
1578 |
case map (uncurry to_opt) (filter (not_equal Unit o fst) (Rs ~~ us)) of |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1579 |
[] => raise REP ("Nitpick_Kodkod.to_product", Rs) |
33192 | 1580 |
| rs => fold1 kk_product rs |
1581 |
and to_nth_pair_sel n res_T res_R u = |
|
1582 |
case u of |
|
1583 |
Tuple (_, _, us) => to_rep res_R (nth us n) |
|
1584 |
| _ => let |
|
1585 |
val R = rep_of u |
|
1586 |
val (a_T, b_T) = HOLogic.dest_prodT (type_of u) |
|
1587 |
val Rs = |
|
1588 |
case unopt_rep R of |
|
1589 |
Struct (Rs as [_, _]) => Rs |
|
1590 |
| _ => |
|
1591 |
let |
|
1592 |
val res_card = card_of_rep res_R |
|
1593 |
val other_card = card_of_rep R div res_card |
|
1594 |
val (a_card, b_card) = (res_card, other_card) |
|
1595 |
|> n = 1 ? swap |
|
1596 |
in |
|
1597 |
[Atom (a_card, offset_of_type ofs a_T), |
|
1598 |
Atom (b_card, offset_of_type ofs b_T)] |
|
1599 |
end |
|
1600 |
val nth_R = nth Rs n |
|
1601 |
val j0 = if n = 0 then 0 else arity_of_rep (hd Rs) |
|
1602 |
in |
|
1603 |
case arity_of_rep nth_R of |
|
1604 |
0 => to_guard [u] res_R |
|
1605 |
(to_rep res_R (Cst (Unity, res_T, Unit))) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1606 |
| _ => to_project res_R nth_R (to_rep (Opt (Struct Rs)) u) j0 |
33192 | 1607 |
end |
1608 |
and to_set_bool_op connective set_oper u1 u2 = |
|
1609 |
let |
|
1610 |
val min_R = min_rep (rep_of u1) (rep_of u2) |
|
1611 |
val r1 = to_rep min_R u1 |
|
1612 |
val r2 = to_rep min_R u2 |
|
1613 |
in |
|
1614 |
case min_R of |
|
1615 |
Vect (k, Atom _) => kk_vect_set_bool_op connective k r1 r2 |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1616 |
| Func (_, Formula Neut) => set_oper r1 r2 |
33192 | 1617 |
| Func (Unit, Atom (2, j0)) => |
1618 |
connective (formula_from_atom j0 r1) (formula_from_atom j0 r2) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1619 |
| Func (_, Atom _) => set_oper (kk_join r1 true_atom) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1620 |
(kk_join r2 true_atom) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1621 |
| _ => raise REP ("Nitpick_Kodkod.to_set_bool_op", [min_R]) |
33192 | 1622 |
end |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1623 |
and to_bit_word_unary_op T R oper = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1624 |
let |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1625 |
val Ts = strip_type T ||> single |> op @ |
34126 | 1626 |
fun int_arg j = int_expr_from_atom kk (nth Ts j) (KK.Var (1, j)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1627 |
in |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1628 |
kk_comprehension (decls_for_atom_schema 0 (atom_schema_of_rep R)) |
34126 | 1629 |
(KK.FormulaLet |
1630 |
(map (fn j => KK.AssignIntReg (j, int_arg j)) (0 upto 1), |
|
1631 |
KK.IntEq (KK.IntReg 1, oper (KK.IntReg 0)))) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1632 |
end |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1633 |
and to_bit_word_binary_op T R opt_guard opt_oper = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1634 |
let |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1635 |
val Ts = strip_type T ||> single |> op @ |
34126 | 1636 |
fun int_arg j = int_expr_from_atom kk (nth Ts j) (KK.Var (1, j)) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1637 |
in |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1638 |
kk_comprehension (decls_for_atom_schema 0 (atom_schema_of_rep R)) |
34126 | 1639 |
(KK.FormulaLet |
1640 |
(map (fn j => KK.AssignIntReg (j, int_arg j)) (0 upto 2), |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1641 |
fold1 kk_and |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1642 |
((case opt_guard of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1643 |
NONE => [] |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1644 |
| SOME guard => |
34126 | 1645 |
[guard (KK.IntReg 0) (KK.IntReg 1) (KK.IntReg 2)]) @ |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1646 |
(case opt_oper of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1647 |
NONE => [] |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1648 |
| SOME oper => |
34126 | 1649 |
[KK.IntEq (KK.IntReg 2, |
1650 |
oper (KK.IntReg 0) (KK.IntReg 1))])))) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34121
diff
changeset
|
1651 |
end |
36127
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1652 |
and to_apply (R as Formula _) func_u arg_u = |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1653 |
raise REP ("Nitpick_Kodkod.to_apply", [R]) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1654 |
| to_apply res_R func_u arg_u = |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1655 |
case unopt_rep (rep_of func_u) of |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1656 |
Unit => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1657 |
let val j0 = offset_of_type ofs (type_of func_u) in |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1658 |
to_guard [arg_u] res_R |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1659 |
(rel_expr_from_rel_expr kk res_R (Atom (1, j0)) (KK.Atom j0)) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1660 |
end |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1661 |
| Atom (1, j0) => |
33192 | 1662 |
to_guard [arg_u] res_R |
36127
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1663 |
(rel_expr_from_rel_expr kk res_R (Atom (1, j0)) (to_r func_u)) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1664 |
| Atom (k, _) => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1665 |
let |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1666 |
val dom_card = card_of_rep (rep_of arg_u) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1667 |
val ran_R = Atom (exact_root dom_card k, |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1668 |
offset_of_type ofs (range_type (type_of func_u))) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1669 |
in |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1670 |
to_apply_vect dom_card ran_R res_R (to_vect dom_card ran_R func_u) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1671 |
arg_u |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1672 |
end |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1673 |
| Vect (1, R') => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1674 |
to_guard [arg_u] res_R |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1675 |
(rel_expr_from_rel_expr kk res_R R' (to_r func_u)) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1676 |
| Vect (k, R') => to_apply_vect k R' res_R (to_r func_u) arg_u |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1677 |
| Func (R, Formula Neut) => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1678 |
to_guard [arg_u] res_R (rel_expr_from_formula kk res_R |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1679 |
(kk_subset (to_opt R arg_u) (to_r func_u))) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1680 |
| Func (Unit, R2) => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1681 |
to_guard [arg_u] res_R |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1682 |
(rel_expr_from_rel_expr kk res_R R2 (to_r func_u)) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1683 |
| Func (R1, R2) => |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1684 |
rel_expr_from_rel_expr kk res_R R2 |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1685 |
(kk_n_fold_join kk true R1 R2 (to_opt R1 arg_u) (to_r func_u)) |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1686 |
|> body_rep R2 = Formula Neut ? to_guard [arg_u] res_R |
e91292c520be
fix bug in Nitpick's handling of "<" (exposed by "GCD.setprod_coprime_int")
blanchet
parents:
35695
diff
changeset
|
1687 |
| _ => raise NUT ("Nitpick_Kodkod.to_apply", [func_u]) |
33192 | 1688 |
and to_apply_vect k R' res_R func_r arg_u = |
1689 |
let |
|
1690 |
val arg_R = one_rep ofs (type_of arg_u) (unopt_rep (rep_of arg_u)) |
|
1691 |
val vect_r = vect_from_rel_expr kk k res_R (Vect (k, R')) func_r |
|
1692 |
val vect_rs = unpack_vect_in_chunks kk (arity_of_rep res_R) k vect_r |
|
1693 |
in |
|
1694 |
kk_case_switch kk arg_R res_R (to_opt arg_R arg_u) |
|
1695 |
(all_singletons_for_rep arg_R) vect_rs |
|
1696 |
end |
|
1697 |
and to_could_be_unrep neg u = |
|
34126 | 1698 |
if neg andalso is_opt_rep (rep_of u) then kk_no (to_r u) else KK.False |
33192 | 1699 |
and to_compare_with_unrep u r = |
33892
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1700 |
if is_opt_rep (rep_of u) then |
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1701 |
kk_rel_if (kk_some (to_r u)) r (empty_rel_for_rep (rep_of u)) |
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1702 |
else |
3937da7e13ea
fixed arity of some empty relations in Nitpick's Kodkod generator;
blanchet
parents:
33886
diff
changeset
|
1703 |
r |
33192 | 1704 |
in to_f_with_polarity Pos u end |
1705 |
||
1706 |
end; |