author | blanchet |
Tue, 27 Oct 2009 19:00:17 +0100 | |
changeset 33558 | a2db56854b83 |
parent 33232 | f93390060bbe |
child 33565 | 5fad8e36dfb1 |
permissions | -rw-r--r-- |
33192 | 1 |
(* Title: HOL/Nitpick/Tools/nitpick_model.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2009 |
|
4 |
||
5 |
Model reconstruction for Nitpick. |
|
6 |
*) |
|
7 |
||
8 |
signature NITPICK_MODEL = |
|
9 |
sig |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
10 |
type scope = Nitpick_Scope.scope |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
11 |
type rep = Nitpick_Rep.rep |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
12 |
type nut = Nitpick_Nut.nut |
33192 | 13 |
|
14 |
type params = { |
|
15 |
show_skolems: bool, |
|
16 |
show_datatypes: bool, |
|
17 |
show_consts: bool} |
|
18 |
||
19 |
structure NameTable : TABLE |
|
20 |
||
21 |
val tuple_list_for_name : |
|
22 |
nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list |
|
23 |
val reconstruct_hol_model : |
|
24 |
params -> scope -> (term option * int list) list -> styp list -> nut list |
|
25 |
-> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list |
|
26 |
-> Pretty.T * bool |
|
27 |
val prove_hol_model : |
|
28 |
scope -> Time.time option -> nut list -> nut list -> nut NameTable.table |
|
29 |
-> Kodkod.raw_bound list -> term -> bool option |
|
30 |
end; |
|
31 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
32 |
structure Nitpick_Model : NITPICK_MODEL = |
33192 | 33 |
struct |
34 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
35 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
36 |
open Nitpick_HOL |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
37 |
open Nitpick_Scope |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
38 |
open Nitpick_Peephole |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
39 |
open Nitpick_Rep |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
40 |
open Nitpick_Nut |
33192 | 41 |
|
42 |
type params = { |
|
43 |
show_skolems: bool, |
|
44 |
show_datatypes: bool, |
|
45 |
show_consts: bool} |
|
46 |
||
47 |
val unknown = "?" |
|
48 |
val unrep = "\<dots>" |
|
49 |
val maybe_mixfix = "_\<^sup>?" |
|
50 |
val base_mixfix = "_\<^bsub>base\<^esub>" |
|
51 |
val step_mixfix = "_\<^bsub>step\<^esub>" |
|
52 |
val abs_mixfix = "\<guillemotleft>_\<guillemotright>" |
|
53 |
val non_opt_name = nitpick_prefix ^ "non_opt" |
|
54 |
||
55 |
(* string -> typ -> int -> string *) |
|
56 |
fun atom_name prefix (T as Type (s, _)) j = |
|
57 |
prefix ^ substring (short_name s, 0, 1) ^ nat_subscript (j + 1) |
|
58 |
| atom_name prefix (T as TFree (s, _)) j = |
|
59 |
prefix ^ perhaps (try (unprefix "'")) s ^ nat_subscript (j + 1) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
60 |
| atom_name _ T _ = raise TYPE ("Nitpick_Model.atom_name", [T], []) |
33192 | 61 |
(* bool -> typ -> int -> term *) |
62 |
fun atom for_auto T j = |
|
63 |
if for_auto then |
|
64 |
Free (atom_name (hd (space_explode "." nitpick_prefix)) T j, T) |
|
65 |
else |
|
66 |
Const (atom_name "" T j, T) |
|
67 |
||
68 |
(* nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list *) |
|
69 |
fun tuple_list_for_name rel_table bounds name = |
|
70 |
the (AList.lookup (op =) bounds (the_rel rel_table name)) handle NUT _ => [[]] |
|
71 |
||
72 |
(* term -> term *) |
|
73 |
fun unbox_term (Const (@{const_name FunBox}, _) $ t1) = unbox_term t1 |
|
74 |
| unbox_term (Const (@{const_name PairBox}, |
|
75 |
Type ("fun", [T1, Type ("fun", [T2, T3])])) $ t1 $ t2) = |
|
76 |
let val Ts = map unbox_type [T1, T2] in |
|
77 |
Const (@{const_name Pair}, Ts ---> Type ("*", Ts)) |
|
78 |
$ unbox_term t1 $ unbox_term t2 |
|
79 |
end |
|
80 |
| unbox_term (Const (s, T)) = Const (s, unbox_type T) |
|
81 |
| unbox_term (t1 $ t2) = unbox_term t1 $ unbox_term t2 |
|
82 |
| unbox_term (Free (s, T)) = Free (s, unbox_type T) |
|
83 |
| unbox_term (Var (x, T)) = Var (x, unbox_type T) |
|
84 |
| unbox_term (Bound j) = Bound j |
|
85 |
| unbox_term (Abs (s, T, t')) = Abs (s, unbox_type T, unbox_term t') |
|
86 |
||
87 |
(* typ -> typ -> (typ * typ) * (typ * typ) *) |
|
88 |
fun factor_out_types (T1 as Type ("*", [T11, T12])) |
|
89 |
(T2 as Type ("*", [T21, T22])) = |
|
90 |
let val (n1, n2) = pairself num_factors_in_type (T11, T21) in |
|
91 |
if n1 = n2 then |
|
92 |
let |
|
93 |
val ((T11', opt_T12'), (T21', opt_T22')) = factor_out_types T12 T22 |
|
94 |
in |
|
95 |
((Type ("*", [T11, T11']), opt_T12'), |
|
96 |
(Type ("*", [T21, T21']), opt_T22')) |
|
97 |
end |
|
98 |
else if n1 < n2 then |
|
99 |
case factor_out_types T1 T21 of |
|
100 |
(p1, (T21', NONE)) => (p1, (T21', SOME T22)) |
|
101 |
| (p1, (T21', SOME T22')) => |
|
102 |
(p1, (T21', SOME (Type ("*", [T22', T22])))) |
|
103 |
else |
|
104 |
swap (factor_out_types T2 T1) |
|
105 |
end |
|
106 |
| factor_out_types (Type ("*", [T11, T12])) T2 = ((T11, SOME T12), (T2, NONE)) |
|
107 |
| factor_out_types T1 (Type ("*", [T21, T22])) = ((T1, NONE), (T21, SOME T22)) |
|
108 |
| factor_out_types T1 T2 = ((T1, NONE), (T2, NONE)) |
|
109 |
||
110 |
(* bool -> typ -> typ -> (term * term) list -> term *) |
|
111 |
fun make_plain_fun maybe_opt T1 T2 = |
|
112 |
let |
|
113 |
(* typ -> typ -> (term * term) list -> term *) |
|
114 |
fun aux T1 T2 [] = |
|
115 |
Const (if maybe_opt orelse T2 <> bool_T then @{const_name undefined} |
|
116 |
else non_opt_name, T1 --> T2) |
|
117 |
| aux T1 T2 ((t1, t2) :: ps) = |
|
118 |
Const (@{const_name fun_upd}, [T1 --> T2, T1, T2] ---> T1 --> T2) |
|
119 |
$ aux T1 T2 ps $ t1 $ t2 |
|
120 |
in aux T1 T2 o rev end |
|
121 |
(* term -> bool *) |
|
122 |
fun is_plain_fun (Const (s, _)) = s mem [@{const_name undefined}, non_opt_name] |
|
123 |
| is_plain_fun (Const (@{const_name fun_upd}, _) $ t0 $ _ $ _) = |
|
124 |
is_plain_fun t0 |
|
125 |
| is_plain_fun _ = false |
|
126 |
(* term -> bool * (term list * term list) *) |
|
127 |
val dest_plain_fun = |
|
128 |
let |
|
129 |
(* term -> term list * term list *) |
|
130 |
fun aux (Const (s, _)) = (s <> non_opt_name, ([], [])) |
|
131 |
| aux (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) = |
|
132 |
let val (s, (ts1, ts2)) = aux t0 in (s, (t1 :: ts1, t2 :: ts2)) end |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
133 |
| aux t = raise TERM ("Nitpick_Model.dest_plain_fun", [t]) |
33192 | 134 |
in apsnd (pairself rev) o aux end |
135 |
||
136 |
(* typ -> term -> term list * term *) |
|
137 |
fun break_in_two (Type ("*", [T1, T2])) |
|
138 |
(Const (@{const_name Pair}, _) $ t1 $ t2) = |
|
139 |
break_in_two T2 t2 |>> cons t1 |
|
140 |
| break_in_two _ (Const (@{const_name Pair}, _) $ t1 $ t2) = ([t1], t2) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
141 |
| break_in_two _ t = raise TERM ("Nitpick_Model.break_in_two", [t]) |
33192 | 142 |
(* typ -> term -> term -> term *) |
143 |
fun pair_up (Type ("*", [T1', T2'])) |
|
144 |
(t1 as Const (@{const_name Pair}, |
|
145 |
Type ("fun", [_, Type ("fun", [_, T1])])) $ t11 $ t12) |
|
146 |
t2 = |
|
147 |
if T1 = T1' then HOLogic.mk_prod (t1, t2) |
|
148 |
else HOLogic.mk_prod (t11, pair_up T2' t12 t2) |
|
149 |
| pair_up _ t1 t2 = HOLogic.mk_prod (t1, t2) |
|
150 |
(* typ -> term -> term list * term list -> (term * term) list*) |
|
151 |
fun multi_pair_up T1 t1 (ts2, ts3) = map2 (pair o pair_up T1 t1) ts2 ts3 |
|
152 |
||
153 |
(* typ -> typ -> typ -> term -> term *) |
|
154 |
fun typecast_fun (Type ("fun", [T1', T2'])) T1 T2 t = |
|
155 |
let |
|
156 |
(* typ -> typ -> typ -> term -> term *) |
|
157 |
fun do_curry T1a T1b T2 t = |
|
158 |
let |
|
159 |
val (maybe_opt, ps) = dest_plain_fun t |
|
160 |
val ps = |
|
161 |
ps |>> map (break_in_two T1a #>> mk_flat_tuple T1a) |
|
162 |
|> uncurry (map2 (fn (t1a, t1b) => fn t2 => (t1a, (t1b, t2)))) |
|
163 |
|> AList.coalesce (op =) |
|
164 |
|> map (apsnd (make_plain_fun maybe_opt T1b T2)) |
|
165 |
in make_plain_fun maybe_opt T1a (T1b --> T2) ps end |
|
166 |
(* typ -> typ -> term -> term *) |
|
167 |
and do_uncurry T1 T2 t = |
|
168 |
let |
|
169 |
val (maybe_opt, tsp) = dest_plain_fun t |
|
170 |
val ps = |
|
171 |
tsp |> op ~~ |
|
172 |
|> maps (fn (t1, t2) => |
|
173 |
multi_pair_up T1 t1 (snd (dest_plain_fun t2))) |
|
174 |
in make_plain_fun maybe_opt T1 T2 ps end |
|
175 |
(* typ -> typ -> typ -> typ -> term -> term *) |
|
176 |
and do_arrow T1' T2' _ _ (Const (s, _)) = Const (s, T1' --> T2') |
|
177 |
| do_arrow T1' T2' T1 T2 |
|
178 |
(Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) = |
|
179 |
Const (@{const_name fun_upd}, |
|
180 |
[T1' --> T2', T1', T2'] ---> T1' --> T2') |
|
181 |
$ do_arrow T1' T2' T1 T2 t0 $ do_term T1' T1 t1 $ do_term T2' T2 t2 |
|
182 |
| do_arrow _ _ _ _ t = |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
183 |
raise TERM ("Nitpick_Model.typecast_fun.do_arrow", [t]) |
33192 | 184 |
and do_fun T1' T2' T1 T2 t = |
185 |
case factor_out_types T1' T1 of |
|
186 |
((_, NONE), (_, NONE)) => t |> do_arrow T1' T2' T1 T2 |
|
187 |
| ((_, NONE), (T1a, SOME T1b)) => |
|
188 |
t |> do_curry T1a T1b T2 |> do_arrow T1' T2' T1a (T1b --> T2) |
|
189 |
| ((T1a', SOME T1b'), (_, NONE)) => |
|
190 |
t |> do_arrow T1a' (T1b' --> T2') T1 T2 |> do_uncurry T1' T2' |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
191 |
| _ => raise TYPE ("Nitpick_Model.typecast_fun.do_fun", [T1, T1'], []) |
33192 | 192 |
(* typ -> typ -> term -> term *) |
193 |
and do_term (Type ("fun", [T1', T2'])) (Type ("fun", [T1, T2])) t = |
|
194 |
do_fun T1' T2' T1 T2 t |
|
195 |
| do_term (T' as Type ("*", Ts' as [T1', T2'])) (Type ("*", [T1, T2])) |
|
196 |
(Const (@{const_name Pair}, _) $ t1 $ t2) = |
|
197 |
Const (@{const_name Pair}, Ts' ---> T') |
|
198 |
$ do_term T1' T1 t1 $ do_term T2' T2 t2 |
|
199 |
| do_term T' T t = |
|
200 |
if T = T' then t |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
201 |
else raise TYPE ("Nitpick_Model.typecast_fun.do_term", [T, T'], []) |
33192 | 202 |
in if T1' = T1 andalso T2' = T2 then t else do_fun T1' T2' T1 T2 t end |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
203 |
| typecast_fun T' _ _ _ = raise TYPE ("Nitpick_Model.typecast_fun", [T'], []) |
33192 | 204 |
|
205 |
(* term -> string *) |
|
206 |
fun truth_const_sort_key @{const True} = "0" |
|
207 |
| truth_const_sort_key @{const False} = "2" |
|
208 |
| truth_const_sort_key _ = "1" |
|
209 |
||
210 |
(* typ -> term list -> term *) |
|
211 |
fun mk_tuple (Type ("*", [T1, T2])) ts = |
|
212 |
HOLogic.mk_prod (mk_tuple T1 ts, |
|
213 |
mk_tuple T2 (List.drop (ts, length (HOLogic.flatten_tupleT T1)))) |
|
214 |
| mk_tuple _ (t :: _) = t |
|
215 |
||
216 |
(* string * string * string * string -> scope -> nut list -> nut list |
|
217 |
-> nut list -> nut NameTable.table -> Kodkod.raw_bound list -> typ -> typ |
|
218 |
-> rep -> int list list -> term *) |
|
219 |
fun reconstruct_term (maybe_name, base_name, step_name, abs_name) |
|
220 |
({ext_ctxt as {thy, ctxt, ...}, card_assigns, datatypes, ofs, ...} |
|
221 |
: scope) sel_names rel_table bounds = |
|
222 |
let |
|
223 |
val for_auto = (maybe_name = "") |
|
224 |
(* bool -> typ -> typ -> (term * term) list -> term *) |
|
225 |
fun make_set maybe_opt T1 T2 = |
|
226 |
let |
|
227 |
val empty_const = Const (@{const_name Set.empty}, T1 --> T2) |
|
228 |
val insert_const = Const (@{const_name insert}, |
|
229 |
[T1, T1 --> T2] ---> T1 --> T2) |
|
230 |
(* (term * term) list -> term *) |
|
231 |
fun aux [] = |
|
232 |
if maybe_opt andalso not (is_precise_type datatypes T1) then |
|
233 |
insert_const $ Const (unrep, T1) $ empty_const |
|
234 |
else |
|
235 |
empty_const |
|
236 |
| aux ((t1, t2) :: zs) = |
|
237 |
aux zs |> t2 <> @{const False} |
|
238 |
? curry (op $) (insert_const |
|
239 |
$ (t1 |> t2 <> @{const True} |
|
240 |
? curry (op $) |
|
241 |
(Const (maybe_name, |
|
242 |
T1 --> T1)))) |
|
243 |
in aux end |
|
244 |
(* typ -> typ -> typ -> (term * term) list -> term *) |
|
245 |
fun make_map T1 T2 T2' = |
|
246 |
let |
|
247 |
val update_const = Const (@{const_name fun_upd}, |
|
248 |
[T1 --> T2, T1, T2] ---> T1 --> T2) |
|
249 |
(* (term * term) list -> term *) |
|
250 |
fun aux' [] = Const (@{const_name Map.empty}, T1 --> T2) |
|
251 |
| aux' ((t1, t2) :: ps) = |
|
252 |
(case t2 of |
|
253 |
Const (@{const_name None}, _) => aux' ps |
|
254 |
| _ => update_const $ aux' ps $ t1 $ t2) |
|
255 |
fun aux ps = |
|
256 |
if not (is_precise_type datatypes T1) then |
|
257 |
update_const $ aux' ps $ Const (unrep, T1) |
|
258 |
$ (Const (@{const_name Some}, T2' --> T2) $ Const (unknown, T2')) |
|
259 |
else |
|
260 |
aux' ps |
|
261 |
in aux end |
|
262 |
(* typ list -> term -> term *) |
|
263 |
fun setify_mapify_funs Ts t = |
|
264 |
(case fastype_of1 (Ts, t) of |
|
265 |
Type ("fun", [T1, T2]) => |
|
266 |
if is_plain_fun t then |
|
267 |
case T2 of |
|
268 |
@{typ bool} => |
|
269 |
let |
|
270 |
val (maybe_opt, ts_pair) = |
|
271 |
dest_plain_fun t ||> pairself (map (setify_mapify_funs Ts)) |
|
272 |
in |
|
273 |
make_set maybe_opt T1 T2 |
|
274 |
(sort_wrt (truth_const_sort_key o snd) (op ~~ ts_pair)) |
|
275 |
end |
|
276 |
| Type (@{type_name option}, [T2']) => |
|
277 |
let |
|
278 |
val ts_pair = snd (dest_plain_fun t) |
|
279 |
|> pairself (map (setify_mapify_funs Ts)) |
|
280 |
in make_map T1 T2 T2' (rev (op ~~ ts_pair)) end |
|
281 |
| _ => raise SAME () |
|
282 |
else |
|
283 |
raise SAME () |
|
284 |
| _ => raise SAME ()) |
|
285 |
handle SAME () => |
|
286 |
case t of |
|
287 |
t1 $ t2 => setify_mapify_funs Ts t1 $ setify_mapify_funs Ts t2 |
|
288 |
| Abs (s, T, t') => Abs (s, T, setify_mapify_funs (T :: Ts) t') |
|
289 |
| _ => t |
|
290 |
(* bool -> typ -> typ -> typ -> term list -> term list -> term *) |
|
291 |
fun make_fun maybe_opt T1 T2 T' ts1 ts2 = |
|
292 |
ts1 ~~ ts2 |> T1 = @{typ bisim_iterator} ? rev |
|
293 |
|> make_plain_fun (maybe_opt andalso not for_auto) T1 T2 |
|
294 |
|> unbox_term |
|
295 |
|> typecast_fun (unbox_type T') (unbox_type T1) (unbox_type T2) |
|
296 |
(* (typ * int) list -> typ -> typ -> int -> term *) |
|
297 |
fun term_for_atom seen (T as Type ("fun", [T1, T2])) T' j = |
|
298 |
let |
|
299 |
val k1 = card_of_type card_assigns T1 |
|
300 |
val k2 = card_of_type card_assigns T2 |
|
301 |
in |
|
302 |
term_for_rep seen T T' (Vect (k1, Atom (k2, 0))) |
|
303 |
[nth_combination (replicate k1 (k2, 0)) j] |
|
304 |
handle General.Subscript => |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
305 |
raise ARG ("Nitpick_Model.reconstruct_term.term_for_atom", |
33192 | 306 |
signed_string_of_int j ^ " for " ^ |
307 |
string_for_rep (Vect (k1, Atom (k2, 0)))) |
|
308 |
end |
|
309 |
| term_for_atom seen (Type ("*", [T1, T2])) _ j = |
|
310 |
let val k1 = card_of_type card_assigns T1 in |
|
311 |
list_comb (HOLogic.pair_const T1 T2, |
|
312 |
map2 (fn T => term_for_atom seen T T) [T1, T2] |
|
313 |
[j div k1, j mod k1]) |
|
314 |
end |
|
315 |
| term_for_atom seen @{typ prop} _ j = |
|
316 |
HOLogic.mk_Trueprop (term_for_atom seen bool_T bool_T j) |
|
317 |
| term_for_atom _ @{typ bool} _ j = |
|
318 |
if j = 0 then @{const False} else @{const True} |
|
319 |
| term_for_atom _ @{typ unit} _ _ = @{const Unity} |
|
320 |
| term_for_atom seen T _ j = |
|
321 |
if T = nat_T then |
|
322 |
HOLogic.mk_number nat_T j |
|
323 |
else if T = int_T then |
|
324 |
HOLogic.mk_number int_T |
|
325 |
(int_for_atom (card_of_type card_assigns int_T, 0) j) |
|
326 |
else if is_fp_iterator_type T then |
|
327 |
HOLogic.mk_number nat_T (card_of_type card_assigns T - j - 1) |
|
328 |
else if T = @{typ bisim_iterator} then |
|
329 |
HOLogic.mk_number nat_T j |
|
330 |
else case datatype_spec datatypes T of |
|
331 |
NONE => atom for_auto T j |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
332 |
| SOME {shallow = true, ...} => atom for_auto T j |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
333 |
| SOME {co, constrs, ...} => |
33192 | 334 |
let |
335 |
(* styp -> int list *) |
|
336 |
fun tuples_for_const (s, T) = |
|
337 |
tuple_list_for_name rel_table bounds (ConstName (s, T, Any)) |
|
338 |
(* unit -> indexname * typ *) |
|
339 |
fun var () = ((atom_name "" T j, 0), T) |
|
340 |
val discr_jsss = map (tuples_for_const o discr_for_constr o #const) |
|
341 |
constrs |
|
342 |
val real_j = j + offset_of_type ofs T |
|
343 |
val constr_x as (constr_s, constr_T) = |
|
344 |
get_first (fn (jss, {const, ...}) => |
|
345 |
if [real_j] mem jss then SOME const else NONE) |
|
346 |
(discr_jsss ~~ constrs) |> the |
|
347 |
val arg_Ts = curried_binder_types constr_T |
|
348 |
val sel_xs = map (boxed_nth_sel_for_constr ext_ctxt constr_x) |
|
349 |
(index_seq 0 (length arg_Ts)) |
|
350 |
val sel_Rs = |
|
351 |
map (fn x => get_first |
|
352 |
(fn ConstName (s', T', R) => |
|
353 |
if (s', T') = x then SOME R else NONE |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
354 |
| u => raise NUT ("Nitpick_Model.reconstruct_\ |
33192 | 355 |
\term.term_for_atom", [u])) |
356 |
sel_names |> the) sel_xs |
|
357 |
val arg_Rs = map (snd o dest_Func) sel_Rs |
|
358 |
val sel_jsss = map tuples_for_const sel_xs |
|
359 |
val arg_jsss = |
|
360 |
map (map_filter (fn js => if hd js = real_j then SOME (tl js) |
|
361 |
else NONE)) sel_jsss |
|
362 |
val uncur_arg_Ts = binder_types constr_T |
|
363 |
in |
|
364 |
if co andalso (T, j) mem seen then |
|
365 |
Var (var ()) |
|
366 |
else |
|
367 |
let |
|
368 |
val seen = seen |> co ? cons (T, j) |
|
369 |
val ts = |
|
370 |
if length arg_Ts = 0 then |
|
371 |
[] |
|
372 |
else |
|
373 |
map3 (fn Ts => term_for_rep seen Ts Ts) arg_Ts arg_Rs |
|
374 |
arg_jsss |
|
375 |
|> mk_tuple (HOLogic.mk_tupleT uncur_arg_Ts) |
|
376 |
|> dest_n_tuple (length uncur_arg_Ts) |
|
377 |
val t = |
|
378 |
if constr_s = @{const_name Abs_Frac} then |
|
379 |
let |
|
380 |
val num_T = body_type T |
|
381 |
(* int -> term *) |
|
382 |
val mk_num = HOLogic.mk_number num_T |
|
383 |
in |
|
384 |
case ts of |
|
385 |
[Const (@{const_name Pair}, _) $ t1 $ t2] => |
|
386 |
(case snd (HOLogic.dest_number t1) of |
|
387 |
0 => mk_num 0 |
|
388 |
| n1 => case HOLogic.dest_number t2 |> snd of |
|
389 |
1 => mk_num n1 |
|
390 |
| n2 => Const (@{const_name HOL.divide}, |
|
391 |
[num_T, num_T] ---> num_T) |
|
392 |
$ mk_num n1 $ mk_num n2) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
393 |
| _ => raise TERM ("Nitpick_Model.reconstruct_term.term_\ |
33192 | 394 |
\for_atom (Abs_Frac)", ts) |
395 |
end |
|
396 |
else if not for_auto andalso is_abs_fun thy constr_x then |
|
397 |
Const (abs_name, constr_T) $ the_single ts |
|
398 |
else |
|
399 |
list_comb (Const constr_x, ts) |
|
400 |
in |
|
401 |
if co then |
|
402 |
let val var = var () in |
|
403 |
if exists_subterm (equal (Var var)) t then |
|
404 |
Const (@{const_name The}, (T --> bool_T) --> T) |
|
405 |
$ Abs ("\<omega>", T, |
|
406 |
Const (@{const_name "op ="}, [T, T] ---> bool_T) |
|
407 |
$ Bound 0 $ abstract_over (Var var, t)) |
|
408 |
else |
|
409 |
t |
|
410 |
end |
|
411 |
else |
|
412 |
t |
|
413 |
end |
|
414 |
end |
|
415 |
(* (typ * int) list -> int -> rep -> typ -> typ -> typ -> int list |
|
416 |
-> term *) |
|
417 |
and term_for_vect seen k R T1 T2 T' js = |
|
418 |
make_fun true T1 T2 T' (map (term_for_atom seen T1 T1) (index_seq 0 k)) |
|
419 |
(map (term_for_rep seen T2 T2 R o single) |
|
420 |
(batch_list (arity_of_rep R) js)) |
|
421 |
(* (typ * int) list -> typ -> typ -> rep -> int list list -> term *) |
|
422 |
and term_for_rep seen T T' Unit [[]] = term_for_atom seen T T' 0 |
|
423 |
| term_for_rep seen T T' (R as Atom (k, j0)) [[j]] = |
|
424 |
if j >= j0 andalso j < j0 + k then term_for_atom seen T T' (j - j0) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
425 |
else raise REP ("Nitpick_Model.reconstruct_term.term_for_rep", [R]) |
33192 | 426 |
| term_for_rep seen (Type ("*", [T1, T2])) _ (Struct [R1, R2]) [js] = |
427 |
let |
|
428 |
val arity1 = arity_of_rep R1 |
|
429 |
val (js1, js2) = chop arity1 js |
|
430 |
in |
|
431 |
list_comb (HOLogic.pair_const T1 T2, |
|
432 |
map3 (fn T => term_for_rep seen T T) [T1, T2] [R1, R2] |
|
433 |
[[js1], [js2]]) |
|
434 |
end |
|
435 |
| term_for_rep seen (Type ("fun", [T1, T2])) T' (R as Vect (k, R')) [js] = |
|
436 |
term_for_vect seen k R' T1 T2 T' js |
|
437 |
| term_for_rep seen (Type ("fun", [T1, T2])) T' (Func (R1, Formula Neut)) |
|
438 |
jss = |
|
439 |
let |
|
440 |
val jss1 = all_combinations_for_rep R1 |
|
441 |
val ts1 = map (term_for_rep seen T1 T1 R1 o single) jss1 |
|
442 |
val ts2 = |
|
443 |
map (fn js => term_for_rep seen T2 T2 (Atom (2, 0)) |
|
444 |
[[int_for_bool (js mem jss)]]) jss1 |
|
445 |
in make_fun false T1 T2 T' ts1 ts2 end |
|
446 |
| term_for_rep seen (Type ("fun", [T1, T2])) T' (Func (R1, R2)) jss = |
|
447 |
let |
|
448 |
val arity1 = arity_of_rep R1 |
|
449 |
val jss1 = all_combinations_for_rep R1 |
|
450 |
val ts1 = map (term_for_rep seen T1 T1 R1 o single) jss1 |
|
451 |
val grouped_jss2 = AList.group (op =) (map (chop arity1) jss) |
|
452 |
val ts2 = map (term_for_rep seen T2 T2 R2 o the_default [] |
|
453 |
o AList.lookup (op =) grouped_jss2) jss1 |
|
454 |
in make_fun true T1 T2 T' ts1 ts2 end |
|
455 |
| term_for_rep seen T T' (Opt R) jss = |
|
456 |
if null jss then Const (unknown, T) else term_for_rep seen T T' R jss |
|
457 |
| term_for_rep seen T _ R jss = |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
458 |
raise ARG ("Nitpick_Model.reconstruct_term.term_for_rep", |
33192 | 459 |
Refute.string_of_typ T ^ " " ^ string_for_rep R ^ " " ^ |
460 |
string_of_int (length jss)) |
|
461 |
in |
|
462 |
(not for_auto ? setify_mapify_funs []) o unbox_term oooo term_for_rep [] |
|
463 |
end |
|
464 |
||
465 |
(* scope -> nut list -> nut NameTable.table -> Kodkod.raw_bound list -> nut |
|
466 |
-> term *) |
|
467 |
fun term_for_name scope sel_names rel_table bounds name = |
|
468 |
let val T = type_of name in |
|
469 |
tuple_list_for_name rel_table bounds name |
|
470 |
|> reconstruct_term ("", "", "", "") scope sel_names rel_table bounds T T |
|
471 |
(rep_of name) |
|
472 |
end |
|
473 |
||
474 |
(* Proof.context |
|
475 |
-> (string * string * string * string * string) * Proof.context *) |
|
476 |
fun add_wacky_syntax ctxt = |
|
477 |
let |
|
478 |
(* term -> string *) |
|
479 |
val name_of = fst o dest_Const |
|
480 |
val thy = ProofContext.theory_of ctxt |> Context.reject_draft |
|
481 |
val (maybe_t, thy) = |
|
33202 | 482 |
Sign.declare_const ((@{binding nitpick_maybe}, @{typ "'a => 'a"}), |
483 |
Mixfix (maybe_mixfix, [1000], 1000)) thy |
|
33192 | 484 |
val (base_t, thy) = |
33202 | 485 |
Sign.declare_const ((@{binding nitpick_base}, @{typ "'a => 'a"}), |
486 |
Mixfix (base_mixfix, [1000], 1000)) thy |
|
33192 | 487 |
val (step_t, thy) = |
33202 | 488 |
Sign.declare_const ((@{binding nitpick_step}, @{typ "'a => 'a"}), |
489 |
Mixfix (step_mixfix, [1000], 1000)) thy |
|
33192 | 490 |
val (abs_t, thy) = |
33202 | 491 |
Sign.declare_const ((@{binding nitpick_abs}, @{typ "'a => 'b"}), |
492 |
Mixfix (abs_mixfix, [40], 40)) thy |
|
33192 | 493 |
in |
494 |
((name_of maybe_t, name_of base_t, name_of step_t, name_of abs_t), |
|
495 |
ProofContext.transfer_syntax thy ctxt) |
|
496 |
end |
|
497 |
||
498 |
(* term -> term *) |
|
499 |
fun unfold_outer_the_binders (t as Const (@{const_name The}, _) |
|
500 |
$ Abs (s, T, Const (@{const_name "op ="}, _) |
|
501 |
$ Bound 0 $ t')) = |
|
502 |
betapply (Abs (s, T, t'), t) |> unfold_outer_the_binders |
|
503 |
| unfold_outer_the_binders t = t |
|
504 |
(* typ list -> int -> term * term -> bool *) |
|
505 |
fun bisimilar_values _ 0 _ = true |
|
506 |
| bisimilar_values coTs max_depth (t1, t2) = |
|
507 |
let val T = fastype_of t1 in |
|
508 |
if exists_subtype (member (op =) coTs) T then |
|
509 |
let |
|
510 |
val ((head1, args1), (head2, args2)) = |
|
511 |
pairself (strip_comb o unfold_outer_the_binders) (t1, t2) |
|
512 |
val max_depth = max_depth - (if T mem coTs then 1 else 0) |
|
513 |
in |
|
514 |
head1 = head2 |
|
515 |
andalso forall (bisimilar_values coTs max_depth) (args1 ~~ args2) |
|
516 |
end |
|
517 |
else |
|
518 |
t1 = t2 |
|
519 |
end |
|
520 |
||
521 |
(* params -> scope -> (term option * int list) list -> styp list -> nut list |
|
522 |
-> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list |
|
523 |
-> Pretty.T * bool *) |
|
524 |
fun reconstruct_hol_model {show_skolems, show_datatypes, show_consts} |
|
525 |
({ext_ctxt as {thy, ctxt, max_bisim_depth, boxes, user_axioms, debug, |
|
526 |
wfs, destroy_constrs, specialize, skolemize, |
|
527 |
star_linear_preds, uncurry, fast_descrs, tac_timeout, |
|
528 |
evals, case_names, def_table, nondef_table, user_nondefs, |
|
529 |
simp_table, psimp_table, intro_table, ground_thm_table, |
|
530 |
ersatz_table, skolems, special_funs, unrolled_preds, |
|
531 |
wf_cache}, |
|
532 |
card_assigns, bisim_depth, datatypes, ofs} : scope) formats all_frees |
|
533 |
free_names sel_names nonsel_names rel_table bounds = |
|
534 |
let |
|
535 |
val (wacky_names as (_, base_name, step_name, _), ctxt) = |
|
536 |
add_wacky_syntax ctxt |
|
537 |
val ext_ctxt = |
|
538 |
{thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes, |
|
539 |
wfs = wfs, user_axioms = user_axioms, debug = debug, |
|
540 |
destroy_constrs = destroy_constrs, specialize = specialize, |
|
541 |
skolemize = skolemize, star_linear_preds = star_linear_preds, |
|
542 |
uncurry = uncurry, fast_descrs = fast_descrs, tac_timeout = tac_timeout, |
|
543 |
evals = evals, case_names = case_names, def_table = def_table, |
|
544 |
nondef_table = nondef_table, user_nondefs = user_nondefs, |
|
545 |
simp_table = simp_table, psimp_table = psimp_table, |
|
546 |
intro_table = intro_table, ground_thm_table = ground_thm_table, |
|
547 |
ersatz_table = ersatz_table, skolems = skolems, |
|
548 |
special_funs = special_funs, unrolled_preds = unrolled_preds, |
|
549 |
wf_cache = wf_cache} |
|
550 |
val scope = {ext_ctxt = ext_ctxt, card_assigns = card_assigns, |
|
551 |
bisim_depth = bisim_depth, datatypes = datatypes, ofs = ofs} |
|
552 |
(* typ -> typ -> rep -> int list list -> term *) |
|
553 |
val term_for_rep = reconstruct_term wacky_names scope sel_names rel_table |
|
554 |
bounds |
|
555 |
(* typ -> typ -> typ *) |
|
556 |
fun nth_value_of_type T card n = term_for_rep T T (Atom (card, 0)) [[n]] |
|
557 |
(* dtype_spec list -> dtype_spec -> bool *) |
|
558 |
fun is_codatatype_wellformed (cos : dtype_spec list) |
|
559 |
({typ, card, ...} : dtype_spec) = |
|
560 |
let |
|
561 |
val ts = map (nth_value_of_type typ card) (index_seq 0 card) |
|
562 |
val max_depth = Integer.sum (map #card cos) |
|
563 |
in |
|
564 |
forall (not o bisimilar_values (map #typ cos) max_depth) |
|
565 |
(all_distinct_unordered_pairs_of ts) |
|
566 |
end |
|
567 |
(* string -> Pretty.T *) |
|
568 |
fun pretty_for_assign name = |
|
569 |
let |
|
570 |
val (oper, (t1, T'), T) = |
|
571 |
case name of |
|
572 |
FreeName (s, T, _) => |
|
573 |
let val t = Free (s, unbox_type T) in |
|
574 |
("=", (t, format_term_type thy def_table formats t), T) |
|
575 |
end |
|
576 |
| ConstName (s, T, _) => |
|
577 |
(assign_operator_for_const (s, T), |
|
578 |
user_friendly_const ext_ctxt (base_name, step_name) formats (s, T), |
|
579 |
T) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
580 |
| _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\ |
33192 | 581 |
\pretty_for_assign", [name]) |
582 |
val t2 = if rep_of name = Any then |
|
583 |
Const (@{const_name undefined}, T') |
|
584 |
else |
|
585 |
tuple_list_for_name rel_table bounds name |
|
586 |
|> term_for_rep T T' (rep_of name) |
|
587 |
in |
|
588 |
Pretty.block (Pretty.breaks |
|
589 |
[(setmp_CRITICAL show_question_marks false o setmp_show_all_types) |
|
590 |
(Syntax.pretty_term ctxt) t1, |
|
591 |
Pretty.str oper, Syntax.pretty_term ctxt t2]) |
|
592 |
end |
|
593 |
(* dtype_spec -> Pretty.T *) |
|
594 |
fun pretty_for_datatype ({typ, card, precise, ...} : dtype_spec) = |
|
595 |
Pretty.block (Pretty.breaks |
|
596 |
[Syntax.pretty_typ ctxt (unbox_type typ), Pretty.str "=", |
|
597 |
Pretty.enum "," "{" "}" |
|
598 |
(map (Syntax.pretty_term ctxt o nth_value_of_type typ card) |
|
599 |
(index_seq 0 card) @ |
|
600 |
(if precise then [] else [Pretty.str unrep]))]) |
|
601 |
(* typ -> dtype_spec list *) |
|
602 |
fun integer_datatype T = |
|
603 |
[{typ = T, card = card_of_type card_assigns T, co = false, |
|
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
604 |
precise = false, shallow = false, constrs = []}] |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
605 |
handle TYPE ("Nitpick_HOL.card_of_type", _, _) => [] |
33192 | 606 |
val (codatatypes, datatypes) = |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
607 |
datatypes |> filter_out #shallow |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
608 |
|> List.partition #co |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
609 |
||> append (integer_datatype nat_T @ integer_datatype int_T) |
33192 | 610 |
val block_of_datatypes = |
611 |
if show_datatypes andalso not (null datatypes) then |
|
612 |
[Pretty.big_list ("Datatype" ^ plural_s_for_list datatypes ^ ":") |
|
613 |
(map pretty_for_datatype datatypes)] |
|
614 |
else |
|
615 |
[] |
|
616 |
val block_of_codatatypes = |
|
617 |
if show_datatypes andalso not (null codatatypes) then |
|
618 |
[Pretty.big_list ("Codatatype" ^ plural_s_for_list codatatypes ^ ":") |
|
619 |
(map pretty_for_datatype codatatypes)] |
|
620 |
else |
|
621 |
[] |
|
622 |
(* bool -> string -> nut list -> Pretty.T list *) |
|
623 |
fun block_of_names show title names = |
|
624 |
if show andalso not (null names) then |
|
625 |
Pretty.str (title ^ plural_s_for_list names ^ ":") |
|
626 |
:: map (Pretty.indent indent_size o pretty_for_assign) |
|
627 |
(sort_wrt (original_name o nickname_of) names) |
|
628 |
else |
|
629 |
[] |
|
630 |
val (skolem_names, nonskolem_nonsel_names) = |
|
631 |
List.partition is_skolem_name nonsel_names |
|
632 |
val (eval_names, noneval_nonskolem_nonsel_names) = |
|
633 |
List.partition (String.isPrefix eval_prefix o nickname_of) |
|
634 |
nonskolem_nonsel_names |
|
635 |
||> filter_out (equal @{const_name bisim_iterator_max} o nickname_of) |
|
636 |
val free_names = |
|
637 |
map (fn x as (s, T) => |
|
638 |
case filter (equal x o nickname_of pairf (unbox_type o type_of)) |
|
639 |
free_names of |
|
640 |
[name] => name |
|
641 |
| [] => FreeName (s, T, Any) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33202
diff
changeset
|
642 |
| _ => raise TERM ("Nitpick_Model.reconstruct_hol_model", |
33192 | 643 |
[Const x])) all_frees |
644 |
val chunks = block_of_names true "Free variable" free_names @ |
|
645 |
block_of_names show_skolems "Skolem constant" skolem_names @ |
|
646 |
block_of_names true "Evaluated term" eval_names @ |
|
647 |
block_of_datatypes @ block_of_codatatypes @ |
|
648 |
block_of_names show_consts "Constant" |
|
649 |
noneval_nonskolem_nonsel_names |
|
650 |
in |
|
651 |
(Pretty.chunks (if null chunks then [Pretty.str "Empty assignment"] |
|
652 |
else chunks), |
|
653 |
bisim_depth >= 0 |
|
654 |
orelse forall (is_codatatype_wellformed codatatypes) codatatypes) |
|
655 |
end |
|
656 |
||
657 |
(* scope -> Time.time option -> nut list -> nut list -> nut NameTable.table |
|
658 |
-> Kodkod.raw_bound list -> term -> bool option *) |
|
659 |
fun prove_hol_model (scope as {ext_ctxt as {thy, ctxt, ...}, card_assigns, ...}) |
|
660 |
auto_timeout free_names sel_names rel_table bounds prop = |
|
661 |
let |
|
662 |
(* typ * int -> term *) |
|
663 |
fun free_type_assm (T, k) = |
|
664 |
let |
|
665 |
(* int -> term *) |
|
666 |
val atom = atom true T |
|
667 |
fun equation_for_atom j = HOLogic.eq_const T $ Bound 0 $ atom j |
|
668 |
val eqs = map equation_for_atom (index_seq 0 k) |
|
669 |
val compreh_assm = |
|
670 |
Const (@{const_name All}, (T --> bool_T) --> bool_T) |
|
671 |
$ Abs ("x", T, foldl1 HOLogic.mk_disj eqs) |
|
672 |
val distinct_assm = distinctness_formula T (map atom (index_seq 0 k)) |
|
673 |
in HOLogic.mk_conj (compreh_assm, distinct_assm) end |
|
674 |
(* nut -> term *) |
|
675 |
fun free_name_assm name = |
|
676 |
HOLogic.mk_eq (Free (nickname_of name, type_of name), |
|
677 |
term_for_name scope sel_names rel_table bounds name) |
|
678 |
val freeT_assms = map free_type_assm (filter (is_TFree o fst) card_assigns) |
|
679 |
val model_assms = map free_name_assm free_names |
|
680 |
val assm = List.foldr HOLogic.mk_conj @{const True} |
|
681 |
(freeT_assms @ model_assms) |
|
682 |
(* bool -> bool *) |
|
683 |
fun try_out negate = |
|
684 |
let |
|
685 |
val concl = (negate ? curry (op $) @{const Not}) |
|
686 |
(ObjectLogic.atomize_term thy prop) |
|
687 |
val goal = HOLogic.mk_Trueprop (HOLogic.mk_imp (assm, concl)) |
|
688 |
|> map_types (map_type_tfree |
|
689 |
(fn (s, []) => TFree (s, HOLogic.typeS) |
|
690 |
| x => TFree x)) |
|
691 |
|> cterm_of thy |> Goal.init |
|
692 |
in |
|
693 |
(goal |> SINGLE (DETERM_TIMEOUT auto_timeout |
|
694 |
(auto_tac (clasimpset_of ctxt))) |
|
695 |
|> the |> Goal.finish ctxt; true) |
|
696 |
handle THM _ => false |
|
697 |
| TimeLimit.TimeOut => false |
|
698 |
end |
|
699 |
in |
|
700 |
if silence try_out false then SOME true |
|
701 |
else if silence try_out true then SOME false |
|
702 |
else NONE |
|
703 |
end |
|
704 |
||
705 |
end; |