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