| author | hoelzl | 
| Mon, 06 Oct 2014 16:27:31 +0200 | |
| changeset 58588 | 93d87fd1583d | 
| parent 55890 | bd7927cca152 | 
| child 58928 | 23d0ffd48006 | 
| permissions | -rw-r--r-- | 
| 33982 | 1 | (* Title: HOL/Tools/Nitpick/nitpick_scope.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 | Scope enumerator for Nitpick. | |
| 6 | *) | |
| 7 | ||
| 8 | signature NITPICK_SCOPE = | |
| 9 | sig | |
| 35070 
96136eb6218f
split "nitpick_hol.ML" into two files to make it more manageable;
 blanchet parents: 
34982diff
changeset | 10 | type hol_context = Nitpick_HOL.hol_context | 
| 33192 | 11 | |
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 12 | type constr_spec = | 
| 55889 | 13 |     {const: string * typ,
 | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 14 | delta: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 15 | epsilon: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 16 | exclusive: bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 17 | explicit_max: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 18 | total: bool} | 
| 33192 | 19 | |
| 55890 | 20 | type data_type_spec = | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 21 |     {typ: typ,
 | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 22 | card: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 23 | co: bool, | 
| 38126 | 24 | self_rec: bool, | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 25 | complete: bool * bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 26 | concrete: bool * bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 27 | deep: bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 28 | constrs: constr_spec list} | 
| 33192 | 29 | |
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 30 | type scope = | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 31 |     {hol_ctxt: hol_context,
 | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 32 | binarize: bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 33 | card_assigns: (typ * int) list, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 34 | bits: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 35 | bisim_depth: int, | 
| 55890 | 36 | data_types: data_type_spec list, | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 37 | ofs: int Typtab.table} | 
| 33192 | 38 | |
| 55890 | 39 | val is_asymmetric_non_data_type : typ -> bool | 
| 40 | val data_type_spec : data_type_spec list -> typ -> data_type_spec option | |
| 41 | val constr_spec : data_type_spec list -> string * typ -> constr_spec | |
| 42 | val is_complete_type : data_type_spec list -> bool -> typ -> bool | |
| 43 | val is_concrete_type : data_type_spec list -> bool -> typ -> bool | |
| 44 | val is_exact_type : data_type_spec list -> bool -> typ -> bool | |
| 33192 | 45 | val offset_of_type : int Typtab.table -> typ -> int | 
| 46 | val spec_of_type : scope -> typ -> int * int | |
| 47 | val pretties_for_scope : scope -> bool -> Pretty.T list | |
| 48 | val multiline_string_for_scope : scope -> string | |
| 35814 | 49 | val scopes_equivalent : scope * scope -> bool | 
| 33192 | 50 | val scope_less_eq : scope -> scope -> bool | 
| 38126 | 51 | val is_self_recursive_constr_type : typ -> bool | 
| 33192 | 52 | val all_scopes : | 
| 55889 | 53 | hol_context -> bool -> (typ option * int list) list -> | 
| 54 | ((string * typ) option * int list) list -> | |
| 55 | ((string * typ) option * int list) list -> int list -> int list -> | |
| 56 | typ list -> typ list -> typ list -> typ list -> int * scope list | |
| 33192 | 57 | end; | 
| 58 | ||
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 59 | structure Nitpick_Scope : NITPICK_SCOPE = | 
| 33192 | 60 | struct | 
| 61 | ||
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 62 | open Nitpick_Util | 
| 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 63 | open Nitpick_HOL | 
| 33192 | 64 | |
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 65 | type constr_spec = | 
| 55889 | 66 |   {const: string * typ,
 | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 67 | delta: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 68 | epsilon: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 69 | exclusive: bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 70 | explicit_max: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 71 | total: bool} | 
