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