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