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