src/HOL/Tools/Nitpick/nitpick_scope.ML
author blanchet
Thu, 25 Feb 2010 16:33:39 +0100
changeset 35385 29f81babefd7
parent 35280 54ab4921f826
child 35665 ff2bf50505ab
permissions -rw-r--r--
improved precision of infinite "shallow" datatypes in Nitpick;
e.g. strings used for variable names, instead of an opaque type
blanchet@33982
     1
(*  Title:      HOL/Tools/Nitpick/nitpick_scope.ML
blanchet@33192
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@34969
     3
    Copyright   2008, 2009, 2010
blanchet@33192
     4
blanchet@33192
     5
Scope enumerator for Nitpick.
blanchet@33192
     6
*)
blanchet@33192
     7
blanchet@33192
     8
signature NITPICK_SCOPE =
blanchet@33192
     9
sig
blanchet@33705
    10
  type styp = Nitpick_Util.styp
blanchet@35067
    11
  type hol_context = Nitpick_HOL.hol_context
blanchet@33192
    12
blanchet@33192
    13
  type constr_spec = {
blanchet@33192
    14
    const: styp,
blanchet@33192
    15
    delta: int,
blanchet@33192
    16
    epsilon: int,
blanchet@33192
    17
    exclusive: bool,
blanchet@33192
    18
    explicit_max: int,
blanchet@33192
    19
    total: bool}
blanchet@33192
    20
blanchet@33192
    21
  type dtype_spec = {
blanchet@33192
    22
    typ: typ,
blanchet@33192
    23
    card: int,
blanchet@33192
    24
    co: bool,
blanchet@35179
    25
    standard: bool,
blanchet@35385
    26
    complete: bool * bool,
blanchet@35385
    27
    concrete: bool * bool,
blanchet@34969
    28
    deep: bool,
blanchet@33192
    29
    constrs: constr_spec list}
blanchet@33192
    30
blanchet@33192
    31
  type scope = {
blanchet@35067
    32
    hol_ctxt: hol_context,
blanchet@35190
    33
    binarize: bool,
blanchet@33192
    34
    card_assigns: (typ * int) list,
blanchet@34121
    35
    bits: int,
blanchet@33192
    36
    bisim_depth: int,
blanchet@33192
    37
    datatypes: dtype_spec list,
blanchet@33192
    38
    ofs: int Typtab.table}
blanchet@33192
    39
blanchet@33192
    40
  val datatype_spec : dtype_spec list -> typ -> dtype_spec option
blanchet@33192
    41
  val constr_spec : dtype_spec list -> styp -> constr_spec
blanchet@35385
    42
  val is_complete_type : dtype_spec list -> bool -> typ -> bool
blanchet@35385
    43
  val is_concrete_type : dtype_spec list -> bool -> typ -> bool
blanchet@35385
    44
  val is_exact_type : dtype_spec list -> bool -> typ -> bool
blanchet@33192
    45
  val offset_of_type : int Typtab.table -> typ -> int
blanchet@33192
    46
  val spec_of_type : scope -> typ -> int * int
blanchet@33192
    47
  val pretties_for_scope : scope -> bool -> Pretty.T list
blanchet@33192
    48
  val multiline_string_for_scope : scope -> string
blanchet@33192
    49
  val scopes_equivalent : scope -> scope -> bool
blanchet@33192
    50
  val scope_less_eq : scope -> scope -> bool
blanchet@33192
    51
  val all_scopes :
blanchet@35190
    52
    hol_context -> bool -> int -> (typ option * int list) list
blanchet@33192
    53
    -> (styp option * int list) list -> (styp option * int list) list
blanchet@35385
    54
    -> int list -> int list -> typ list -> typ list -> typ list -> typ list
blanchet@34121
    55
    -> int * scope list
blanchet@33192
    56
end;
blanchet@33192
    57
blanchet@33224
    58
structure Nitpick_Scope : NITPICK_SCOPE =
blanchet@33192
    59
struct
blanchet@33192
    60
blanchet@33224
    61
open Nitpick_Util
blanchet@33224
    62
open Nitpick_HOL
blanchet@33192
    63
blanchet@33192
    64
type constr_spec = {
blanchet@33192
    65
  const: styp,
blanchet@33192
    66
  delta: int,
blanchet@33192
    67
  epsilon: int,
blanchet@33192
    68
  exclusive: bool,
blanchet@33192
    69
  explicit_max: int,
blanchet@33192
    70
  total: bool}
blanchet@33192
    71
blanchet@33192
    72
type dtype_spec = {
blanchet@33192
    73
  typ: typ,
blanchet@33192
    74
  card: int,
blanchet@33192
    75
  co: bool,
blanchet@35179
    76
  standard: bool,
blanchet@35385
    77
  complete: bool * bool,
blanchet@35385
    78
  concrete: bool * bool,
blanchet@34969
    79
  deep: bool,
blanchet@33192
    80
  constrs: constr_spec list}
blanchet@33192
    81
blanchet@33192
    82