| 33192 | 72 | |
| 55890 | 73 | type data_type_spec = | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 74 |   {typ: typ,
 | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 75 | card: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 76 | co: bool, | 
| 38126 | 77 | self_rec: bool, | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 78 | complete: bool * bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 79 | concrete: bool * bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 80 | deep: bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 81 | constrs: constr_spec list} | 
| 33192 | 82 | |
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 83 | type scope = | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 84 |   {hol_ctxt: hol_context,
 | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 85 | binarize: bool, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 86 | card_assigns: (typ * int) list, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 87 | bits: int, | 
| 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 88 | bisim_depth: int, | 
| 55890 | 89 | data_types: data_type_spec list, | 
| 36390 
eee4ee6a5cbe
remove "show_skolems" option and change style of record declarations
 blanchet parents: 
36386diff
changeset | 90 | ofs: int Typtab.table} | 
| 33192 | 91 | |
| 55889 | 92 | datatype row_kind = Card of typ | Max of string * typ | 
| 33192 | 93 | |
| 94 | type row = row_kind * int list | |
| 95 | type block = row list | |
| 96 | ||
| 55890 | 97 | val is_asymmetric_non_data_type = | 
| 38127 | 98 | is_iterator_type orf is_integer_type orf is_bit_type | 
| 99 | ||
| 55890 | 100 | fun data_type_spec (dtypes : data_type_spec list) T = | 
| 34121 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
 blanchet parents: 
