| author | wenzelm | 
| Tue, 13 Oct 2020 20:29:13 +0200 | |
| changeset 72471 | aca85e8d873d | 
| parent 72195 | 16f2288b30cf | 
| child 79799 | 2746dfc9ceae | 
| 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 | ||
| 69593 | 110 | fun is_complete_type dtypes facto (Type (\<^type_name>\<open>fun\<close>, [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 | 
| 69593 | 112 | | is_complete_type dtypes facto (Type (\<^type_name>\<open>prod\<close>, Ts)) = | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 113 | forall (is_complete_type dtypes facto) Ts | 
| 69593 | 114 | | is_complete_type dtypes facto (Type (\<^type_name>\<open>set\<close>, [T'])) = | 
| 46083 | 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 | 
| 69593 | 120 | and is_concrete_type dtypes facto (Type (\<^type_name>\<open>fun\<close>, [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 | 
| 69593 | 122 | | is_concrete_type dtypes facto (Type (\<^type_name>\<open>prod\<close>, Ts)) = | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 123 | forall (is_concrete_type dtypes facto) Ts | 
| 69593 | 124 | | is_concrete_type dtypes facto (Type (\<^type_name>\<open>set\<close>, [T'])) = | 
| 46083 | 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 | 
| 69593 | 145 | val boring_Ts = [\<^typ>\<open>unsigned_bit\<close>, \<^typ>\<open>signed_bit\<close>, | 
| 146 | \<^typ>\<open>bisim_iterator\<close>] | |
| 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) = | |
| 58928 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
55890diff
changeset | 183 | quintuple_for_scope | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
55890diff
changeset | 184 | (fn ctxt => pretty_maybe_quote (Thy_Header.get_keywords' ctxt) o pretty_for_type ctxt) | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
55890diff
changeset | 185 | (fn ctxt => pretty_maybe_quote (Thy_Header.get_keywords' ctxt) o Syntax.pretty_term ctxt) | 
| 
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
 wenzelm parents: 
55890diff
changeset | 186 | Pretty.str scope | 
| 33192 | 187 | in | 
| 38188 | 188 | standard_blocks "card" primary_cards @ | 
| 189 | (if verbose then | |
| 190 | standard_blocks "card" secondary_cards @ | |
| 191 | standard_blocks "max" maxes @ | |
| 192 | standard_blocks "iter" iters @ | |
| 193 | miscs | |
| 194 | else | |
| 195 | []) | |
| 196 | |> pretty_serial_commas "and" | |
| 33192 | 197 | end | 
| 198 | ||
| 199 | fun multiline_string_for_scope scope = | |
| 200 | let | |
| 72195 | 201 | val code_type = Print_Mode.setmp [] o Syntax.string_of_typ o Config.put show_markup false; | 
| 202 | val code_term = Print_Mode.setmp [] o Syntax.string_of_term o Config.put show_markup false; | |
| 38188 | 203 | val (primary_cards, secondary_cards, maxes, iters, miscs) = | 
| 72195 | 204 | quintuple_for_scope code_type code_term I scope | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 205 | val cards = primary_cards @ secondary_cards | 
| 33192 | 206 | in | 
| 38188 | 207 | case (if null cards then [] else ["card: " ^ commas (map implode cards)]) @ | 
| 208 | (if null maxes then [] else ["max: " ^ commas (map implode maxes)]) @ | |
| 209 | (if null iters then [] else ["iter: " ^ commas (map implode iters)]) @ | |
| 210 | miscs of | |
| 33192 | 211 | [] => "empty" | 
| 55628 | 212 | | lines => cat_lines lines | 
| 33192 | 213 | end | 
| 214 | ||
| 35814 | 215 | fun scopes_equivalent (s1 : scope, s2 : scope) = | 
| 55890 | 216 | #data_types s1 = #data_types s2 andalso #card_assigns s1 = #card_assigns s2 | 
| 55889 | 217 | |
| 33192 | 218 | fun scope_less_eq (s1 : scope) (s2 : scope) = | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58928diff
changeset | 219 | (s1, s2) |> apply2 (map snd o #card_assigns) |> op ~~ |> forall (op <=) | 
| 33192 | 220 | |
| 221 | fun rank_of_row (_, ks) = length ks | |
| 55889 | 222 | |
| 33192 | 223 | fun rank_of_block block = fold Integer.max (map rank_of_row block) 1 | 
| 55889 | 224 | |
| 41991 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 225 | fun project_row _ (y, []) = (y, [1]) (* desperate measure *) | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 226 | | project_row column (y, ks) = (y, [nth ks (Int.min (column, length ks - 1))]) | 
| 55889 | 227 | |
| 33192 | 228 | fun project_block (column, block) = map (project_row column) block | 
| 229 | ||
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 230 | 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 | 231 | case triple_lookup eq assigns key of | 
| 33192 | 232 | SOME ks => ks | 
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 233 |   | NONE => raise ARG ("Nitpick_Scope.lookup_ints_assign", "")
 | 
| 55889 | 234 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 235 | fun lookup_type_ints_assign thy assigns T = | 
| 36384 | 236 | 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 | 237 |   handle ARG ("Nitpick_Scope.lookup_ints_assign", _) =>
 | 
| 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 238 |          raise TYPE ("Nitpick_Scope.lookup_type_ints_assign", [T], [])
 | 
| 55889 | 239 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 240 | 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 | 241 | lookup_ints_assign (const_match thy) assigns x | 
| 33232 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 242 |   handle ARG ("Nitpick_Scope.lookup_ints_assign", _) =>
 | 
| 
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
 blanchet parents: 
33192diff
changeset | 243 |          raise TERM ("Nitpick_Scope.lookup_const_ints_assign", [Const x])
 | 
| 33192 | 244 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 245 | 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 | 246 | SOME (Max constr, lookup_const_ints_assign thy maxes_assigns constr) | 
| 33192 | 247 |   handle TERM ("lookup_const_ints_assign", _) => NONE
 | 
| 248 | ||
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 249 | val max_bits = 31 (* Kodkod limit *) | 
| 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 250 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 251 | 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 | 252 | iters_assigns bitss bisim_depths T = | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 253 | case T of | 
| 69593 | 254 | \<^typ>\<open>unsigned_bit\<close> => | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 255 | [(Card T, map (Integer.min max_bits o Integer.max 1) bitss)] | 
| 69593 | 256 | | \<^typ>\<open>signed_bit\<close> => | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 257 | [(Card T, map (Integer.add 1 o Integer.min max_bits o Integer.max 1) bitss)] | 
| 69593 | 258 | | \<^typ>\<open>unsigned_bit word\<close> => | 
| 34126 | 259 | [(Card T, lookup_type_ints_assign thy cards_assigns nat_T)] | 
| 69593 | 260 | | \<^typ>\<open>signed_bit word\<close> => | 
| 34126 | 261 | [(Card T, lookup_type_ints_assign thy cards_assigns int_T)] | 
| 69593 | 262 | | \<^typ>\<open>bisim_iterator\<close> => | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 263 | [(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 | 264 | | _ => | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 265 | if is_fp_iterator_type T then | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 266 | [(Card T, map (Integer.add 1 o Integer.max 0) | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 267 | (lookup_const_ints_assign thy iters_assigns | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 268 | (const_for_iterator_type T)))] | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 269 | else | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 270 | (Card T, lookup_type_ints_assign thy cards_assigns T) :: | 
| 55890 | 271 | (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 | 272 | [_] => [] | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 273 | | constrs => map_filter (row_for_constr thy maxes_assigns) constrs) | 
| 33192 | 274 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 275 | 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 | 276 | bitss bisim_depths mono_Ts nonmono_Ts = | 
| 33192 | 277 | let | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 278 | 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 | 279 | iters_assigns bitss bisim_depths | 
| 33192 | 280 | val mono_block = maps block_for mono_Ts | 
| 281 | val nonmono_blocks = map block_for nonmono_Ts | |
| 282 | in mono_block :: nonmono_blocks end | |
| 283 | ||
| 284 | val sync_threshold = 5 | |
| 38186 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 285 | val linearity = 5 | 
| 33192 | 286 | |
| 38186 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 287 | val all_combinations_ordered_smartly = | 
| 33192 | 288 | let | 
| 38186 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 289 | fun cost [] = 0 | 
| 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 290 | | cost [k] = k | 
| 
c28018f5a1d6
example tweaking -- also prevents Nitpick_Tests from using more than 1 thread
 blanchet parents: 
38180diff
changeset | 291 | | cost (k :: ks) = | 
| 34121 
5e831d805118
get rid of polymorphic equality in Nitpick's code + a few minor cleanups
 blanchet parents: 
33982diff
changeset | 292 | if k < sync_threshold andalso forall (curry (op =) k) ks then | 
| 33192 | 293 | k - sync_threshold | 
| 294 | else | |
| 38187 | 295 | k :: ks |> map (fn k => (k + linearity) * (k + linearity)) | 
| 296 | |> Integer.sum | |
| 33192 | 297 | in | 
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58928diff
changeset | 298 | all_combinations #> map (`cost) #> sort (int_ord o apply2 fst) #> map snd | 
| 33192 | 299 | end | 
| 300 | ||
| 301 | 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 | 302 | exists (exists_subtype (curry (op =) (body_type T))) (binder_types T) | 
| 33192 | 303 | |
| 304 | fun constr_max maxes x = the_default ~1 (AList.lookup (op =) maxes x) | |
| 305 | ||
| 55889 | 306 | type scope_desc = (typ * int) list * ((string * typ) * int) list | 
| 33192 | 307 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 308 | 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 | 309 | (card_assigns, max_assigns) (T, k) = | 
| 55890 | 310 | case binarized_and_boxed_data_type_constrs hol_ctxt binarize T of | 
| 33192 | 311 | [] => false | 
| 312 | | xs => | |
| 313 | let | |
| 34126 | 314 | val dom_cards = | 
| 315 | map (Integer.prod o map (bounded_card_of_type k ~1 card_assigns) | |
| 33192 | 316 | 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 | 317 | val maxes = map (constr_max max_assigns) xs | 
| 34126 | 318 | fun effective_max card ~1 = card | 
| 33192 | 319 | | effective_max card max = Int.min (card, max) | 
| 34126 | 320 | val max = map2 effective_max dom_cards maxes |> Integer.sum | 
| 321 | in max < k end | |
| 55889 | 322 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 323 | 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 | 324 | max_assigns = | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 325 | exists (is_surely_inconsistent_card_assign hol_ctxt binarize | 
| 34126 | 326 | (seen @ rest, max_assigns)) seen | 
| 33192 | 327 | |
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 328 | fun repair_card_assigns hol_ctxt binarize (card_assigns, max_assigns) = | 
| 33192 | 329 | let | 
| 330 | fun aux seen [] = SOME seen | |
| 35280 
54ab4921f826
fixed a few bugs in Nitpick and removed unreferenced variables
 blanchet parents: 
35220diff
changeset | 331 | | aux _ ((_, 0) :: _) = NONE | 
| 34126 | 332 | | aux seen ((T, k) :: rest) = | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 333 | (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 | 334 | ((T, k) :: seen) rest max_assigns then | 
| 33192 | 335 | raise SAME () | 
| 336 | else | |
| 34126 | 337 | 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 | 338 | SOME assigns => SOME assigns | 
| 33192 | 339 | | NONE => raise SAME ()) | 
| 34126 | 340 | 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 | 341 | in aux [] (rev card_assigns) end | 
| 33192 | 342 | |
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 343 | fun repair_iterator_assign ctxt assigns (T as Type (_, Ts), k) = | 
| 69593 | 344 | (T, if T = \<^typ>\<open>bisim_iterator\<close> then | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 345 | let | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 346 | 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 | 347 | in Int.min (k, Integer.sum co_cards) end | 
| 33192 | 348 | else if is_fp_iterator_type T then | 
| 349 | case Ts of | |
| 350 | [] => 1 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 351 | | _ => bounded_card_of_type k ~1 assigns (foldr1 HOLogic.mk_prodT Ts) | 
| 33192 | 352 | else | 
| 353 | k) | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 354 | | repair_iterator_assign _ _ assign = assign | 
| 33192 | 355 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 356 | fun add_row_to_scope_descriptor (kind, ks) (card_assigns, max_assigns) = | 
| 33192 | 357 | case kind of | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 358 | 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 | 359 | | Max x => (card_assigns, (x, the_single ks) :: max_assigns) | 
| 55889 | 360 | |
| 33192 | 361 | fun scope_descriptor_from_block block = | 
| 362 | fold_rev add_row_to_scope_descriptor block ([], []) | |
| 55889 | 363 | |
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 364 | 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 | 365 | columns = | 
| 33192 | 366 | let | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 367 | val (card_assigns, max_assigns) = | 
| 33192 | 368 | maps project_block (columns ~~ blocks) |> scope_descriptor_from_block | 
| 369 | in | |
| 41991 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 370 | (card_assigns, max_assigns) | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 371 | |> repair_card_assigns hol_ctxt binarize | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 372 | |> Option.map | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 373 | (fn card_assigns => | 
| 
ea02b9ee3085
prevent an exception if "card" is empty (e.g., "nitpick [card]")
 blanchet parents: 
41052diff
changeset | 374 | (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 | 375 | max_assigns)) | 
| 33192 | 376 | end | 
| 377 | ||
| 38124 | 378 | fun offset_table_for_card_assigns dtypes assigns = | 
| 33192 | 379 | let | 
| 380 | 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 | 381 | | aux next reusable ((T, k) :: assigns) = | 
| 55890 | 382 | 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 | 383 | aux next reusable assigns | 
| 55890 | 384 | else if length (these (Option.map #constrs (data_type_spec dtypes T))) | 
| 33192 | 385 | > 1 then | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 386 | Typtab.update_new (T, next) #> aux (next + k) reusable assigns | 
| 33192 | 387 | else | 
| 388 | 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 | 389 | SOME j0 => Typtab.update_new (T, j0) #> aux next reusable assigns | 
| 33192 | 390 | | 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 | 391 | #> aux (next + k) ((k, next) :: reusable) assigns | 
| 38124 | 392 | in Typtab.empty |> aux 0 [] assigns end | 
| 33192 | 393 | |
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 394 | 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 | 395 | Integer.prod o map (bounded_card_of_type max max card_assigns) o binder_types | 
| 33192 | 396 | |
| 38162 
824e940a3dd0
minor symmetry breaking for codatatypes like llist
 blanchet parents: 
38127diff
changeset | 397 | 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 | 398 | 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 | 399 | constrs = | 
| 33192 | 400 | let | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 401 | val max = constr_max max_assigns x | 
| 33192 | 402 | fun next_delta () = if null constrs then 0 else #epsilon (hd constrs) | 
| 403 |     val {delta, epsilon, exclusive, total} =
 | |
| 404 | if max = 0 then | |
| 405 | let val delta = next_delta () in | |
| 406 |           {delta = delta, epsilon = delta, exclusive = true, total = false}
 | |
| 407 | end | |
| 38162 
824e940a3dd0
minor symmetry breaking for codatatypes like llist
 blanchet parents: 
38127diff
changeset | 408 | else if num_self_recs > 0 then | 
| 38193 | 409 | (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 | 410 | if self_rec then | 
| 38193 | 411 | case List.last constrs of | 
| 412 |                {delta = 0, epsilon = 1, exclusive = true, ...} =>
 | |
| 413 |                {delta = 1, epsilon = card, exclusive = (num_self_recs = 1),
 | |
| 414 | total = false} | |
| 35072 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 415 | | _ => raise SAME () | 
| 
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 | if domain_card 2 card_assigns T = 1 then | 
| 38162 
824e940a3dd0
minor symmetry breaking for codatatypes like llist
 blanchet parents: 
38127diff
changeset | 418 |                {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 | 419 | else | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 420 | raise SAME () | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 421 | else | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 422 | raise SAME ()) | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 423 | handle SAME () => | 
| 
d79308423aea
optimize Nitpick's encoding for other datatypes than list that have a constant constructor like "Nil";
 blanchet parents: 
35071diff
changeset | 424 |                {delta = 0, epsilon = card, exclusive = false, total = false}
 | 
| 33192 | 425 | else if card = sum_dom_cards (card + 1) then | 
| 426 | 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 | 427 |           {delta = delta, epsilon = delta + domain_card card card_assigns T,
 | 
| 33192 | 428 | exclusive = true, total = true} | 
| 429 | end | |
| 430 | else | |
| 431 |         {delta = 0, epsilon = card,
 | |
| 432 | exclusive = (num_self_recs + num_non_self_recs = 1), total = false} | |
| 433 | in | |
| 434 |     {const = x, delta = delta, epsilon = epsilon, exclusive = exclusive,
 | |
| 435 | explicit_max = max, total = total} :: constrs | |
| 436 | end | |
| 437 | ||
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 438 | 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 | 439 | let val card = card_of_type card_assigns T in | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 440 | card = bounded_exact_card_of_type hol_ctxt | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 441 | (if facto then finitizable_dataTs else []) (card + 1) 0 | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 442 | card_assigns T | 
| 34123 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 443 | end | 
| 
c4988215a691
distinguish better between "complete" (vs. incomplete) types and "concrete" (vs. abstract) types in Nitpick;
 blanchet parents: 
34121diff
changeset | 444 | |
| 55890 | 445 | fun data_type_spec_from_scope_descriptor (hol_ctxt as {ctxt, ...})
 | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 446 | binarize deep_dataTs finitizable_dataTs (desc as (card_assigns, _)) | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 447 | (T, card) = | 
| 33192 | 448 | 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 | 449 | val deep = member (op =) deep_dataTs T | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 450 | val co = is_codatatype ctxt T | 
| 55890 | 451 | val xs = binarized_and_boxed_data_type_constrs hol_ctxt binarize T | 
| 33192 | 452 | val self_recs = map (is_self_recursive_constr_type o snd) xs | 
| 453 | val (num_self_recs, num_non_self_recs) = | |
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58928diff
changeset | 454 | List.partition I self_recs |> apply2 length | 
| 38126 | 455 | val self_rec = num_self_recs > 0 | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 456 | fun is_complete facto = | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 457 | has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 458 | fun is_concrete facto = | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 459 | is_word_type T orelse | 
| 47909 
5f1afeebafbc
fixed "real" after they were redefined as a 'quotient_type'
 blanchet parents: 
46083diff
changeset | 460 | (* FIXME: looks wrong; other types than just functions might be | 
| 45402 | 461 | abstract. "is_complete" is also suspicious. *) | 
| 35385 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 462 | xs |> maps (binder_types o snd) |> maps binder_types | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 463 | |> forall (has_exact_card hol_ctxt facto finitizable_dataTs | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 464 | card_assigns) | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 465 | val complete = pair_from_fun is_complete | 
| 
29f81babefd7
improved precision of infinite "shallow" datatypes in Nitpick;
 blanchet parents: 
35280diff
changeset | 466 | val concrete = pair_from_fun is_concrete | 
| 33192 | 467 | 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 | 468 | map (domain_card max card_assigns o snd) xs |> Integer.sum | 
| 33192 | 469 | val constrs = | 
| 55888 | 470 | fold_rev (add_constr_spec desc (not co) card sum_dom_cards num_self_recs | 
| 471 | num_non_self_recs) | |
| 59058 
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
 wenzelm parents: 
58928diff
changeset | 472 | (sort (bool_ord o swap o apply2 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 | 473 | in | 
| 55888 | 474 |     {typ = T, card = card, co = co, self_rec = self_rec, complete = complete,
 | 
| 475 | 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 | 476 | end | 
| 33192 | 477 | |
| 55888 | 478 | fun scope_from_descriptor (hol_ctxt as {ctxt, ...}) binarize deep_dataTs
 | 
| 36386 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 479 | finitizable_dataTs (desc as (card_assigns, _)) = | 
| 33192 | 480 | let | 
| 55890 | 481 | val data_types = | 
| 482 | map (data_type_spec_from_scope_descriptor hol_ctxt binarize deep_dataTs | |
| 483 | finitizable_dataTs desc) | |
| 484 | (filter (is_data_type ctxt o fst) card_assigns) | |
| 69593 | 485 | val bits = card_of_type card_assigns \<^typ>\<open>signed_bit\<close> - 1 | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 486 |                handle TYPE ("Nitpick_HOL.card_of_type", _, _) =>
 | 
| 69593 | 487 | card_of_type card_assigns \<^typ>\<open>unsigned_bit\<close> | 
| 34124 
c4628a1dcf75
added support for binary nat/int representation to Nitpick
 blanchet parents: 
34123diff
changeset | 488 |                       handle TYPE ("Nitpick_HOL.card_of_type", _, _) => 0
 | 
| 69593 | 489 | val bisim_depth = card_of_type card_assigns \<^typ>\<open>bisim_iterator\<close> - 1 | 
| 33192 | 490 | in | 
| 35190 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 491 |     {hol_ctxt = hol_ctxt, binarize = binarize, card_assigns = card_assigns,
 | 
| 55890 | 492 | data_types = data_types, bits = bits, bisim_depth = bisim_depth, | 
| 493 | ofs = offset_table_for_card_assigns data_types card_assigns} | |
| 33192 | 494 | end | 
| 495 | ||
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 496 | fun repair_cards_assigns_wrt_boxing_etc _ _ [] = [] | 
| 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 497 | | repair_cards_assigns_wrt_boxing_etc thy Ts ((SOME T, ks) :: cards_assigns) = | 
| 33192 | 498 | (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 | 499 | Ts |> filter (curry (type_match thy o swap) T) |> map (rpair ks o SOME) | 
| 33192 | 500 | else | 
| 35665 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 501 | [(SOME T, ks)]) @ | 
| 
ff2bf50505ab
added "finitize" option to Nitpick + remove dependency on "Coinductive_List"
 blanchet parents: 
35385diff
changeset | 502 | 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 | 503 | | 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 | 504 | (NONE, ks) :: repair_cards_assigns_wrt_boxing_etc thy Ts cards_assigns | 
| 33192 | 505 | |
| 38180 
7a88032f9265
bump up the max cardinalities, to use up more of the time given to us by the user
 blanchet parents: 
38162diff
changeset | 506 | 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 | 507 | val distinct_threshold = 1000 | 
| 33192 | 508 | |
| 36386 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 509 | fun all_scopes (hol_ctxt as {thy, ...}) binarize cards_assigns maxes_assigns
 | 
| 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 510 | iters_assigns bitss bisim_depths mono_Ts nonmono_Ts deep_dataTs | 
| 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 511 | finitizable_dataTs = | 
| 33192 | 512 | let | 
| 38240 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 513 | val cards_assigns = | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 514 | repair_cards_assigns_wrt_boxing_etc thy mono_Ts cards_assigns | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 515 | val blocks = | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 516 | blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns | 
| 
a44d108a8d39
local versions of Nitpick.register_xxx functions
 blanchet parents: 
38193diff
changeset | 517 | iters_assigns bitss bisim_depths mono_Ts nonmono_Ts | 
| 33192 | 518 | 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 | 519 | val all = all_combinations_ordered_smartly (map (rpair 0) ranks) | 
| 33957 | 520 | 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 | 521 | val descs = | 
| 
ce653cc27a94
make sure that Nitpick uses binary notation consistently if "binary_ints" is enabled
 blanchet parents: 
35179diff
changeset | 522 | 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 | 523 | head | 
| 33192 | 524 | 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 | 525 | (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 | 526 | descs |> length descs <= distinct_threshold ? distinct (op =) | 
| 36386 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 527 | |> map (scope_from_descriptor hol_ctxt binarize deep_dataTs | 
| 
2132f15b366f
Fruhjahrsputz: remove three mostly useless Nitpick options
 blanchet parents: 
36385diff
changeset | 528 | finitizable_dataTs)) | 
| 33192 | 529 | end | 
| 530 | ||
| 531 | end; |