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