more work on Nitpick's support for nonstandard models + fix in model reconstruction
1 (* Title: HOL/Tools/Nitpick/nitpick_model.ML
2 Author: Jasmin Blanchette, TU Muenchen
5 Model reconstruction for Nitpick.
8 signature NITPICK_MODEL =
10 type styp = Nitpick_Util.styp
11 type scope = Nitpick_Scope.scope
12 type rep = Nitpick_Rep.rep
13 type nut = Nitpick_Nut.nut
20 structure NameTable : TABLE
22 val tuple_list_for_name :
23 nut NameTable.table -> Kodkod.raw_bound list -> nut -> int list list
24 val reconstruct_hol_model :
25 params -> scope -> (term option * int list) list -> styp list -> nut list
26 -> nut list -> nut list -> nut NameTable.table -> Kodkod.raw_bound list
29 scope -> Time.time option -> nut list -> nut list -> nut NameTable.table
30 -> Kodkod.raw_bound list -> term -> bool option
33 structure Nitpick_Model : NITPICK_MODEL =
52 val maybe_mixfix = "_\<^sup>?"
53 val base_mixfix = "_\<^bsub>base\<^esub>"
54 val step_mixfix = "_\<^bsub>step\<^esub>"
55 val abs_mixfix = "\<guillemotleft>_\<guillemotright>"
56 val cyclic_co_val_name = "\<omega>"
57 val cyclic_type_name = "\<xi>"
58 val opt_flag = nitpick_prefix ^ "opt"
59 val non_opt_flag = nitpick_prefix ^ "non_opt"
61 type atom_pool = ((string * int) * int list) list
63 (* atom_pool Unsynchronized.ref -> string -> int -> int -> string *)
64 fun nth_atom_suffix pool s j k =
65 (case AList.lookup (op =) (!pool) (s, k) of
67 (case find_index (curry (op =) j) js of
68 ~1 => (Unsynchronized.change pool (cons ((s, k), j :: js));
71 | NONE => (Unsynchronized.change pool (cons ((s, k), [j])); 1))
73 |> (s <> "" andalso Symbol.is_ascii_digit (List.last (explode s)))
75 (* atom_pool Unsynchronized.ref -> string -> typ -> int -> int -> string *)
76 fun nth_atom_name pool prefix (T as Type (s, _)) j k =
77 let val s' = shortest_name s in
78 prefix ^ (if String.isPrefix "\\" s' then s' else substring (s', 0, 1)) ^
79 nth_atom_suffix pool s j k
81 | nth_atom_name pool prefix (T as TFree (s, _)) j k =
82 prefix ^ perhaps (try (unprefix "'")) s ^ nth_atom_suffix pool s j k
83 | nth_atom_name _ _ T _ _ =
84 raise TYPE ("Nitpick_Model.nth_atom_name", [T], [])
85 (* atom_pool Unsynchronized.ref -> bool -> typ -> int -> int -> term *)
86 fun nth_atom pool for_auto T j k =
88 Free (nth_atom_name pool (hd (space_explode "." nitpick_prefix)) T j k, T)
90 Const (nth_atom_name pool "" T j k, T)
93 fun extract_real_number (Const (@{const_name divide}, _) $ t1 $ t2) =
94 real (snd (HOLogic.dest_number t1)) / real (snd (HOLogic.dest_number t2))
95 | extract_real_number t = real (snd (HOLogic.dest_number t))
96 (* term * term -> order *)
97 fun nice_term_ord (Abs (_, _, t1), Abs (_, _, t2)) = nice_term_ord (t1, t2)
98 | nice_term_ord tp = Real.compare (pairself extract_real_number tp)
99 handle TERM ("dest_number", _) =>
101 (t11 $ t12, t21 $ t22) =>
102 (case nice_term_ord (t11, t21) of
103 EQUAL => nice_term_ord (t12, t22)
105 | _ => TermOrd.fast_term_ord tp
107 (* nut NameTable.table -> KK.raw_bound list -> nut -> int list list *)
108 fun tuple_list_for_name rel_table bounds name =
109 the (AList.lookup (op =) bounds (the_rel rel_table name)) handle NUT _ => [[]]
112 fun unbit_and_unbox_term (Const (@{const_name FunBox}, _) $ t1) =
113 unbit_and_unbox_term t1
114 | unbit_and_unbox_term (Const (@{const_name PairBox},
115 Type ("fun", [T1, Type ("fun", [T2, T3])]))
117 let val Ts = map unbit_and_unbox_type [T1, T2] in
118 Const (@{const_name Pair}, Ts ---> Type ("*", Ts))
119 $ unbit_and_unbox_term t1 $ unbit_and_unbox_term t2
121 | unbit_and_unbox_term (Const (s, T)) = Const (s, unbit_and_unbox_type T)
122 | unbit_and_unbox_term (t1 $ t2) =
123 unbit_and_unbox_term t1 $ unbit_and_unbox_term t2
124 | unbit_and_unbox_term (Free (s, T)) = Free (s, unbit_and_unbox_type T)
125 | unbit_and_unbox_term (Var (x, T)) = Var (x, unbit_and_unbox_type T)
126 | unbit_and_unbox_term (Bound j) = Bound j
127 | unbit_and_unbox_term (Abs (s, T, t')) =
128 Abs (s, unbit_and_unbox_type T, unbit_and_unbox_term t')
130 (* typ -> typ -> (typ * typ) * (typ * typ) *)
131 fun factor_out_types (T1 as Type ("*", [T11, T12]))
132 (T2 as Type ("*", [T21, T22])) =
133 let val (n1, n2) = pairself num_factors_in_type (T11, T21) in
136 val ((T11', opt_T12'), (T21', opt_T22')) = factor_out_types T12 T22
138 ((Type ("*", [T11, T11']), opt_T12'),
139 (Type ("*", [T21, T21']), opt_T22'))
142 case factor_out_types T1 T21 of
143 (p1, (T21', NONE)) => (p1, (T21', SOME T22))
144 | (p1, (T21', SOME T22')) =>
145 (p1, (T21', SOME (Type ("*", [T22', T22]))))
147 swap (factor_out_types T2 T1)
149 | factor_out_types (Type ("*", [T11, T12])) T2 = ((T11, SOME T12), (T2, NONE))
150 | factor_out_types T1 (Type ("*", [T21, T22])) = ((T1, NONE), (T21, SOME T22))
151 | factor_out_types T1 T2 = ((T1, NONE), (T2, NONE))
153 (* bool -> typ -> typ -> (term * term) list -> term *)
154 fun make_plain_fun maybe_opt T1 T2 =
156 (* typ -> typ -> (term * term) list -> term *)
158 Const (if maybe_opt then opt_flag else non_opt_flag, T1 --> T2)
159 | aux T1 T2 ((t1, t2) :: ps) =
160 Const (@{const_name fun_upd}, (T1 --> T2) --> T1 --> T2 --> T1 --> T2)
161 $ aux T1 T2 ps $ t1 $ t2
162 in aux T1 T2 o rev end
164 fun is_plain_fun (Const (s, _)) = (s = opt_flag orelse s = non_opt_flag)
165 | is_plain_fun (Const (@{const_name fun_upd}, _) $ t0 $ _ $ _) =
167 | is_plain_fun _ = false
168 (* term -> bool * (term list * term list) *)
171 (* term -> bool * (term list * term list) *)
172 fun aux (Const (s, _)) = (s <> non_opt_flag, ([], []))
173 | aux (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
174 let val (s, (ts1, ts2)) = aux t0 in (s, (t1 :: ts1, t2 :: ts2)) end
175 | aux t = raise TERM ("Nitpick_Model.dest_plain_fun", [t])
176 in apsnd (pairself rev) o aux end
178 (* typ -> typ -> typ -> term -> term * term *)
179 fun break_in_two T T1 T2 t =
181 val ps = HOLogic.flat_tupleT_paths T
182 val cut = length (HOLogic.strip_tupleT T1)
183 val (ps1, ps2) = pairself HOLogic.flat_tupleT_paths (T1, T2)
184 val (ts1, ts2) = t |> HOLogic.strip_ptuple ps |> chop cut
185 in (HOLogic.mk_ptuple ps1 T1 ts1, HOLogic.mk_ptuple ps2 T2 ts2) end
186 (* typ -> term -> term -> term *)
187 fun pair_up (Type ("*", [T1', T2']))
188 (t1 as Const (@{const_name Pair},
189 Type ("fun", [_, Type ("fun", [_, T1])])) $ t11 $ t12)
191 if T1 = T1' then HOLogic.mk_prod (t1, t2)
192 else HOLogic.mk_prod (t11, pair_up T2' t12 t2)
193 | pair_up _ t1 t2 = HOLogic.mk_prod (t1, t2)
194 (* typ -> term -> term list * term list -> (term * term) list*)
195 fun multi_pair_up T1 t1 (ts2, ts3) = map2 (pair o pair_up T1 t1) ts2 ts3
197 (* typ -> typ -> typ -> term -> term *)
198 fun typecast_fun (Type ("fun", [T1', T2'])) T1 T2 t =
200 (* typ -> typ -> typ -> typ -> term -> term *)
201 fun do_curry T1 T1a T1b T2 t =
203 val (maybe_opt, ps) = dest_plain_fun t
205 ps |>> map (break_in_two T1 T1a T1b)
206 |> uncurry (map2 (fn (t1a, t1b) => fn t2 => (t1a, (t1b, t2))))
207 |> AList.coalesce (op =)
208 |> map (apsnd (make_plain_fun maybe_opt T1b T2))
209 in make_plain_fun maybe_opt T1a (T1b --> T2) ps end
210 (* typ -> typ -> term -> term *)
211 and do_uncurry T1 T2 t =
213 val (maybe_opt, tsp) = dest_plain_fun t
216 |> maps (fn (t1, t2) =>
217 multi_pair_up T1 t1 (snd (dest_plain_fun t2)))
218 in make_plain_fun maybe_opt T1 T2 ps end
219 (* typ -> typ -> typ -> typ -> term -> term *)
220 and do_arrow T1' T2' _ _ (Const (s, _)) = Const (s, T1' --> T2')
221 | do_arrow T1' T2' T1 T2
222 (Const (@{const_name fun_upd}, _) $ t0 $ t1 $ t2) =
223 Const (@{const_name fun_upd},
224 (T1' --> T2') --> T1' --> T2' --> T1' --> T2')
225 $ do_arrow T1' T2' T1 T2 t0 $ do_term T1' T1 t1 $ do_term T2' T2 t2
226 | do_arrow _ _ _ _ t =
227 raise TERM ("Nitpick_Model.typecast_fun.do_arrow", [t])
228 and do_fun T1' T2' T1 T2 t =
229 case factor_out_types T1' T1 of
230 ((_, NONE), (_, NONE)) => t |> do_arrow T1' T2' T1 T2
231 | ((_, NONE), (T1a, SOME T1b)) =>
232 t |> do_curry T1 T1a T1b T2 |> do_arrow T1' T2' T1a (T1b --> T2)
233 | ((T1a', SOME T1b'), (_, NONE)) =>
234 t |> do_arrow T1a' (T1b' --> T2') T1 T2 |> do_uncurry T1' T2'
235 | _ => raise TYPE ("Nitpick_Model.typecast_fun.do_fun", [T1, T1'], [])
236 (* typ -> typ -> term -> term *)
237 and do_term (Type ("fun", [T1', T2'])) (Type ("fun", [T1, T2])) t =
238 do_fun T1' T2' T1 T2 t
239 | do_term (T' as Type ("*", Ts' as [T1', T2'])) (Type ("*", [T1, T2]))
240 (Const (@{const_name Pair}, _) $ t1 $ t2) =
241 Const (@{const_name Pair}, Ts' ---> T')
242 $ do_term T1' T1 t1 $ do_term T2' T2 t2
245 else raise TYPE ("Nitpick_Model.typecast_fun.do_term", [T, T'], [])
246 in if T1' = T1 andalso T2' = T2 then t else do_fun T1' T2' T1 T2 t end
247 | typecast_fun T' _ _ _ =
248 raise TYPE ("Nitpick_Model.typecast_fun", [T'], [])
251 fun truth_const_sort_key @{const True} = "0"
252 | truth_const_sort_key @{const False} = "2"
253 | truth_const_sort_key _ = "1"
255 (* typ -> term list -> term *)
256 fun mk_tuple (Type ("*", [T1, T2])) ts =
257 HOLogic.mk_prod (mk_tuple T1 ts,
258 mk_tuple T2 (List.drop (ts, length (HOLogic.flatten_tupleT T1))))
259 | mk_tuple _ (t :: _) = t
260 | mk_tuple T [] = raise TYPE ("Nitpick_Model.mk_tuple", [T], [])
262 (* bool -> atom_pool -> string * string * string * string -> scope -> nut list
263 -> nut list -> nut list -> nut NameTable.table -> KK.raw_bound list -> typ
264 -> typ -> rep -> int list list -> term *)
265 fun reconstruct_term elaborate pool (maybe_name, base_name, step_name, abs_name)
266 ({hol_ctxt as {thy, ctxt, ...}, card_assigns, bits, datatypes, ofs, ...}
267 : scope) sel_names rel_table bounds =
269 val for_auto = (maybe_name = "")
270 (* int list list -> int *)
271 fun value_of_bits jss =
273 val j0 = offset_of_type ofs @{typ unsigned_bit}
274 val js = map (Integer.add (~ j0) o the_single) jss
276 fold (fn j => Integer.add (reasonable_power 2 j |> j = bits ? op ~))
279 (* bool -> typ -> typ -> (term * term) list -> term *)
280 fun make_set maybe_opt T1 T2 =
282 val empty_const = Const (@{const_name Set.empty}, T1 --> T2)
283 val insert_const = Const (@{const_name insert},
284 T1 --> (T1 --> T2) --> T1 --> T2)
285 (* (term * term) list -> term *)
287 if maybe_opt andalso not (is_complete_type datatypes T1) then
288 insert_const $ Const (unrep, T1) $ empty_const
291 | aux ((t1, t2) :: zs) =
292 aux zs |> t2 <> @{const False}
293 ? curry (op $) (insert_const
294 $ (t1 |> t2 <> @{const True}
299 (* typ -> typ -> typ -> (term * term) list -> term *)
300 fun make_map T1 T2 T2' =
302 val update_const = Const (@{const_name fun_upd},
303 (T1 --> T2) --> T1 --> T2 --> T1 --> T2)
304 (* (term * term) list -> term *)
305 fun aux' [] = Const (@{const_name Map.empty}, T1 --> T2)
306 | aux' ((t1, t2) :: ps) =
308 Const (@{const_name None}, _) => aux' ps
309 | _ => update_const $ aux' ps $ t1 $ t2)
311 if not (is_complete_type datatypes T1) then
312 update_const $ aux' ps $ Const (unrep, T1)
313 $ (Const (@{const_name Some}, T2' --> T2) $ Const (unknown, T2'))
317 (* typ list -> term -> term *)
318 fun polish_funs Ts t =
319 (case fastype_of1 (Ts, t) of
320 Type ("fun", [T1, T2]) =>
321 if is_plain_fun t then
325 val (maybe_opt, ts_pair) =
326 dest_plain_fun t ||> pairself (map (polish_funs Ts))
328 make_set maybe_opt T1 T2
329 (sort_wrt (truth_const_sort_key o snd) (op ~~ ts_pair))
331 | Type (@{type_name option}, [T2']) =>
333 val ts_pair = snd (dest_plain_fun t)
334 |> pairself (map (polish_funs Ts))
335 in make_map T1 T2 T2' (rev (op ~~ ts_pair)) end
339 | _ => raise SAME ())
342 (t1 as Const (@{const_name fun_upd}, _) $ t11 $ _)
343 $ (t2 as Const (s, _)) =>
344 if s = unknown then polish_funs Ts t11
345 else polish_funs Ts t1 $ polish_funs Ts t2
346 | t1 $ t2 => polish_funs Ts t1 $ polish_funs Ts t2
347 | Abs (s, T, t') => Abs (s, T, polish_funs (T :: Ts) t')
348 | Const (s, Type ("fun", [T1, T2])) =>
349 if s = opt_flag orelse s = non_opt_flag then
350 Abs ("x", T1, Const (unknown, T2))
354 (* bool -> typ -> typ -> typ -> term list -> term list -> term *)
355 fun make_fun maybe_opt T1 T2 T' ts1 ts2 =
356 ts1 ~~ ts2 |> sort (nice_term_ord o pairself fst)
357 |> make_plain_fun maybe_opt T1 T2
358 |> unbit_and_unbox_term
359 |> typecast_fun (unbit_and_unbox_type T')
360 (unbit_and_unbox_type T1)
361 (unbit_and_unbox_type T2)
362 (* (typ * int) list -> typ -> typ -> int -> term *)
363 fun term_for_atom seen (T as Type ("fun", [T1, T2])) T' j k =
365 val k1 = card_of_type card_assigns T1
366 val k2 = card_of_type card_assigns T2
368 term_for_rep seen T T' (Vect (k1, Atom (k2, 0)))
369 [nth_combination (replicate k1 (k2, 0)) j]
370 handle General.Subscript =>
371 raise ARG ("Nitpick_Model.reconstruct_term.term_for_atom",
372 signed_string_of_int j ^ " for " ^
373 string_for_rep (Vect (k1, Atom (k2, 0))))
375 | term_for_atom seen (Type ("*", [T1, T2])) _ j k =
377 val k1 = card_of_type card_assigns T1
380 list_comb (HOLogic.pair_const T1 T2,
381 map3 (fn T => term_for_atom seen T T) [T1, T2]
382 [j div k2, j mod k2] [k1, k2]) (* ### k2 or k1? FIXME *)
384 | term_for_atom seen @{typ prop} _ j k =
385 HOLogic.mk_Trueprop (term_for_atom seen bool_T bool_T j k)
386 | term_for_atom _ @{typ bool} _ j _ =
387 if j = 0 then @{const False} else @{const True}
388 | term_for_atom _ @{typ unit} _ _ _ = @{const Unity}
389 | term_for_atom seen T _ j k =
391 HOLogic.mk_number nat_T j
392 else if T = int_T then
393 HOLogic.mk_number int_T (int_for_atom (k, 0) j)
394 else if is_fp_iterator_type T then
395 HOLogic.mk_number nat_T (k - j - 1)
396 else if T = @{typ bisim_iterator} then
397 HOLogic.mk_number nat_T j
398 else case datatype_spec datatypes T of
399 NONE => nth_atom pool for_auto T j k
400 | SOME {deep = false, ...} => nth_atom pool for_auto T j k
401 | SOME {co, standard, constrs, ...} =>
403 (* styp -> int list *)
404 fun tuples_for_const (s, T) =
405 tuple_list_for_name rel_table bounds (ConstName (s, T, Any))
408 nth_atom pool for_auto (Type (cyclic_type_name, [])) j k
409 fun cyclic_var () = Var ((nth_atom_name pool "" T j k, 0), T)
411 val discr_jsss = map (tuples_for_const o discr_for_constr o #const)
413 val real_j = j + offset_of_type ofs T
414 val constr_x as (constr_s, constr_T) =
415 get_first (fn (jss, {const, ...}) =>
416 if member (op =) jss [real_j] then SOME const
418 (discr_jsss ~~ constrs) |> the
419 val arg_Ts = curried_binder_types constr_T
420 val sel_xs = map (boxed_nth_sel_for_constr hol_ctxt constr_x)
421 (index_seq 0 (length arg_Ts))
423 map (fn x => get_first
424 (fn ConstName (s', T', R) =>
425 if (s', T') = x then SOME R else NONE
426 | u => raise NUT ("Nitpick_Model.reconstruct_\
427 \term.term_for_atom", [u]))
428 sel_names |> the) sel_xs
429 val arg_Rs = map (snd o dest_Func) sel_Rs
430 val sel_jsss = map tuples_for_const sel_xs
432 map (map_filter (fn js => if hd js = real_j then SOME (tl js)
434 val uncur_arg_Ts = binder_types constr_T
435 val maybe_cyclic = co orelse not standard
437 if maybe_cyclic andalso not (null seen) andalso
438 member (op =) (seen |> elaborate ? (fst o split_last)) (T, j) then
440 else if constr_s = @{const_name Word} then
442 (if T = @{typ "unsigned_bit word"} then nat_T else int_T)
443 (value_of_bits (the_single arg_jsss))
446 val seen = seen |> maybe_cyclic ? cons (T, j)
448 if length arg_Ts = 0 then
451 map3 (fn Ts => term_for_rep seen Ts Ts) arg_Ts arg_Rs
453 |> mk_tuple (HOLogic.mk_tupleT uncur_arg_Ts)
454 |> dest_n_tuple (length uncur_arg_Ts)
456 if constr_s = @{const_name Abs_Frac} then
458 val num_T = body_type T
460 val mk_num = HOLogic.mk_number num_T
463 [Const (@{const_name Pair}, _) $ t1 $ t2] =>
464 (case snd (HOLogic.dest_number t1) of
466 | n1 => case HOLogic.dest_number t2 |> snd of
468 | n2 => Const (@{const_name divide},
469 num_T --> num_T --> num_T)
470 $ mk_num n1 $ mk_num n2)
471 | _ => raise TERM ("Nitpick_Model.reconstruct_term.\
472 \term_for_atom (Abs_Frac)", ts)
474 else if not for_auto andalso
475 (is_abs_fun thy constr_x orelse
476 constr_s = @{const_name Quot}) then
477 Const (abs_name, constr_T) $ the_single ts
479 list_comb (Const constr_x, ts)
482 let val var = cyclic_var () in
483 if elaborate andalso not standard andalso
484 length seen = 1 andalso
485 exists_subterm (fn Const (s, _) =>
486 String.isPrefix cyclic_type_name s
487 | t' => t' = var) t then
488 let val atom = cyclic_atom () in
489 HOLogic.mk_eq (atom, subst_atomic [(var, atom)] t)
491 else if exists_subterm (curry (op =) var) t then
493 Const (@{const_name The}, (T --> bool_T) --> T)
494 $ Abs (cyclic_co_val_name, T,
495 Const (@{const_name "op ="}, T --> T --> bool_T)
496 $ Bound 0 $ abstract_over (var, t))
506 (* (typ * int) list -> int -> rep -> typ -> typ -> typ -> int list
508 and term_for_vect seen k R T1 T2 T' js =
509 make_fun true T1 T2 T'
510 (map (fn j => term_for_atom seen T1 T1 j k) (index_seq 0 k))
511 (map (term_for_rep seen T2 T2 R o single)
512 (batch_list (arity_of_rep R) js))
513 (* (typ * int) list -> typ -> typ -> rep -> int list list -> term *)
514 and term_for_rep seen T T' Unit [[]] = term_for_atom seen T T' 0 1
515 | term_for_rep seen T T' (R as Atom (k, j0)) [[j]] =
516 if j >= j0 andalso j < j0 + k then term_for_atom seen T T' (j - j0) k
517 else raise REP ("Nitpick_Model.reconstruct_term.term_for_rep", [R])
518 | term_for_rep seen (Type ("*", [T1, T2])) _ (Struct [R1, R2]) [js] =
520 val arity1 = arity_of_rep R1
521 val (js1, js2) = chop arity1 js
523 list_comb (HOLogic.pair_const T1 T2,
524 map3 (fn T => term_for_rep seen T T) [T1, T2] [R1, R2]
527 | term_for_rep seen (Type ("fun", [T1, T2])) T' (R as Vect (k, R')) [js] =
528 term_for_vect seen k R' T1 T2 T' js
529 | term_for_rep seen (Type ("fun", [T1, T2])) T' (Func (R1, Formula Neut))
532 val jss1 = all_combinations_for_rep R1
533 val ts1 = map (term_for_rep seen T1 T1 R1 o single) jss1
535 map (fn js => term_for_rep seen T2 T2 (Atom (2, 0))
536 [[int_for_bool (member (op =) jss js)]])
538 in make_fun false T1 T2 T' ts1 ts2 end
539 | term_for_rep seen (Type ("fun", [T1, T2])) T' (Func (R1, R2)) jss =
541 val arity1 = arity_of_rep R1
542 val jss1 = all_combinations_for_rep R1
543 val ts1 = map (term_for_rep seen T1 T1 R1 o single) jss1
544 val grouped_jss2 = AList.group (op =) (map (chop arity1) jss)
545 val ts2 = map (term_for_rep seen T2 T2 R2 o the_default []
546 o AList.lookup (op =) grouped_jss2) jss1
547 in make_fun true T1 T2 T' ts1 ts2 end
548 | term_for_rep seen T T' (Opt R) jss =
549 if null jss then Const (unknown, T) else term_for_rep seen T T' R jss
550 | term_for_rep seen T _ R jss =
551 raise ARG ("Nitpick_Model.reconstruct_term.term_for_rep",
552 Refute.string_of_typ T ^ " " ^ string_for_rep R ^ " " ^
553 string_of_int (length jss))
554 in polish_funs [] o unbit_and_unbox_term oooo term_for_rep [] end
556 (* atom_pool -> scope -> nut list -> nut NameTable.table -> KK.raw_bound list
558 fun term_for_name pool scope sel_names rel_table bounds name =
559 let val T = type_of name in
560 tuple_list_for_name rel_table bounds name
561 |> reconstruct_term false pool ("", "", "", "") scope sel_names rel_table
562 bounds T T (rep_of name)
565 (* Proof.context -> (string * string * string * string) * Proof.context *)
566 fun add_wacky_syntax ctxt =
569 val name_of = fst o dest_Const
570 val thy = ProofContext.theory_of ctxt |> Context.reject_draft
572 Sign.declare_const ((@{binding nitpick_maybe}, @{typ "'a => 'a"}),
573 Mixfix (maybe_mixfix, [1000], 1000)) thy
575 Sign.declare_const ((@{binding nitpick_base}, @{typ "'a => 'a"}),
576 Mixfix (base_mixfix, [1000], 1000)) thy
578 Sign.declare_const ((@{binding nitpick_step}, @{typ "'a => 'a"}),
579 Mixfix (step_mixfix, [1000], 1000)) thy
581 Sign.declare_const ((@{binding nitpick_abs}, @{typ "'a => 'b"}),
582 Mixfix (abs_mixfix, [40], 40)) thy
584 ((name_of maybe_t, name_of base_t, name_of step_t, name_of abs_t),
585 ProofContext.transfer_syntax thy ctxt)
589 fun unfold_outer_the_binders (t as Const (@{const_name The}, _)
590 $ Abs (s, T, Const (@{const_name "op ="}, _)
592 betapply (Abs (s, T, t'), t) |> unfold_outer_the_binders
593 | unfold_outer_the_binders t = t
594 (* typ list -> int -> term * term -> bool *)
595 fun bisimilar_values _ 0 _ = true
596 | bisimilar_values coTs max_depth (t1, t2) =
597 let val T = fastype_of t1 in
598 if exists_subtype (member (op =) coTs) T then
600 val ((head1, args1), (head2, args2)) =
601 pairself (strip_comb o unfold_outer_the_binders) (t1, t2)
602 val max_depth = max_depth - (if member (op =) coTs T then 1 else 0)
604 head1 = head2 andalso
605 forall (bisimilar_values coTs max_depth) (args1 ~~ args2)
611 (* params -> scope -> (term option * int list) list -> styp list -> nut list
612 -> nut list -> nut list -> nut NameTable.table -> KK.raw_bound list
613 -> Pretty.T * bool *)
614 fun reconstruct_hol_model {show_skolems, show_datatypes, show_consts}
615 ({hol_ctxt as {thy, ctxt, max_bisim_depth, boxes, stds, wfs,
616 user_axioms, debug, binary_ints, destroy_constrs,
617 specialize, skolemize, star_linear_preds, uncurry,
618 fast_descrs, tac_timeout, evals, case_names, def_table,
619 nondef_table, user_nondefs, simp_table, psimp_table,
620 intro_table, ground_thm_table, ersatz_table, skolems,
621 special_funs, unrolled_preds, wf_cache, constr_cache},
622 card_assigns, bits, bisim_depth, datatypes, ofs} : scope)
623 formats all_frees free_names sel_names nonsel_names rel_table bounds =
625 val pool = Unsynchronized.ref []
626 val (wacky_names as (_, base_name, step_name, _), ctxt) =
627 add_wacky_syntax ctxt
629 {thy = thy, ctxt = ctxt, max_bisim_depth = max_bisim_depth, boxes = boxes,
630 stds = stds, wfs = wfs, user_axioms = user_axioms, debug = debug,
631 binary_ints = binary_ints, destroy_constrs = destroy_constrs,
632 specialize = specialize, skolemize = skolemize,
633 star_linear_preds = star_linear_preds, uncurry = uncurry,
634 fast_descrs = fast_descrs, tac_timeout = tac_timeout, evals = evals,
635 case_names = case_names, def_table = def_table,
636 nondef_table = nondef_table, user_nondefs = user_nondefs,
637 simp_table = simp_table, psimp_table = psimp_table,
638 intro_table = intro_table, ground_thm_table = ground_thm_table,
639 ersatz_table = ersatz_table, skolems = skolems,
640 special_funs = special_funs, unrolled_preds = unrolled_preds,
641 wf_cache = wf_cache, constr_cache = constr_cache}
642 val scope = {hol_ctxt = hol_ctxt, card_assigns = card_assigns,
643 bits = bits, bisim_depth = bisim_depth, datatypes = datatypes,
645 (* bool -> typ -> typ -> rep -> int list list -> term *)
646 fun term_for_rep elaborate =
647 reconstruct_term elaborate pool wacky_names scope sel_names rel_table
649 (* nat -> typ -> nat -> typ *)
650 fun nth_value_of_type card T n =
651 term_for_rep true T T (Atom (card, 0)) [[n]]
652 (* nat -> typ -> typ list *)
653 fun all_values_of_type card T =
654 index_seq 0 card |> map (nth_value_of_type card T) |> sort nice_term_ord
655 (* dtype_spec list -> dtype_spec -> bool *)
656 fun is_codatatype_wellformed (cos : dtype_spec list)
657 ({typ, card, ...} : dtype_spec) =
659 val ts = all_values_of_type card typ
660 val max_depth = Integer.sum (map #card cos)
662 forall (not o bisimilar_values (map #typ cos) max_depth)
663 (all_distinct_unordered_pairs_of ts)
665 (* string -> Pretty.T *)
666 fun pretty_for_assign name =
668 val (oper, (t1, T'), T) =
670 FreeName (s, T, _) =>
671 let val t = Free (s, unbit_and_unbox_type T) in
672 ("=", (t, format_term_type thy def_table formats t), T)
674 | ConstName (s, T, _) =>
675 (assign_operator_for_const (s, T),
676 user_friendly_const hol_ctxt (base_name, step_name) formats (s, T),
678 | _ => raise NUT ("Nitpick_Model.reconstruct_hol_model.\
679 \pretty_for_assign", [name])
680 val t2 = if rep_of name = Any then
681 Const (@{const_name undefined}, T')
683 tuple_list_for_name rel_table bounds name
684 |> term_for_rep false T T' (rep_of name)
686 Pretty.block (Pretty.breaks
687 [setmp_show_all_types (Syntax.pretty_term ctxt) t1,
688 Pretty.str oper, Syntax.pretty_term ctxt t2])
690 (* dtype_spec -> Pretty.T *)
691 fun pretty_for_datatype ({typ, card, complete, ...} : dtype_spec) =
692 Pretty.block (Pretty.breaks
693 [Syntax.pretty_typ ctxt (unbit_and_unbox_type typ), Pretty.str "=",
694 Pretty.enum "," "{" "}"
695 (map (Syntax.pretty_term ctxt) (all_values_of_type card typ) @
696 (if complete then [] else [Pretty.str unrep]))])
697 (* typ -> dtype_spec list *)
698 fun integer_datatype T =
699 [{typ = T, card = card_of_type card_assigns T, co = false,
700 standard = true, complete = false, concrete = true, deep = true,
702 handle TYPE ("Nitpick_HOL.card_of_type", _, _) => []
703 val (codatatypes, datatypes) =
704 datatypes |> filter #deep |> List.partition #co
705 ||> append (integer_datatype nat_T @ integer_datatype int_T)
706 val block_of_datatypes =
707 if show_datatypes andalso not (null datatypes) then
708 [Pretty.big_list ("Datatype" ^ plural_s_for_list datatypes ^ ":")
709 (map pretty_for_datatype datatypes)]
712 val block_of_codatatypes =
713 if show_datatypes andalso not (null codatatypes) then
714 [Pretty.big_list ("Codatatype" ^ plural_s_for_list codatatypes ^ ":")
715 (map pretty_for_datatype codatatypes)]
718 (* bool -> string -> nut list -> Pretty.T list *)
719 fun block_of_names show title names =
720 if show andalso not (null names) then
721 Pretty.str (title ^ plural_s_for_list names ^ ":")
722 :: map (Pretty.indent indent_size o pretty_for_assign)
723 (sort_wrt (original_name o nickname_of) names)
726 val (skolem_names, nonskolem_nonsel_names) =
727 List.partition is_skolem_name nonsel_names
728 val (eval_names, noneval_nonskolem_nonsel_names) =
729 List.partition (String.isPrefix eval_prefix o nickname_of)
730 nonskolem_nonsel_names
731 ||> filter_out (curry (op =) @{const_name bisim_iterator_max}
734 map (fn x as (s, T) =>
735 case filter (curry (op =) x
736 o pairf nickname_of (unbit_and_unbox_type o type_of))
739 | [] => FreeName (s, T, Any)
740 | _ => raise TERM ("Nitpick_Model.reconstruct_hol_model",
741 [Const x])) all_frees
742 val chunks = block_of_names true "Free variable" free_names @
743 block_of_names show_skolems "Skolem constant" skolem_names @
744 block_of_names true "Evaluated term" eval_names @
745 block_of_datatypes @ block_of_codatatypes @
746 block_of_names show_consts "Constant"
747 noneval_nonskolem_nonsel_names
749 (Pretty.chunks (if null chunks then [Pretty.str "Empty assignment"]
751 bisim_depth >= 0 orelse
752 forall (is_codatatype_wellformed codatatypes) codatatypes)
755 (* scope -> Time.time option -> nut list -> nut list -> nut NameTable.table
756 -> KK.raw_bound list -> term -> bool option *)
757 fun prove_hol_model (scope as {hol_ctxt as {thy, ctxt, debug, ...},
759 auto_timeout free_names sel_names rel_table bounds prop =
761 val pool = Unsynchronized.ref []
762 (* typ * int -> term *)
763 fun free_type_assm (T, k) =
766 fun atom j = nth_atom pool true T j k
767 fun equation_for_atom j = HOLogic.eq_const T $ Bound 0 $ atom j
768 val eqs = map equation_for_atom (index_seq 0 k)
770 Const (@{const_name All}, (T --> bool_T) --> bool_T)
771 $ Abs ("x", T, foldl1 HOLogic.mk_disj eqs)
772 val distinct_assm = distinctness_formula T (map atom (index_seq 0 k))
773 in s_conj (compreh_assm, distinct_assm) end
775 fun free_name_assm name =
776 HOLogic.mk_eq (Free (nickname_of name, type_of name),
777 term_for_name pool scope sel_names rel_table bounds name)
778 val freeT_assms = map free_type_assm (filter (is_TFree o fst) card_assigns)
779 val model_assms = map free_name_assm free_names
780 val assm = foldr1 s_conj (freeT_assms @ model_assms)
784 val concl = (negate ? curry (op $) @{const Not})
785 (ObjectLogic.atomize_term thy prop)
786 val prop = HOLogic.mk_Trueprop (HOLogic.mk_imp (assm, concl))
787 |> map_types (map_type_tfree
788 (fn (s, []) => TFree (s, HOLogic.typeS)
790 val _ = if debug then
791 priority ((if negate then "Genuineness" else "Spuriousness") ^
792 " goal: " ^ Syntax.string_of_term ctxt prop ^ ".")
795 val goal = prop |> cterm_of thy |> Goal.init
797 (goal |> SINGLE (DETERM_TIMEOUT auto_timeout
798 (auto_tac (clasimpset_of ctxt)))
799 |> the |> Goal.finish ctxt; true)
800 handle THM _ => false
801 | TimeLimit.TimeOut => false
804 if try_out false then SOME true
805 else if try_out true then SOME false