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