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