type scope = {
blanchet@35067
    83
  hol_ctxt: hol_context,
blanchet@35190
    84
  binarize: bool,
blanchet@33192
    85
  card_assigns: (typ * int) list,
blanchet@34121
    86
  bits: int,
blanchet@33192
    87
  bisim_depth: int,
blanchet@33192
    88
  datatypes: dtype_spec list,
blanchet@33192
    89
  ofs: int Typtab.table}
blanchet@33192
    90
blanchet@33192
    91
datatype row_kind = Card of typ | Max of styp
blanchet@33192
    92
blanchet@33192
    93
type row = row_kind * int list
blanchet@33192
    94
type block = row list
blanchet@33192
    95
blanchet@33192
    96
(* dtype_spec list -> typ -> dtype_spec option *)
blanchet@33192
    97
fun datatype_spec (dtypes : dtype_spec list) T =
blanchet@34118
    98
  List.find (curry (op =) T o #typ) dtypes
blanchet@33192
    99
blanchet@33192
   100
(* dtype_spec list -> styp -> constr_spec *)
blanchet@33224
   101
fun constr_spec [] x = raise TERM ("Nitpick_Scope.constr_spec", [Const x])
blanchet@33192
   102
  | constr_spec ({constrs, ...} :: dtypes : dtype_spec list) (x as (s, T)) =
blanchet@34118
   103
    case List.find (curry (op =) (s, body_type T) o (apsnd body_type o #const))
blanchet@33192
   104
                   constrs of
blanchet@33192
   105
      SOME c => c
blanchet@33192
   106
    | NONE => constr_spec dtypes x
blanchet@33192
   107
blanchet@35385
   108
(* dtype_spec list -> bool -> typ -> bool *)
blanchet@35385
   109
fun is_complete_type dtypes facto (Type ("fun", [T1, T2])) =
blanchet@35385
   110
    is_concrete_type dtypes facto T1 andalso is_complete_type dtypes facto T2
blanchet@35385
   111
  | is_complete_type dtypes facto (Type ("*", Ts)) =
blanchet@35385
   112
    forall (is_complete_type dtypes facto) Ts
blanchet@35385
   113
  | is_complete_type dtypes facto T =
blanchet@35220
   114
    not (is_integer_like_type T) andalso not (is_bit_type T) andalso
blanchet@35385
   115
    fun_from_pair (#complete (the (datatype_spec dtypes T))) facto
blanchet@33192
   116
    handle Option.Option => true
blanchet@35385
   117
and is_concrete_type dtypes facto (Type ("fun", [T1, T2])) =
blanchet@35385
   118
    is_complete_type dtypes facto T1 andalso is_concrete_type dtypes facto T2
blanchet@35385
   119
  | is_concrete_type dtypes facto (Type ("*", Ts)) =
blanchet@35385
   120
    forall (is_concrete_type dtypes facto) Ts
blanchet@35385
   121
  | is_concrete_type dtypes facto T =
blanchet@35385
   122
    fun_from_pair (#concrete (the (datatype_spec dtypes T))) facto
blanchet@35385
   123
    handle Option.Option => true
blanchet@35385
   124
fun is_exact_type dtypes facto =
blanchet@35385
   125
  is_complete_type dtypes facto andf is_concrete_type dtypes facto
blanchet@33192
   126
blanchet@33192
   127
(* int Typtab.table -> typ -> int *)
blanchet@33192
   128
fun offset_of_type ofs T =
blanchet@33192
   129
  case Typtab.lookup ofs T of
blanchet@33192
   130
    SOME j0 => j0
blanchet@33192
   131
  | NONE => Typtab.lookup ofs dummyT |> the_default 0
blanchet@33192
   132
blanchet@33192
   133
(* scope -> typ -> int * int *)
blanchet@33192
   134
fun spec_of_type ({card_assigns, ofs, ...} : scope) T =
blanchet@33192
   135
  (card_of_type card_assigns T
blanchet@33224
   136
   handle TYPE ("Nitpick_HOL.card_of_type", _, _) => ~1, offset_of_type ofs T)
blanchet@33192
   137
blanchet@33192
   138
(* (string -> string) -> scope
blanchet@33192
   139
   -> string list * string list * string list * string list * string list *)
blanchet@35220
   140
fun quintuple_for_scope quote
blanchet@35280
   141
        ({hol_ctxt = {thy, ctxt, stds, ...}, card_assigns, bits, bisim_depth,
blanchet@35220
   142
         datatypes, ...} : scope) =
blanchet@33192
   143
  let
blanchet@35072
   144
    val boring_Ts = [@{typ unsigned_bit}, @{typ signed_bit},
blanchet@34121
   145
                     @{typ bisim_iterator}]
blanchet@34120
   146
    val (iter_assigns, card_assigns) =
blanchet@34121
   147
      card_assigns |> filter_out (member (op =) boring_Ts o fst)
blanchet@33192
   148
                   |> List.partition (is_fp_iterator_type o fst)
blanchet@34121
   149
    val (secondary_card_assigns, primary_card_assigns) =
blanchet@35220
   150
      card_assigns |> List.partition ((is_integer_type orf is_datatype thy stds)
blanchet@34121
   151
                                      o fst)
blanchet@33192
   152
    val cards =
blanchet@33192
   153
      map (fn (T, k) => quote (string_for_type ctxt T) ^ " = " ^
blanchet@33192
   154
                        string_of_int k)
blanchet@33192
   155
    fun maxes () =
blanchet@33192
   156
      maps (map_filter
blanchet@33192
   157
                (fn {const, explicit_max, ...} =>
blanchet@33192
   158
                    if explicit_max < 0 then
blanchet@33192
   159
                      NONE
blanchet@33192
   160
                    else
blanchet@33192
   161
                      SOME (Syntax.string_of_term ctxt (Const const) ^ " = " ^
blanchet@33192
   162
                            string_of_int explicit_max))
blanchet@33192
   163
                 o #constrs) datatypes
blanchet@33192
   164
    fun iters () =
blanchet@33192
   165
      map (fn (T, k) =>
blanchet@33192
   166
              quote (Syntax.string_of_term ctxt
blanchet@33192
   167
                         (Const (const_for_iterator_type T))) ^ " = " ^
blanchet@34120
   168
              string_of_int (k - 1)) iter_assigns
blanchet@34121
   169
    fun miscs () =
blanchet@34121
   170
      (if bits = 0 then [] else ["bits = " ^ string_of_int bits]) @
blanchet@34121
   171
      (if bisim_depth < 0 andalso forall (not o #co) datatypes then []
blanchet@35178
   172
       else ["bisim_depth = " ^ signed_string_of_int bisim_depth])
blanchet@33192
   173
  in
blanchet@33192
   174
    setmp_show_all_types
blanchet@34121
   175
        (fn () => (cards primary_card_assigns, cards secondary_card_assigns,
blanchet@34121
   176
                   maxes (), iters (), miscs ())) ()
blanchet@33192
   177
  end
blanchet@33192
   178
blanchet@33192
   179
(* scope -> bool -> Pretty.T list *)
blanchet@33192
   180
fun pretties_for_scope scope verbose =
blanchet@33192
   181
  let
blanchet@34121
   182
    val (primary_cards, secondary_cards, maxes, iters, bisim_depths) =
blanchet@33192
   183
      quintuple_for_scope maybe_quote scope
blanchet@34121
   184
    val ss = map (prefix "card ") primary_cards @
blanchet@33192
   185
             (if verbose then
blanchet@34121
   186
                map (prefix "card ") secondary_cards @
blanchet@33192
   187
                map (prefix "max ") maxes @
blanchet@33192
   188
                map (prefix "iter ") iters @
blanchet@33192
   189
                bisim_depths
blanchet@33192
   190
              else
blanchet@33192
   191
                [])
blanchet@33192
   192
  in
blanchet@33192
   193
    if null ss then []
blanchet@33192
   194
    else serial_commas "and" ss |> map Pretty.str |> Pretty.breaks
blanchet@33192
   195
  end
blanchet@33192
   196
blanchet@33192
   197
(* scope -> string *)
blanchet@33192
   198
fun multiline_string_for_scope scope =
blanchet@33192
   199
  let
blanchet@34121
   200
    val (primary_cards, secondary_cards, maxes, iters, bisim_depths) =
blanchet@33192
   201
      quintuple_for_scope I scope
blanchet@34121
   202
    val cards = primary_cards @ secondary_cards
blanchet@33192
   203
  in
blanchet@33192
   204
    case (if null cards then [] else ["card: " ^ commas cards]) @
blanchet@33192
   205
         (if null maxes then [] else ["max: " ^ commas maxes]) @
blanchet@33192
   206
         (if null iters then [] else ["iter: " ^ commas iters]) @
blanchet@33192
   207
         bisim_depths of
blanchet@33192
   208
      [] => "empty"
blanchet@33192
   209
    | lines => space_implode "\n" lines
blanchet@33192
   210
  end
blanchet@33192
   211
blanchet@33192
   212
(* scope -> scope -> bool *)
blanchet@33192
   213
fun scopes_equivalent (s1 : scope) (s2 : scope) =
blanchet@33192
   214
  #datatypes s1 = #datatypes s2 andalso #card_assigns s1 = #card_assigns s2
blanchet@33192
   215
fun scope_less_eq (s1 : scope) (s2 : scope) =
blanchet@33192
   216
  (s1, s2) |> pairself (map snd o #card_assigns) |> op ~~ |> forall (op <=)
blanchet@33192
   217
blanchet@33192
   218
(* row -> int *)
blanchet@33192
   219
fun rank_of_row (_, ks) = length ks
blanchet@33192
   220
(* block -> int *)
blanchet@33192
   221
fun rank_of_block block = fold Integer.max (map rank_of_row block) 1
blanchet@33192
   222
(* int -> typ * int list -> typ * int list *)
blanchet@33192
   223
fun project_row column (y, ks) = (y, [nth ks (Int.min (column, length ks - 1))])
blanchet@33192
   224
(* int -> block -> block *)
blanchet@33192
   225
fun project_block (column, block) = map (project_row column) block
blanchet@33192
   226
blanchet@33192
   227
(* (''a * ''a -> bool) -> (''a option * int list) list -> ''a -> int list *)
blanchet@34120
   228
fun lookup_ints_assign eq assigns key =
blanchet@34120
   229
  case triple_lookup eq assigns key of
blanchet@33192
   230
    SOME ks => ks
blanchet@33224
   231
  | NONE => raise ARG ("Nitpick_Scope.lookup_ints_assign", "")
blanchet@33192
   232
(* theory -> (typ option * int list) list -> typ -> int list *)
blanchet@34120
   233
fun lookup_type_ints_assign thy assigns T =
blanchet@34120
   234
  map (curry Int.max 1) (lookup_ints_assign (type_match thy) assigns T)
blanchet@33224
   235
  handle ARG ("Nitpick_Scope.lookup_ints_assign", _) =>
blanchet@33224
   236
         raise TYPE ("Nitpick_Scope.lookup_type_ints_assign", [T], [])
blanchet@33192
   237
(* theory -> (styp option * int list) list -> styp -> int list *)
blanchet@34120
   238
fun lookup_const_ints_assign thy assigns x =
blanchet@34120
   239
  lookup_ints_assign (const_match thy) assigns x
blanchet@33224
   240
  handle ARG ("Nitpick_Scope.lookup_ints_assign", _) =>
blanchet@33224
   241
         raise TERM ("Nitpick_Scope.lookup_const_ints_assign", [Const x])
blanchet@33192
   242
blanchet@33192
   243
(* theory -> (styp option * int list) list -> styp -> row option *)
blanchet@34120
   244
fun row_for_constr thy maxes_assigns constr =
blanchet@34120
   245
  SOME (Max constr, lookup_const_ints_assign thy maxes_assigns constr)
blanchet@33192
   246
  handle TERM ("lookup_const_ints_assign", _) => NONE
blanchet@33192
   247
blanchet@34121
   248
val max_bits = 31 (* Kodkod limit *)
blanchet@34121
   249
blanchet@35190
   250
(* hol_context -> bool -> (typ option * int list) list
blanchet@35190
   251
   -> (styp option * int list) list -> (styp option * int list) list -> int list
blanchet@35190
   252
   -> int list -> typ -> block *)
blanchet@35190
   253
fun block_for_type (hol_ctxt as {thy, ...}) binarize cards_assigns maxes_assigns
blanchet@34121
   254
                   iters_assigns bitss bisim_depths T =
blanchet@34121
   255
  if T = @{typ unsigned_bit} then
blanchet@34121
   256
    [(Card T, map (Integer.min max_bits o Integer.max 1) bitss)]
blanchet@34121
   257
  else if T = @{typ signed_bit} then
blanchet@34121
   258
    [(Card T, map (Integer.add 1 o Integer.min max_bits o Integer.max 1) bitss)]
blanchet@34123
   259
  else if T = @{typ "unsigned_bit word"} then
blanchet@34123
   260
    [(Card T, lookup_type_ints_assign thy cards_assigns nat_T)]
blanchet@34123
   261
  else if T = @{typ "signed_bit word"} then
blanchet@34123
   262
    [(Card T, lookup_type_ints_assign thy cards_assigns int_T)]
blanchet@34121
   263
  else if T = @{typ bisim_iterator} then
blanchet@34121
   264
    [(Card T, map (Integer.add 1 o Integer.max 0) bisim_depths)]
blanchet@33571
   265
  else if is_fp_iterator_type T then
blanchet@34121
   266
    [(Card T, map (Integer.add 1 o Integer.max 0)
blanchet@34120
   267
                  (lookup_const_ints_assign thy iters_assigns
blanchet@33571
   268
                                            (const_for_iterator_type T)))]
blanchet@33571
   269
  else
blanchet@34120
   270
    (Card T, lookup_type_ints_assign thy cards_assigns T) ::
blanchet@35190
   271
    (case binarized_and_boxed_datatype_constrs hol_ctxt binarize T of
blanchet@33571
   272
       [_] => []
blanchet@34120
   273
     | constrs => map_filter (row_for_constr thy maxes_assigns) constrs)
blanchet@33192
   274
blanchet@35190
   275
(* hol_context -> bool -> (typ option * int list) list
blanchet@35190
   276
   -> (styp option * int list) list -> (styp option * int list) list -> int list
blanchet@35190
   277
   -> int list -> typ list -> typ list -> block list *)
blanchet@35190
   278
fun blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns iters_assigns
blanchet@35190
   279
                     bitss bisim_depths mono_Ts nonmono_Ts =
blanchet@33192
   280
  let
blanchet@33192
   281
    (* typ -> block *)
blanchet@35190
   282
    val block_for = block_for_type hol_ctxt binarize cards_assigns maxes_assigns
blanchet@34121
   283
                                   iters_assigns bitss bisim_depths
blanchet@33192
   284
    val mono_block = maps block_for mono_Ts
blanchet@33192
   285
    val nonmono_blocks = map block_for nonmono_Ts
blanchet@33192
   286
  in mono_block :: nonmono_blocks end
blanchet@33192
   287
blanchet@33192
   288
val sync_threshold = 5
blanchet@33192
   289
blanchet@33192
   290
(* int list -> int list list *)
blanchet@33192
   291
fun all_combinations_ordered_smartly ks =
blanchet@33192
   292
  let
blanchet@33192
   293
    (* int list -> int *)
blanchet@33192
   294
    fun cost_with_monos [] = 0
blanchet@33192
   295
      | cost_with_monos (k :: ks) =
blanchet@34118
   296
        if k < sync_threshold andalso forall (curry (op =) k) ks then
blanchet@33192
   297
          k - sync_threshold
blanchet@33192
   298
        else
blanchet@33192
   299
          k * (k + 1) div 2 + Integer.sum ks
blanchet@33192
   300
    fun cost_without_monos [] = 0
blanchet@33192
   301
      | cost_without_monos [k] = k
blanchet@33192
   302
      | cost_without_monos (_ :: k :: ks) =
blanchet@34118
   303
        if k < sync_threshold andalso forall (curry (op =) k) ks then
blanchet@33192
   304
          k - sync_threshold
blanchet@33192
   305
        else
blanchet@33192
   306
          Integer.sum (k :: ks)
blanchet@33192
   307
  in
blanchet@33192
   308
    ks |> all_combinations
blanchet@33192
   309
       |> map (`(if fst (hd ks) > 1 then cost_with_monos
blanchet@33192
   310
                 else cost_without_monos))
blanchet@33192
   311
       |> sort (int_ord o pairself fst) |> map snd
blanchet@33192
   312
  end
blanchet@33192
   313
blanchet@33192
   314
(* typ -> bool *)
blanchet@33192
   315
fun is_self_recursive_constr_type T =
blanchet@34118
   316
  exists (exists_subtype (curry (op =) (body_type T))) (binder_types T)
blanchet@33192
   317
blanchet@33192
   318
(* (styp * int) list -> styp -> int *)
blanchet@33192
   319
fun constr_max maxes x = the_default ~1 (AList.lookup (op =) maxes x)
blanchet@33192
   320
blanchet@33192
   321
type scope_desc = (typ * int) list * (styp * int) list
blanchet@33192
   322
blanchet@35190
   323
(* hol_context -> bool -> scope_desc -> typ * int -> bool *)
blanchet@35190
   324
fun is_surely_inconsistent_card_assign hol_ctxt binarize
blanchet@35190
   325
                                       (card_assigns, max_assigns) (T, k) =
blanchet@35190
   326
  case binarized_and_boxed_datatype_constrs hol_ctxt binarize T of
blanchet@33192
   327
    [] => false
blanchet@33192
   328
  | xs =>
blanchet@33192
   329
    let
blanchet@34123
   330
      val dom_cards =
blanchet@34123
   331
        map (Integer.prod o map (bounded_card_of_type k ~1 card_assigns)
blanchet@33192
   332
             o binder_types o snd) xs
blanchet@34120
   333
      val maxes = map (constr_max max_assigns) xs
blanchet@33192
   334
      (* int -> int -> int *)
blanchet@34123
   335
      fun effective_max card ~1 = card
blanchet@33192
   336
        | effective_max card max = Int.min (card, max)
blanchet@34123
   337
      val max = map2 effective_max dom_cards maxes |> Integer.sum
blanchet@34123
   338
    in max < k end
blanchet@35190
   339
(* hol_context -> bool -> (typ * int) list -> (typ * int) list
blanchet@35190
   340
   -> (styp * int) list -> bool *)
blanchet@35190
   341
fun is_surely_inconsistent_scope_description hol_ctxt binarize seen rest
blanchet@35190
   342
                                             max_assigns =
blanchet@35190
   343
  exists (is_surely_inconsistent_card_assign hol_ctxt binarize
blanchet@34123
   344
                                             (seen @ rest, max_assigns)) seen
blanchet@33192
   345
blanchet@35190
   346
(* hol_context -> bool -> scope_desc -> (typ * int) list option *)
blanchet@35190
   347
fun repair_card_assigns hol_ctxt binarize (card_assigns, max_assigns) =
blanchet@33192
   348
  let
blanchet@33192
   349
    (* (typ * int) list -> (typ * int) list -> (typ * int) list option *)
blanchet@33192
   350
    fun aux seen [] = SOME seen
blanchet@35280
   351
      | aux _ ((_, 0) :: _) = NONE
blanchet@34123
   352
      | aux seen ((T, k) :: rest) =
blanchet@35190
   353
        (if is_surely_inconsistent_scope_description hol_ctxt binarize
blanchet@35190
   354
                ((T, k) :: seen) rest max_assigns then
blanchet@33192
   355
           raise SAME ()
blanchet@33192
   356
         else
blanchet@34123
   357
           case aux ((T, k) :: seen) rest of
blanchet@34120
   358
             SOME assigns => SOME assigns
blanchet@33192
   359
           | NONE => raise SAME ())
blanchet@34123
   360
        handle SAME () => aux seen ((T, k - 1) :: rest)
blanchet@34120
   361
  in aux [] (rev card_assigns) end
blanchet@33192
   362
blanchet@33192
   363
(* theory -> (typ * int) list -> typ * int -> typ * int *)
blanchet@35280
   364
fun repair_iterator_assign thy assigns (T as Type (_, Ts), k) =
blanchet@33192
   365
    (T, if T = @{typ bisim_iterator} then
blanchet@34120
   366
          let
blanchet@34120
   367
            val co_cards = map snd (filter (is_codatatype thy o fst) assigns)
blanchet@34120
   368
          in Int.min (k, Integer.sum co_cards) end
blanchet@33192
   369
        else if is_fp_iterator_type T then
blanchet@33192
   370
          case Ts of
blanchet@33192
   371
            [] => 1
blanchet@34120
   372
          | _ => bounded_card_of_type k ~1 assigns (foldr1 HOLogic.mk_prodT Ts)
blanchet@33192
   373
        else
blanchet@33192
   374
          k)
blanchet@34120
   375
  | repair_iterator_assign _ _ assign = assign
blanchet@33192
   376
blanchet@33192
   377
(* row -> scope_desc -> scope_desc *)
blanchet@34120
   378
fun add_row_to_scope_descriptor (kind, ks) (card_assigns, max_assigns) =
blanchet@33192
   379
  case kind of
blanchet@34120
   380
    Card T => ((T, the_single ks) :: card_assigns, max_assigns)
blanchet@34120
   381
  | Max x => (card_assigns, (x, the_single ks) :: max_assigns)
blanchet@33192
   382
(* block -> scope_desc *)
blanchet@33192
   383
fun scope_descriptor_from_block block =
blanchet@33192
   384
  fold_rev add_row_to_scope_descriptor block ([], [])
blanchet@35190
   385
(* hol_context -> bool -> block list -> int list -> scope_desc option *)
blanchet@35190
   386
fun scope_descriptor_from_combination (hol_ctxt as {thy, ...}) binarize blocks
blanchet@35190
   387
                                      columns =
blanchet@33192
   388
  let
blanchet@34120
   389
    val (card_assigns, max_assigns) =
blanchet@33192
   390
      maps project_block (columns ~~ blocks) |> scope_descriptor_from_block
blanchet@35190
   391
    val card_assigns =
blanchet@35190
   392
      repair_card_assigns hol_ctxt binarize (card_assigns, max_assigns) |> the
blanchet@33192
   393
  in
blanchet@34120
   394
    SOME (map (repair_iterator_assign thy card_assigns) card_assigns,
blanchet@34120
   395
          max_assigns)
blanchet@33192
   396
  end
blanchet@33192
   397
  handle Option.Option => NONE
blanchet@33192
   398
blanchet@35280
   399
(* (typ * int) list -> dtype_spec list -> int Typtab.table *)
blanchet@35280
   400
fun offset_table_for_card_assigns assigns dtypes =
blanchet@33192
   401
  let
blanchet@33192
   402
    (* int -> (int * int) list -> (typ * int) list -> int Typtab.table
blanchet@33192
   403
       -> int Typtab.table *)
blanchet@33192
   404
    fun aux next _ [] = Typtab.update_new (dummyT, next)
blanchet@34120
   405
      | aux next reusable ((T, k) :: assigns) =
blanchet@35280
   406
        if k = 1 orelse is_iterator_type T orelse is_integer_type T
blanchet@35280
   407
           orelse is_bit_type T then
blanchet@34120
   408
          aux next reusable assigns
blanchet@33192
   409
        else if length (these (Option.map #constrs (datatype_spec dtypes T)))
blanchet@33192
   410
                > 1 then
blanchet@34120
   411
          Typtab.update_new (T, next) #> aux (next + k) reusable assigns
blanchet@33192
   412
        else
blanchet@33192
   413
          case AList.lookup (op =) reusable k of
blanchet@34120
   414
            SOME j0 => Typtab.update_new (T, j0) #> aux next reusable assigns
blanchet@33192
   415
          | NONE => Typtab.update_new (T, next)
blanchet@34120
   416
                    #> aux (next + k) ((k, next) :: reusable) assigns
blanchet@34120
   417
  in aux 0 [] assigns Typtab.empty end
blanchet@33192
   418
blanchet@33192
   419
(* int -> (typ * int) list -> typ -> int *)
blanchet@34120
   420
fun domain_card max card_assigns =
blanchet@34120
   421
  Integer.prod o map (bounded_card_of_type max max card_assigns) o binder_types
blanchet@33192
   422
blanchet@33192
   423
(* scope_desc -> bool -> int -> (int -> int) -> int -> int -> bool * styp
blanchet@33192
   424
   -> constr_spec list -> constr_spec list *)
blanchet@34120
   425
fun add_constr_spec (card_assigns, max_assigns) co card sum_dom_cards
blanchet@35280
   426
                    num_self_recs num_non_self_recs (self_rec, x as (_, T))
blanchet@34120
   427
                    constrs =
blanchet@33192
   428
  let
blanchet@34120
   429
    val max = constr_max max_assigns x
blanchet@33192
   430
    (* unit -> int *)
blanchet@33192
   431
    fun next_delta () = if null constrs then 0 else #epsilon (hd constrs)
blanchet@33192
   432
    val {delta, epsilon, exclusive, total} =
blanchet@33192
   433
      if max = 0 then
blanchet@33192
   434
        let val delta = next_delta () in
blanchet@33192
   435
          {delta = delta, epsilon = delta, exclusive = true, total = false}
blanchet@33192
   436
        end
blanchet@33192
   437
      else if not co andalso num_self_recs > 0 then
blanchet@35069
   438
        (if num_self_recs = 1 andalso num_non_self_recs = 1 then
blanchet@35069
   439
           if self_rec then
blanchet@35069
   440
             case constrs of
blanchet@35069
   441
               [{delta = 0, epsilon = 1, exclusive = true, ...}] =>
blanchet@35069
   442
               {delta = 1, epsilon = card, exclusive = true, total = false}
blanchet@35069
   443
             | _ => raise SAME ()
blanchet@35069
   444
           else
blanchet@35069
   445
             if domain_card 2 card_assigns T = 1 then
blanchet@35069
   446
               {delta = 0, epsilon = 1, exclusive = true, total = true}
blanchet@35069
   447
             else
blanchet@35069
   448
               raise SAME ()
blanchet@35069
   449
         else
blanchet@35069
   450
           raise SAME ())
blanchet@35069
   451
        handle SAME () =>
blanchet@35069
   452
               {delta = 0, epsilon = card, exclusive = false, total = false}
blanchet@33192
   453
      else if card = sum_dom_cards (card + 1) then
blanchet@33192
   454
        let val delta = next_delta () in
blanchet@34120
   455
          {delta = delta, epsilon = delta + domain_card card card_assigns T,
blanchet@33192
   456
           exclusive = true, total = true}
blanchet@33192
   457
        end
blanchet@33192
   458
      else
blanchet@33192
   459
        {delta = 0, epsilon = card,
blanchet@33192
   460
         exclusive = (num_self_recs + num_non_self_recs = 1), total = false}
blanchet@33192
   461
  in
blanchet@33192
   462
    {const = x, delta = delta, epsilon = epsilon, exclusive = exclusive,
blanchet@33192
   463
     explicit_max = max, total = total} :: constrs
blanchet@33192
   464
  end
blanchet@33192
   465
blanchet@35385
   466
(* hol_context -> bool -> typ list -> (typ * int) list -> typ -> bool *)
blanchet@35385
   467
fun has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T =
blanchet@34120
   468
  let val card = card_of_type card_assigns T in
blanchet@35385
   469
    card = bounded_exact_card_of_type hol_ctxt
blanchet@35385
   470
               (if facto then finitizable_dataTs else []) (card + 1) 0
blanchet@35385
   471
               card_assigns T
blanchet@34120
   472
  end
blanchet@34120
   473
blanchet@35385
   474
(* hol_context -> bool -> typ list -> typ list -> scope_desc -> typ * int
blanchet@35385
   475
   -> dtype_spec *)
blanchet@35220
   476
fun datatype_spec_from_scope_descriptor (hol_ctxt as {thy, stds, ...}) binarize
blanchet@35385
   477
        deep_dataTs finitizable_dataTs (desc as (card_assigns, _)) (T, card) =
blanchet@33192
   478
  let
blanchet@34969
   479
    val deep = member (op =) deep_dataTs T
blanchet@33192
   480
    val co = is_codatatype thy T
blanchet@35220
   481
    val standard = is_standard_datatype thy stds T
blanchet@35190
   482
    val xs = binarized_and_boxed_datatype_constrs hol_ctxt binarize T
blanchet@33192
   483
    val self_recs = map (is_self_recursive_constr_type o snd) xs
blanchet@33192
   484
    val (num_self_recs, num_non_self_recs) =
blanchet@34120
   485
      List.partition I self_recs |> pairself length
blanchet@35385
   486
    (* bool -> bool *)
blanchet@35385
   487
    fun is_complete facto =
blanchet@35385
   488
      has_exact_card hol_ctxt facto finitizable_dataTs card_assigns T
blanchet@35385
   489
    fun is_concrete facto =
blanchet@35385
   490
      is_word_type T orelse
blanchet@35385
   491
      xs |> maps (binder_types o snd) |> maps binder_types
blanchet@35385
   492
         |> forall (has_exact_card hol_ctxt facto finitizable_dataTs
blanchet@35385
   493
                                   card_assigns)
blanchet@35385
   494
    val complete = pair_from_fun is_complete
blanchet@35385
   495
    val concrete = pair_from_fun is_concrete
blanchet@33192
   496
    (* int -> int *)
blanchet@33192
   497
    fun sum_dom_cards max =
blanchet@34120
   498
      map (domain_card max card_assigns o snd) xs |> Integer.sum
blanchet@33192
   499
    val constrs =
blanchet@33192
   500
      fold_rev (add_constr_spec desc co card sum_dom_cards num_self_recs
blanchet@35069
   501
                                num_non_self_recs)
blanchet@35069
   502
               (sort (bool_ord o swap o pairself fst) (self_recs ~~ xs)) []
blanchet@33549
   503
  in
blanchet@35179
   504
    {typ = T, card = card, co = co, standard = standard, complete = complete,
blanchet@35179
   505
     concrete = concrete, deep = deep, constrs = constrs}
blanchet@33549
   506
  end
blanchet@33192
   507
blanchet@35385
   508
(* hol_context -> bool -> int -> typ list -> typ list -> scope_desc -> scope *)
blanchet@35220
   509
fun scope_from_descriptor (hol_ctxt as {thy, stds, ...}) binarize sym_break
blanchet@35385
   510
                          deep_dataTs finitizable_dataTs
blanchet@35385
   511
                          (desc as (card_assigns, _)) =
blanchet@33192
   512
  let
blanchet@33549
   513
    val datatypes =
blanchet@35190
   514
      map (datatype_spec_from_scope_descriptor hol_ctxt binarize deep_dataTs
blanchet@35385
   515
                                               finitizable_dataTs desc)
blanchet@35385
   516
          (filter (is_datatype thy stds o fst) card_assigns)
blanchet@34121
   517
    val bits = card_of_type card_assigns @{typ signed_bit} - 1
blanchet@34121
   518
               handle TYPE ("Nitpick_HOL.card_of_type", _, _) =>
blanchet@34121
   519
                      card_of_type card_assigns @{typ unsigned_bit}
blanchet@34121
   520
                      handle TYPE ("Nitpick_HOL.card_of_type", _, _) => 0
blanchet@34120
   521
    val bisim_depth = card_of_type card_assigns @{typ bisim_iterator} - 1
blanchet@33192
   522
  in
blanchet@35190
   523
    {hol_ctxt = hol_ctxt, binarize = binarize, card_assigns = card_assigns,
blanchet@35190
   524
     datatypes = datatypes, bits = bits, bisim_depth = bisim_depth,
blanchet@33192
   525
     ofs = if sym_break <= 0 then Typtab.empty
blanchet@35280
   526
           else offset_table_for_card_assigns card_assigns datatypes}
blanchet@33192
   527
  end
blanchet@33192
   528
blanchet@33192
   529
(* theory -> typ list -> (typ option * int list) list
blanchet@33192
   530
   -> (typ option * int list) list *)
blanchet@34121
   531
fun repair_cards_assigns_wrt_boxing _ _ [] = []
blanchet@34121
   532
  | repair_cards_assigns_wrt_boxing thy Ts ((SOME T, ks) :: cards_assigns) =
blanchet@33192
   533
    (if is_fun_type T orelse is_pair_type T then
blanchet@35190
   534
       Ts |> filter (curry (type_match thy o swap) T o unarize_and_unbox_type)
blanchet@33192
   535
          |> map (rpair ks o SOME)
blanchet@33192
   536
     else
blanchet@34121
   537
       [(SOME T, ks)]) @ repair_cards_assigns_wrt_boxing thy Ts cards_assigns
blanchet@34121
   538
  | repair_cards_assigns_wrt_boxing thy Ts ((NONE, ks) :: cards_assigns) =
blanchet@34121
   539
    (NONE, ks) :: repair_cards_assigns_wrt_boxing thy Ts cards_assigns
blanchet@33192
   540
blanchet@33571
   541
val max_scopes = 4096
blanchet@33192
   542
val distinct_threshold = 512
blanchet@33192
   543
blanchet@35190
   544
(* hol_context -> bool -> int -> (typ option * int list) list
blanchet@33192
   545
   -> (styp option * int list) list -> (styp option * int list) list -> int list
blanchet@35385
   546
   -> typ list -> typ list -> typ list ->typ list -> int * scope list *)
blanchet@35190
   547
fun all_scopes (hol_ctxt as {thy, ...}) binarize sym_break cards_assigns
blanchet@35190
   548
               maxes_assigns iters_assigns bitss bisim_depths mono_Ts nonmono_Ts
blanchet@35385
   549
               deep_dataTs finitizable_dataTs =
blanchet@33192
   550
  let
blanchet@34121
   551
    val cards_assigns = repair_cards_assigns_wrt_boxing thy mono_Ts
blanchet@34121
   552
                                                        cards_assigns
blanchet@35190
   553
    val blocks = blocks_for_types hol_ctxt binarize cards_assigns maxes_assigns
blanchet@34121
   554
                                  iters_assigns bitss bisim_depths mono_Ts
blanchet@34121
   555
                                  nonmono_Ts
blanchet@33192
   556
    val ranks = map rank_of_block blocks
blanchet@33571
   557
    val all = all_combinations_ordered_smartly (map (rpair 0) ranks)
haftmann@33956
   558
    val head = take max_scopes all
blanchet@35190
   559
    val descs =
blanchet@35190
   560
      map_filter (scope_descriptor_from_combination hol_ctxt binarize blocks)
blanchet@35190
   561
                 head
blanchet@33192
   562
  in
blanchet@33571
   563
    (length all - length head,
blanchet@33571
   564
     descs |> length descs <= distinct_threshold ? distinct (op =)
blanchet@35190
   565
           |> map (scope_from_descriptor hol_ctxt binarize sym_break
blanchet@35385
   566
                                         deep_dataTs finitizable_dataTs))
blanchet@33192
   567
  end
blanchet@33192
   568
blanchet@33192
   569
end;