author | blanchet |
Wed, 24 Mar 2010 14:43:35 +0100 | |
changeset 35968 | b7f98ff9c7d9 |
parent 35814 | 234eaa508359 |
child 36380 | 1e8fcaccb3e8 |
permissions | -rw-r--r-- |
33982 | 1 |
(* Title: HOL/Tools/Nitpick/nitpick_scope.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 |
Scope enumerator for Nitpick. |
|
6 |
*) |
|
7 |
||
8 |
signature NITPICK_SCOPE = |
|
9 |
sig |
|
33705
947184dc75c9
removed a few global names in Nitpick (styp, nat_less, pairf)
blanchet
parents:
33580
diff
changeset
|
10 |
type styp = Nitpick_Util.styp |
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
11 |
type hol_context = Nitpick_HOL.hol_context |
33192 | 12 |
|
13 |
type constr_spec = { |
|
14 |
const: styp, |
|
15 |
delta: int, |
|
16 |
epsilon: int, |
|
17 |
exclusive: bool, |
|
18 |
explicit_max: int, |
|
19 |
total: bool} |
|
20 |
||
21 |
type dtype_spec = { |
|
22 |
typ: typ, |
|
23 |
card: int, |
|
24 |
co: bool, |
|
35179
4b198af5beb5
redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents:
35178
diff
changeset
|
25 |
standard: bool, |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
26 |
complete: bool * bool, |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
27 |
concrete: bool * bool, |
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
|
28 |
deep: bool, |
33192 | 29 |
constrs: constr_spec list} |
30 |
||
31 |
type scope = { |
|
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
32 |
hol_ctxt: hol_context, |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
33 |
binarize: bool, |
33192 | 34 |
card_assigns: (typ * int) list, |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
35 |
bits: int, |
33192 | 36 |
bisim_depth: int, |
37 |
datatypes: dtype_spec list, |
|
38 |
ofs: int Typtab.table} |
|
39 |
||
40 |
val datatype_spec : dtype_spec list -> typ -> dtype_spec option |
|
41 |
val constr_spec : dtype_spec list -> styp -> constr_spec |
|
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
42 |
val is_complete_type : dtype_spec list -> bool -> typ -> bool |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
43 |
val is_concrete_type : dtype_spec list -> bool -> typ -> bool |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
44 |
val is_exact_type : dtype_spec list -> bool -> typ -> bool |
33192 | 45 |
val offset_of_type : int Typtab.table -> typ -> int |
46 |
val spec_of_type : scope -> typ -> int * int |
|
47 |
val pretties_for_scope : scope -> bool -> Pretty.T list |
|
48 |
val multiline_string_for_scope : scope -> string |
|
35814 | 49 |
val scopes_equivalent : scope * scope -> bool |
33192 | 50 |
val scope_less_eq : scope -> scope -> bool |
51 |
val all_scopes : |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
52 |
hol_context -> bool -> int -> (typ option * int list) list |
33192 | 53 |
-> (styp option * int list) list -> (styp option * int list) list |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
54 |
-> int list -> int list -> typ list -> typ list -> typ list -> typ list |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
55 |
-> int * scope list |
33192 | 56 |
end; |
57 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
58 |
structure Nitpick_Scope : NITPICK_SCOPE = |
33192 | 59 |
struct |
60 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
61 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
62 |
open Nitpick_HOL |
33192 | 63 |
|
64 |
type constr_spec = { |
|
65 |
const: styp, |
|
66 |
delta: int, |
|
67 |
epsilon: int, |
|
68 |
exclusive: bool, |
|
69 |
explicit_max: int, |
|
70 |
total: bool} |
|
71 |
||
72 |
type dtype_spec = { |
|
73 |
typ: typ, |
|
74 |
card: int, |
|
75 |
co: bool, |
|
35179
4b198af5beb5
redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents:
35178
diff
changeset
|
76 |
standard: bool, |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
77 |
complete: bool * bool, |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
78 |
concrete: bool * bool, |
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
|
79 |
deep: bool, |
33192 | 80 |
constrs: constr_spec list} |
81 |
||
82 |
type scope = { |
|
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
83 |
hol_ctxt: hol_context, |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
84 |
binarize: bool, |
33192 | 85 |
card_assigns: (typ * int) list, |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
86 |
bits: int, |
33192 | 87 |
bisim_depth: int, |
88 |
datatypes: dtype_spec list, |
|
89 |
ofs: int Typtab.table} |
|
90 |
||
91 |
datatype row_kind = Card of typ | Max of styp |
|
92 |
||
93 |
type row = row_kind * int list |
|
94 |
type block = row list |
|
95 |
||
96 |
(* dtype_spec list -> typ -> dtype_spec option *) |
|
97 |
fun datatype_spec (dtypes : dtype_spec list) T = |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
98 |
List.find (curry (op =) T o #typ) dtypes |
33192 | 99 |
|
100 |
(* dtype_spec list -> styp -> constr_spec *) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
101 |
fun constr_spec [] x = raise TERM ("Nitpick_Scope.constr_spec", [Const x]) |
33192 | 102 |
| constr_spec ({constrs, ...} :: dtypes : dtype_spec list) (x as (s, T)) = |
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
103 |
case List.find (curry (op =) (s, body_type T) o (apsnd body_type o #const)) |
33192 | 104 |
constrs of |
105 |
SOME c => c |
|
106 |
| NONE => constr_spec dtypes x |
|
107 |
||
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
108 |
(* dtype_spec list -> bool -> typ -> bool *) |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
109 |
fun is_complete_type dtypes facto (Type (@{type_name fun}, [T1, T2])) = |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
110 |
is_concrete_type dtypes facto T1 andalso is_complete_type dtypes facto T2 |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
111 |
| is_complete_type dtypes facto (Type (@{type_name fin_fun}, [T1, T2])) = |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
112 |
is_exact_type dtypes facto T1 andalso is_complete_type dtypes facto T2 |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
113 |
| is_complete_type dtypes facto (Type (@{type_name "*"}, Ts)) = |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
114 |
forall (is_complete_type dtypes facto) Ts |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
115 |
| is_complete_type dtypes facto T = |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
116 |
not (is_integer_like_type T) andalso not (is_bit_type T) andalso |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
117 |
fun_from_pair (#complete (the (datatype_spec dtypes T))) facto |
33192 | 118 |
handle Option.Option => true |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
119 |
and is_concrete_type dtypes facto (Type (@{type_name fun}, [T1, T2])) = |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
120 |
is_complete_type dtypes facto T1 andalso is_concrete_type dtypes facto T2 |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
121 |
| is_concrete_type dtypes facto (Type (@{type_name fin_fun}, [_, T2])) = |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
122 |
is_concrete_type dtypes facto T2 |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
123 |
| is_concrete_type dtypes facto (Type (@{type_name "*"}, Ts)) = |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
124 |
forall (is_concrete_type dtypes facto) Ts |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
125 |
| is_concrete_type dtypes facto T = |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
126 |
fun_from_pair (#concrete (the (datatype_spec dtypes T))) facto |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
127 |
handle Option.Option => true |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
128 |
and is_exact_type dtypes facto = |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
129 |
is_complete_type dtypes facto andf is_concrete_type dtypes facto |
33192 | 130 |
|
131 |
(* int Typtab.table -> typ -> int *) |
|
132 |
fun offset_of_type ofs T = |
|
133 |
case Typtab.lookup ofs T of |
|
134 |
SOME j0 => j0 |
|
135 |
| NONE => Typtab.lookup ofs dummyT |> the_default 0 |
|
136 |
||
137 |
(* scope -> typ -> int * int *) |
|
138 |
fun spec_of_type ({card_assigns, ofs, ...} : scope) T = |
|
139 |
(card_of_type card_assigns T |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
140 |
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => ~1, offset_of_type ofs T) |
33192 | 141 |
|
142 |
(* (string -> string) -> scope |
|
143 |
-> string list * string list * string list * string list * string list *) |
|
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
144 |
fun quintuple_for_scope quote |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
145 |
({hol_ctxt = {thy, ctxt, stds, ...}, card_assigns, bits, bisim_depth, |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
146 |
datatypes, ...} : scope) = |
33192 | 147 |
let |
35075
888802be2019
handle Nitpick's nonstandard model enumeration in a cleaner way;
blanchet
parents:
35072
diff
changeset
|
148 |
val boring_Ts = [@{typ unsigned_bit}, @{typ signed_bit}, |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
149 |
@{typ bisim_iterator}] |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
150 |
val (iter_assigns, card_assigns) = |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
151 |
card_assigns |> filter_out (member (op =) boring_Ts o fst) |
33192 | 152 |
|> List.partition (is_fp_iterator_type o fst) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
153 |
val (secondary_card_assigns, primary_card_assigns) = |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
154 |
card_assigns |> List.partition ((is_integer_type orf is_datatype thy stds) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
155 |
o fst) |
33192 | 156 |
val cards = |
157 |
map (fn (T, k) => quote (string_for_type ctxt T) ^ " = " ^ |
|
158 |
string_of_int k) |
|
159 |
fun maxes () = |
|
160 |
maps (map_filter |
|
161 |
(fn {const, explicit_max, ...} => |
|
162 |
if explicit_max < 0 then |
|
163 |
NONE |
|
164 |
else |
|
165 |
SOME (Syntax.string_of_term ctxt (Const const) ^ " = " ^ |
|
166 |
string_of_int explicit_max)) |
|
167 |
o #constrs) datatypes |
|
168 |
fun iters () = |
|
169 |
map (fn (T, k) => |
|
170 |
quote (Syntax.string_of_term ctxt |
|
171 |
(Const (const_for_iterator_type T))) ^ " = " ^ |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
172 |
string_of_int (k - 1)) iter_assigns |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
173 |
fun miscs () = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
174 |
(if bits = 0 then [] else ["bits = " ^ string_of_int bits]) @ |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
175 |
(if bisim_depth < 0 andalso forall (not o #co) datatypes then [] |
35178 | 176 |
else ["bisim_depth = " ^ signed_string_of_int bisim_depth]) |
33192 | 177 |
in |
178 |
setmp_show_all_types |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
179 |
(fn () => (cards primary_card_assigns, cards secondary_card_assigns, |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
180 |
maxes (), iters (), miscs ())) () |
33192 | 181 |
end |
182 |
||
183 |
(* scope -> bool -> Pretty.T list *) |
|
184 |
fun pretties_for_scope scope verbose = |
|
185 |
let |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
186 |
val (primary_cards, secondary_cards, maxes, iters, bisim_depths) = |
33192 | 187 |
quintuple_for_scope maybe_quote scope |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
188 |
val ss = map (prefix "card ") primary_cards @ |
33192 | 189 |
(if verbose then |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
190 |
map (prefix "card ") secondary_cards @ |
33192 | 191 |
map (prefix "max ") maxes @ |
192 |
map (prefix "iter ") iters @ |
|
193 |
bisim_depths |
|
194 |
else |
|
195 |
[]) |
|
196 |
in |
|
35968
b7f98ff9c7d9
simplify Nitpick parameter parsing code a little bit + make compile
blanchet
parents:
35814
diff
changeset
|
197 |
if null ss then |
b7f98ff9c7d9
simplify Nitpick parameter parsing code a little bit + make compile
blanchet
parents:
35814
diff
changeset
|
198 |
[] |
b7f98ff9c7d9
simplify Nitpick parameter parsing code a little bit + make compile
blanchet
parents:
35814
diff
changeset
|
199 |
else |
b7f98ff9c7d9
simplify Nitpick parameter parsing code a little bit + make compile
blanchet
parents:
35814
diff
changeset
|
200 |
Sledgehammer_Util.serial_commas "and" ss |
b7f98ff9c7d9
simplify Nitpick parameter parsing code a little bit + make compile
blanchet
parents:
35814
diff
changeset
|
201 |
|> map Pretty.str |> Pretty.breaks |
33192 | 202 |
end |
203 |
||
204 |
(* scope -> string *) |
|
205 |
fun multiline_string_for_scope scope = |
|
206 |
let |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
207 |
val (primary_cards, secondary_cards, maxes, iters, bisim_depths) = |
33192 | 208 |
quintuple_for_scope I scope |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
209 |
val cards = primary_cards @ secondary_cards |
33192 | 210 |
in |
211 |
case (if null cards then [] else ["card: " ^ commas cards]) @ |
|
212 |
(if null maxes then [] else ["max: " ^ commas maxes]) @ |
|
213 |
(if null iters then [] else ["iter: " ^ commas iters]) @ |
|
214 |
bisim_depths of |
|
215 |
[] => "empty" |
|
216 |
| lines => space_implode "\n" lines |
|
217 |
end |
|
218 |
||
35814 | 219 |
(* scope * scope -> bool *) |
220 |
fun scopes_equivalent (s1 : scope, s2 : scope) = |
|
33192 | 221 |
#datatypes s1 = #datatypes s2 andalso #card_assigns s1 = #card_assigns s2 |
222 |
fun scope_less_eq (s1 : scope) (s2 : scope) = |
|
223 |
(s1, s2) |> pairself (map snd o #card_assigns) |> op ~~ |> forall (op <=) |
|
224 |
||
225 |
(* row -> int *) |
|
226 |
fun rank_of_row (_, ks) = length ks |
|
227 |
(* block -> int *) |
|
228 |
fun rank_of_block block = fold Integer.max (map rank_of_row block) 1 |
|
229 |
(* int -> typ * int list -> typ * int list *) |
|
230 |
fun project_row column (y, ks) = (y, [nth ks (Int.min (column, length ks - 1))]) |
|
231 |
(* int -> block -> block *) |
|
232 |
fun project_block (column, block) = map (project_row column) block |
|
233 |
||
234 |
(* (''a * ''a -> bool) -> (''a option * int list) list -> ''a -> int list *) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
235 |
fun lookup_ints_assign eq assigns key = |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
236 |
case triple_lookup eq assigns key of |
33192 | 237 |
SOME ks => ks |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
238 |
| NONE => raise ARG ("Nitpick_Scope.lookup_ints_assign", "") |
33192 | 239 |
(* theory -> (typ option * int list) list -> typ -> int list *) |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
240 |
fun lookup_type_ints_assign thy assigns T = |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
241 |
map (curry Int.max 1) (lookup_ints_assign (type_match thy) assigns T) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
242 |
handle ARG ("Nitpick_Scope.lookup_ints_assign", _) => |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
243 |
raise TYPE ("Nitpick_Scope.lookup_type_ints_assign", [T], []) |
33192 | 244 |
(* theory -> (styp option * int list) list -> styp -> int list *) |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
245 |
fun lookup_const_ints_assign thy assigns x = |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
246 |
lookup_ints_assign (const_match thy) assigns x |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
247 |
handle ARG ("Nitpick_Scope.lookup_ints_assign", _) => |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
248 |
raise TERM ("Nitpick_Scope.lookup_const_ints_assign", [Const x]) |
33192 | 249 |
|
250 |
(* theory -> (styp option * int list) list -> styp -> row option *) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
251 |
fun row_for_constr thy maxes_assigns constr = |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
252 |
SOME (Max constr, lookup_const_ints_assign thy maxes_assigns constr) |
33192 | 253 |
handle TERM ("lookup_const_ints_assign", _) => NONE |
254 |
||
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
255 |
val max_bits = 31 (* Kodkod limit *) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
256 |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
257 |
(* hol_context -> bool -> (typ option * int list) list |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
258 |
-> (styp option * int list) list -> (styp option * int list) list -> int list |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
259 |
-> int list -> typ -> block *) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
260 |
fun block_for_type (hol_ctxt as {thy, ...}) binarize cards_assigns maxes_assigns |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
261 |
iters_assigns bitss bisim_depths T = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
262 |
if T = @{typ unsigned_bit} then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
263 |
[(Card T, map (Integer.min max_bits o Integer.max 1) bitss)] |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
264 |
else if T = @{typ signed_bit} then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
265 |
[(Card T, map (Integer.add 1 o Integer.min max_bits o Integer.max 1) bitss)] |
34126 | 266 |
else if T = @{typ "unsigned_bit word"} then |
267 |
[(Card T, lookup_type_ints_assign thy cards_assigns nat_T)] |
|
268 |
else if T = @{typ "signed_bit word"} then |
|
269 |
[(Card T, lookup_type_ints_assign thy cards_assigns int_T)] |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
270 |
else if T = @{typ bisim_iterator} then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
271 |
[(Card T, map (Integer.add 1 o Integer.max 0) bisim_depths)] |
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
272 |
else if is_fp_iterator_type T then |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
273 |
[(Card T, map (Integer.add 1 o Integer.max 0) |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
274 |
(lookup_const_ints_assign thy iters_assigns |
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
275 |
(const_for_iterator_type T)))] |
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
276 |
else |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
277 |
(Card T, lookup_type_ints_assign thy cards_assigns T) :: |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
278 |
(case binarized_and_boxed_datatype_constrs hol_ctxt binarize T of |
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
279 |
[_] => [] |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
280 |
| constrs => map_filter (row_for_constr thy maxes_assigns) constrs) |
33192 | 281 |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
282 |
(* hol_context -> bool -> (typ option * int list) list |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
283 |
-> (styp option * int list) list -> (styp option * int list) list -> int list |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
284 |
-> int list -> typ list -> typ list -> block list *) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
285 |
fun blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns iters_assigns |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
286 |
bitss bisim_depths mono_Ts nonmono_Ts = |
33192 | 287 |
let |
288 |
(* typ -> block *) |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
289 |
val block_for = block_for_type hol_ctxt binarize cards_assigns maxes_assigns |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
290 |
iters_assigns bitss bisim_depths |
33192 | 291 |
val mono_block = maps block_for mono_Ts |
292 |
val nonmono_blocks = map block_for nonmono_Ts |
|
293 |
in mono_block :: nonmono_blocks end |
|
294 |
||
295 |
val sync_threshold = 5 |
|
296 |
||
297 |
(* int list -> int list list *) |
|
298 |
fun all_combinations_ordered_smartly ks = |
|
299 |
let |
|
300 |
(* int list -> int *) |
|
301 |
fun cost_with_monos [] = 0 |
|
302 |
| cost_with_monos (k :: ks) = |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
303 |
if k < sync_threshold andalso forall (curry (op =) k) ks then |
33192 | 304 |
k - sync_threshold |
305 |
else |
|
306 |
k * (k + 1) div 2 + Integer.sum ks |
|
307 |
fun cost_without_monos [] = 0 |
|
308 |
| cost_without_monos [k] = k |
|
309 |
| cost_without_monos (_ :: k :: ks) = |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
310 |
if k < sync_threshold andalso forall (curry (op =) k) ks then |
33192 | 311 |
k - sync_threshold |
312 |
else |
|
313 |
Integer.sum (k :: ks) |
|
314 |
in |
|
315 |
ks |> all_combinations |
|
316 |
|> map (`(if fst (hd ks) > 1 then cost_with_monos |
|
317 |
else cost_without_monos)) |
|
318 |
|> sort (int_ord o pairself fst) |> map snd |
|
319 |
end |
|
320 |
||
321 |
(* typ -> bool *) |
|
322 |
fun is_self_recursive_constr_type T = |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
323 |
exists (exists_subtype (curry (op =) (body_type T))) (binder_types T) |
33192 | 324 |
|
325 |
(* (styp * int) list -> styp -> int *) |
|
326 |
fun constr_max maxes x = the_default ~1 (AList.lookup (op =) maxes x) |
|
327 |
||
328 |
type scope_desc = (typ * int) list * (styp * int) list |
|
329 |
||
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
330 |
(* hol_context -> bool -> scope_desc -> typ * int -> bool *) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
331 |
fun is_surely_inconsistent_card_assign hol_ctxt binarize |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
332 |
(card_assigns, max_assigns) (T, k) = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
333 |
case binarized_and_boxed_datatype_constrs hol_ctxt binarize T of |
33192 | 334 |
[] => false |
335 |
| xs => |
|
336 |
let |
|
34126 | 337 |
val dom_cards = |
338 |
map (Integer.prod o map (bounded_card_of_type k ~1 card_assigns) |
|
33192 | 339 |
o binder_types o snd) xs |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
340 |
val maxes = map (constr_max max_assigns) xs |
33192 | 341 |
(* int -> int -> int *) |
34126 | 342 |
fun effective_max card ~1 = card |
33192 | 343 |
| effective_max card max = Int.min (card, max) |
34126 | 344 |
val max = map2 effective_max dom_cards maxes |> Integer.sum |
345 |
in max < k end |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
346 |
(* hol_context -> bool -> (typ * int) list -> (typ * int) list |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
347 |
-> (styp * int) list -> bool *) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
348 |
fun is_surely_inconsistent_scope_description hol_ctxt binarize seen rest |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
349 |
max_assigns = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
350 |
exists (is_surely_inconsistent_card_assign hol_ctxt binarize |
34126 | 351 |
(seen @ rest, max_assigns)) seen |
33192 | 352 |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
353 |
(* hol_context -> bool -> scope_desc -> (typ * int) list option *) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
354 |
fun repair_card_assigns hol_ctxt binarize (card_assigns, max_assigns) = |
33192 | 355 |
let |
356 |
(* (typ * int) list -> (typ * int) list -> (typ * int) list option *) |
|
357 |
fun aux seen [] = SOME seen |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
358 |
| aux _ ((_, 0) :: _) = NONE |
34126 | 359 |
| aux seen ((T, k) :: rest) = |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
360 |
(if is_surely_inconsistent_scope_description hol_ctxt binarize |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
361 |
((T, k) :: seen) rest max_assigns then |
33192 | 362 |
raise SAME () |
363 |
else |
|
34126 | 364 |
case aux ((T, k) :: seen) rest of |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
365 |
SOME assigns => SOME assigns |
33192 | 366 |
| NONE => raise SAME ()) |
34126 | 367 |
handle SAME () => aux seen ((T, k - 1) :: rest) |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
368 |
in aux [] (rev card_assigns) end |
33192 | 369 |
|
370 |
(* theory -> (typ * int) list -> typ * int -> typ * int *) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
371 |
fun repair_iterator_assign thy assigns (T as Type (_, Ts), k) = |
33192 | 372 |
(T, if T = @{typ bisim_iterator} then |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
373 |
let |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
374 |
val co_cards = map snd (filter (is_codatatype thy o fst) assigns) |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
375 |
in Int.min (k, Integer.sum co_cards) end |
33192 | 376 |
else if is_fp_iterator_type T then |
377 |
case Ts of |
|
378 |
[] => 1 |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
379 |
| _ => bounded_card_of_type k ~1 assigns (foldr1 HOLogic.mk_prodT Ts) |
33192 | 380 |
else |
381 |
k) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
382 |
| repair_iterator_assign _ _ assign = assign |
33192 | 383 |
|
384 |
(* row -> scope_desc -> scope_desc *) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
385 |
fun add_row_to_scope_descriptor (kind, ks) (card_assigns, max_assigns) = |
33192 | 386 |
case kind of |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
387 |
Card T => ((T, the_single ks) :: card_assigns, max_assigns) |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
388 |
| Max x => (card_assigns, (x, the_single ks) :: max_assigns) |
33192 | 389 |
(* block -> scope_desc *) |
390 |
fun scope_descriptor_from_block block = |
|
391 |
fold_rev add_row_to_scope_descriptor block ([], []) |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
392 |
(* hol_context -> bool -> block list -> int list -> scope_desc option *) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
393 |
fun scope_descriptor_from_combination (hol_ctxt as {thy, ...}) binarize blocks |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
394 |
columns = |
33192 | 395 |
let |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
396 |
val (card_assigns, max_assigns) = |
33192 | 397 |
maps project_block (columns ~~ blocks) |> scope_descriptor_from_block |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
398 |
val card_assigns = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
399 |
repair_card_assigns hol_ctxt binarize (card_assigns, max_assigns) |> the |
33192 | 400 |
in |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
401 |
SOME (map (repair_iterator_assign thy card_assigns) card_assigns, |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
402 |
max_assigns) |
33192 | 403 |
end |
404 |
handle Option.Option => NONE |
|
405 |
||
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
406 |
(* (typ * int) list -> dtype_spec list -> int Typtab.table *) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
407 |
fun offset_table_for_card_assigns assigns dtypes = |
33192 | 408 |
let |
409 |
(* int -> (int * int) list -> (typ * int) list -> int Typtab.table |
|
410 |
-> int Typtab.table *) |
|
411 |
fun aux next _ [] = Typtab.update_new (dummyT, next) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
412 |
| aux next reusable ((T, k) :: assigns) = |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
413 |
if k = 1 orelse is_iterator_type T orelse is_integer_type T |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
414 |
orelse is_bit_type T then |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
415 |
aux next reusable assigns |
33192 | 416 |
else if length (these (Option.map #constrs (datatype_spec dtypes T))) |
417 |
> 1 then |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
418 |
Typtab.update_new (T, next) #> aux (next + k) reusable assigns |
33192 | 419 |
else |
420 |
case AList.lookup (op =) reusable k of |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
421 |
SOME j0 => Typtab.update_new (T, j0) #> aux next reusable assigns |
33192 | 422 |
| NONE => Typtab.update_new (T, next) |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
423 |
#> aux (next + k) ((k, next) :: reusable) assigns |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
424 |
in aux 0 [] assigns Typtab.empty end |
33192 | 425 |
|
426 |
(* int -> (typ * int) list -> typ -> int *) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
427 |
fun domain_card max card_assigns = |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
428 |
Integer.prod o map (bounded_card_of_type max max card_assigns) o binder_types |
33192 | 429 |
|
430 |
(* scope_desc -> bool -> int -> (int -> int) -> int -> int -> bool * styp |
|
431 |
-> constr_spec list -> constr_spec list *) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
432 |
fun add_constr_spec (card_assigns, max_assigns) co card sum_dom_cards |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
433 |
num_self_recs num_non_self_recs (self_rec, x as (_, T)) |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
434 |
constrs = |
33192 | 435 |
let |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
436 |
val max = constr_max max_assigns x |
33192 | 437 |
(* unit -> int *) |
438 |
fun next_delta () = if null constrs then 0 else #epsilon (hd constrs) |
|
439 |
val {delta, epsilon, exclusive, total} = |
|
440 |
if max = 0 then |
|
441 |
let val delta = next_delta () in |
|
442 |
{delta = delta, epsilon = delta, exclusive = true, total = false} |
|
443 |
end |
|
444 |
else if not co andalso num_self_recs > 0 then |
|
35072
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
445 |
(if num_self_recs = 1 andalso num_non_self_recs = 1 then |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
446 |
if self_rec then |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
447 |
case constrs of |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
448 |
[{delta = 0, epsilon = 1, exclusive = true, ...}] => |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
449 |
{delta = 1, epsilon = card, exclusive = true, total = false} |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
450 |
| _ => raise SAME () |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
451 |
else |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
452 |
if domain_card 2 card_assigns T = 1 then |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
453 |
{delta = 0, epsilon = 1, exclusive = true, total = true} |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
454 |
else |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
455 |
raise SAME () |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
456 |
else |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
457 |
raise SAME ()) |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
458 |
handle SAME () => |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
459 |
{delta = 0, epsilon = card, exclusive = false, total = false} |
33192 | 460 |
else if card = sum_dom_cards (card + 1) then |
461 |
let val delta = next_delta () in |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
462 |
{delta = delta, epsilon = delta + domain_card card card_assigns T, |
33192 | 463 |
exclusive = true, total = true} |
464 |
end |
|
465 |
else |
|
466 |
{delta = 0, epsilon = card, |
|
467 |
exclusive = (num_self_recs + num_non_self_recs = 1), total = false} |
|
468 |
in |
|
469 |
{const = x, delta = delta, epsilon = epsilon, exclusive = exclusive, |
|
470 |
explicit_max = max, total = total} :: constrs |
|
471 |
end |
|
472 |
||
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
473 |
(* hol_context -> bool -> typ list -> (typ * int) list -> typ -> bool *) |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
474 |
fun has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T = |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
475 |
let val card = card_of_type card_assigns T in |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
476 |
card = bounded_exact_card_of_type hol_ctxt |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
477 |
(if facto then finitizable_dataTs else []) (card + 1) 0 |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
478 |
card_assigns T |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
479 |
end |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
480 |
|
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
481 |
(* hol_context -> bool -> typ list -> typ list -> scope_desc -> typ * int |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
482 |
-> dtype_spec *) |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
483 |
fun datatype_spec_from_scope_descriptor (hol_ctxt as {thy, stds, ...}) binarize |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
484 |
deep_dataTs finitizable_dataTs (desc as (card_assigns, _)) (T, card) = |
33192 | 485 |
let |
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
|
486 |
val deep = member (op =) deep_dataTs T |
33192 | 487 |
val co = is_codatatype thy T |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
488 |
val standard = is_standard_datatype thy stds T |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
489 |
val xs = binarized_and_boxed_datatype_constrs hol_ctxt binarize T |
33192 | 490 |
val self_recs = map (is_self_recursive_constr_type o snd) xs |
491 |
val (num_self_recs, num_non_self_recs) = |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
492 |
List.partition I self_recs |> pairself length |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
493 |
(* bool -> bool *) |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
494 |
fun is_complete facto = |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
495 |
has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
496 |
fun is_concrete facto = |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
497 |
is_word_type T orelse |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
498 |
xs |> maps (binder_types o snd) |> maps binder_types |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
499 |
|> forall (has_exact_card hol_ctxt facto finitizable_dataTs |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
500 |
card_assigns) |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
501 |
val complete = pair_from_fun is_complete |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
502 |
val concrete = pair_from_fun is_concrete |
33192 | 503 |
(* int -> int *) |
504 |
fun sum_dom_cards max = |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
505 |
map (domain_card max card_assigns o snd) xs |> Integer.sum |
33192 | 506 |
val constrs = |
507 |
fold_rev (add_constr_spec desc co card sum_dom_cards num_self_recs |
|
35072
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
508 |
num_non_self_recs) |
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
blanchet
parents:
35071
diff
changeset
|
509 |
(sort (bool_ord o swap o pairself fst) (self_recs ~~ xs)) [] |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
510 |
in |
35179
4b198af5beb5
redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents:
35178
diff
changeset
|
511 |
{typ = T, card = card, co = co, standard = standard, complete = complete, |
4b198af5beb5
redo Nitpick's nonstandard values as cyclic values (instead of additional constructors)
blanchet
parents:
35178
diff
changeset
|
512 |
concrete = concrete, deep = deep, constrs = constrs} |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
513 |
end |
33192 | 514 |
|
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
515 |
(* hol_context -> bool -> int -> typ list -> typ list -> scope_desc -> scope *) |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
516 |
fun scope_from_descriptor (hol_ctxt as {thy, stds, ...}) binarize sym_break |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
517 |
deep_dataTs finitizable_dataTs |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
518 |
(desc as (card_assigns, _)) = |
33192 | 519 |
let |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
520 |
val datatypes = |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
521 |
map (datatype_spec_from_scope_descriptor hol_ctxt binarize deep_dataTs |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
522 |
finitizable_dataTs desc) |
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
523 |
(filter (is_datatype thy stds o fst) card_assigns) |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
524 |
val bits = card_of_type card_assigns @{typ signed_bit} - 1 |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
525 |
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
526 |
card_of_type card_assigns @{typ unsigned_bit} |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
527 |
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => 0 |
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
528 |
val bisim_depth = card_of_type card_assigns @{typ bisim_iterator} - 1 |
33192 | 529 |
in |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
530 |
{hol_ctxt = hol_ctxt, binarize = binarize, card_assigns = card_assigns, |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
531 |
datatypes = datatypes, bits = bits, bisim_depth = bisim_depth, |
33192 | 532 |
ofs = if sym_break <= 0 then Typtab.empty |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
533 |
else offset_table_for_card_assigns card_assigns datatypes} |
33192 | 534 |
end |
535 |
||
536 |
(* theory -> typ list -> (typ option * int list) list |
|
537 |
-> (typ option * int list) list *) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
538 |
fun repair_cards_assigns_wrt_boxing_etc _ _ [] = [] |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
539 |
| repair_cards_assigns_wrt_boxing_etc thy Ts ((SOME T, ks) :: cards_assigns) = |
33192 | 540 |
(if is_fun_type T orelse is_pair_type T then |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
541 |
Ts |> filter (curry (type_match thy o swap) T) |> map (rpair ks o SOME) |
33192 | 542 |
else |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
543 |
[(SOME T, ks)]) @ |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
544 |
repair_cards_assigns_wrt_boxing_etc thy Ts cards_assigns |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
545 |
| repair_cards_assigns_wrt_boxing_etc thy Ts ((NONE, ks) :: cards_assigns) = |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
546 |
(NONE, ks) :: repair_cards_assigns_wrt_boxing_etc thy Ts cards_assigns |
33192 | 547 |
|
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
548 |
val max_scopes = 4096 |
33192 | 549 |
val distinct_threshold = 512 |
550 |
||
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
551 |
(* hol_context -> bool -> int -> (typ option * int list) list |
33192 | 552 |
-> (styp option * int list) list -> (styp option * int list) list -> int list |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
553 |
-> typ list -> typ list -> typ list ->typ list -> int * scope list *) |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
554 |
fun all_scopes (hol_ctxt as {thy, ...}) binarize sym_break cards_assigns |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
555 |
maxes_assigns iters_assigns bitss bisim_depths mono_Ts nonmono_Ts |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
556 |
deep_dataTs finitizable_dataTs = |
33192 | 557 |
let |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
558 |
val cards_assigns = repair_cards_assigns_wrt_boxing_etc thy mono_Ts |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35385
diff
changeset
|
559 |
cards_assigns |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
560 |
val blocks = blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
561 |
iters_assigns bitss bisim_depths mono_Ts |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
562 |
nonmono_Ts |
33192 | 563 |
val ranks = map rank_of_block blocks |
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
564 |
val all = all_combinations_ordered_smartly (map (rpair 0) ranks) |
33957 | 565 |
val head = take max_scopes all |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
566 |
val descs = |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
567 |
map_filter (scope_descriptor_from_combination hol_ctxt binarize blocks) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
568 |
head |
33192 | 569 |
in |
33580
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
570 |
(length all - length head, |
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
blanchet
parents:
33558
diff
changeset
|
571 |
descs |> length descs <= distinct_threshold ? distinct (op =) |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35179
diff
changeset
|
572 |
|> map (scope_from_descriptor hol_ctxt binarize sym_break |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35280
diff
changeset
|
573 |
deep_dataTs finitizable_dataTs)) |
33192 | 574 |
end |
575 |
||
576 |
end; |