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