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