33982diff
changeset | 101 | List.find (curry (op =) T o #typ) dtypes | 
| 33192 | 102 | |
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 103 | fun constr_spec [] x = raise TERM ("Nitpick_Scope.constr_spec", [Const x])
 | 
| 55890 | 104 |   | constr_spec ({constrs, ...} :: dtypes : data_type_spec list) (x as (s, T)) =
 | 
| 34121 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
 blanchet parents: 
33982diff
changeset | 105 | case List.find (curry (op =) (s, body_type T) o (apsnd body_type o #const)) | 
| 33192 | 106 | constrs of | 
| 107 | SOME c => c | |
| 108 | | NONE => constr_spec dtypes x | |
| 109 | ||
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 110 | fun is_complete_type dtypes facto (Type (@{type_name fun}, [T1, T2])) =
 | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 111 | is_concrete_type dtypes facto T1 andalso is_complete_type dtypes facto T2 | 
| 38190 
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality-1 types
 blanchet parents: 
38188diff
changeset | 112 |   | is_complete_type dtypes facto (Type (@{type_name prod}, Ts)) =
 | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 113 | forall (is_complete_type dtypes facto) Ts | 
| 46083 | 114 |   | is_complete_type dtypes facto (Type (@{type_name set}, [T'])) =
 | 
| 115 | is_concrete_type dtypes facto T' | |
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 116 | | is_complete_type dtypes facto T = | 
| 35220 
2bcdae5f4fdb
added support for nonstandard "nat"s to Nitpick and fixed bugs in binary "nat"s and "int"s
 blanchet parents: 
35190diff
changeset | 117 | not (is_integer_like_type T) andalso not (is_bit_type T) andalso | 
| 55890 | 118 | fun_from_pair (#complete (the (data_type_spec dtypes T))) facto | 
| 33192 | 119 | handle Option.Option => true | 
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 120 | and is_concrete_type dtypes facto (Type (@{type_name fun}, [T1, T2])) =
 | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 121 | is_complete_type dtypes facto T1 andalso is_concrete_type dtypes facto T2 | 
| 38190 
b02e204b613a
get rid of all "optimizations" regarding "unit" and other cardinality-1 types
 blanchet parents: 
38188diff
changeset | 122 |   | is_concrete_type dtypes facto (Type (@{type_name prod}, Ts)) =
 | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 123 | forall (is_concrete_type dtypes facto) Ts | 
| 46083 | 124 |   | is_concrete_type dtypes facto (Type (@{type_name set}, [T'])) =
 | 
| 125 | is_complete_type dtypes facto T' | |
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 126 | | is_concrete_type dtypes facto T = | 
| 55890 | 127 | fun_from_pair (#concrete (the (data_type_spec dtypes T))) facto | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 128 | handle Option.Option => true | 
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 129 | and is_exact_type dtypes facto = | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 130 | is_complete_type dtypes facto andf is_concrete_type dtypes facto | 
| 33192 | 131 | |
| 132 | fun offset_of_type ofs T = | |
| 133 | case Typtab.lookup ofs T of | |
| 134 | SOME j0 => j0 | |
| 135 | | NONE => Typtab.lookup ofs dummyT |> the_default 0 | |
| 136 | ||
| 137 | fun spec_of_type ({card_assigns, ofs, ...} : scope) T =
 | |
| 138 | (card_of_type card_assigns T | |
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 139 |    handle TYPE ("Nitpick_HOL.card_of_type", _, _) => ~1, offset_of_type ofs T)
 | 
| 33192 | 140 | |
| 38188 | 141 | fun quintuple_for_scope code_type code_term code_string | 
| 55888 | 142 |         ({hol_ctxt = {ctxt, ...}, card_assigns, bits, bisim_depth,
 | 
| 55890 | 143 | data_types, ...} : scope) = | 
| 33192 | 144 | let | 
| 35075 
888802be2019
handle Nitpick's nonstandard model enumeration in a cleaner way;
 blanchet parents: 
35072diff
changeset | 145 |     val boring_Ts = [@{typ unsigned_bit}, @{typ signed_bit},
 | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 146 |                      @{typ bisim_iterator}]
 | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 147 | val (iter_assigns, card_assigns) = | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 148 | card_assigns |> filter_out (member (op =) boring_Ts o fst) | 
| 33192 | 149 | |> List.partition (is_fp_iterator_type o fst) | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 150 | val (secondary_card_assigns, primary_card_assigns) = | 
| 37256 
0dca1ec52999
thread along context instead of theory for typedef lookup
 blanchet parents: 
36390diff
changeset | 151 | card_assigns | 
| 55890 | 152 | |> List.partition ((is_integer_type orf is_data_type ctxt) o fst) | 
| 33192 | 153 | val cards = | 
| 38188 | 154 | map (fn (T, k) => | 
| 155 |               [code_type ctxt T, code_string (" = " ^ string_of_int k)])
 | |
| 33192 | 156 | fun maxes () = | 
| 157 | maps (map_filter | |
| 158 |                 (fn {const, explicit_max, ...} =>
 | |
| 159 | if explicit_max < 0 then | |
| 160 | NONE | |
| 161 | else | |
| 38188 | 162 | SOME [code_term ctxt (Const const), | 
| 163 |                             code_string (" = " ^ string_of_int explicit_max)])
 | |
| 55890 | 164 | o #constrs) data_types | 
| 33192 | 165 | fun iters () = | 
| 166 | map (fn (T, k) => | |
| 38188 | 167 | [code_term ctxt (Const (const_for_iterator_type T)), | 
| 168 |                code_string (" = " ^ string_of_int (k - 1))]) iter_assigns
 | |
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 169 | fun miscs () = | 
| 38188 | 170 | (if bits = 0 then [] | 
| 171 |        else [code_string ("bits = " ^ string_of_int bits)]) @
 | |
| 55890 | 172 | (if bisim_depth < 0 andalso forall (not o #co) data_types then [] | 
| 38188 | 173 |        else [code_string ("bisim_depth = " ^ signed_string_of_int bisim_depth)])
 | 
| 33192 | 174 | in | 
| 39118 
12f3788be67b
turned show_all_types into proper configuration option;
 wenzelm parents: 
38240diff
changeset | 175 | (cards primary_card_assigns, cards secondary_card_assigns, | 
| 
12f3788be67b
turned show_all_types into proper configuration option;
 wenzelm parents: 
38240diff
changeset | 176 | maxes (), iters (), miscs ()) | 
| 33192 | 177 | end | 
| 178 | ||
| 179 | fun pretties_for_scope scope verbose = | |
| 180 | let | |
| 38188 | 181 | fun standard_blocks s = map (Pretty.block o cons (Pretty.str (s ^ " "))) | 
| 182 | val (primary_cards, secondary_cards, maxes, iters, miscs) = | |
| 183 | quintuple_for_scope (pretty_maybe_quote oo pretty_for_type) | |
| 184 | (pretty_maybe_quote oo Syntax.pretty_term) | |
| 185 | Pretty.str scope | |
| 33192 | 186 | in | 
| 38188 | 187 | standard_blocks "card" primary_cards @ | 
| 188 | (if verbose then | |
| 189 | standard_blocks "card" secondary_cards @ | |
| 190 | standard_blocks "max" maxes @ | |
| 191 | standard_blocks "iter" iters @ | |
| 192 | miscs | |
| 193 | else | |
| 194 | []) | |
| 195 | |> pretty_serial_commas "and" | |
| 33192 | 196 | end | 
| 197 | ||
| 198 | fun multiline_string_for_scope scope = | |
| 199 | let | |
| 38188 | 200 | val (primary_cards, secondary_cards, maxes, iters, miscs) = | 
| 201 | quintuple_for_scope Syntax.string_of_typ Syntax.string_of_term I scope | |
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 202 | val cards = primary_cards @ secondary_cards | 
| 33192 | 203 | in | 
| 38188 | 204 | case (if null cards then [] else ["card: " ^ commas (map implode cards)]) @ | 
| 205 | (if null maxes then [] else ["max: " ^ commas (map implode maxes)]) @ | |
| 206 | (if null iters then [] else ["iter: " ^ commas (map implode iters)]) @ | |
| 207 | miscs of | |
| 33192 | 208 | [] => "empty" | 
| 55628 | 209 | | lines => cat_lines lines | 
| 33192 | 210 | end | 
| 211 | ||
| 35814 | 212 | fun scopes_equivalent (s1 : scope, s2 : scope) = | 
| 55890 | 213 | #data_types s1 = #data_types s2 andalso #card_assigns s1 = #card_assigns s2 | 
| 55889 | 214 | |
| 33192 | 215 | fun scope_less_eq (s1 : scope) (s2 : scope) = | 
| 216 | (s1, s2) |> pairself (map snd o #card_assigns) |> op ~~ |> forall (op <=) | |
| 217 | ||
| 218 | fun rank_of_row (_, ks) = length ks | |
| 55889 | 219 | |
| 33192 | 220 | fun rank_of_block block = fold Integer.max (map rank_of_row block) 1 | 
| 55889 | 221 | |
| 41991 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 222 | fun project_row _ (y, []) = (y, [1]) (* desperate measure *) | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 223 | | project_row column (y, ks) = (y, [nth ks (Int.min (column, length ks - 1))]) | 
| 55889 | 224 | |
| 33192 | 225 | fun project_block (column, block) = map (project_row column) block | 
| 226 | ||
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 227 | fun lookup_ints_assign eq assigns key = | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 228 | case triple_lookup eq assigns key of | 
| 33192 | 229 | SOME ks => ks | 
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 230 |   | NONE => raise ARG ("Nitpick_Scope.lookup_ints_assign", "")
 | 
| 55889 | 231 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 232 | fun lookup_type_ints_assign thy assigns T = | 
| 36384 | 233 | map (Integer.max 1) (lookup_ints_assign (type_match thy) assigns T) | 
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 234 |   handle ARG ("Nitpick_Scope.lookup_ints_assign", _) =>
 | 
| 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 235 |          raise TYPE ("Nitpick_Scope.lookup_type_ints_assign", [T], [])
 | 
| 55889 | 236 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 237 | fun lookup_const_ints_assign thy assigns x = | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 238 | lookup_ints_assign (const_match thy) assigns x | 
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 239 |   handle ARG ("Nitpick_Scope.lookup_ints_assign", _) =>
 | 
| 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 240 |          raise TERM ("Nitpick_Scope.lookup_const_ints_assign", [Const x])
 | 
| 33192 | 241 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 242 | fun row_for_constr thy maxes_assigns constr = | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 243 | SOME (Max constr, lookup_const_ints_assign thy maxes_assigns constr) | 
| 33192 | 244 |   handle TERM ("lookup_const_ints_assign", _) => NONE
 | 
| 245 | ||
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 246 | val max_bits = 31 (* Kodkod limit *) | 
| 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 247 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 248 | fun block_for_type (hol_ctxt as {thy, ...}) binarize cards_assigns maxes_assigns
 | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 249 | iters_assigns bitss bisim_depths T = | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 250 | case T of | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 251 |     @{typ unsigned_bit} =>
 | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 252 | [(Card T, map (Integer.min max_bits o Integer.max 1) bitss)] | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 253 |   | @{typ signed_bit} =>
 | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 254 | [(Card T, map (Integer.add 1 o Integer.min max_bits o Integer.max 1) bitss)] | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 255 |   | @{typ "unsigned_bit word"} =>
 | 
| 34126 | 256 | [(Card T, lookup_type_ints_assign thy cards_assigns nat_T)] | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 257 |   | @{typ "signed_bit word"} =>
 | 
| 34126 | 258 | [(Card T, lookup_type_ints_assign thy cards_assigns int_T)] | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 259 |   | @{typ bisim_iterator} =>
 | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 260 | [(Card T, map (Integer.add 1 o Integer.max 0) bisim_depths)] | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 261 | | _ => | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 262 | if is_fp_iterator_type T then | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 263 | [(Card T, map (Integer.add 1 o Integer.max 0) | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 264 | (lookup_const_ints_assign thy iters_assigns | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 265 | (const_for_iterator_type T)))] | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 266 | else | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 267 | (Card T, lookup_type_ints_assign thy cards_assigns T) :: | 
| 55890 | 268 | (case binarized_and_boxed_data_type_constrs hol_ctxt binarize T of | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 269 | [_] => [] | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 270 | | constrs => map_filter (row_for_constr thy maxes_assigns) constrs) | 
| 33192 | 271 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 272 | fun blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns iters_assigns | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 273 | bitss bisim_depths mono_Ts nonmono_Ts = | 
| 33192 | 274 | let | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 275 | val block_for = block_for_type hol_ctxt binarize cards_assigns maxes_assigns | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 276 | iters_assigns bitss bisim_depths | 
| 33192 | 277 | val mono_block = maps block_for mono_Ts | 
| 278 | val nonmono_blocks = map block_for nonmono_Ts | |
| 279 | in mono_block :: nonmono_blocks end | |
| 280 | ||
| 281 | val sync_threshold = 5 | |
| 38186 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 282 | val linearity = 5 | 
| 33192 | 283 | |
| 38186 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 284 | val all_combinations_ordered_smartly = | 
| 33192 | 285 | let | 
| 38186 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 286 | fun cost [] = 0 | 
| 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 287 | | cost [k] = k | 
| 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 288 | | cost (k :: ks) = | 
| 34121 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
 blanchet parents: 
33982diff
changeset | 289 | if k < sync_threshold andalso forall (curry (op =) k) ks then | 
| 33192 | 290 | k - sync_threshold | 
| 291 | else | |
| 38187 | 292 | k :: ks |> map (fn k => (k + linearity) * (k + linearity)) | 
| 293 | |> Integer.sum | |
| 33192 | 294 | in | 
| 38187 | 295 | all_combinations #> map (`cost) #> sort (int_ord o pairself fst) #> map snd | 
| 33192 | 296 | end | 
| 297 | ||
| 298 | fun is_self_recursive_constr_type T = | |
| 34121 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
 blanchet parents: 
33982diff
changeset | 299 | exists (exists_subtype (curry (op =) (body_type T))) (binder_types T) | 
| 33192 | 300 | |
| 301 | fun constr_max maxes x = the_default ~1 (AList.lookup (op =) maxes x) | |
| 302 | ||
| 55889 | 303 | type scope_desc = (typ * int) list * ((string * typ) * int) list | 
| 33192 | 304 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 305 | fun is_surely_inconsistent_card_assign hol_ctxt binarize | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 306 | (card_assigns, max_assigns) (T, k) = | 
| 55890 | 307 | case binarized_and_boxed_data_type_constrs hol_ctxt binarize T of | 
| 33192 | 308 | [] => false | 
| 309 | | xs => | |
| 310 | let | |
| 34126 | 311 | val dom_cards = | 
| 312 | map (Integer.prod o map (bounded_card_of_type k ~1 card_assigns) | |
| 33192 | 313 | o binder_types o snd) xs | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 314 | val maxes = map (constr_max max_assigns) xs | 
| 34126 | 315 | fun effective_max card ~1 = card | 
| 33192 | 316 | | effective_max card max = Int.min (card, max) | 
| 34126 | 317 | val max = map2 effective_max dom_cards maxes |> Integer.sum | 
| 318 | in max < k end | |
| 55889 | 319 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 320 | fun is_surely_inconsistent_scope_description hol_ctxt binarize seen rest | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 321 | max_assigns = | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 322 | exists (is_surely_inconsistent_card_assign hol_ctxt binarize | 
| 34126 | 323 | (seen @ rest, max_assigns)) seen | 
| 33192 | 324 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 325 | fun repair_card_assigns hol_ctxt binarize (card_assigns, max_assigns) = | 
| 33192 | 326 | let | 
| 327 | fun aux seen [] = SOME seen | |
| 35280 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
 blanchet parents: 
35220diff
changeset | 328 | | aux _ ((_, 0) :: _) = NONE | 
| 34126 | 329 | | aux seen ((T, k) :: rest) = | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 330 | (if is_surely_inconsistent_scope_description hol_ctxt binarize | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 331 | ((T, k) :: seen) rest max_assigns then | 
| 33192 | 332 | raise SAME () | 
| 333 | else | |
| 34126 | 334 | case aux ((T, k) :: seen) rest of | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 335 | SOME assigns => SOME assigns | 
| 33192 | 336 | | NONE => raise SAME ()) | 
| 34126 | 337 | handle SAME () => aux seen ((T, k - 1) :: rest) | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 338 | in aux [] (rev card_assigns) end | 
| 33192 | 339 | |
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 340 | fun repair_iterator_assign ctxt assigns (T as Type (_, Ts), k) = | 
| 33192 | 341 |     (T, if T = @{typ bisim_iterator} then
 | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 342 | let | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 343 | val co_cards = map snd (filter (is_codatatype ctxt o fst) assigns) | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 344 | in Int.min (k, Integer.sum co_cards) end | 
| 33192 | 345 | else if is_fp_iterator_type T then | 
| 346 | case Ts of | |
| 347 | [] => 1 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 348 | | _ => bounded_card_of_type k ~1 assigns (foldr1 HOLogic.mk_prodT Ts) | 
| 33192 | 349 | else | 
| 350 | k) | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 351 | | repair_iterator_assign _ _ assign = assign | 
| 33192 | 352 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 353 | fun add_row_to_scope_descriptor (kind, ks) (card_assigns, max_assigns) = | 
| 33192 | 354 | case kind of | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 355 | Card T => ((T, the_single ks) :: card_assigns, max_assigns) | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 356 | | Max x => (card_assigns, (x, the_single ks) :: max_assigns) | 
| 55889 | 357 | |
| 33192 | 358 | fun scope_descriptor_from_block block = | 
| 359 | fold_rev add_row_to_scope_descriptor block ([], []) | |
| 55889 | 360 | |
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 361 | fun scope_descriptor_from_combination (hol_ctxt as {ctxt, ...}) binarize blocks
 | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 362 | columns = | 
| 33192 | 363 | let | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 364 | val (card_assigns, max_assigns) = | 
| 33192 | 365 | maps project_block (columns ~~ blocks) |> scope_descriptor_from_block | 
| 366 | in | |
| 41991 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 367 | (card_assigns, max_assigns) | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 368 | |> repair_card_assigns hol_ctxt binarize | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 369 | |> Option.map | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 370 | (fn card_assigns => | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 371 | (map (repair_iterator_assign ctxt card_assigns) card_assigns, | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 372 | max_assigns)) | 
| 33192 | 373 | end | 
| 374 | ||
| 38124 | 375 | fun offset_table_for_card_assigns dtypes assigns = | 
| 33192 | 376 | let | 
| 377 | fun aux next _ [] = Typtab.update_new (dummyT, next) | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 378 | | aux next reusable ((T, k) :: assigns) = | 
| 55890 | 379 | if k = 1 orelse is_asymmetric_non_data_type T then | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 380 | aux next reusable assigns | 
| 55890 | 381 | else if length (these (Option.map #constrs (data_type_spec dtypes T))) | 
| 33192 | 382 | > 1 then | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 383 | Typtab.update_new (T, next) #> aux (next + k) reusable assigns | 
| 33192 | 384 | else | 
| 385 | case AList.lookup (op =) reusable k of | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 386 | SOME j0 => Typtab.update_new (T, j0) #> aux next reusable assigns | 
| 33192 | 387 | | NONE => Typtab.update_new (T, next) | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 388 | #> aux (next + k) ((k, next) :: reusable) assigns | 
| 38124 | 389 | in Typtab.empty |> aux 0 [] assigns end | 
| 33192 | 390 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 391 | fun domain_card max card_assigns = | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 392 | Integer.prod o map (bounded_card_of_type max max card_assigns) o binder_types | 
| 33192 | 393 | |
| 38162 
824e940a3dd0
minor symmetry breaking for codatatypes like llist
 blanchet parents: 
38127diff
changeset | 394 | fun add_constr_spec (card_assigns, max_assigns) acyclic card sum_dom_cards | 
| 35280 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
 blanchet parents: 
35220diff
changeset | 395 | num_self_recs num_non_self_recs (self_rec, x as (_, T)) | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 396 | constrs = | 
| 33192 | 397 | let | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 398 | val max = constr_max max_assigns x | 
| 33192 | 399 | fun next_delta () = if null constrs then 0 else #epsilon (hd constrs) | 
| 400 |     val {delta, epsilon, exclusive, total} =
 | |
| 401 | if max = 0 then | |
| 402 | let val delta = next_delta () in | |
| 403 |           {delta = delta, epsilon = delta, exclusive = true, total = false}
 | |
| 404 | end | |
| 38162 
824e940a3dd0
minor symmetry breaking for codatatypes like llist
 blanchet parents: 
38127diff
changeset | 405 | else if num_self_recs > 0 then | 
| 38193 | 406 | (if num_non_self_recs = 1 then | 
| 35072 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 407 | if self_rec then | 
| 38193 | 408 | case List.last constrs of | 
| 409 |                {delta = 0, epsilon = 1, exclusive = true, ...} =>
 | |
| 410 |                {delta = 1, epsilon = card, exclusive = (num_self_recs = 1),
 | |
| 411 | total = false} | |
| 35072 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 412 | | _ => raise SAME () | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 413 | else | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 414 | if domain_card 2 card_assigns T = 1 then | 
| 38162 
824e940a3dd0
minor symmetry breaking for codatatypes like llist
 blanchet parents: 
38127diff
changeset | 415 |                {delta = 0, epsilon = 1, exclusive = acyclic, total = acyclic}
 | 
| 35072 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 416 | else | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 417 | raise SAME () | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 418 | else | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 419 | raise SAME ()) | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 420 | handle SAME () => | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 421 |                {delta = 0, epsilon = card, exclusive = false, total = false}
 | 
| 33192 | 422 | else if card = sum_dom_cards (card + 1) then | 
| 423 | let val delta = next_delta () in | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 424 |           {delta = delta, epsilon = delta + domain_card card card_assigns T,
 | 
| 33192 | 425 | exclusive = true, total = true} | 
| 426 | end | |
| 427 | else | |
| 428 |         {delta = 0, epsilon = card,
 | |
| 429 | exclusive = (num_self_recs + num_non_self_recs = 1), total = false} | |
| 430 | in | |
| 431 |     {const = x, delta = delta, epsilon = epsilon, exclusive = exclusive,
 | |
| 432 | explicit_max = max, total = total} :: constrs | |
| 433 | end | |
| 434 | ||
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 435 | fun has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T = | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 436 | let val card = card_of_type card_assigns T in | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 437 | card = bounded_exact_card_of_type hol_ctxt | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 438 | (if facto then finitizable_dataTs else []) (card + 1) 0 | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 439 | card_assigns T | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 440 | end | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 441 | |
| 55890 | 442 | fun data_type_spec_from_scope_descriptor (hol_ctxt as {ctxt, ...})
 | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 443 | binarize deep_dataTs finitizable_dataTs (desc as (card_assigns, _)) | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 444 | (T, card) = | 
| 33192 | 445 | let | 
| 34982 
7b8c366e34a2
added support for nonstandard models to Nitpick (based on an idea by Koen Claessen) and did other fixes to Nitpick
 blanchet parents: 
34936diff
changeset | 446 | val deep = member (op =) deep_dataTs T | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 447 | val co = is_codatatype ctxt T | 
| 55890 | 448 | val xs = binarized_and_boxed_data_type_constrs hol_ctxt binarize T | 
| 33192 | 449 | val self_recs = map (is_self_recursive_constr_type o snd) xs | 
| 450 | val (num_self_recs, num_non_self_recs) = | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 451 | List.partition I self_recs |> pairself length | 
| 38126 | 452 | val self_rec = num_self_recs > 0 | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 453 | fun is_complete facto = | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 454 | has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 455 | fun is_concrete facto = | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 456 | is_word_type T orelse | 
| 47909 
5f1afeebafbc
fixed "real" after they were redefined as a 'quotient_type'
 blanchet parents: 
46083diff
changeset | 457 | (* FIXME: looks wrong; other types than just functions might be | 
| 45402 | 458 | abstract. "is_complete" is also suspicious. *) | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 459 | xs |> maps (binder_types o snd) |> maps binder_types | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 460 | |> forall (has_exact_card hol_ctxt facto finitizable_dataTs | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 461 | card_assigns) | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 462 | val complete = pair_from_fun is_complete | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 463 | val concrete = pair_from_fun is_concrete | 
| 33192 | 464 | fun sum_dom_cards max = | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 465 | map (domain_card max card_assigns o snd) xs |> Integer.sum | 
| 33192 | 466 | val constrs = | 
| 55888 | 467 | fold_rev (add_constr_spec desc (not co) card sum_dom_cards num_self_recs | 
| 468 | num_non_self_recs) | |
| 35072 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 469 | (sort (bool_ord o swap o pairself fst) (self_recs ~~ xs)) [] | 
| 33558 
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
 blanchet parents: 
33232diff
changeset | 470 | in | 
| 55888 | 471 |     {typ = T, card = card, co = co, self_rec = self_rec, complete = complete,
 | 
| 472 | concrete = concrete, deep = deep, constrs = constrs} | |
| 33558 
a2db56854b83
optimized Nitpick's encoding and rendering of datatypes whose constructors don't appear in the problem
 blanchet parents: 
33232diff
changeset | 473 | end | 
| 33192 | 474 | |
| 55888 | 475 | fun scope_from_descriptor (hol_ctxt as {ctxt, ...}) binarize deep_dataTs
 | 
| 36386 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 476 | finitizable_dataTs (desc as (card_assigns, _)) = | 
| 33192 | 477 | let | 
| 55890 | 478 | val data_types = | 
| 479 | map (data_type_spec_from_scope_descriptor hol_ctxt binarize deep_dataTs | |
| 480 | finitizable_dataTs desc) | |
| 481 | (filter (is_data_type ctxt o fst) card_assigns) | |
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 482 |     val bits = card_of_type card_assigns @{typ signed_bit} - 1
 | 
| 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 483 |                handle TYPE ("Nitpick_HOL.card_of_type", _, _) =>
 | 
| 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 484 |                       card_of_type card_assigns @{typ unsigned_bit}
 | 
| 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 485 |                       handle TYPE ("Nitpick_HOL.card_of_type", _, _) => 0
 | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 486 |     val bisim_depth = card_of_type card_assigns @{typ bisim_iterator} - 1
 | 
| 33192 | 487 | in | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 488 |     {hol_ctxt = hol_ctxt, binarize = binarize, card_assigns = card_assigns,
 | 
| 55890 | 489 | data_types = data_types, bits = bits, bisim_depth = bisim_depth, | 
| 490 | ofs = offset_table_for_card_assigns data_types card_assigns} | |
| 33192 | 491 | end | 
| 492 | ||
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 493 | fun repair_cards_assigns_wrt_boxing_etc _ _ [] = [] | 
| 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 494 | | repair_cards_assigns_wrt_boxing_etc thy Ts ((SOME T, ks) :: cards_assigns) = | 
| 33192 | 495 | (if is_fun_type T orelse is_pair_type T then | 
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 496 | Ts |> filter (curry (type_match thy o swap) T) |> map (rpair ks o SOME) | 
| 33192 | 497 | else | 
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 498 | [(SOME T, ks)]) @ | 
| 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 499 | repair_cards_assigns_wrt_boxing_etc thy Ts cards_assigns | 
| 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 500 | | repair_cards_assigns_wrt_boxing_etc thy Ts ((NONE, ks) :: cards_assigns) = | 
| 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 501 | (NONE, ks) :: repair_cards_assigns_wrt_boxing_etc thy Ts cards_assigns | 
| 33192 | 502 | |
| 38180 
7a88032f9265
bump up the max cardinalities, to use up more of the time given to us by the user
 blanchet parents: 
38162diff
changeset | 503 | val max_scopes = 5000 | 
| 
7a88032f9265
bump up the max cardinalities, to use up more of the time given to us by the user
 blanchet parents: 
38162diff
changeset | 504 | val distinct_threshold = 1000 | 
| 33192 | 505 | |
| 36386 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 506 | fun all_scopes (hol_ctxt as {thy, ...}) binarize cards_assigns maxes_assigns
 | 
| 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 507 | iters_assigns bitss bisim_depths mono_Ts nonmono_Ts deep_dataTs | 
| 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 508 | finitizable_dataTs = | 
| 33192 | 509 | let | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 510 | val cards_assigns = | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 511 | repair_cards_assigns_wrt_boxing_etc thy mono_Ts cards_assigns | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 512 | val blocks = | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 513 | blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 514 | iters_assigns bitss bisim_depths mono_Ts nonmono_Ts | 
| 33192 | 515 | val ranks = map rank_of_block blocks | 
| 33580 
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
 blanchet parents: 
33558diff
changeset | 516 | val all = all_combinations_ordered_smartly (map (rpair 0) ranks) | 
| 33957 | 517 | val head = take max_scopes all | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 518 | val descs = | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 519 | map_filter (scope_descriptor_from_combination hol_ctxt binarize blocks) | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 520 | head | 
| 33192 | 521 | in | 
| 33580 
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
 blanchet parents: 
33558diff
changeset | 522 | (length all - length head, | 
| 
45c33e97cb86
added datatype constructor cache in Nitpick (to speed up the scope enumeration) and never test more than 4096 scopes
 blanchet parents: 
33558diff
changeset | 523 | descs |> length descs <= distinct_threshold ? distinct (op =) | 
| 36386 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 524 | |> map (scope_from_descriptor hol_ctxt binarize deep_dataTs | 
| 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 525 | finitizable_dataTs)) | 
| 33192 | 526 | end | 
| 527 | ||
| 528 | end; |