author | blanchet |
Thu, 05 Nov 2009 17:03:22 +0100 | |
changeset 33580 | 45c33e97cb86 |
parent 33558 | a2db56854b83 |
child 33705 | 947184dc75c9 |
permissions | -rw-r--r-- |
33192 | 1 |
(* Title: HOL/Nitpick/Tools/nitpick_scope.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2008, 2009 |
|
4 |
||
5 |
Scope enumerator for Nitpick. |
|
6 |
*) |
|
7 |
||
8 |
signature NITPICK_SCOPE = |
|
9 |
sig |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
10 |
type extended_context = Nitpick_HOL.extended_context |
33192 | 11 |
|
12 |
type constr_spec = { |
|
13 |
const: styp, |
|
14 |
delta: int, |
|
15 |
epsilon: int, |
|
16 |
exclusive: bool, |
|
17 |
explicit_max: int, |
|
18 |
total: bool} |
|
19 |
||
20 |
type dtype_spec = { |
|
21 |
typ: typ, |
|
22 |
card: int, |
|
23 |
co: bool, |
|
24 |
precise: bool, |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
25 |
shallow: bool, |
33192 | 26 |
constrs: constr_spec list} |
27 |
||
28 |
type scope = { |
|
29 |
ext_ctxt: extended_context, |
|
30 |
card_assigns: (typ * int) list, |
|
31 |
bisim_depth: int, |
|
32 |
datatypes: dtype_spec list, |
|
33 |
ofs: int Typtab.table} |
|
34 |
||
35 |
val datatype_spec : dtype_spec list -> typ -> dtype_spec option |
|
36 |
val constr_spec : dtype_spec list -> styp -> constr_spec |
|
37 |
val is_precise_type : dtype_spec list -> typ -> bool |
|
38 |
val is_fully_comparable_type : dtype_spec list -> typ -> bool |
|
39 |
val offset_of_type : int Typtab.table -> typ -> int |
|
40 |
val spec_of_type : scope -> typ -> int * int |
|
41 |
val pretties_for_scope : scope -> bool -> Pretty.T list |
|
42 |
val multiline_string_for_scope : scope -> string |
|
43 |
val scopes_equivalent : scope -> scope -> bool |
|
44 |
val scope_less_eq : scope -> scope -> bool |
|
45 |
val all_scopes : |
|
46 |
extended_context -> int -> (typ option * int list) list |
|
47 |
-> (styp option * int list) list -> (styp option * int list) list |
|
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
|
48 |
-> int list -> typ list -> typ list -> typ list -> int * scope list |
33192 | 49 |
end; |
50 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
51 |
structure Nitpick_Scope : NITPICK_SCOPE = |
33192 | 52 |
struct |
53 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
54 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
55 |
open Nitpick_HOL |
33192 | 56 |
|
57 |
type constr_spec = { |
|
58 |
const: styp, |
|
59 |
delta: int, |
|
60 |
epsilon: int, |
|
61 |
exclusive: bool, |
|
62 |
explicit_max: int, |
|
63 |
total: bool} |
|
64 |
||
65 |
type dtype_spec = { |
|
66 |
typ: typ, |
|
67 |
card: int, |
|
68 |
co: bool, |
|
69 |
precise: bool, |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
70 |
shallow: bool, |
33192 | 71 |
constrs: constr_spec list} |
72 |
||
73 |
type scope = { |
|
74 |
ext_ctxt: extended_context, |
|
75 |
card_assigns: (typ * int) list, |
|
76 |
bisim_depth: int, |
|
77 |
datatypes: dtype_spec list, |
|
78 |
ofs: int Typtab.table} |
|
79 |
||
80 |
datatype row_kind = Card of typ | Max of styp |
|
81 |
||
82 |
type row = row_kind * int list |
|
83 |
type block = row list |
|
84 |
||
85 |
(* dtype_spec list -> typ -> dtype_spec option *) |
|
86 |
fun datatype_spec (dtypes : dtype_spec list) T = |
|
87 |
List.find (equal T o #typ) dtypes |
|
88 |
||
89 |
(* dtype_spec list -> styp -> constr_spec *) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
90 |
fun constr_spec [] x = raise TERM ("Nitpick_Scope.constr_spec", [Const x]) |
33192 | 91 |
| constr_spec ({constrs, ...} :: dtypes : dtype_spec list) (x as (s, T)) = |
92 |
case List.find (equal (s, body_type T) o (apsnd body_type o #const)) |
|
93 |
constrs of |
|
94 |
SOME c => c |
|
95 |
| NONE => constr_spec dtypes x |
|
96 |
||
97 |
(* dtype_spec list -> typ -> bool *) |
|
98 |
fun is_precise_type dtypes (Type ("fun", Ts)) = |
|
99 |
forall (is_precise_type dtypes) Ts |
|
100 |
| is_precise_type dtypes (Type ("*", Ts)) = forall (is_precise_type dtypes) Ts |
|
101 |
| is_precise_type dtypes T = |
|
102 |
T <> nat_T andalso T <> int_T |
|
103 |
andalso #precise (the (datatype_spec dtypes T)) |
|
104 |
handle Option.Option => true |
|
105 |
fun is_fully_comparable_type dtypes (Type ("fun", [T1, T2])) = |
|
106 |
is_precise_type dtypes T1 andalso is_fully_comparable_type dtypes T2 |
|
107 |
| is_fully_comparable_type dtypes (Type ("*", Ts)) = |
|
108 |
forall (is_fully_comparable_type dtypes) Ts |
|
109 |
| is_fully_comparable_type _ _ = true |
|
110 |
||
111 |
(* int Typtab.table -> typ -> int *) |
|
112 |
fun offset_of_type ofs T = |
|
113 |
case Typtab.lookup ofs T of |
|
114 |
SOME j0 => j0 |
|
115 |
| NONE => Typtab.lookup ofs dummyT |> the_default 0 |
|
116 |
||
117 |
(* scope -> typ -> int * int *) |
|
118 |
fun spec_of_type ({card_assigns, ofs, ...} : scope) T = |
|
119 |
(card_of_type card_assigns T |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
120 |
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => ~1, offset_of_type ofs T) |
33192 | 121 |
|
122 |
(* (string -> string) -> scope |
|
123 |
-> string list * string list * string list * string list * string list *) |
|
124 |
fun quintuple_for_scope quote ({ext_ctxt as {thy, ctxt, ...}, card_assigns, |
|
125 |
bisim_depth, datatypes, ...} : scope) = |
|
126 |
let |
|
127 |
val (iter_asgns, card_asgns) = |
|
128 |
card_assigns |> filter_out (equal @{typ bisim_iterator} o fst) |
|
129 |
|> List.partition (is_fp_iterator_type o fst) |
|
130 |
val (unimportant_card_asgns, important_card_asgns) = |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
131 |
card_asgns |> List.partition ((is_integer_type orf is_datatype thy) o fst) |
33192 | 132 |
val cards = |
133 |
map (fn (T, k) => quote (string_for_type ctxt T) ^ " = " ^ |
|
134 |
string_of_int k) |
|
135 |
fun maxes () = |
|
136 |
maps (map_filter |
|
137 |
(fn {const, explicit_max, ...} => |
|
138 |
if explicit_max < 0 then |
|
139 |
NONE |
|
140 |
else |
|
141 |
SOME (Syntax.string_of_term ctxt (Const const) ^ " = " ^ |
|
142 |
string_of_int explicit_max)) |
|
143 |
o #constrs) datatypes |
|
144 |
fun iters () = |
|
145 |
map (fn (T, k) => |
|
146 |
quote (Syntax.string_of_term ctxt |
|
147 |
(Const (const_for_iterator_type T))) ^ " = " ^ |
|
148 |
string_of_int (k - 1)) iter_asgns |
|
149 |
fun bisims () = |
|
150 |
if bisim_depth < 0 andalso forall (not o #co) datatypes then [] |
|
151 |
else ["bisim_depth = " ^ string_of_int bisim_depth] |
|
152 |
in |
|
153 |
setmp_show_all_types |
|
154 |
(fn () => (cards important_card_asgns, cards unimportant_card_asgns, |
|
155 |
maxes (), iters (), bisims ())) () |
|
156 |
end |
|
157 |
||
158 |
(* scope -> bool -> Pretty.T list *) |
|
159 |
fun pretties_for_scope scope verbose = |
|
160 |
let |
|
161 |
val (important_cards, unimportant_cards, maxes, iters, bisim_depths) = |
|
162 |
quintuple_for_scope maybe_quote scope |
|
163 |
val ss = map (prefix "card ") important_cards @ |
|
164 |
(if verbose then |
|
165 |
map (prefix "card ") unimportant_cards @ |
|
166 |
map (prefix "max ") maxes @ |
|
167 |
map (prefix "iter ") iters @ |
|
168 |
bisim_depths |
|
169 |
else |
|
170 |
[]) |
|
171 |
in |
|
172 |
if null ss then [] |
|
173 |
else serial_commas "and" ss |> map Pretty.str |> Pretty.breaks |
|
174 |
end |
|
175 |
||
176 |
(* scope -> string *) |
|
177 |
fun multiline_string_for_scope scope = |
|
178 |
let |
|
179 |
val (important_cards, unimportant_cards, maxes, iters, bisim_depths) = |
|
180 |
quintuple_for_scope I scope |
|
181 |
val cards = important_cards @ unimportant_cards |
|
182 |
in |
|
183 |
case (if null cards then [] else ["card: " ^ commas cards]) @ |
|
184 |
(if null maxes then [] else ["max: " ^ commas maxes]) @ |
|
185 |
(if null iters then [] else ["iter: " ^ commas iters]) @ |
|
186 |
bisim_depths of |
|
187 |
[] => "empty" |
|
188 |
| lines => space_implode "\n" lines |
|
189 |
end |
|
190 |
||
191 |
(* scope -> scope -> bool *) |
|
192 |
fun scopes_equivalent (s1 : scope) (s2 : scope) = |
|
193 |
#datatypes s1 = #datatypes s2 andalso #card_assigns s1 = #card_assigns s2 |
|
194 |
fun scope_less_eq (s1 : scope) (s2 : scope) = |
|
195 |
(s1, s2) |> pairself (map snd o #card_assigns) |> op ~~ |> forall (op <=) |
|
196 |
||
197 |
(* row -> int *) |
|
198 |
fun rank_of_row (_, ks) = length ks |
|
199 |
(* block -> int *) |
|
200 |
fun rank_of_block block = fold Integer.max (map rank_of_row block) 1 |
|
201 |
(* int -> typ * int list -> typ * int list *) |
|
202 |
fun project_row column (y, ks) = (y, [nth ks (Int.min (column, length ks - 1))]) |
|
203 |
(* int -> block -> block *) |
|
204 |
fun project_block (column, block) = map (project_row column) block |
|
205 |
||
206 |
(* (''a * ''a -> bool) -> (''a option * int list) list -> ''a -> int list *) |
|
207 |
fun lookup_ints_assign eq asgns key = |
|
208 |
case triple_lookup eq asgns key of |
|
209 |
SOME ks => ks |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
210 |
| NONE => raise ARG ("Nitpick_Scope.lookup_ints_assign", "") |
33192 | 211 |
(* theory -> (typ option * int list) list -> typ -> int list *) |
212 |
fun lookup_type_ints_assign thy asgns T = |
|
213 |
map (curry Int.max 1) (lookup_ints_assign (type_match thy) asgns T) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
214 |
handle ARG ("Nitpick_Scope.lookup_ints_assign", _) => |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
215 |
raise TYPE ("Nitpick_Scope.lookup_type_ints_assign", [T], []) |
33192 | 216 |
(* theory -> (styp option * int list) list -> styp -> int list *) |
217 |
fun lookup_const_ints_assign thy asgns x = |
|
218 |
lookup_ints_assign (const_match thy) asgns x |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
219 |
handle ARG ("Nitpick_Scope.lookup_ints_assign", _) => |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
220 |
raise TERM ("Nitpick_Scope.lookup_const_ints_assign", [Const x]) |
33192 | 221 |
|
222 |
(* theory -> (styp option * int list) list -> styp -> row option *) |
|
223 |
fun row_for_constr thy maxes_asgns constr = |
|
224 |
SOME (Max constr, lookup_const_ints_assign thy maxes_asgns constr) |
|
225 |
handle TERM ("lookup_const_ints_assign", _) => NONE |
|
226 |
||
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
|
227 |
(* extended_context -> (typ option * int list) list |
33192 | 228 |
-> (styp option * int list) list -> (styp option * int list) list -> int list |
229 |
-> typ -> block *) |
|
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
|
230 |
fun block_for_type (ext_ctxt as {thy, ...}) cards_asgns maxes_asgns iters_asgns |
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
|
231 |
bisim_depths 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
|
232 |
if T = @{typ bisim_iterator} then |
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
|
233 |
[(Card T, map (fn k => Int.max (0, k) + 1) bisim_depths)] |
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
|
234 |
else if is_fp_iterator_type T then |
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
|
235 |
[(Card T, map (fn k => Int.max (0, k) + 1) |
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
|
236 |
(lookup_const_ints_assign thy iters_asgns |
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
|
237 |
(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
|
238 |
else |
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
|
239 |
(Card T, lookup_type_ints_assign thy cards_asgns 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
|
240 |
(case datatype_constrs ext_ctxt T of |
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
|
241 |
[_] => [] |
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
|
242 |
| constrs => map_filter (row_for_constr thy maxes_asgns) constrs) |
33192 | 243 |
|
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
|
244 |
(* extended_context -> (typ option * int list) list |
33192 | 245 |
-> (styp option * int list) list -> (styp option * int list) list -> int list |
246 |
-> typ list -> typ list -> block list *) |
|
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
|
247 |
fun blocks_for_types ext_ctxt cards_asgns maxes_asgns iters_asgns bisim_depths |
33192 | 248 |
mono_Ts nonmono_Ts = |
249 |
let |
|
250 |
(* typ -> block *) |
|
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
|
251 |
val block_for = block_for_type ext_ctxt cards_asgns maxes_asgns iters_asgns |
33192 | 252 |
bisim_depths |
253 |
val mono_block = maps block_for mono_Ts |
|
254 |
val nonmono_blocks = map block_for nonmono_Ts |
|
255 |
in mono_block :: nonmono_blocks end |
|
256 |
||
257 |
val sync_threshold = 5 |
|
258 |
||
259 |
(* int list -> int list list *) |
|
260 |
fun all_combinations_ordered_smartly ks = |
|
261 |
let |
|
262 |
(* int list -> int *) |
|
263 |
fun cost_with_monos [] = 0 |
|
264 |
| cost_with_monos (k :: ks) = |
|
265 |
if k < sync_threshold andalso forall (equal k) ks then |
|
266 |
k - sync_threshold |
|
267 |
else |
|
268 |
k * (k + 1) div 2 + Integer.sum ks |
|
269 |
fun cost_without_monos [] = 0 |
|
270 |
| cost_without_monos [k] = k |
|
271 |
| cost_without_monos (_ :: k :: ks) = |
|
272 |
if k < sync_threshold andalso forall (equal k) ks then |
|
273 |
k - sync_threshold |
|
274 |
else |
|
275 |
Integer.sum (k :: ks) |
|
276 |
in |
|
277 |
ks |> all_combinations |
|
278 |
|> map (`(if fst (hd ks) > 1 then cost_with_monos |
|
279 |
else cost_without_monos)) |
|
280 |
|> sort (int_ord o pairself fst) |> map snd |
|
281 |
end |
|
282 |
||
283 |
(* typ -> bool *) |
|
284 |
fun is_self_recursive_constr_type T = |
|
285 |
exists (exists_subtype (equal (body_type T))) (binder_types T) |
|
286 |
||
287 |
(* (styp * int) list -> styp -> int *) |
|
288 |
fun constr_max maxes x = the_default ~1 (AList.lookup (op =) maxes x) |
|
289 |
||
290 |
type scope_desc = (typ * int) list * (styp * int) list |
|
291 |
||
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
|
292 |
(* extended_context -> scope_desc -> typ * int -> bool *) |
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
|
293 |
fun is_surely_inconsistent_card_assign ext_ctxt (card_asgns, max_asgns) (T, k) = |
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
|
294 |
case datatype_constrs ext_ctxt T of |
33192 | 295 |
[] => false |
296 |
| xs => |
|
297 |
let |
|
298 |
val precise_cards = |
|
299 |
map (Integer.prod |
|
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
|
300 |
o map (bounded_precise_card_of_type ext_ctxt k 0 card_asgns) |
33192 | 301 |
o binder_types o snd) xs |
302 |
val maxes = map (constr_max max_asgns) xs |
|
303 |
(* int -> int -> int *) |
|
304 |
fun effective_max 0 ~1 = k |
|
305 |
| effective_max 0 max = max |
|
306 |
| effective_max card ~1 = card |
|
307 |
| effective_max card max = Int.min (card, max) |
|
308 |
val max = map2 effective_max precise_cards maxes |> Integer.sum |
|
309 |
(* unit -> int *) |
|
310 |
fun doms_card () = |
|
311 |
xs |> map (Integer.prod o map (bounded_card_of_type k ~1 card_asgns) |
|
312 |
o binder_types o snd) |
|
313 |
|> Integer.sum |
|
314 |
in |
|
315 |
max < k |
|
316 |
orelse (forall (not_equal 0) precise_cards andalso doms_card () < k) |
|
317 |
end |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
318 |
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => false |
33192 | 319 |
|
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
|
320 |
(* extended_context -> scope_desc -> bool *) |
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
|
321 |
fun is_surely_inconsistent_scope_description ext_ctxt |
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
|
322 |
(desc as (card_asgns, _)) = |
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
|
323 |
exists (is_surely_inconsistent_card_assign ext_ctxt desc) card_asgns |
33192 | 324 |
|
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
|
325 |
(* extended_context -> scope_desc -> (typ * int) list option *) |
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
|
326 |
fun repair_card_assigns ext_ctxt (card_asgns, max_asgns) = |
33192 | 327 |
let |
328 |
(* (typ * int) list -> (typ * int) list -> (typ * int) list option *) |
|
329 |
fun aux seen [] = SOME seen |
|
330 |
| aux seen ((T, 0) :: _) = NONE |
|
331 |
| aux seen ((T, k) :: asgns) = |
|
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
|
332 |
(if is_surely_inconsistent_scope_description ext_ctxt |
33192 | 333 |
((T, k) :: seen, max_asgns) then |
334 |
raise SAME () |
|
335 |
else |
|
336 |
case aux ((T, k) :: seen) asgns of |
|
337 |
SOME asgns => SOME asgns |
|
338 |
| NONE => raise SAME ()) |
|
339 |
handle SAME () => aux seen ((T, k - 1) :: asgns) |
|
340 |
in aux [] (rev card_asgns) end |
|
341 |
||
342 |
(* theory -> (typ * int) list -> typ * int -> typ * int *) |
|
343 |
fun repair_iterator_assign thy asgns (T as Type (s, Ts), k) = |
|
344 |
(T, if T = @{typ bisim_iterator} then |
|
345 |
let val co_cards = map snd (filter (is_codatatype thy o fst) asgns) in |
|
346 |
Int.min (k, Integer.sum co_cards) |
|
347 |
end |
|
348 |
else if is_fp_iterator_type T then |
|
349 |
case Ts of |
|
350 |
[] => 1 |
|
351 |
| _ => bounded_card_of_type k ~1 asgns (foldr1 HOLogic.mk_prodT Ts) |
|
352 |
else |
|
353 |
k) |
|
354 |
| repair_iterator_assign _ _ asgn = asgn |
|
355 |
||
356 |
(* row -> scope_desc -> scope_desc *) |
|
357 |
fun add_row_to_scope_descriptor (kind, ks) (card_asgns, max_asgns) = |
|
358 |
case kind of |
|
359 |
Card T => ((T, the_single ks) :: card_asgns, max_asgns) |
|
360 |
| Max x => (card_asgns, (x, the_single ks) :: max_asgns) |
|
361 |
(* block -> scope_desc *) |
|
362 |
fun scope_descriptor_from_block block = |
|
363 |
fold_rev add_row_to_scope_descriptor block ([], []) |
|
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
|
364 |
(* extended_context -> block list -> int list -> scope_desc option *) |
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
|
365 |
fun scope_descriptor_from_combination (ext_ctxt as {thy, ...}) blocks columns = |
33192 | 366 |
let |
367 |
val (card_asgns, max_asgns) = |
|
368 |
maps project_block (columns ~~ blocks) |> scope_descriptor_from_block |
|
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
|
369 |
val card_asgns = repair_card_assigns ext_ctxt (card_asgns, max_asgns) |> the |
33192 | 370 |
in |
371 |
SOME (map (repair_iterator_assign thy card_asgns) card_asgns, max_asgns) |
|
372 |
end |
|
373 |
handle Option.Option => NONE |
|
374 |
||
375 |
(* theory -> (typ * int) list -> dtype_spec list -> int Typtab.table *) |
|
376 |
fun offset_table_for_card_assigns thy asgns dtypes = |
|
377 |
let |
|
378 |
(* int -> (int * int) list -> (typ * int) list -> int Typtab.table |
|
379 |
-> int Typtab.table *) |
|
380 |
fun aux next _ [] = Typtab.update_new (dummyT, next) |
|
381 |
| aux next reusable ((T, k) :: asgns) = |
|
382 |
if k = 1 orelse is_integer_type T then |
|
383 |
aux next reusable asgns |
|
384 |
else if length (these (Option.map #constrs (datatype_spec dtypes T))) |
|
385 |
> 1 then |
|
386 |
Typtab.update_new (T, next) #> aux (next + k) reusable asgns |
|
387 |
else |
|
388 |
case AList.lookup (op =) reusable k of |
|
389 |
SOME j0 => Typtab.update_new (T, j0) #> aux next reusable asgns |
|
390 |
| NONE => Typtab.update_new (T, next) |
|
391 |
#> aux (next + k) ((k, next) :: reusable) asgns |
|
392 |
in aux 0 [] asgns Typtab.empty end |
|
393 |
||
394 |
(* int -> (typ * int) list -> typ -> int *) |
|
395 |
fun domain_card max card_asgns = |
|
396 |
Integer.prod o map (bounded_card_of_type max max card_asgns) o binder_types |
|
397 |
||
398 |
(* scope_desc -> bool -> int -> (int -> int) -> int -> int -> bool * styp |
|
399 |
-> constr_spec list -> constr_spec list *) |
|
400 |
fun add_constr_spec (card_asgns, max_asgns) co card sum_dom_cards num_self_recs |
|
401 |
num_non_self_recs (self_rec, x as (s, T)) constrs = |
|
402 |
let |
|
403 |
val max = constr_max max_asgns x |
|
404 |
(* int -> int *) |
|
405 |
fun bound k = Int.min (card, (max >= 0 ? curry Int.min max) k) |
|
406 |
(* unit -> int *) |
|
407 |
fun next_delta () = if null constrs then 0 else #epsilon (hd constrs) |
|
408 |
val {delta, epsilon, exclusive, total} = |
|
409 |
if max = 0 then |
|
410 |
let val delta = next_delta () in |
|
411 |
{delta = delta, epsilon = delta, exclusive = true, total = false} |
|
412 |
end |
|
413 |
else if not co andalso num_self_recs > 0 then |
|
414 |
if not self_rec andalso num_non_self_recs = 1 |
|
415 |
andalso domain_card 2 card_asgns T = 1 then |
|
416 |
{delta = 0, epsilon = 1, exclusive = (s = @{const_name Nil}), |
|
417 |
total = true} |
|
418 |
else if s = @{const_name Cons} then |
|
419 |
{delta = 1, epsilon = card, exclusive = true, total = false} |
|
420 |
else |
|
421 |
{delta = 0, epsilon = card, exclusive = false, total = false} |
|
422 |
else if card = sum_dom_cards (card + 1) then |
|
423 |
let val delta = next_delta () in |
|
424 |
{delta = delta, epsilon = delta + domain_card card card_asgns T, |
|
425 |
exclusive = true, total = true} |
|
426 |
end |
|
427 |
else |
|
428 |
{delta = 0, epsilon = card, |
|
429 |
exclusive = (num_self_recs + num_non_self_recs = 1), total = false} |
|
430 |
in |
|
431 |
{const = x, delta = delta, epsilon = epsilon, exclusive = exclusive, |
|
432 |
explicit_max = max, total = total} :: constrs |
|
433 |
end |
|
434 |
||
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
435 |
(* extended_context -> typ list -> scope_desc -> typ * int -> dtype_spec *) |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
436 |
fun datatype_spec_from_scope_descriptor (ext_ctxt as {thy, ...}) shallow_dataTs |
33192 | 437 |
(desc as (card_asgns, _)) (T, card) = |
438 |
let |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
439 |
val shallow = T mem shallow_dataTs |
33192 | 440 |
val co = is_codatatype thy T |
441 |
val xs = boxed_datatype_constrs ext_ctxt T |
|
442 |
val self_recs = map (is_self_recursive_constr_type o snd) xs |
|
443 |
val (num_self_recs, num_non_self_recs) = |
|
444 |
List.partition (equal true) self_recs |> pairself length |
|
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
|
445 |
val precise = (card = bounded_precise_card_of_type ext_ctxt (card + 1) 0 |
33192 | 446 |
card_asgns T) |
447 |
(* int -> int *) |
|
448 |
fun sum_dom_cards max = |
|
449 |
map (domain_card max card_asgns o snd) xs |> Integer.sum |
|
450 |
val constrs = |
|
451 |
fold_rev (add_constr_spec desc co card sum_dom_cards num_self_recs |
|
452 |
num_non_self_recs) (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
|
453 |
in |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
454 |
{typ = T, card = card, co = co, precise = precise, shallow = shallow, |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
455 |
constrs = constrs} |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
456 |
end |
33192 | 457 |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
458 |
(* extended_context -> int -> typ list -> scope_desc -> scope *) |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
459 |
fun scope_from_descriptor (ext_ctxt as {thy, ...}) sym_break shallow_dataTs |
33192 | 460 |
(desc as (card_asgns, _)) = |
461 |
let |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
462 |
val datatypes = |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
463 |
map (datatype_spec_from_scope_descriptor ext_ctxt shallow_dataTs desc) |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
464 |
(filter (is_datatype thy o fst) card_asgns) |
33192 | 465 |
val bisim_depth = card_of_type card_asgns @{typ bisim_iterator} - 1 |
466 |
in |
|
467 |
{ext_ctxt = ext_ctxt, card_assigns = card_asgns, datatypes = datatypes, |
|
468 |
bisim_depth = bisim_depth, |
|
469 |
ofs = if sym_break <= 0 then Typtab.empty |
|
470 |
else offset_table_for_card_assigns thy card_asgns datatypes} |
|
471 |
end |
|
472 |
||
473 |
(* theory -> typ list -> (typ option * int list) list |
|
474 |
-> (typ option * int list) list *) |
|
475 |
fun fix_cards_assigns_wrt_boxing _ _ [] = [] |
|
476 |
| fix_cards_assigns_wrt_boxing thy Ts ((SOME T, ks) :: cards_asgns) = |
|
477 |
(if is_fun_type T orelse is_pair_type T then |
|
478 |
Ts |> filter (curry (type_match thy o swap) T o unbox_type) |
|
479 |
|> map (rpair ks o SOME) |
|
480 |
else |
|
481 |
[(SOME T, ks)]) @ fix_cards_assigns_wrt_boxing thy Ts cards_asgns |
|
482 |
| fix_cards_assigns_wrt_boxing thy Ts ((NONE, ks) :: cards_asgns) = |
|
483 |
(NONE, ks) :: fix_cards_assigns_wrt_boxing thy Ts cards_asgns |
|
484 |
||
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
|
485 |
val max_scopes = 4096 |
33192 | 486 |
val distinct_threshold = 512 |
487 |
||
488 |
(* extended_context -> int -> (typ option * int list) list |
|
489 |
-> (styp option * int list) list -> (styp option * int list) list -> int list |
|
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
|
490 |
-> typ list -> typ list -> typ list -> int * scope list *) |
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
|
491 |
fun all_scopes (ext_ctxt as {thy, ...}) sym_break cards_asgns maxes_asgns |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
492 |
iters_asgns bisim_depths mono_Ts nonmono_Ts shallow_dataTs = |
33192 | 493 |
let |
494 |
val cards_asgns = fix_cards_assigns_wrt_boxing thy mono_Ts cards_asgns |
|
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
|
495 |
val blocks = blocks_for_types ext_ctxt cards_asgns maxes_asgns iters_asgns |
33192 | 496 |
bisim_depths mono_Ts nonmono_Ts |
497 |
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
|
498 |
val all = all_combinations_ordered_smartly (map (rpair 0) ranks) |
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
|
499 |
val head = Library.take (max_scopes, all) |
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
|
500 |
val descs = map_filter (scope_descriptor_from_combination ext_ctxt blocks) |
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
|
501 |
head |
33192 | 502 |
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
|
503 |
(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
|
504 |
descs |> length descs <= distinct_threshold ? distinct (op =) |
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
|
505 |
|> map (scope_from_descriptor ext_ctxt sym_break shallow_dataTs)) |
33192 | 506 |
end |
507 |
||
508 |
end; |