author | blanchet |
Tue, 13 Apr 2010 13:26:06 +0200 | |
changeset 36128 | a3d8d5329438 |
parent 35671 | ed2c3830d881 |
child 36385 | ff5f88702590 |
permissions | -rw-r--r-- |
33982 | 1 |
(* Title: HOL/Tools/Nitpick/nitpick_nut.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:
34936
diff
changeset
|
3 |
Copyright 2008, 2009, 2010 |
33192 | 4 |
|
5 |
Nitpick underlying terms (nuts). |
|
6 |
*) |
|
7 |
||
8 |
signature NITPICK_NUT = |
|
9 |
sig |
|
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
10 |
type hol_context = Nitpick_HOL.hol_context |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
11 |
type scope = Nitpick_Scope.scope |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
12 |
type name_pool = Nitpick_Peephole.name_pool |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
13 |
type rep = Nitpick_Rep.rep |
33192 | 14 |
|
15 |
datatype cst = |
|
16 |
Unity | |
|
17 |
False | |
|
18 |
True | |
|
19 |
Iden | |
|
20 |
Num of int | |
|
21 |
Unknown | |
|
22 |
Unrep | |
|
23 |
Suc | |
|
24 |
Add | |
|
25 |
Subtract | |
|
26 |
Multiply | |
|
27 |
Divide | |
|
28 |
Gcd | |
|
29 |
Lcm | |
|
30 |
Fracs | |
|
31 |
NormFrac | |
|
32 |
NatToInt | |
|
33 |
IntToNat |
|
34 |
||
35 |
datatype op1 = |
|
36 |
Not | |
|
37 |
Finite | |
|
38 |
Converse | |
|
39 |
Closure | |
|
40 |
SingletonSet | |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
41 |
IsUnknown | |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
42 |
SafeThe | |
33192 | 43 |
First | |
44 |
Second | |
|
45 |
Cast |
|
46 |
||
47 |
datatype op2 = |
|
48 |
All | |
|
49 |
Exist | |
|
50 |
Or | |
|
51 |
And | |
|
52 |
Less | |
|
53 |
Subset | |
|
54 |
DefEq | |
|
55 |
Eq | |
|
56 |
The | |
|
57 |
Eps | |
|
58 |
Triad | |
|
59 |
Union | |
|
60 |
SetDifference | |
|
61 |
Intersect | |
|
62 |
Composition | |
|
63 |
Product | |
|
64 |
Image | |
|
65 |
Apply | |
|
66 |
Lambda |
|
67 |
||
68 |
datatype op3 = |
|
69 |
Let | |
|
70 |
If |
|
71 |
||
72 |
datatype nut = |
|
73 |
Cst of cst * typ * rep | |
|
74 |
Op1 of op1 * typ * rep * nut | |
|
75 |
Op2 of op2 * typ * rep * nut * nut | |
|
76 |
Op3 of op3 * typ * rep * nut * nut * nut | |
|
77 |
Tuple of typ * rep * nut list | |
|
78 |
Construct of nut list * typ * rep * nut list | |
|
79 |
BoundName of int * typ * rep * string | |
|
80 |
FreeName of string * typ * rep | |
|
81 |
ConstName of string * typ * rep | |
|
82 |
BoundRel of Kodkod.n_ary_index * typ * rep * string | |
|
83 |
FreeRel of Kodkod.n_ary_index * typ * rep * string | |
|
84 |
RelReg of int * typ * rep | |
|
85 |
FormulaReg of int * typ * rep |
|
86 |
||
87 |
structure NameTable : TABLE |
|
88 |
||
89 |
exception NUT of string * nut list |
|
90 |
||
91 |
val string_for_nut : Proof.context -> nut -> string |
|
92 |
val inline_nut : nut -> bool |
|
93 |
val type_of : nut -> typ |
|
94 |
val rep_of : nut -> rep |
|
95 |
val nickname_of : nut -> string |
|
96 |
val is_skolem_name : nut -> bool |
|
97 |
val is_eval_name : nut -> bool |
|
98 |
val is_Cst : cst -> nut -> bool |
|
99 |
val fold_nut : (nut -> 'a -> 'a) -> nut -> 'a -> 'a |
|
100 |
val map_nut : (nut -> nut) -> nut -> nut |
|
101 |
val untuple : (nut -> 'a) -> nut -> 'a list |
|
102 |
val add_free_and_const_names : |
|
103 |
nut -> nut list * nut list -> nut list * nut list |
|
104 |
val name_ord : (nut * nut) -> order |
|
105 |
val the_name : 'a NameTable.table -> nut -> 'a |
|
106 |
val the_rel : nut NameTable.table -> nut -> Kodkod.n_ary_index |
|
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
107 |
val nut_from_term : hol_context -> op2 -> term -> nut |
33192 | 108 |
val choose_reps_for_free_vars : |
109 |
scope -> nut list -> rep NameTable.table -> nut list * rep NameTable.table |
|
110 |
val choose_reps_for_consts : |
|
111 |
scope -> bool -> nut list -> rep NameTable.table |
|
112 |
-> nut list * rep NameTable.table |
|
113 |
val choose_reps_for_all_sels : |
|
114 |
scope -> rep NameTable.table -> nut list * rep NameTable.table |
|
115 |
val choose_reps_in_nut : |
|
116 |
scope -> bool -> rep NameTable.table -> bool -> nut -> nut |
|
117 |
val rename_free_vars : |
|
118 |
nut list -> name_pool -> nut NameTable.table |
|
119 |
-> nut list * name_pool * nut NameTable.table |
|
120 |
val rename_vars_in_nut : name_pool -> nut NameTable.table -> nut -> nut |
|
121 |
end; |
|
122 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
123 |
structure Nitpick_Nut : NITPICK_NUT = |
33192 | 124 |
struct |
125 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
126 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
127 |
open Nitpick_HOL |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
128 |
open Nitpick_Scope |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
129 |
open Nitpick_Peephole |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
130 |
open Nitpick_Rep |
33192 | 131 |
|
34126 | 132 |
structure KK = Kodkod |
133 |
||
33192 | 134 |
datatype cst = |
135 |
Unity | |
|
136 |
False | |
|
137 |
True | |
|
138 |
Iden | |
|
139 |
Num of int | |
|
140 |
Unknown | |
|
141 |
Unrep | |
|
142 |
Suc | |
|
143 |
Add | |
|
144 |
Subtract | |
|
145 |
Multiply | |
|
146 |
Divide | |
|
147 |
Gcd | |
|
148 |
Lcm | |
|
149 |
Fracs | |
|
150 |
NormFrac | |
|
151 |
NatToInt | |
|
152 |
IntToNat |
|
153 |
||
154 |
datatype op1 = |
|
155 |
Not | |
|
156 |
Finite | |
|
157 |
Converse | |
|
158 |
Closure | |
|
159 |
SingletonSet | |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
160 |
IsUnknown | |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
161 |
SafeThe | |
33192 | 162 |
First | |
163 |
Second | |
|
164 |
Cast |
|
165 |
||
166 |
datatype op2 = |
|
167 |
All | |
|
168 |
Exist | |
|
169 |
Or | |
|
170 |
And | |
|
171 |
Less | |
|
172 |
Subset | |
|
173 |
DefEq | |
|
174 |
Eq | |
|
175 |
The | |
|
176 |
Eps | |
|
177 |
Triad | |
|
178 |
Union | |
|
179 |
SetDifference | |
|
180 |
Intersect | |
|
181 |
Composition | |
|
182 |
Product | |
|
183 |
Image | |
|
184 |
Apply | |
|
185 |
Lambda |
|
186 |
||
187 |
datatype op3 = |
|
188 |
Let | |
|
189 |
If |
|
190 |
||
191 |
datatype nut = |
|
192 |
Cst of cst * typ * rep | |
|
193 |
Op1 of op1 * typ * rep * nut | |
|
194 |
Op2 of op2 * typ * rep * nut * nut | |
|
195 |
Op3 of op3 * typ * rep * nut * nut * nut | |
|
196 |
Tuple of typ * rep * nut list | |
|
197 |
Construct of nut list * typ * rep * nut list | |
|
198 |
BoundName of int * typ * rep * string | |
|
199 |
FreeName of string * typ * rep | |
|
200 |
ConstName of string * typ * rep | |
|
34126 | 201 |
BoundRel of KK.n_ary_index * typ * rep * string | |
202 |
FreeRel of KK.n_ary_index * typ * rep * string | |
|
33192 | 203 |
RelReg of int * typ * rep | |
204 |
FormulaReg of int * typ * rep |
|
205 |
||
206 |
exception NUT of string * nut list |
|
207 |
||
208 |
(* cst -> string *) |
|
209 |
fun string_for_cst Unity = "Unity" |
|
210 |
| string_for_cst False = "False" |
|
211 |
| string_for_cst True = "True" |
|
212 |
| string_for_cst Iden = "Iden" |
|
213 |
| string_for_cst (Num j) = "Num " ^ signed_string_of_int j |
|
214 |
| string_for_cst Unknown = "Unknown" |
|
215 |
| string_for_cst Unrep = "Unrep" |
|
216 |
| string_for_cst Suc = "Suc" |
|
217 |
| string_for_cst Add = "Add" |
|
218 |
| string_for_cst Subtract = "Subtract" |
|
219 |
| string_for_cst Multiply = "Multiply" |
|
220 |
| string_for_cst Divide = "Divide" |
|
221 |
| string_for_cst Gcd = "Gcd" |
|
222 |
| string_for_cst Lcm = "Lcm" |
|
223 |
| string_for_cst Fracs = "Fracs" |
|
224 |
| string_for_cst NormFrac = "NormFrac" |
|
225 |
| string_for_cst NatToInt = "NatToInt" |
|
226 |
| string_for_cst IntToNat = "IntToNat" |
|
227 |
||
228 |
(* op1 -> string *) |
|
229 |
fun string_for_op1 Not = "Not" |
|
230 |
| string_for_op1 Finite = "Finite" |
|
231 |
| string_for_op1 Converse = "Converse" |
|
232 |
| string_for_op1 Closure = "Closure" |
|
233 |
| string_for_op1 SingletonSet = "SingletonSet" |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
234 |
| string_for_op1 IsUnknown = "IsUnknown" |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
235 |
| string_for_op1 SafeThe = "SafeThe" |
33192 | 236 |
| string_for_op1 First = "First" |
237 |
| string_for_op1 Second = "Second" |
|
238 |
| string_for_op1 Cast = "Cast" |
|
239 |
||
240 |
(* op2 -> string *) |
|
241 |
fun string_for_op2 All = "All" |
|
242 |
| string_for_op2 Exist = "Exist" |
|
243 |
| string_for_op2 Or = "Or" |
|
244 |
| string_for_op2 And = "And" |
|
245 |
| string_for_op2 Less = "Less" |
|
246 |
| string_for_op2 Subset = "Subset" |
|
247 |
| string_for_op2 DefEq = "DefEq" |
|
248 |
| string_for_op2 Eq = "Eq" |
|
249 |
| string_for_op2 The = "The" |
|
250 |
| string_for_op2 Eps = "Eps" |
|
251 |
| string_for_op2 Triad = "Triad" |
|
252 |
| string_for_op2 Union = "Union" |
|
253 |
| string_for_op2 SetDifference = "SetDifference" |
|
254 |
| string_for_op2 Intersect = "Intersect" |
|
255 |
| string_for_op2 Composition = "Composition" |
|
256 |
| string_for_op2 Product = "Product" |
|
257 |
| string_for_op2 Image = "Image" |
|
258 |
| string_for_op2 Apply = "Apply" |
|
259 |
| string_for_op2 Lambda = "Lambda" |
|
260 |
||
261 |
(* op3 -> string *) |
|
262 |
fun string_for_op3 Let = "Let" |
|
263 |
| string_for_op3 If = "If" |
|
264 |
||
265 |
(* int -> Proof.context -> nut -> string *) |
|
266 |
fun basic_string_for_nut indent ctxt u = |
|
267 |
let |
|
268 |
(* nut -> string *) |
|
269 |
val sub = basic_string_for_nut (indent + 1) ctxt |
|
270 |
in |
|
271 |
(if indent = 0 then "" else "\n" ^ implode (replicate (2 * indent) " ")) ^ |
|
272 |
"(" ^ |
|
273 |
(case u of |
|
274 |
Cst (c, T, R) => |
|
275 |
"Cst " ^ string_for_cst c ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^ |
|
276 |
string_for_rep R |
|
277 |
| Op1 (oper, T, R, u1) => |
|
278 |
"Op1 " ^ string_for_op1 oper ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^ |
|
279 |
string_for_rep R ^ " " ^ sub u1 |
|
280 |
| Op2 (oper, T, R, u1, u2) => |
|
281 |
"Op2 " ^ string_for_op2 oper ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^ |
|
282 |
string_for_rep R ^ " " ^ sub u1 ^ " " ^ sub u2 |
|
283 |
| Op3 (oper, T, R, u1, u2, u3) => |
|
284 |
"Op3 " ^ string_for_op3 oper ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^ |
|
285 |
string_for_rep R ^ " " ^ sub u1 ^ " " ^ sub u2 ^ " " ^ sub u3 |
|
286 |
| Tuple (T, R, us) => |
|
287 |
"Tuple " ^ Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ |
|
288 |
implode (map sub us) |
|
289 |
| Construct (us', T, R, us) => |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
290 |
"Construct " ^ implode (map sub us') ^ " " ^ |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
291 |
Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
292 |
implode (map sub us) |
33192 | 293 |
| BoundName (j, T, R, nick) => |
294 |
"BoundName " ^ signed_string_of_int j ^ " " ^ |
|
295 |
Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ nick |
|
296 |
| FreeName (s, T, R) => |
|
297 |
"FreeName " ^ s ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^ |
|
298 |
string_for_rep R |
|
299 |
| ConstName (s, T, R) => |
|
300 |
"ConstName " ^ s ^ " " ^ Syntax.string_of_typ ctxt T ^ " " ^ |
|
301 |
string_for_rep R |
|
302 |
| BoundRel ((n, j), T, R, nick) => |
|
303 |
"BoundRel " ^ string_of_int n ^ "." ^ signed_string_of_int j ^ " " ^ |
|
304 |
Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ nick |
|
305 |
| FreeRel ((n, j), T, R, nick) => |
|
306 |
"FreeRel " ^ string_of_int n ^ "." ^ signed_string_of_int j ^ " " ^ |
|
307 |
Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R ^ " " ^ nick |
|
308 |
| RelReg (j, T, R) => |
|
309 |
"RelReg " ^ signed_string_of_int j ^ " " ^ Syntax.string_of_typ ctxt T ^ |
|
310 |
" " ^ string_for_rep R |
|
311 |
| FormulaReg (j, T, R) => |
|
312 |
"FormulaReg " ^ signed_string_of_int j ^ " " ^ |
|
313 |
Syntax.string_of_typ ctxt T ^ " " ^ string_for_rep R) ^ |
|
314 |
")" |
|
315 |
end |
|
316 |
(* Proof.context -> nut -> string *) |
|
317 |
val string_for_nut = basic_string_for_nut 0 |
|
318 |
||
319 |
(* nut -> bool *) |
|
320 |
fun inline_nut (Op1 _) = false |
|
321 |
| inline_nut (Op2 _) = false |
|
322 |
| inline_nut (Op3 _) = false |
|
323 |
| inline_nut (Tuple (_, _, us)) = forall inline_nut us |
|
324 |
| inline_nut _ = true |
|
325 |
||
326 |
(* nut -> typ *) |
|
327 |
fun type_of (Cst (_, T, _)) = T |
|
328 |
| type_of (Op1 (_, T, _, _)) = T |
|
329 |
| type_of (Op2 (_, T, _, _, _)) = T |
|
330 |
| type_of (Op3 (_, T, _, _, _, _)) = T |
|
331 |
| type_of (Tuple (T, _, _)) = T |
|
332 |
| type_of (Construct (_, T, _, _)) = T |
|
333 |
| type_of (BoundName (_, T, _, _)) = T |
|
334 |
| type_of (FreeName (_, T, _)) = T |
|
335 |
| type_of (ConstName (_, T, _)) = T |
|
336 |
| type_of (BoundRel (_, T, _, _)) = T |
|
337 |
| type_of (FreeRel (_, T, _, _)) = T |
|
338 |
| type_of (RelReg (_, T, _)) = T |
|
339 |
| type_of (FormulaReg (_, T, _)) = T |
|
340 |
||
341 |
(* nut -> rep *) |
|
342 |
fun rep_of (Cst (_, _, R)) = R |
|
343 |
| rep_of (Op1 (_, _, R, _)) = R |
|
344 |
| rep_of (Op2 (_, _, R, _, _)) = R |
|
345 |
| rep_of (Op3 (_, _, R, _, _, _)) = R |
|
346 |
| rep_of (Tuple (_, R, _)) = R |
|
347 |
| rep_of (Construct (_, _, R, _)) = R |
|
348 |
| rep_of (BoundName (_, _, R, _)) = R |
|
349 |
| rep_of (FreeName (_, _, R)) = R |
|
350 |
| rep_of (ConstName (_, _, R)) = R |
|
351 |
| rep_of (BoundRel (_, _, R, _)) = R |
|
352 |
| rep_of (FreeRel (_, _, R, _)) = R |
|
353 |
| rep_of (RelReg (_, _, R)) = R |
|
354 |
| rep_of (FormulaReg (_, _, R)) = R |
|
355 |
||
356 |
(* nut -> string *) |
|
357 |
fun nickname_of (BoundName (_, _, _, nick)) = nick |
|
358 |
| nickname_of (FreeName (s, _, _)) = s |
|
359 |
| nickname_of (ConstName (s, _, _)) = s |
|
360 |
| nickname_of (BoundRel (_, _, _, nick)) = nick |
|
361 |
| nickname_of (FreeRel (_, _, _, nick)) = nick |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
362 |
| nickname_of u = raise NUT ("Nitpick_Nut.nickname_of", [u]) |
33192 | 363 |
|
364 |
(* nut -> bool *) |
|
365 |
fun is_skolem_name u = |
|
366 |
space_explode name_sep (nickname_of u) |
|
367 |
|> exists (String.isPrefix skolem_prefix) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
368 |
handle NUT ("Nitpick_Nut.nickname_of", _) => false |
33192 | 369 |
fun is_eval_name u = |
370 |
String.isPrefix eval_prefix (nickname_of u) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
371 |
handle NUT ("Nitpick_Nut.nickname_of", _) => false |
33192 | 372 |
(* cst -> nut -> bool *) |
373 |
fun is_Cst cst (Cst (cst', _, _)) = (cst = cst') |
|
374 |
| is_Cst _ _ = false |
|
375 |
||
376 |
(* (nut -> 'a -> 'a) -> nut -> 'a -> 'a *) |
|
377 |
fun fold_nut f u = |
|
378 |
case u of |
|
379 |
Op1 (_, _, _, u1) => fold_nut f u1 |
|
380 |
| Op2 (_, _, _, u1, u2) => fold_nut f u1 #> fold_nut f u2 |
|
381 |
| Op3 (_, _, _, u1, u2, u3) => fold_nut f u1 #> fold_nut f u2 #> fold_nut f u3 |
|
382 |
| Tuple (_, _, us) => fold (fold_nut f) us |
|
383 |
| Construct (us', _, _, us) => fold (fold_nut f) us #> fold (fold_nut f) us' |
|
384 |
| _ => f u |
|
385 |
(* (nut -> nut) -> nut -> nut *) |
|
386 |
fun map_nut f u = |
|
387 |
case u of |
|
388 |
Op1 (oper, T, R, u1) => Op1 (oper, T, R, map_nut f u1) |
|
389 |
| Op2 (oper, T, R, u1, u2) => Op2 (oper, T, R, map_nut f u1, map_nut f u2) |
|
390 |
| Op3 (oper, T, R, u1, u2, u3) => |
|
391 |
Op3 (oper, T, R, map_nut f u1, map_nut f u2, map_nut f u3) |
|
392 |
| Tuple (T, R, us) => Tuple (T, R, map (map_nut f) us) |
|
393 |
| Construct (us', T, R, us) => |
|
394 |
Construct (map (map_nut f) us', T, R, map (map_nut f) us) |
|
395 |
| _ => f u |
|
396 |
||
397 |
(* nut * nut -> order *) |
|
398 |
fun name_ord (BoundName (j1, _, _, _), BoundName (j2, _, _, _)) = |
|
399 |
int_ord (j1, j2) |
|
400 |
| name_ord (BoundName _, _) = LESS |
|
401 |
| name_ord (_, BoundName _) = GREATER |
|
402 |
| name_ord (FreeName (s1, T1, _), FreeName (s2, T2, _)) = |
|
403 |
(case fast_string_ord (s1, s2) of |
|
35408 | 404 |
EQUAL => Term_Ord.typ_ord (T1, T2) |
33192 | 405 |
| ord => ord) |
406 |
| name_ord (FreeName _, _) = LESS |
|
407 |
| name_ord (_, FreeName _) = GREATER |
|
408 |
| name_ord (ConstName (s1, T1, _), ConstName (s2, T2, _)) = |
|
409 |
(case fast_string_ord (s1, s2) of |
|
35408 | 410 |
EQUAL => Term_Ord.typ_ord (T1, T2) |
33192 | 411 |
| ord => ord) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
412 |
| name_ord (u1, u2) = raise NUT ("Nitpick_Nut.name_ord", [u1, u2]) |
33192 | 413 |
|
414 |
(* nut -> nut -> int *) |
|
415 |
fun num_occs_in_nut needle_u stack_u = |
|
416 |
fold_nut (fn u => if u = needle_u then Integer.add 1 else I) stack_u 0 |
|
417 |
(* nut -> nut -> bool *) |
|
418 |
val is_subterm_of = not_equal 0 oo num_occs_in_nut |
|
419 |
||
420 |
(* nut -> nut -> nut -> nut *) |
|
421 |
fun substitute_in_nut needle_u needle_u' = |
|
422 |
map_nut (fn u => if u = needle_u then needle_u' else u) |
|
423 |
||
424 |
(* nut -> nut list * nut list -> nut list * nut list *) |
|
425 |
val add_free_and_const_names = |
|
426 |
fold_nut (fn u => case u of |
|
427 |
FreeName _ => apfst (insert (op =) u) |
|
428 |
| ConstName _ => apsnd (insert (op =) u) |
|
429 |
| _ => I) |
|
430 |
||
431 |
(* nut -> rep -> nut *) |
|
432 |
fun modify_name_rep (BoundName (j, T, _, nick)) R = BoundName (j, T, R, nick) |
|
433 |
| modify_name_rep (FreeName (s, T, _)) R = FreeName (s, T, R) |
|
434 |
| modify_name_rep (ConstName (s, T, _)) R = ConstName (s, T, R) |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
435 |
| modify_name_rep u _ = raise NUT ("Nitpick_Nut.modify_name_rep", [u]) |
33192 | 436 |
|
437 |
structure NameTable = Table(type key = nut val ord = name_ord) |
|
438 |
||
439 |
(* 'a NameTable.table -> nut -> 'a *) |
|
440 |
fun the_name table name = |
|
441 |
case NameTable.lookup table name of |
|
442 |
SOME u => u |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
443 |
| NONE => raise NUT ("Nitpick_Nut.the_name", [name]) |
34126 | 444 |
(* nut NameTable.table -> nut -> KK.n_ary_index *) |
33192 | 445 |
fun the_rel table name = |
446 |
case the_name table name of |
|
447 |
FreeRel (x, _, _, _) => x |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
448 |
| u => raise NUT ("Nitpick_Nut.the_rel", [u]) |
33192 | 449 |
|
450 |
(* typ * term -> typ * term *) |
|
451 |
fun mk_fst (_, Const (@{const_name Pair}, T) $ t1 $ _) = (domain_type T, t1) |
|
452 |
| mk_fst (T, t) = |
|
453 |
let val res_T = fst (HOLogic.dest_prodT T) in |
|
454 |
(res_T, Const (@{const_name fst}, T --> res_T) $ t) |
|
455 |
end |
|
456 |
fun mk_snd (_, Const (@{const_name Pair}, T) $ _ $ t2) = |
|
457 |
(domain_type (range_type T), t2) |
|
458 |
| mk_snd (T, t) = |
|
459 |
let val res_T = snd (HOLogic.dest_prodT T) in |
|
460 |
(res_T, Const (@{const_name snd}, T --> res_T) $ t) |
|
461 |
end |
|
462 |
(* typ * term -> (typ * term) list *) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
463 |
fun factorize (z as (Type (@{type_name "*"}, _), _)) = |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
464 |
maps factorize [mk_fst z, mk_snd z] |
33192 | 465 |
| factorize z = [z] |
466 |
||
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
467 |
(* hol_context -> op2 -> term -> nut *) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
468 |
fun nut_from_term (hol_ctxt as {thy, stds, fast_descrs, ...}) eq = |
33192 | 469 |
let |
470 |
(* string list -> typ list -> term -> nut *) |
|
471 |
fun aux eq ss Ts t = |
|
472 |
let |
|
473 |
(* term -> nut *) |
|
474 |
val sub = aux Eq ss Ts |
|
475 |
val sub' = aux eq ss Ts |
|
476 |
(* string -> typ -> term -> nut *) |
|
477 |
fun sub_abs s T = aux eq (s :: ss) (T :: Ts) |
|
478 |
(* typ -> term -> term -> nut *) |
|
479 |
fun sub_equals T t1 t2 = |
|
480 |
let |
|
481 |
val (binder_Ts, body_T) = strip_type (domain_type T) |
|
482 |
val n = length binder_Ts |
|
483 |
in |
|
484 |
if eq = Eq andalso n > 0 then |
|
485 |
let |
|
486 |
val t1 = incr_boundvars n t1 |
|
487 |
val t2 = incr_boundvars n t2 |
|
488 |
val xs = map Bound (n - 1 downto 0) |
|
489 |
val equation = Const (@{const_name "op ="}, |
|
490 |
body_T --> body_T --> bool_T) |
|
491 |
$ betapplys (t1, xs) $ betapplys (t2, xs) |
|
492 |
val t = |
|
493 |
fold_rev (fn T => fn (t, j) => |
|
494 |
(Const (@{const_name All}, T --> bool_T) |
|
495 |
$ Abs ("x" ^ nat_subscript j, T, t), j - 1)) |
|
496 |
binder_Ts (equation, n) |> fst |
|
497 |
in sub' t end |
|
498 |
else |
|
499 |
Op2 (eq, bool_T, Any, aux Eq ss Ts t1, aux Eq ss Ts t2) |
|
500 |
end |
|
501 |
(* op2 -> string -> typ -> term -> nut *) |
|
502 |
fun do_quantifier quant s T t1 = |
|
503 |
let |
|
504 |
val bound_u = BoundName (length Ts, T, Any, s) |
|
505 |
val body_u = sub_abs s T t1 |
|
506 |
in |
|
507 |
if is_subterm_of bound_u body_u then |
|
508 |
Op2 (quant, bool_T, Any, bound_u, body_u) |
|
509 |
else |
|
510 |
body_u |
|
511 |
end |
|
512 |
(* term -> term list -> nut *) |
|
513 |
fun do_apply t0 ts = |
|
514 |
let |
|
515 |
val (ts', t2) = split_last ts |
|
516 |
val t1 = list_comb (t0, ts') |
|
517 |
val T1 = fastype_of1 (Ts, t1) |
|
518 |
in Op2 (Apply, range_type T1, Any, sub t1, sub t2) end |
|
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
519 |
(* op2 -> string -> styp -> term -> nut *) |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
520 |
fun do_description_operator oper undef_s (x as (_, T)) t1 = |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
521 |
if fast_descrs then |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
522 |
Op2 (oper, range_type T, Any, sub t1, |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
523 |
sub (Const (undef_s, range_type T))) |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
524 |
else |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
525 |
do_apply (Const x) [t1] |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
526 |
(* styp -> term list -> nut *) |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
527 |
fun do_construct (x as (_, T)) ts = |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
528 |
case num_binder_types T - length ts of |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
529 |
0 => Construct (map ((fn (s', T') => ConstName (s', T', Any)) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
530 |
o nth_sel_for_constr x) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
531 |
(~1 upto num_sels_for_constr_type T - 1), |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
532 |
body_type T, Any, |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
533 |
ts |> map (`(curry fastype_of1 Ts)) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
534 |
|> maps factorize |> map (sub o snd)) |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
535 |
| k => sub (eta_expand Ts t k) |
33192 | 536 |
in |
537 |
case strip_comb t of |
|
538 |
(Const (@{const_name all}, _), [Abs (s, T, t1)]) => |
|
539 |
do_quantifier All s T t1 |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
540 |
| (t0 as Const (@{const_name all}, _), [t1]) => |
33192 | 541 |
sub' (t0 $ eta_expand Ts t1 1) |
542 |
| (Const (@{const_name "=="}, T), [t1, t2]) => sub_equals T t1 t2 |
|
543 |
| (Const (@{const_name "==>"}, _), [t1, t2]) => |
|
544 |
Op2 (Or, prop_T, Any, Op1 (Not, prop_T, Any, sub t1), sub' t2) |
|
545 |
| (Const (@{const_name Pure.conjunction}, _), [t1, t2]) => |
|
546 |
Op2 (And, prop_T, Any, sub' t1, sub' t2) |
|
547 |
| (Const (@{const_name Trueprop}, _), [t1]) => sub' t1 |
|
548 |
| (Const (@{const_name Not}, _), [t1]) => |
|
549 |
(case sub t1 of |
|
550 |
Op1 (Not, _, _, u11) => u11 |
|
551 |
| u1 => Op1 (Not, bool_T, Any, u1)) |
|
552 |
| (Const (@{const_name False}, T), []) => Cst (False, T, Any) |
|
553 |
| (Const (@{const_name True}, T), []) => Cst (True, T, Any) |
|
554 |
| (Const (@{const_name All}, _), [Abs (s, T, t1)]) => |
|
555 |
do_quantifier All s T t1 |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
556 |
| (t0 as Const (@{const_name All}, _), [t1]) => |
33192 | 557 |
sub' (t0 $ eta_expand Ts t1 1) |
558 |
| (Const (@{const_name Ex}, _), [Abs (s, T, t1)]) => |
|
559 |
do_quantifier Exist s T t1 |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
560 |
| (t0 as Const (@{const_name Ex}, _), [t1]) => |
33192 | 561 |
sub' (t0 $ eta_expand Ts t1 1) |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
562 |
| (Const (x as (@{const_name The}, _)), [t1]) => |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
563 |
do_description_operator The @{const_name undefined_fast_The} x t1 |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
564 |
| (Const (x as (@{const_name Eps}, _)), [t1]) => |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
565 |
do_description_operator Eps @{const_name undefined_fast_Eps} x t1 |
33192 | 566 |
| (Const (@{const_name "op ="}, T), [t1, t2]) => sub_equals T t1 t2 |
567 |
| (Const (@{const_name "op &"}, _), [t1, t2]) => |
|
568 |
Op2 (And, bool_T, Any, sub' t1, sub' t2) |
|
569 |
| (Const (@{const_name "op |"}, _), [t1, t2]) => |
|
570 |
Op2 (Or, bool_T, Any, sub t1, sub t2) |
|
571 |
| (Const (@{const_name "op -->"}, _), [t1, t2]) => |
|
572 |
Op2 (Or, bool_T, Any, Op1 (Not, bool_T, Any, sub t1), sub' t2) |
|
573 |
| (Const (@{const_name If}, T), [t1, t2, t3]) => |
|
574 |
Op3 (If, nth_range_type 3 T, Any, sub t1, sub t2, sub t3) |
|
575 |
| (Const (@{const_name Let}, T), [t1, Abs (s, T', t2)]) => |
|
576 |
Op3 (Let, nth_range_type 2 T, Any, BoundName (length Ts, T', Any, s), |
|
577 |
sub t1, sub_abs s T' t2) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
578 |
| (t0 as Const (@{const_name Let}, _), [t1, t2]) => |
33192 | 579 |
sub (t0 $ t1 $ eta_expand Ts t2 1) |
580 |
| (@{const Unity}, []) => Cst (Unity, @{typ unit}, Any) |
|
581 |
| (Const (@{const_name Pair}, T), [t1, t2]) => |
|
582 |
Tuple (nth_range_type 2 T, Any, map sub [t1, t2]) |
|
583 |
| (Const (@{const_name fst}, T), [t1]) => |
|
584 |
Op1 (First, range_type T, Any, sub t1) |
|
585 |
| (Const (@{const_name snd}, T), [t1]) => |
|
586 |
Op1 (Second, range_type T, Any, sub t1) |
|
587 |
| (Const (@{const_name Id}, T), []) => Cst (Iden, T, Any) |
|
588 |
| (Const (@{const_name insert}, T), [t1, t2]) => |
|
589 |
(case t2 of |
|
590 |
Abs (_, _, @{const False}) => |
|
591 |
Op1 (SingletonSet, nth_range_type 2 T, Any, sub t1) |
|
592 |
| _ => |
|
593 |
Op2 (Union, nth_range_type 2 T, Any, |
|
594 |
Op1 (SingletonSet, nth_range_type 2 T, Any, sub t1), sub t2)) |
|
595 |
| (Const (@{const_name converse}, T), [t1]) => |
|
596 |
Op1 (Converse, range_type T, Any, sub t1) |
|
597 |
| (Const (@{const_name trancl}, T), [t1]) => |
|
598 |
Op1 (Closure, range_type T, Any, sub t1) |
|
599 |
| (Const (@{const_name rel_comp}, T), [t1, t2]) => |
|
600 |
Op2 (Composition, nth_range_type 2 T, Any, sub t1, sub t2) |
|
601 |
| (Const (@{const_name Sigma}, T), [t1, Abs (s, T', t2')]) => |
|
602 |
Op2 (Product, nth_range_type 2 T, Any, sub t1, sub_abs s T' t2') |
|
603 |
| (Const (@{const_name image}, T), [t1, t2]) => |
|
604 |
Op2 (Image, nth_range_type 2 T, Any, sub t1, sub t2) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
605 |
| (Const (x as (s as @{const_name Suc}, T)), []) => |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
606 |
if is_built_in_const thy stds false x then Cst (Suc, T, Any) |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
607 |
else if is_constr thy stds x then do_construct x [] |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
608 |
else ConstName (s, T, Any) |
33192 | 609 |
| (Const (@{const_name finite}, T), [t1]) => |
35070
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
blanchet
parents:
34982
diff
changeset
|
610 |
(if is_finite_type hol_ctxt (domain_type T) then |
33877
e779bea3d337
fix Nitpick soundness bug related to "finite (UNIV::'a set)" where "'a" is constrained by a sort to be infinite
blanchet
parents:
33853
diff
changeset
|
611 |
Cst (True, bool_T, Any) |
e779bea3d337
fix Nitpick soundness bug related to "finite (UNIV::'a set)" where "'a" is constrained by a sort to be infinite
blanchet
parents:
33853
diff
changeset
|
612 |
else case t1 of |
e779bea3d337
fix Nitpick soundness bug related to "finite (UNIV::'a set)" where "'a" is constrained by a sort to be infinite
blanchet
parents:
33853
diff
changeset
|
613 |
Const (@{const_name top}, _) => Cst (False, bool_T, Any) |
e779bea3d337
fix Nitpick soundness bug related to "finite (UNIV::'a set)" where "'a" is constrained by a sort to be infinite
blanchet
parents:
33853
diff
changeset
|
614 |
| _ => Op1 (Finite, bool_T, Any, sub t1)) |
33192 | 615 |
| (Const (@{const_name nat}, T), []) => Cst (IntToNat, T, Any) |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
616 |
| (Const (x as (s as @{const_name zero_class.zero}, T)), []) => |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
617 |
if is_built_in_const thy stds false x then Cst (Num 0, T, Any) |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
618 |
else if is_constr thy stds x then do_construct x [] |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
619 |
else ConstName (s, T, Any) |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
620 |
| (Const (x as (s as @{const_name one_class.one}, T)), []) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
621 |
if is_built_in_const thy stds false x then Cst (Num 1, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
622 |
else ConstName (s, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
623 |
| (Const (x as (s as @{const_name plus_class.plus}, T)), []) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
624 |
if is_built_in_const thy stds false x then Cst (Add, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
625 |
else ConstName (s, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
626 |
| (Const (@{const_name minus_class.minus}, |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
627 |
Type (@{type_name fun}, |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
628 |
[T1 as Type (@{type_name fun}, [_, @{typ bool}]), _])), |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
629 |
[t1, t2]) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
630 |
Op2 (SetDifference, T1, Any, sub t1, sub t2) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
631 |
| (Const (x as (s as @{const_name minus_class.minus}, T)), []) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
632 |
if is_built_in_const thy stds false x then Cst (Subtract, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
633 |
else ConstName (s, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
634 |
| (Const (x as (s as @{const_name times_class.times}, T)), []) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
635 |
if is_built_in_const thy stds false x then Cst (Multiply, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
636 |
else ConstName (s, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
637 |
| (Const (x as (s as @{const_name div_class.div}, T)), []) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
638 |
if is_built_in_const thy stds false x then Cst (Divide, T, Any) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
639 |
else ConstName (s, T, Any) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
640 |
| (t0 as Const (x as (@{const_name ord_class.less}, _)), |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
641 |
ts as [t1, t2]) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
642 |
if is_built_in_const thy stds false x then |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
643 |
Op2 (Less, bool_T, Any, sub t1, sub t2) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
644 |
else |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
645 |
do_apply t0 ts |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
646 |
| (Const (@{const_name ord_class.less_eq}, |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
647 |
Type (@{type_name fun}, |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
648 |
[Type (@{type_name fun}, [_, @{typ bool}]), _])), |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
649 |
[t1, t2]) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
650 |
Op2 (Subset, bool_T, Any, sub t1, sub t2) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
651 |
(* FIXME: find out if this case is necessary *) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
652 |
| (t0 as Const (x as (@{const_name ord_class.less_eq}, _)), |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
653 |
ts as [t1, t2]) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
654 |
if is_built_in_const thy stds false x then |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
655 |
Op1 (Not, bool_T, Any, Op2 (Less, bool_T, Any, sub t2, sub t1)) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
656 |
else |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
657 |
do_apply t0 ts |
33192 | 658 |
| (Const (@{const_name nat_gcd}, T), []) => Cst (Gcd, T, Any) |
659 |
| (Const (@{const_name nat_lcm}, T), []) => Cst (Lcm, T, Any) |
|
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
660 |
| (Const (x as (s as @{const_name uminus_class.uminus}, T)), []) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
661 |
if is_built_in_const thy stds false x then |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
662 |
let val num_T = domain_type T in |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
663 |
Op2 (Apply, num_T --> num_T, Any, |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
664 |
Cst (Subtract, num_T --> num_T --> num_T, Any), |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
665 |
Cst (Num 0, num_T, Any)) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
666 |
end |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
667 |
else |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
668 |
ConstName (s, T, Any) |
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
669 |
| (Const (@{const_name unknown}, T), []) => Cst (Unknown, T, Any) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
670 |
| (Const (@{const_name is_unknown}, _), [t1]) => |
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
671 |
Op1 (IsUnknown, bool_T, Any, sub t1) |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
672 |
| (Const (@{const_name safe_The}, |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
673 |
Type (@{type_name fun}, [_, T2])), [t1]) => |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
674 |
Op1 (SafeThe, T2, Any, sub t1) |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
675 |
| (Const (x as (@{const_name safe_Eps}, _)), [t1]) => |
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
676 |
do_description_operator Eps @{const_name undefined_fast_Eps} x t1 |
33192 | 677 |
| (Const (@{const_name Frac}, T), []) => Cst (Fracs, T, Any) |
678 |
| (Const (@{const_name norm_frac}, T), []) => Cst (NormFrac, T, Any) |
|
679 |
| (Const (@{const_name of_nat}, T as @{typ "nat => int"}), []) => |
|
680 |
Cst (NatToInt, T, Any) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
681 |
| (Const (@{const_name of_nat}, |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
682 |
T as @{typ "unsigned_bit word => signed_bit word"}), []) => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
683 |
Cst (NatToInt, T, Any) |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
684 |
| (Const (@{const_name semilattice_inf_class.inf}, |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
685 |
Type (@{type_name fun}, |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
686 |
[T1 as Type (@{type_name fun}, [_, @{typ bool}]), _])), |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
687 |
[t1, t2]) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
688 |
Op2 (Intersect, T1, Any, sub t1, sub t2) |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
689 |
| (Const (@{const_name semilattice_sup_class.sup}, |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
690 |
Type (@{type_name fun}, |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
691 |
[T1 as Type (@{type_name fun}, [_, @{typ bool}]), _])), |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
692 |
[t1, t2]) => |
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
693 |
Op2 (Union, T1, Any, sub t1, sub t2) |
33192 | 694 |
| (t0 as Const (x as (s, T)), ts) => |
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
695 |
if is_constr thy stds x then |
35671
ed2c3830d881
improved Nitpick's precision for "card" and "setsum" + fix incorrect outcome code w.r.t. "bisim_depth = -1"
blanchet
parents:
35665
diff
changeset
|
696 |
do_construct x ts |
33192 | 697 |
else if String.isPrefix numeral_prefix s then |
698 |
Cst (Num (the (Int.fromString (unprefix numeral_prefix s))), T, Any) |
|
699 |
else |
|
35220
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
blanchet
parents:
35190
diff
changeset
|
700 |
(case arity_of_built_in_const thy stds fast_descrs x of |
33192 | 701 |
SOME n => |
702 |
(case n - length ts of |
|
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
703 |
0 => raise TERM ("Nitpick_Nut.nut_from_term.aux", [t]) |
33192 | 704 |
| k => if k > 0 then sub (eta_expand Ts t k) |
705 |
else do_apply t0 ts) |
|
706 |
| NONE => if null ts then ConstName (s, T, Any) |
|
707 |
else do_apply t0 ts) |
|
708 |
| (Free (s, T), []) => FreeName (s, T, Any) |
|
33877
e779bea3d337
fix Nitpick soundness bug related to "finite (UNIV::'a set)" where "'a" is constrained by a sort to be infinite
blanchet
parents:
33853
diff
changeset
|
709 |
| (Var _, []) => raise TERM ("Nitpick_Nut.nut_from_term.aux", [t]) |
33192 | 710 |
| (Bound j, []) => |
711 |
BoundName (length Ts - j - 1, nth Ts j, Any, nth ss j) |
|
712 |
| (Abs (s, T, t1), []) => |
|
713 |
Op2 (Lambda, T --> fastype_of1 (T :: Ts, t1), Any, |
|
714 |
BoundName (length Ts, T, Any, s), sub_abs s T t1) |
|
715 |
| (t0, ts) => do_apply t0 ts |
|
716 |
end |
|
717 |
in aux eq [] [] end |
|
718 |
||
719 |
(* scope -> typ -> rep *) |
|
720 |
fun rep_for_abs_fun scope T = |
|
721 |
let val (R1, R2) = best_non_opt_symmetric_reps_for_fun_type scope T in |
|
722 |
Func (R1, (card_of_rep R1 <> card_of_rep R2 ? Opt) R2) |
|
723 |
end |
|
724 |
||
725 |
(* scope -> nut -> nut list * rep NameTable.table |
|
726 |
-> nut list * rep NameTable.table *) |
|
727 |
fun choose_rep_for_free_var scope v (vs, table) = |
|
728 |
let |
|
729 |
val R = best_non_opt_set_rep_for_type scope (type_of v) |
|
730 |
val v = modify_name_rep v R |
|
731 |
in (v :: vs, NameTable.update (v, R) table) end |
|
732 |
(* scope -> bool -> nut -> nut list * rep NameTable.table |
|
733 |
-> nut list * rep NameTable.table *) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
734 |
fun choose_rep_for_const (scope as {hol_ctxt = {thy, ...}, ...}) all_exact v |
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
735 |
(vs, table) = |
33192 | 736 |
let |
737 |
val x as (s, T) = (nickname_of v, type_of v) |
|
738 |
val R = (if is_abs_fun thy x then |
|
739 |
rep_for_abs_fun |
|
740 |
else if is_rep_fun thy x then |
|
741 |
Func oo best_non_opt_symmetric_reps_for_fun_type |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
742 |
else if all_exact orelse is_skolem_name v orelse |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
743 |
member (op =) [@{const_name undefined_fast_The}, |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
744 |
@{const_name undefined_fast_Eps}, |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
745 |
@{const_name bisim}, |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
746 |
@{const_name bisim_iterator_max}] |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
747 |
(original_name s) then |
33192 | 748 |
best_non_opt_set_rep_for_type |
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
749 |
else if member (op =) [@{const_name set}, @{const_name distinct}, |
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
750 |
@{const_name ord_class.less}, |
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
751 |
@{const_name ord_class.less_eq}] |
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
752 |
(original_name s) then |
33192 | 753 |
best_set_rep_for_type |
754 |
else |
|
755 |
best_opt_set_rep_for_type) scope T |
|
756 |
val v = modify_name_rep v R |
|
757 |
in (v :: vs, NameTable.update (v, R) table) end |
|
758 |
||
759 |
(* scope -> nut list -> rep NameTable.table -> nut list * rep NameTable.table *) |
|
760 |
fun choose_reps_for_free_vars scope vs table = |
|
761 |
fold (choose_rep_for_free_var scope) vs ([], table) |
|
762 |
(* scope -> bool -> nut list -> rep NameTable.table |
|
763 |
-> nut list * rep NameTable.table *) |
|
34123
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
764 |
fun choose_reps_for_consts scope all_exact vs table = |
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
blanchet
parents:
34121
diff
changeset
|
765 |
fold (choose_rep_for_const scope all_exact) vs ([], table) |
33192 | 766 |
|
767 |
(* scope -> styp -> int -> nut list * rep NameTable.table |
|
768 |
-> nut list * rep NameTable.table *) |
|
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
769 |
fun choose_rep_for_nth_sel_for_constr (scope as {hol_ctxt, binarize, ...}) |
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
770 |
(x as (_, T)) n (vs, table) = |
33192 | 771 |
let |
35190
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
blanchet
parents:
35185
diff
changeset
|
772 |
val (s', T') = binarized_and_boxed_nth_sel_for_constr hol_ctxt binarize x n |
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
773 |
val R' = if n = ~1 orelse is_word_type (body_type T) orelse |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
774 |
(is_fun_type (range_type T') andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
775 |
is_boolean_type (body_type T')) then |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
776 |
best_non_opt_set_rep_for_type scope T' |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
777 |
else |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
778 |
best_opt_set_rep_for_type scope T' |> unopt_rep |
33192 | 779 |
val v = ConstName (s', T', R') |
780 |
in (v :: vs, NameTable.update (v, R') table) end |
|
781 |
(* scope -> styp -> nut list * rep NameTable.table |
|
782 |
-> nut list * rep NameTable.table *) |
|
783 |
fun choose_rep_for_sels_for_constr scope (x as (_, T)) = |
|
784 |
fold_rev (choose_rep_for_nth_sel_for_constr scope x) |
|
785 |
(~1 upto num_sels_for_constr_type T - 1) |
|
786 |
(* scope -> dtype_spec -> nut list * rep NameTable.table |
|
787 |
-> nut list * rep NameTable.table *) |
|
34982
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
blanchet
parents:
34936
diff
changeset
|
788 |
fun choose_rep_for_sels_of_datatype _ ({deep = false, ...} : dtype_spec) = I |
33558
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
789 |
| choose_rep_for_sels_of_datatype scope {constrs, ...} = |
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
blanchet
parents:
33232
diff
changeset
|
790 |
fold_rev (choose_rep_for_sels_for_constr scope o #const) constrs |
33192 | 791 |
(* scope -> rep NameTable.table -> nut list * rep NameTable.table *) |
792 |
fun choose_reps_for_all_sels (scope as {datatypes, ...}) = |
|
793 |
fold (choose_rep_for_sels_of_datatype scope) datatypes o pair [] |
|
794 |
||
795 |
(* scope -> nut -> rep NameTable.table -> rep NameTable.table *) |
|
796 |
fun choose_rep_for_bound_var scope v table = |
|
797 |
let val R = best_one_rep_for_type scope (type_of v) in |
|
798 |
NameTable.update (v, R) table |
|
799 |
end |
|
800 |
||
801 |
(* A nut is said to be constructive if whenever it evaluates to unknown in our |
|
35312 | 802 |
three-valued logic, it would evaluate to a unrepresentable value ("Unrep") |
33631 | 803 |
according to the HOL semantics. For example, "Suc n" is constructive if "n" |
35312 | 804 |
is representable or "Unrep", because unknown implies "Unrep". *) |
33192 | 805 |
(* nut -> bool *) |
806 |
fun is_constructive u = |
|
807 |
is_Cst Unrep u orelse |
|
808 |
(not (is_fun_type (type_of u)) andalso not (is_opt_rep (rep_of u))) orelse |
|
809 |
case u of |
|
810 |
Cst (Num _, _, _) => true |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
811 |
| Cst (cst, T, _) => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
812 |
cst = Suc orelse (body_type T = nat_T andalso cst = Add) |
33192 | 813 |
| Op2 (Apply, _, _, u1, u2) => forall is_constructive [u1, u2] |
814 |
| Op3 (If, _, _, u1, u2, u3) => |
|
815 |
not (is_opt_rep (rep_of u1)) andalso forall is_constructive [u2, u3] |
|
816 |
| Tuple (_, _, us) => forall is_constructive us |
|
817 |
| Construct (_, _, _, us) => forall is_constructive us |
|
818 |
| _ => false |
|
819 |
||
820 |
(* nut -> nut *) |
|
821 |
fun optimize_unit u = |
|
822 |
if rep_of u = Unit then Cst (Unity, type_of u, Unit) else u |
|
823 |
(* typ -> rep -> nut *) |
|
824 |
fun unknown_boolean T R = |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
825 |
Cst (case R of Formula Pos => False | Formula Neg => True | _ => Unknown, |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
826 |
T, R) |
35312 | 827 |
(* nut -> bool *) |
828 |
fun is_fully_representable_set u = |
|
829 |
not (is_opt_rep (rep_of u)) andalso |
|
830 |
case u of |
|
831 |
FreeName _ => true |
|
832 |
| Op1 (SingletonSet, _, _, _) => true |
|
833 |
| Op2 (oper, _, _, u1, u2) => |
|
834 |
member (op =) [Union, SetDifference, Intersect] oper andalso |
|
835 |
forall is_fully_representable_set [u1, u2] |
|
836 |
| _ => false |
|
33192 | 837 |
|
838 |
(* op1 -> typ -> rep -> nut -> nut *) |
|
839 |
fun s_op1 oper T R u1 = |
|
840 |
((if oper = Not then |
|
841 |
if is_Cst True u1 then Cst (False, T, R) |
|
842 |
else if is_Cst False u1 then Cst (True, T, R) |
|
843 |
else raise SAME () |
|
844 |
else |
|
845 |
raise SAME ()) |
|
846 |
handle SAME () => Op1 (oper, T, R, u1)) |
|
847 |
|> optimize_unit |
|
848 |
(* op2 -> typ -> rep -> nut -> nut -> nut *) |
|
849 |
fun s_op2 oper T R u1 u2 = |
|
850 |
((case oper of |
|
851 |
Or => |
|
852 |
if exists (is_Cst True) [u1, u2] then Cst (True, T, unopt_rep R) |
|
853 |
else if is_Cst False u1 then u2 |
|
854 |
else if is_Cst False u2 then u1 |
|
855 |
else raise SAME () |
|
856 |
| And => |
|
857 |
if exists (is_Cst False) [u1, u2] then Cst (False, T, unopt_rep R) |
|
858 |
else if is_Cst True u1 then u2 |
|
859 |
else if is_Cst True u2 then u1 |
|
860 |
else raise SAME () |
|
861 |
| Eq => |
|
862 |
(case pairself (is_Cst Unrep) (u1, u2) of |
|
863 |
(true, true) => unknown_boolean T R |
|
864 |
| (false, false) => raise SAME () |
|
865 |
| _ => if forall (is_opt_rep o rep_of) [u1, u2] then raise SAME () |
|
866 |
else Cst (False, T, Formula Neut)) |
|
867 |
| Triad => |
|
868 |
if is_Cst True u1 then u1 |
|
869 |
else if is_Cst False u2 then u2 |
|
870 |
else raise SAME () |
|
871 |
| Apply => |
|
872 |
if is_Cst Unrep u1 then |
|
873 |
Cst (Unrep, T, R) |
|
874 |
else if is_Cst Unrep u2 then |
|
875 |
if is_constructive u1 then |
|
876 |
Cst (Unrep, T, R) |
|
33631 | 877 |
else if is_boolean_type T then |
35312 | 878 |
if is_fully_representable_set u1 then Cst (False, T, Formula Neut) |
33631 | 879 |
else unknown_boolean T R |
33192 | 880 |
else case u1 of |
881 |
Op2 (Apply, _, _, ConstName (@{const_name List.append}, _, _), _) => |
|
882 |
Cst (Unrep, T, R) |
|
883 |
| _ => raise SAME () |
|
884 |
else |
|
885 |
raise SAME () |
|
886 |
| _ => raise SAME ()) |
|
887 |
handle SAME () => Op2 (oper, T, R, u1, u2)) |
|
888 |
|> optimize_unit |
|
889 |
(* op3 -> typ -> rep -> nut -> nut -> nut -> nut *) |
|
890 |
fun s_op3 oper T R u1 u2 u3 = |
|
891 |
((case oper of |
|
892 |
Let => |
|
893 |
if inline_nut u2 orelse num_occs_in_nut u1 u3 < 2 then |
|
894 |
substitute_in_nut u1 u2 u3 |
|
895 |
else |
|
896 |
raise SAME () |
|
897 |
| _ => raise SAME ()) |
|
898 |
handle SAME () => Op3 (oper, T, R, u1, u2, u3)) |
|
899 |
|> optimize_unit |
|
900 |
(* typ -> rep -> nut list -> nut *) |
|
901 |
fun s_tuple T R us = |
|
902 |
(if exists (is_Cst Unrep) us then Cst (Unrep, T, R) else Tuple (T, R, us)) |
|
903 |
|> optimize_unit |
|
904 |
||
905 |
(* theory -> nut -> nut *) |
|
906 |
fun optimize_nut u = |
|
907 |
case u of |
|
908 |
Op1 (oper, T, R, u1) => s_op1 oper T R (optimize_nut u1) |
|
909 |
| Op2 (oper, T, R, u1, u2) => |
|
910 |
s_op2 oper T R (optimize_nut u1) (optimize_nut u2) |
|
911 |
| Op3 (oper, T, R, u1, u2, u3) => |
|
912 |
s_op3 oper T R (optimize_nut u1) (optimize_nut u2) (optimize_nut u3) |
|
913 |
| Tuple (T, R, us) => s_tuple T R (map optimize_nut us) |
|
914 |
| Construct (us', T, R, us) => Construct (us', T, R, map optimize_nut us) |
|
915 |
| _ => optimize_unit u |
|
916 |
||
917 |
(* (nut -> 'a) -> nut -> 'a list *) |
|
918 |
fun untuple f (Tuple (_, _, us)) = maps (untuple f) us |
|
919 |
| untuple f u = if rep_of u = Unit then [] else [f u] |
|
920 |
||
921 |
(* scope -> bool -> rep NameTable.table -> bool -> nut -> nut *) |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
922 |
fun choose_reps_in_nut (scope as {card_assigns, bits, datatypes, ofs, ...}) |
35185
9b8f351cced6
added yet another hint to Nitpick's output, this time warning about problems for which nothing was effectively tested
blanchet
parents:
35079
diff
changeset
|
923 |
unsound table def = |
33192 | 924 |
let |
925 |
val bool_atom_R = Atom (2, offset_of_type ofs bool_T) |
|
926 |
(* polarity -> bool -> rep *) |
|
927 |
fun bool_rep polar opt = |
|
928 |
if polar = Neut andalso opt then Opt bool_atom_R else Formula polar |
|
929 |
(* nut -> nut -> nut *) |
|
930 |
fun triad u1 u2 = s_op2 Triad (type_of u1) (Opt bool_atom_R) u1 u2 |
|
931 |
(* (polarity -> nut) -> nut *) |
|
932 |
fun triad_fn f = triad (f Pos) (f Neg) |
|
933 |
(* rep NameTable.table -> bool -> polarity -> nut -> nut -> nut *) |
|
934 |
fun unrepify_nut_in_nut table def polar needle_u = |
|
935 |
let val needle_T = type_of needle_u in |
|
936 |
substitute_in_nut needle_u (Cst (if is_fun_type needle_T then Unknown |
|
937 |
else Unrep, needle_T, Any)) |
|
938 |
#> aux table def polar |
|
939 |
end |
|
940 |
(* rep NameTable.table -> bool -> polarity -> nut -> nut *) |
|
941 |
and aux table def polar u = |
|
942 |
let |
|
943 |
(* bool -> polarity -> nut -> nut *) |
|
944 |
val gsub = aux table |
|
945 |
(* nut -> nut *) |
|
946 |
val sub = gsub false Neut |
|
947 |
in |
|
948 |
case u of |
|
949 |
Cst (False, T, _) => Cst (False, T, Formula Neut) |
|
950 |
| Cst (True, T, _) => Cst (True, T, Formula Neut) |
|
951 |
| Cst (Num j, T, _) => |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
952 |
if is_word_type T then |
34126 | 953 |
Cst (if is_twos_complement_representable bits j then Num j |
954 |
else Unrep, T, best_opt_set_rep_for_type scope T) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
955 |
else |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
956 |
(case spec_of_type scope T of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
957 |
(1, j0) => if j = 0 then Cst (Unity, T, Unit) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
958 |
else Cst (Unrep, T, Opt (Atom (1, j0))) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
959 |
| (k, j0) => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
960 |
let |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
961 |
val ok = (if T = int_T then atom_for_int (k, j0) j <> ~1 |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
962 |
else j < k) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
963 |
in |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
964 |
if ok then Cst (Num j, T, Atom (k, j0)) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
965 |
else Cst (Unrep, T, Opt (Atom (k, j0))) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
966 |
end) |
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
967 |
| Cst (Suc, T as Type (@{type_name fun}, [T1, _]), _) => |
33192 | 968 |
let val R = Atom (spec_of_type scope T1) in |
969 |
Cst (Suc, T, Func (R, Opt R)) |
|
970 |
end |
|
971 |
| Cst (Fracs, T, _) => |
|
972 |
Cst (Fracs, T, best_non_opt_set_rep_for_type scope T) |
|
973 |
| Cst (NormFrac, T, _) => |
|
974 |
let val R1 = Atom (spec_of_type scope (domain_type T)) in |
|
975 |
Cst (NormFrac, T, Func (R1, Func (R1, Opt (Struct [R1, R1])))) |
|
976 |
end |
|
977 |
| Cst (cst, T, _) => |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
978 |
if cst = Unknown orelse cst = Unrep then |
33192 | 979 |
case (is_boolean_type T, polar) of |
980 |
(true, Pos) => Cst (False, T, Formula Pos) |
|
981 |
| (true, Neg) => Cst (True, T, Formula Neg) |
|
982 |
| _ => Cst (cst, T, best_opt_set_rep_for_type scope T) |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
983 |
else if member (op =) [Add, Subtract, Multiply, Divide, Gcd, Lcm] |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
984 |
cst then |
33192 | 985 |
let |
986 |
val T1 = domain_type T |
|
987 |
val R1 = Atom (spec_of_type scope T1) |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
988 |
val total = T1 = nat_T andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
989 |
(cst = Subtract orelse cst = Divide orelse cst = Gcd) |
33192 | 990 |
in Cst (cst, T, Func (R1, Func (R1, (not total ? Opt) R1))) end |
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
991 |
else if cst = NatToInt orelse cst = IntToNat then |
33192 | 992 |
let |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
993 |
val (dom_card, dom_j0) = spec_of_type scope (domain_type T) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
994 |
val (ran_card, ran_j0) = spec_of_type scope (range_type T) |
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
995 |
val total = not (is_word_type (domain_type T)) andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
996 |
(if cst = NatToInt then |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
997 |
max_int_for_card ran_card >= dom_card + 1 |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
998 |
else |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
999 |
max_int_for_card dom_card < ran_card) |
33192 | 1000 |
in |
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1001 |
Cst (cst, T, Func (Atom (dom_card, dom_j0), |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1002 |
Atom (ran_card, ran_j0) |> not total ? Opt)) |
33192 | 1003 |
end |
1004 |
else |
|
1005 |
Cst (cst, T, best_set_rep_for_type scope T) |
|
1006 |
| Op1 (Not, T, _, u1) => |
|
1007 |
(case gsub def (flip_polarity polar) u1 of |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1008 |
Op2 (Triad, T, _, u11, u12) => |
33192 | 1009 |
triad (s_op1 Not T (Formula Pos) u12) |
1010 |
(s_op1 Not T (Formula Neg) u11) |
|
1011 |
| u1' => s_op1 Not T (flip_rep_polarity (rep_of u1')) u1') |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1012 |
| Op1 (IsUnknown, T, _, u1) => |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1013 |
let val u1 = sub u1 in |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1014 |
if is_opt_rep (rep_of u1) then Op1 (IsUnknown, T, Formula Neut, u1) |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1015 |
else Cst (False, T, Formula Neut) |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1016 |
end |
33192 | 1017 |
| Op1 (oper, T, _, u1) => |
1018 |
let |
|
1019 |
val u1 = sub u1 |
|
1020 |
val R1 = rep_of u1 |
|
1021 |
val R = case oper of |
|
1022 |
Finite => bool_rep polar (is_opt_rep R1) |
|
1023 |
| _ => (if is_opt_rep R1 then best_opt_set_rep_for_type |
|
1024 |
else best_non_opt_set_rep_for_type) scope T |
|
1025 |
in s_op1 oper T R u1 end |
|
1026 |
| Op2 (Less, T, _, u1, u2) => |
|
1027 |
let |
|
1028 |
val u1 = sub u1 |
|
1029 |
val u2 = sub u2 |
|
1030 |
val R = bool_rep polar (exists (is_opt_rep o rep_of) [u1, u2]) |
|
1031 |
in s_op2 Less T R u1 u2 end |
|
1032 |
| Op2 (Subset, T, _, u1, u2) => |
|
1033 |
let |
|
1034 |
val u1 = sub u1 |
|
1035 |
val u2 = sub u2 |
|
1036 |
val opt = exists (is_opt_rep o rep_of) [u1, u2] |
|
1037 |
val R = bool_rep polar opt |
|
1038 |
in |
|
1039 |
if is_opt_rep R then |
|
1040 |
triad_fn (fn polar => s_op2 Subset T (Formula polar) u1 u2) |
|
1041 |
else if opt andalso polar = Pos andalso |
|
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35312
diff
changeset
|
1042 |
not (is_concrete_type datatypes true (type_of u1)) then |
33192 | 1043 |
Cst (False, T, Formula Pos) |
1044 |
else |
|
1045 |
s_op2 Subset T R u1 u2 |
|
1046 |
end |
|
1047 |
| Op2 (DefEq, T, _, u1, u2) => |
|
1048 |
s_op2 DefEq T (Formula Neut) (sub u1) (sub u2) |
|
1049 |
| Op2 (Eq, T, _, u1, u2) => |
|
1050 |
let |
|
1051 |
val u1' = sub u1 |
|
1052 |
val u2' = sub u2 |
|
1053 |
(* unit -> nut *) |
|
1054 |
fun non_opt_case () = s_op2 Eq T (Formula polar) u1' u2' |
|
1055 |
(* unit -> nut *) |
|
1056 |
fun opt_opt_case () = |
|
1057 |
if polar = Neut then |
|
1058 |
triad_fn (fn polar => s_op2 Eq T (Formula polar) u1' u2') |
|
1059 |
else |
|
1060 |
non_opt_case () |
|
1061 |
(* nut -> nut *) |
|
1062 |
fun hybrid_case u = |
|
1063 |
(* hackish optimization *) |
|
1064 |
if is_constructive u then s_op2 Eq T (Formula Neut) u1' u2' |
|
1065 |
else opt_opt_case () |
|
1066 |
in |
|
35185
9b8f351cced6
added yet another hint to Nitpick's output, this time warning about problems for which nothing was effectively tested
blanchet
parents:
35079
diff
changeset
|
1067 |
if unsound orelse polar = Neg orelse |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35312
diff
changeset
|
1068 |
is_concrete_type datatypes true (type_of u1) then |
33192 | 1069 |
case (is_opt_rep (rep_of u1'), is_opt_rep (rep_of u2')) of |
1070 |
(true, true) => opt_opt_case () |
|
1071 |
| (true, false) => hybrid_case u1' |
|
1072 |
| (false, true) => hybrid_case u2' |
|
1073 |
| (false, false) => non_opt_case () |
|
1074 |
else |
|
1075 |
Cst (False, T, Formula Pos) |
|
1076 |
|> polar = Neut ? (fn pos_u => triad pos_u (gsub def Neg u)) |
|
1077 |
end |
|
1078 |
| Op2 (Image, T, _, u1, u2) => |
|
1079 |
let |
|
1080 |
val u1' = sub u1 |
|
1081 |
val u2' = sub u2 |
|
1082 |
in |
|
1083 |
(case (rep_of u1', rep_of u2') of |
|
1084 |
(Func (R11, R12), Func (R21, Formula Neut)) => |
|
1085 |
if R21 = R11 andalso is_lone_rep R12 then |
|
1086 |
let |
|
1087 |
val R = |
|
1088 |
best_non_opt_set_rep_for_type scope T |
|
1089 |
|> exists (is_opt_rep o rep_of) [u1', u2'] ? opt_rep ofs T |
|
1090 |
in s_op2 Image T R u1' u2' end |
|
1091 |
else |
|
1092 |
raise SAME () |
|
1093 |
| _ => raise SAME ()) |
|
1094 |
handle SAME () => |
|
1095 |
let |
|
1096 |
val T1 = type_of u1 |
|
1097 |
val dom_T = domain_type T1 |
|
1098 |
val ran_T = range_type T1 |
|
1099 |
val x_u = BoundName (~1, dom_T, Any, "image.x") |
|
1100 |
val y_u = BoundName (~2, ran_T, Any, "image.y") |
|
1101 |
in |
|
1102 |
Op2 (Lambda, T, Any, y_u, |
|
1103 |
Op2 (Exist, bool_T, Any, x_u, |
|
1104 |
Op2 (And, bool_T, Any, |
|
1105 |
case u2 of |
|
1106 |
Op2 (Lambda, _, _, u21, u22) => |
|
33571 | 1107 |
if num_occs_in_nut u21 u22 = 0 then |
33192 | 1108 |
u22 |
1109 |
else |
|
1110 |
Op2 (Apply, bool_T, Any, u2, x_u) |
|
1111 |
| _ => Op2 (Apply, bool_T, Any, u2, x_u), |
|
33571 | 1112 |
|
33192 | 1113 |
Op2 (Eq, bool_T, Any, y_u, |
1114 |
Op2 (Apply, ran_T, Any, u1, x_u))))) |
|
1115 |
|> sub |
|
1116 |
end |
|
1117 |
end |
|
1118 |
| Op2 (Apply, T, _, u1, u2) => |
|
1119 |
let |
|
1120 |
val u1 = sub u1 |
|
1121 |
val u2 = sub u2 |
|
1122 |
val T1 = type_of u1 |
|
1123 |
val R1 = rep_of u1 |
|
1124 |
val R2 = rep_of u2 |
|
1125 |
val opt = |
|
1126 |
case (u1, is_opt_rep R2) of |
|
1127 |
(ConstName (@{const_name set}, _, _), false) => false |
|
1128 |
| _ => exists is_opt_rep [R1, R2] |
|
1129 |
val ran_R = |
|
1130 |
if is_boolean_type T then |
|
1131 |
bool_rep polar opt |
|
1132 |
else |
|
36128
a3d8d5329438
make Nitpick output everything to tracing in debug mode;
blanchet
parents:
35671
diff
changeset
|
1133 |
lazy_range_rep ofs T1 (fn () => card_of_type card_assigns T) |
a3d8d5329438
make Nitpick output everything to tracing in debug mode;
blanchet
parents:
35671
diff
changeset
|
1134 |
(unopt_rep R1) |
33192 | 1135 |
|> opt ? opt_rep ofs T |
1136 |
in s_op2 Apply T ran_R u1 u2 end |
|
1137 |
| Op2 (Lambda, T, _, u1, u2) => |
|
1138 |
(case best_set_rep_for_type scope T of |
|
1139 |
Unit => Cst (Unity, T, Unit) |
|
1140 |
| R as Func (R1, _) => |
|
1141 |
let |
|
1142 |
val table' = NameTable.update (u1, R1) table |
|
1143 |
val u1' = aux table' false Neut u1 |
|
1144 |
val u2' = aux table' false Neut u2 |
|
1145 |
val R = |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1146 |
if is_opt_rep (rep_of u2') orelse |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1147 |
(range_type T = bool_T andalso |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1148 |
not (is_Cst False (unrepify_nut_in_nut table false Neut |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1149 |
u1 u2 |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1150 |
|> optimize_nut))) then |
33192 | 1151 |
opt_rep ofs T R |
1152 |
else |
|
1153 |
unopt_rep R |
|
1154 |
in s_op2 Lambda T R u1' u2' end |
|
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1155 |
| _ => raise NUT ("Nitpick_Nut.aux.choose_reps_in_nut", [u])) |
33192 | 1156 |
| Op2 (oper, T, _, u1, u2) => |
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
1157 |
if oper = All orelse oper = Exist then |
33192 | 1158 |
let |
1159 |
val table' = fold (choose_rep_for_bound_var scope) (untuple I u1) |
|
1160 |
table |
|
1161 |
val u1' = aux table' def polar u1 |
|
1162 |
val u2' = aux table' def polar u2 |
|
1163 |
in |
|
1164 |
if polar = Neut andalso is_opt_rep (rep_of u2') then |
|
1165 |
triad_fn (fn polar => gsub def polar u) |
|
1166 |
else |
|
1167 |
let val quant_u = s_op2 oper T (Formula polar) u1' u2' in |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1168 |
if def orelse |
35185
9b8f351cced6
added yet another hint to Nitpick's output, this time warning about problems for which nothing was effectively tested
blanchet
parents:
35079
diff
changeset
|
1169 |
(unsound andalso (polar = Pos) = (oper = All)) orelse |
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35312
diff
changeset
|
1170 |
is_complete_type datatypes true (type_of u1) then |
33192 | 1171 |
quant_u |
1172 |
else |
|
1173 |
let |
|
1174 |
val connective = if oper = All then And else Or |
|
1175 |
val unrepified_u = unrepify_nut_in_nut table def polar |
|
1176 |
u1 u2 |
|
1177 |
in |
|
1178 |
s_op2 connective T |
|
1179 |
(min_rep (rep_of quant_u) (rep_of unrepified_u)) |
|
1180 |
quant_u unrepified_u |
|
1181 |
end |
|
1182 |
end |
|
1183 |
end |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
1184 |
else if oper = Or orelse oper = And then |
33192 | 1185 |
let |
1186 |
val u1' = gsub def polar u1 |
|
1187 |
val u2' = gsub def polar u2 |
|
1188 |
in |
|
1189 |
(if polar = Neut then |
|
1190 |
case (is_opt_rep (rep_of u1'), is_opt_rep (rep_of u2')) of |
|
1191 |
(true, true) => triad_fn (fn polar => gsub def polar u) |
|
1192 |
| (true, false) => |
|
1193 |
s_op2 oper T (Opt bool_atom_R) |
|
1194 |
(triad_fn (fn polar => gsub def polar u1)) u2' |
|
1195 |
| (false, true) => |
|
1196 |
s_op2 oper T (Opt bool_atom_R) |
|
1197 |
u1' (triad_fn (fn polar => gsub def polar u2)) |
|
1198 |
| (false, false) => raise SAME () |
|
1199 |
else |
|
1200 |
raise SAME ()) |
|
1201 |
handle SAME () => s_op2 oper T (Formula polar) u1' u2' |
|
1202 |
end |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
1203 |
else if oper = The orelse oper = Eps then |
33192 | 1204 |
let |
1205 |
val u1' = sub u1 |
|
1206 |
val opt1 = is_opt_rep (rep_of u1') |
|
33744
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33631
diff
changeset
|
1207 |
val opt = (oper = Eps orelse opt1) |
33192 | 1208 |
val unopt_R = best_one_rep_for_type scope T |> optable_rep ofs T |
33744
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33631
diff
changeset
|
1209 |
val R = if is_boolean_type T then bool_rep polar opt |
e82531ebf5f3
fixed bug in Nitpick's handling of "The" and "Eps" when the return type is a "bool"
blanchet
parents:
33631
diff
changeset
|
1210 |
else unopt_R |> opt ? opt_rep ofs T |
33192 | 1211 |
val u = Op2 (oper, T, R, u1', sub u2) |
1212 |
in |
|
35385
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
blanchet
parents:
35312
diff
changeset
|
1213 |
if is_complete_type datatypes true T orelse not opt1 then |
33192 | 1214 |
u |
1215 |
else |
|
1216 |
let |
|
1217 |
val x_u = BoundName (~1, T, unopt_R, "descr.x") |
|
1218 |
val R = R |> opt_rep ofs T |
|
1219 |
in |
|
1220 |
Op3 (If, T, R, |
|
1221 |
Op2 (Exist, bool_T, Formula Pos, x_u, |
|
1222 |
s_op2 Apply bool_T (Formula Pos) (gsub false Pos u1) |
|
1223 |
x_u), u, Cst (Unknown, T, R)) |
|
1224 |
end |
|
1225 |
end |
|
1226 |
else |
|
1227 |
let |
|
1228 |
val u1 = sub u1 |
|
1229 |
val u2 = sub u2 |
|
1230 |
val R = |
|
1231 |
best_non_opt_set_rep_for_type scope T |
|
1232 |
|> exists (is_opt_rep o rep_of) [u1, u2] ? opt_rep ofs T |
|
1233 |
in s_op2 oper T R u1 u2 end |
|
1234 |
| Op3 (Let, T, _, u1, u2, u3) => |
|
1235 |
let |
|
1236 |
val u2 = sub u2 |
|
1237 |
val R2 = rep_of u2 |
|
1238 |
val table' = NameTable.update (u1, R2) table |
|
1239 |
val u1 = modify_name_rep u1 R2 |
|
1240 |
val u3 = aux table' false polar u3 |
|
1241 |
in s_op3 Let T (rep_of u3) u1 u2 u3 end |
|
1242 |
| Op3 (If, T, _, u1, u2, u3) => |
|
1243 |
let |
|
1244 |
val u1 = sub u1 |
|
1245 |
val u2 = gsub def polar u2 |
|
1246 |
val u3 = gsub def polar u3 |
|
1247 |
val min_R = min_rep (rep_of u2) (rep_of u3) |
|
1248 |
val R = min_R |> is_opt_rep (rep_of u1) ? opt_rep ofs T |
|
1249 |
in s_op3 If T R u1 u2 u3 end |
|
1250 |
| Tuple (T, _, us) => |
|
1251 |
let |
|
1252 |
val Rs = map (best_one_rep_for_type scope o type_of) us |
|
1253 |
val us = map sub us |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
1254 |
val R = if forall (curry (op =) Unit) Rs then Unit else Struct Rs |
33192 | 1255 |
val R' = (exists (is_opt_rep o rep_of) us ? opt_rep ofs T) R |
1256 |
in s_tuple T R' us end |
|
1257 |
| Construct (us', T, _, us) => |
|
1258 |
let |
|
1259 |
val us = map sub us |
|
1260 |
val Rs = map rep_of us |
|
1261 |
val R = best_one_rep_for_type scope T |
|
1262 |
val {total, ...} = |
|
1263 |
constr_spec datatypes (original_name (nickname_of (hd us')), T) |
|
1264 |
val opt = exists is_opt_rep Rs orelse not total |
|
1265 |
in Construct (map sub us', T, R |> opt ? Opt, us) end |
|
1266 |
| _ => |
|
1267 |
let val u = modify_name_rep u (the_name table u) in |
|
34936
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1268 |
if polar = Neut orelse not (is_boolean_type (type_of u)) orelse |
c4f04bee79f3
some work on Nitpick's support for quotient types;
blanchet
parents:
34288
diff
changeset
|
1269 |
not (is_opt_rep (rep_of u)) then |
33192 | 1270 |
u |
1271 |
else |
|
1272 |
s_op1 Cast (type_of u) (Formula polar) u |
|
1273 |
end |
|
1274 |
end |
|
1275 |
|> optimize_unit |
|
1276 |
in aux table def Pos end |
|
1277 |
||
34126 | 1278 |
(* int -> KK.n_ary_index list -> KK.n_ary_index list |
1279 |
-> int * KK.n_ary_index list *) |
|
33192 | 1280 |
fun fresh_n_ary_index n [] ys = (0, (n, 1) :: ys) |
1281 |
| fresh_n_ary_index n ((m, j) :: xs) ys = |
|
1282 |
if m = n then (j, ys @ ((m, j + 1) :: xs)) |
|
1283 |
else fresh_n_ary_index n xs ((m, j) :: ys) |
|
1284 |
(* int -> name_pool -> int * name_pool *) |
|
1285 |
fun fresh_rel n {rels, vars, formula_reg, rel_reg} = |
|
1286 |
let val (j, rels') = fresh_n_ary_index n rels [] in |
|
1287 |
(j, {rels = rels', vars = vars, formula_reg = formula_reg, |
|
1288 |
rel_reg = rel_reg}) |
|
1289 |
end |
|
1290 |
(* int -> name_pool -> int * name_pool *) |
|
1291 |
fun fresh_var n {rels, vars, formula_reg, rel_reg} = |
|
1292 |
let val (j, vars') = fresh_n_ary_index n vars [] in |
|
1293 |
(j, {rels = rels, vars = vars', formula_reg = formula_reg, |
|
1294 |
rel_reg = rel_reg}) |
|
1295 |
end |
|
1296 |
(* int -> name_pool -> int * name_pool *) |
|
1297 |
fun fresh_formula_reg {rels, vars, formula_reg, rel_reg} = |
|
1298 |
(formula_reg, {rels = rels, vars = vars, formula_reg = formula_reg + 1, |
|
1299 |
rel_reg = rel_reg}) |
|
1300 |
(* int -> name_pool -> int * name_pool *) |
|
1301 |
fun fresh_rel_reg {rels, vars, formula_reg, rel_reg} = |
|
1302 |
(rel_reg, {rels = rels, vars = vars, formula_reg = formula_reg, |
|
1303 |
rel_reg = rel_reg + 1}) |
|
1304 |
||
1305 |
(* nut -> nut list * name_pool * nut NameTable.table |
|
1306 |
-> nut list * name_pool * nut NameTable.table *) |
|
1307 |
fun rename_plain_var v (ws, pool, table) = |
|
1308 |
let |
|
1309 |
val is_formula = (rep_of v = Formula Neut) |
|
1310 |
val fresh = if is_formula then fresh_formula_reg else fresh_rel_reg |
|
1311 |
val (j, pool) = fresh pool |
|
1312 |
val constr = if is_formula then FormulaReg else RelReg |
|
1313 |
val w = constr (j, type_of v, rep_of v) |
|
1314 |
in (w :: ws, pool, NameTable.update (v, w) table) end |
|
1315 |
||
1316 |
(* typ -> rep -> nut list -> nut *) |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
1317 |
fun shape_tuple (T as Type (@{type_name "*"}, [T1, T2])) (R as Struct [R1, R2]) |
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
1318 |
us = |
33192 | 1319 |
let val arity1 = arity_of_rep R1 in |
1320 |
Tuple (T, R, [shape_tuple T1 R1 (List.take (us, arity1)), |
|
1321 |
shape_tuple T2 R2 (List.drop (us, arity1))]) |
|
1322 |
end |
|
35665
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
blanchet
parents:
35408
diff
changeset
|
1323 |
| shape_tuple (T as Type (@{type_name fun}, [_, T2])) (R as Vect (k, R')) us = |
33192 | 1324 |
Tuple (T, R, map (shape_tuple T2 R') (batch_list (length us div k) us)) |
35280
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
blanchet
parents:
35220
diff
changeset
|
1325 |
| shape_tuple T _ [u] = |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1326 |
if type_of u = T then u else raise NUT ("Nitpick_Nut.shape_tuple", [u]) |
33192 | 1327 |
| shape_tuple T Unit [] = Cst (Unity, T, Unit) |
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33192
diff
changeset
|
1328 |
| shape_tuple _ _ us = raise NUT ("Nitpick_Nut.shape_tuple", us) |
33192 | 1329 |
|
1330 |
(* bool -> nut -> nut list * name_pool * nut NameTable.table |
|
1331 |
-> nut list * name_pool * nut NameTable.table *) |
|
1332 |
fun rename_n_ary_var rename_free v (ws, pool, table) = |
|
1333 |
let |
|
1334 |
val T = type_of v |
|
1335 |
val R = rep_of v |
|
1336 |
val arity = arity_of_rep R |
|
1337 |
val nick = nickname_of v |
|
1338 |
val (constr, fresh) = if rename_free then (FreeRel, fresh_rel) |
|
1339 |
else (BoundRel, fresh_var) |
|
1340 |
in |
|
1341 |
if not rename_free andalso arity > 1 then |
|
1342 |
let |
|
1343 |
val atom_schema = atom_schema_of_rep R |
|
1344 |
val type_schema = type_schema_of_rep T R |
|
1345 |
val (js, pool) = funpow arity (fn (js, pool) => |
|
1346 |
let val (j, pool) = fresh 1 pool in |
|
1347 |
(j :: js, pool) |
|
1348 |
end) |
|
1349 |
([], pool) |
|
1350 |
val ws' = map3 (fn j => fn x => fn T => |
|
1351 |
constr ((1, j), T, Atom x, |
|
1352 |
nick ^ " [" ^ string_of_int j ^ "]")) |
|
1353 |
(rev js) atom_schema type_schema |
|
1354 |
in (ws' @ ws, pool, NameTable.update (v, shape_tuple T R ws') table) end |
|
1355 |
else |
|
1356 |
let |
|
34124
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1357 |
val (j, pool) = |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1358 |
case v of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1359 |
ConstName _ => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1360 |
if is_sel_like_and_no_discr nick then |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1361 |
case domain_type T of |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1362 |
@{typ "unsigned_bit word"} => |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1363 |
(snd unsigned_bit_word_sel_rel, pool) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1364 |
| @{typ "signed_bit word"} => (snd signed_bit_word_sel_rel, pool) |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1365 |
| _ => fresh arity pool |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1366 |
else |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1367 |
fresh arity pool |
c4628a1dcf75
added support for binary nat/int representation to Nitpick
blanchet
parents:
34123
diff
changeset
|
1368 |
| _ => fresh arity pool |
33192 | 1369 |
val w = constr ((arity, j), T, R, nick) |
1370 |
in (w :: ws, pool, NameTable.update (v, w) table) end |
|
1371 |
end |
|
1372 |
||
1373 |
(* nut list -> name_pool -> nut NameTable.table |
|
1374 |
-> nut list * name_pool * nut NameTable.table *) |
|
1375 |
fun rename_free_vars vs pool table = |
|
1376 |
let |
|
1377 |
val vs = filter (not_equal Unit o rep_of) vs |
|
1378 |
val (vs, pool, table) = fold (rename_n_ary_var true) vs ([], pool, table) |
|
1379 |
in (rev vs, pool, table) end |
|
1380 |
||
1381 |
(* name_pool -> nut NameTable.table -> nut -> nut *) |
|
1382 |
fun rename_vars_in_nut pool table u = |
|
1383 |
case u of |
|
1384 |
Cst _ => u |
|
1385 |
| Op1 (oper, T, R, u1) => Op1 (oper, T, R, rename_vars_in_nut pool table u1) |
|
1386 |
| Op2 (oper, T, R, u1, u2) => |
|
34121
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
blanchet
parents:
33982
diff
changeset
|
1387 |
if oper = All orelse oper = Exist orelse oper = Lambda then |
33192 | 1388 |
let |
1389 |
val (_, pool, table) = fold (rename_n_ary_var false) (untuple I u1) |
|
1390 |
([], pool, table) |
|
1391 |
in |
|
1392 |
Op2 (oper, T, R, rename_vars_in_nut pool table u1, |
|
1393 |
rename_vars_in_nut pool table u2) |
|
1394 |
end |
|
1395 |
else |
|
1396 |
Op2 (oper, T, R, rename_vars_in_nut pool table u1, |
|
1397 |
rename_vars_in_nut pool table u2) |
|
1398 |
| Op3 (Let, T, R, u1, u2, u3) => |
|
1399 |
if rep_of u2 = Unit orelse inline_nut u2 then |
|
1400 |
let |
|
1401 |
val u2 = rename_vars_in_nut pool table u2 |
|
1402 |
val table = NameTable.update (u1, u2) table |
|
1403 |
in rename_vars_in_nut pool table u3 end |
|
1404 |
else |
|
1405 |
let |
|
1406 |
val bs = untuple I u1 |
|
1407 |
val (_, pool, table') = fold rename_plain_var bs ([], pool, table) |
|
1408 |
in |
|
1409 |
Op3 (Let, T, R, rename_vars_in_nut pool table' u1, |
|
1410 |
rename_vars_in_nut pool table u2, |
|
1411 |
rename_vars_in_nut pool table' u3) |
|
1412 |
end |
|
1413 |
| Op3 (oper, T, R, u1, u2, u3) => |
|
1414 |
Op3 (oper, T, R, rename_vars_in_nut pool table u1, |
|
1415 |
rename_vars_in_nut pool table u2, rename_vars_in_nut pool table u3) |
|
1416 |
| Tuple (T, R, us) => Tuple (T, R, map (rename_vars_in_nut pool table) us) |
|
1417 |
| Construct (us', T, R, us) => |
|
1418 |
Construct (map (rename_vars_in_nut pool table) us', T, R, |
|
1419 |
map (rename_vars_in_nut pool table) us) |
|
1420 |
| _ => the_name table u |
|
1421 |
||
1422 |
end; |