
    i#                       % S SK r S SKJr  S SKJrJr  S SKJr  S SKJ	r	J
r
JrJrJr  S SKJrJrJrJrJrJrJr  S SKJrJrJrJrJrJr  S SKJrJrJ r J!r!  S S	K"J#r#J$r$J%r%J&r&J'r'  S S
K(J)r)J*r*J+r+  S SK,J-r-  \	(       a  S SK.J/r/  S SK0J1r1  \\\-  /\24   r3\\4S'   S\\   S\5\6\5\6S4   4   4S jr7\" SSS9 " S S5      5       r8 " S S\95      r: " S S5      r;g)    N)defaultdict)CallableSequence)	dataclass)TYPE_CHECKINGAnyLiteral	TypeAliascast)
ChoiceNodeChoiceTchoice_equalchoice_from_index
choice_keychoice_permittedchoice_to_index)ConjectureDataConjectureResultSpansStatus_Overrundraw_choice)endswithfind_integerreplace_all
startswith)BytesFloatIntegerOrderingString)
ChoiceTreeprefix_selection_orderrandom_selection_order)MAX_PRECISE_INTEGER)Random)ConjectureRunnerShrinkPredicateTnodesreturn.c                 <    [        U 5      [        S U  5       5      4$ )a5  Returns a sort key such that "simpler" choice sequences are smaller than
"more complicated" ones.

We define sort_key so that x is simpler than y if x is shorter than y or if
they have the same length and map(choice_to_index, x) < map(choice_to_index, y).

The reason for using this ordering is:

1. If x is shorter than y then that means we had to make fewer decisions
   in constructing the test case when we ran x than we did when we ran y.
2. If x is the same length as y then replacing a choice with a lower index
   choice corresponds to replacing it with a simpler/smaller choice.
3. Because choices drawn early in generation potentially get used in more
   places they potentially have a more significant impact on the final
   result, so it makes sense to prioritise reducing earlier choices over
   later ones.
c              3   b   #    U  H%  n[        UR                  UR                  5      v   M'     g 7fN)r   valueconstraints.0nodes     f/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/hypothesis/internal/conjecture/shrinker.py	<genexpr>sort_key.<locals>.<genexpr>X   s$     Nodjj$*:*:;;s   -/)lentuple)r)   s    r3   sort_keyr8   D   s#    & 	E
NNN     TF)slotsfrozenc                       \ rS rSr% \\S'   Sr\S-  \S'   Sr\\S'   Sr	\
\S'   Sr\
\S	'   Sr\
\S
'   Sr\
\S'   S rS rSrg)
ShrinkPass\   functionNname last_prefixr   calls
misalignedshrinks	deletionsc                 V    U R                   c  U R                  R                  U l         g g r-   )r@   r?   __name__selfs    r3   __post_init__ShrinkPass.__post_init__h   s"    99..DI r9   c                 ,    [        U R                  5      $ r-   )hashr@   rI   s    r3   __hash__ShrinkPass.__hash__l   s    DIIr9   r@   )rH   
__module____qualname____firstlineno__r   __annotations__r@   strrB   rC   intrD   rE   rF   rK   rO   __static_attributes__rA   r9   r3   r=   r=   \   sR    MD#*K E3NJGSIs/r9   r=   c                       \ rS rSrSrg)StopShrinkingp   rA   N)rH   rR   rS   rT   rX   rA   r9   r3   rZ   rZ   p   s    r9   rZ   c                      \ rS rSrSrS rSS.SSS\\-  S	\S
-  S\	\\-  \/\
4   S
-  S\
S\
4S jjr\S 5       rS r\S\4S j5       r\S\4S j5       rSFS jrS\\   S\\
\\-  S
-  4   4S jrS\\   S\
4S jrS rS\SS
4S jr\SGS j5       rSFS jrSFS jrSFS jrS r \S  5       r!S! r"S" r#\S\$\%\&4   4S# j5       r'SS$.S%\(S&\
S\
4S' jjr)S(\*\(   SS
4S) jr+\S\\S*4   4S+ j5       r,\S\\-S*4   4S, j5       r.\S\/4S- j5       r0\S. 5       r1\S/ 5       r2S0 r3S1 r4S2 r5S3 r6\S\7\   4S4 j5       r8S5 r9S6 r:S7 r;\S8 5       r<S9\S\(4S: jr=S; r>S< r?S= r@S> rAS? rBS@ rCSA rDSB rESC rFSHSD jrGSErHg
)IShrinkert   a  A shrinker is a child object of a ConjectureRunner which is designed to
manage the associated state of a particular shrink problem. That is, we
have some initial ConjectureData object and some property of interest
that it satisfies, and we want to find a ConjectureData object with a
shortlex (see sort_key above) smaller choice sequence that exhibits the same
property.

Currently the only property of interest we use is that the status is
INTERESTING and the interesting_origin takes on some fixed value, but we
may potentially be interested in other use cases later.
However we assume that data with a status < VALID never satisfies the predicate.

The shrinker keeps track of a value shrink_target which represents the
current best known ConjectureData object satisfying the predicate.
It refines this value by repeatedly running *shrink passes*, which are
methods that perform a series of transformations to the current shrink_target
and evaluate the underlying test function to find new ConjectureData
objects. If any of these satisfy the predicate, the shrink_target
is updated automatically. Shrinking runs until no shrink pass can
improve the shrink_target, at which point it stops. It may also be
terminated if the underlying engine throws RunIsComplete, but that
is handled by the calling code rather than the Shrinker.

=======================
Designing Shrink Passes
=======================

Generally a shrink pass is just any function that calls
cached_test_function and/or consider_new_nodes a number of times,
but there are a couple of useful things to bear in mind.

A shrink pass *makes progress* if running it changes self.shrink_target
(i.e. it tries a shortlex smaller ConjectureData object satisfying
the predicate). The desired end state of shrinking is to find a
value such that no shrink pass can make progress, i.e. that we
are at a local minimum for each shrink pass.

In aid of this goal, the main invariant that a shrink pass much
satisfy is that whether it makes progress must be deterministic.
It is fine (encouraged even) for the specific progress it makes
to be non-deterministic, but if you run a shrink pass, it makes
no progress, and then you immediately run it again, it should
never succeed on the second time. This allows us to stop as soon
as we have run each shrink pass and seen no progress on any of
them.

This means that e.g. it's fine to try each of N deletions
or replacements in a random order, but it's not OK to try N random
deletions (unless you have already shrunk at least once, though we
don't currently take advantage of this loophole).

Shrink passes need to be written so as to be robust against
change in the underlying shrink target. It is generally safe
to assume that the shrink target does not change prior to the
point of first modification - e.g. if you change no bytes at
index ``i``, all spans whose start is ``<= i`` still exist,
as do all blocks, and the data object is still of length
``>= i + 1``. This can only be violated by bad user code which
relies on an external source of non-determinism.

When the underlying shrink_target changes, shrink
passes should not run substantially more test_function calls
on success than they do on failure. Say, no more than a constant
factor more. In particular shrink passes should not iterate to a
fixed point.

This means that shrink passes are often written with loops that
are carefully designed to do the right thing in the case that no
shrinks occurred and try to adapt to any changes to do a reasonable
job. e.g. say we wanted to write a shrink pass that tried deleting
each individual choice (this isn't an especially good pass,
but it leads to a simple illustrative example), we might do it
by iterating over the choice sequence like so:

.. code-block:: python

    i = 0
    while i < len(self.shrink_target.nodes):
        if not self.consider_new_nodes(
            self.shrink_target.nodes[:i] + self.shrink_target.nodes[i + 1 :]
        ):
            i += 1

The reason for writing the loop this way is that i is always a
valid index into the current choice sequence, even if the current sequence
changes as a result of our actions. When the choice sequence changes,
we leave the index where it is rather than restarting from the
beginning, and carry on. This means that the number of steps we
run in this case is always bounded above by the number of steps
we would run if nothing works.

Another thing to bear in mind about shrink pass design is that
they should prioritise *progress*. If you have N operations that
you need to run, you should try to order them in such a way as
to avoid stalling, where you have long periods of test function
invocations where no shrinks happen. This is bad because whenever
we shrink we reduce the amount of work the shrinker has to do
in future, and often speed up the test function, so we ideally
wanted those shrinks to happen much earlier in the process.

Sometimes stalls are inevitable of course - e.g. if the pass
makes no progress, then the entire thing is just one long stall,
but it's helpful to design it so that stalls are less likely
in typical behaviour.

The two easiest ways to do this are:

* Just run the N steps in random order. As long as a
  reasonably large proportion of the operations succeed, this
  guarantees the expected stall length is quite short. The
  book keeping for making sure this does the right thing when
  it succeeds can be quite annoying.
* When you have any sort of nested loop, loop in such a way
  that both loop variables change each time. This prevents
  stalls which occur when one particular value for the outer
  loop is impossible to make progress on, rendering the entire
  inner loop into a stall.

However, although progress is good, too much progress can be
a bad sign! If you're *only* seeing successful reductions,
that's probably a sign that you are making changes that are
too timid. Two useful things to offset this:

* It's worth writing shrink passes which are *adaptive*, in
  the sense that when operations seem to be working really
  well we try to bundle multiple of them together. This can
  often be used to turn what would be O(m) successful calls
  into O(log(m)).
* It's often worth trying one or two special minimal values
  before trying anything more fine grained (e.g. replacing
  the whole thing with zero).

c                 H   ^  U 4S jnT R                   Ul         [        U5      $ )a  It's useful during shrinking to have access to derived values of
the current shrink target.

This decorator allows you to define these as cached properties. They
are calculated once, then cached until the shrink target changes, then
recalculated the next time they are used.c                    >  U R                   TR                     $ ! [         a/    U R                   R                  TR                  T" U 5      5      s $ f = fr-   )_Shrinker__derived_valuesrH   KeyError
setdefault)rJ   fns    r3   accept&Shrinker.derived_value.<locals>.accept  sP    O,,R[[99 O,,77RXNNOs    6AA)rH   property)rd   re   s   ` r3   derived_valueShrinker.derived_value   s!    	O ++r9   F)in_target_phaseenginer'   initial	predicateNallow_transitionexplainrj   c                   Uc  Uc   eXl         U=(       d    S U l        U=(       d    S U l        0 U l        [	        UR
                  5      U l        X l        U R                  5         SU l	        SU l
        U R                   R                  U l        U R                   R                  U l        U R                  U l        [!        U R"                  5      U R%                  S5      U R%                  S5      U R%                  S5      U R%                  S	5      U R%                  S
5      [!        U R&                  5      [!        U R(                  5      [!        U R*                  5      [!        U R,                  5      [!        U R.                  5      [!        U R0                  5      [!        U R2                  5      /U l        U(       a  SOSU l        XPl        g)a4  Create a shrinker for a particular engine, with a given starting
point and predicate. When shrink() is called it will attempt to find an
example for which predicate is True and which is strictly smaller than
initial.

Note that initial is a ConjectureData object, and predicate
takes ConjectureData objects.
Nc                     gNTrA   )datas    r3   <lambda>#Shrinker.__init__.<locals>.<lambda>"  s    dr9   c                     grr   rA   )sourcedestinations     r3   rt   ru   #  s    SWr9   r      XXXXXXXXXXXXXXXfull)rk   _Shrinker__predicate_Shrinker__allow_transitionra   r6   choicesinitial_sizeshrink_targetclear_change_trackingrE   	max_stall
call_countinitial_callsmisaligned_countinitial_misalignedcalls_at_last_shrinkr=   try_trivial_spansnode_programpass_to_descendantreorder_spansminimize_duplicated_choicesminimize_individual_choicesredistribute_numeric_pairslower_integers_togetherlower_duplicated_charactersshrink_passes_Shrinker__extendshould_explain)rJ   rk   rl   rm   rn   ro   rj   s          r3   __init__Shrinker.__init__  s|   ( $(8(DDD$;):"2"X7W&(0 %""$ ![[33"&++">">$($6$6! t--.g&g&g&g&g&t../t))*t778t778t667t334t7780
$ :Iva%r9   c                     0 $ r-   rA   rI   s    r3   cached_calculationsShrinker.cached_calculationsK  s    	r9   c                    ^ ^ UU 4S jnU$ )Nc                    > U R                   /TQ7n TR                  U   $ ! [         a#    TR                  R                  X" 5       5      s $ f = fr-   )rH   r   rb   rc   )f	cache_keykeysrJ   s     r3   re   Shrinker.cached.<locals>.acceptP  sW    +d+IK//	:: K//::9acJJKs   " *AArA   )rJ   r   re   s   `` r3   cachedShrinker.cachedO  s    	K r9   r*   c                 .    U R                   R                  $ )zOReturn the number of calls that have been made to the underlying
test function.)rk   r   rI   s    r3   rC   Shrinker.callsY  s     {{%%%r9   c                 .    U R                   R                  $ r-   )rk   r   rI   s    r3   rD   Shrinker.misaligned_  s    {{+++r9   c                 ^    U R                   U R                  -
  U R                  :  a  [        eg r-   )rC   r   r   rZ   rI   s    r3   check_callsShrinker.check_callsc  s)    ::111T^^C Dr9   r)   c                    US [        U R                  5       n[        XR                  5      (       a  g[        U R                  5      [        U5      :  a  g[	        S U 5       5      (       a  gU R
                  R                  U Vs/ s H  o"R                  PM     snU R                  S9nU R                  nU R                  U5        U R                  5         X@R                  LU4$ s  snf )N)TN)FNc              3   l   #    U  H*  n[        UR                  UR                  5      (       + v   M,     g 7fr-   )r   r.   r/   r0   s     r3   r4   0Shrinker.cached_test_function.<locals>.<genexpr>t  s(     Ted#DJJ0@0@AAAes   24extend)r6   r)   r   r8   anyrk   cached_test_functionr.   r   r   incorporate_test_datar   )rJ   r)   nresultpreviouss        r3   r   Shrinker.cached_test_functiong  s     'DJJ(eZZ((DJJ(5/1  TeTTT 11#$eWWe$T]] 2 
 %%""6* 2 22F;; %s   C)c                 *    U R                  U5      S   $ Nr   )r   )rJ   r)   s     r3   consider_new_nodesShrinker.consider_new_nodes  s    ((/22r9   c                 `   UR                   [        R                  :  d  XR                  L a  gU R	                  U5      (       ak  [        UR                  5      [        U R                  R                  5      :  a4  U R                  U R                  U5      (       a  U R                  U5        gggg)zzTakes a ConjectureData or Overrun object updates the current
shrink_target if this data represents an improvement over it.N)	statusr   VALIDr   r   r8   r)   r   update_shrink_target)rJ   rs   s     r3   r   Shrinker.incorporate_test_data  s     ;;%1C1C)CT""$x0B0B0H0H'II''(:(:DAA%%d+ B J #r9   msgc                 :    U R                   R                  U5        g r-   )rk   debug)rJ   r   s     r3   r   Shrinker.debug  s    #r9   c                 .    U R                   R                  $ r-   )rk   randomrI   s    r3   r   Shrinker.random  s    {{!!!r9   c                 n    U R                  5         U R                  5         U R                  R
                  (       Ga  S nU R                  [        U R                  R                  5      -
  nU R                  R                  U R                  -
  nU R                  R                  U R                  -
  nU R                  SU SU" U5       SU R                   SU SU SU R                   S	35        S
 GH  nU R                  S5        U(       a  U R                  S5        OU R                  S5        U R                  S5        [!        U R"                  S S9 H  nUR$                  S:X  a  M  UR                  S:g  U:w  a  M*  U R                  SUR&                   SUR$                   SU" UR$                  5       SUR                   SUR(                   SUR*                   SU" UR*                  5       S	35        M     GM     U R                  S5        U R-                  5         g! [         a    SU l         GNf = f! U R                  R
                  (       Ga  S nU R                  [        U R                  R                  5      -
  nU R                  R                  U R                  -
  nU R                  R                  U R                  -
  nU R                  SU SU" U5       SU R                   SU SU SU R                   S	35        S
 GH  nU R                  S5        U(       a  U R                  S5        OU R                  S5        U R                  S5        [!        U R"                  S S9 H  nUR$                  S:X  a  M  UR                  S:g  U:w  a  M*  U R                  SUR&                   SUR$                   SU" UR$                  5       SUR                   SUR(                   SUR*                   SU" UR*                  5       S	35        M     GM     U R                  S5        f f = f)zRun the full set of shrinks and update shrink_target.

This method is "mostly idempotent" - calling it twice is unlikely to
have any effect, though it has a non-zero probability of doing so.
Fc                     U S:w  a  S$ S$ )N   s rA   r   s    r3   r   Shrinker.shrink.<locals>.s  s    "#q&30b0r9   z]---------------------
Shrink pass profiling
---------------------

Shrinking made a total of z callz
 of which z shrank and z were misaligned. This deleted z choices out of .)TFr   zUseful passes:zUseless passes:c                 J    U R                   * U R                  U R                  4$ r-   )rC   rF   rE   )ts    r3   rt   !Shrinker.shrink.<locals>.<lambda>  s    xaii&Hr9   keyr   z  * z made z were misaligned, deleting z choiceN)initial_coarse_reductiongreedy_shrinkrZ   r   rk   report_debug_infor   r6   r   r   r   r   r   r   r   rE   sortedr   rC   r@   rD   rF   ro   )rJ   r   total_deletedrC   rD   usefulpass_s          r3   shrinkShrinker.shrink  s   .	))+  {{,,,1 !% 1 1C8J8J8R8R4S S..1C1CC![[99D<S<SS


1 27uQuXJj||nL =,o-=d>O>O=PPQS ,FJJrN

#34

#45JJrN!'**H" !;;!+$!MMQ.69$

"5::,fU[[Mq~FVV`$}}o\%:J:J9K L((-'8%//@R?SSTV" ,* 

2Y  	( #(D	(
 {{,,,1 !% 1 1C8J8J8R8R4S S..1C1CC![[99D<S<SS


1 27uQuXJj||nL =,o-=d>O>O=PPQS ,FJJrN

#34

#45JJrN!'**H" !;;!+$!MMQ.69$

"5::,fU[[Mq~FVV`$}}o\%:J:J9K L((-'8%//@R?SSTV" ,* 

2M -s$    H& &H;7H> :H;;H> >G6P4c           	        ^ ^^^ T R                   (       a  T R                  R                  (       d  g SS-  T l        T R                  nT R                  mT R
                  n[        [        5      nT R                  R                  T R                  S [        T R                  R                  5      S    S9n[        T R                  R                  S S9 GH  u  mm[        UUU4S jU 5       5      (       a  M%  [        UU4S jT R                  R                   5       5      (       a  MV  SnS	n[        S
5       GH&  nUS-
  US-  :  a    Mx  / n[        TT5       Hl  n	TU	   n
U
R                  (       d8  [!        U
R"                  U
R$                  T R&                  S9nU
R)                  US9n
UR+                  U
R,                  5        Mn     US T [/        U5      -   UTS  -   nT R                  R1                  USS9nUR2                  [4        R6                  L a  M  [9        [:        U5      n[=        U5      [=        UR
                  5      :X  a  [?        UR                  TTS  5      (       Gd7  [A        URB                  URB                  SS9 H  u  pURD                  URD                  :X  d   eURD                  T::  d   eURF                  URF                  :X  d   eURD                  T:X  d  Ma  URH                  T:X  d  Ms  URH                  n  O   [K        S5      eUS T UR
                  TU -   UTS  -   nUTT4   R+                  UR
                  TU 5        T R                  R1                  U5      nUR2                  [4        R6                  L a  GMX  [9        [:        U5      nO#UTT4   R+                  UR
                  TT 5        UT R                  La'  T R                  R                  RM                  5             g UR2                  [4        RN                  L a    GMH  T RQ                  U5      (       d  GM  US-  nUS:  d  GM
  UT R                  R                  TT4'     GM     GM     [=        T R                  R                  5      S::  a  g Sn[        U 4S jURS                  5        5       5      n[        S
5       GH  n/ nSnU He  u  u  mmnUTs=::  a  T:  d   S5       e   S5       eURU                  UUT 5        URU                  T R&                  RW                  U5      5        TnMg     T R                  R1                  U5      nUT R                  L d   eUR2                  [4        RN                  :X  a  ST R                  R                  S'     g T RQ                  U5      (       d  M  US-  nUS:  d  M  ST R                  R                  S'     g    g )N   d   r   )prefixc                     U S   U S   -
  * U 4$ Nr   r   rA   xs    r3   rt   "Shrinker.explain.<locals>.<lambda>  s    AaD1Q4K.!9Lr9   r   c              3   n   >#    U  H*  n[        UTS T 5      =(       a    [        UTTS  5      v   M,     g 7fr-   )r   r   )r1   seenendr)   starts     r3   r4   #Shrinker.explain.<locals>.<genexpr>  s:      ,D 4v/OHT5;4OO,s   25c              3   \   >#    U  H!  u  pX4S :w  d  M  UT:*  =(       a    TU:*  v   M#     g7f)r   r   NrA   )r1   r   er   r   s      r3   r4   r     s6      =DA6V# (U
'sax'=s   ,,zor any other generated valuei  
      )r   
with_valuer   r   FstrictzExpected matching prefixesr   c              3   f   >#    U  H&  u  pUTR                   R                  ;   d  M!  X4v   M(     g 7fr-   )r   slice_comments)r1   kvrJ   s      r3   r4   r   H  s-      '
-tqd6H6H6W6W1WFQF~s    1
1z#these chunks must be nonoverlappingzDThe test sometimes passed when commented parts were varied together.r   zAThe test always failed when commented parts were varied together.),r   r   
arg_slicesr   r)   r   r   listrk   passing_choice_sequencesminr   r   r   range
was_forcedr   typer/   r   copyappendr.   r7   r   r   r   OVERRUNr   r   r6   r   zipspansr   labelr   NotImplementedErrorclearr   r   itemsr   choice)rJ   r   r   chunksseen_passing_seqn_same_failuresnote	n_attemptreplacementir2   r.   attemptr   span1span2
result_endn_same_failures_togetherchunks_by_start_index_new_choicesprev_endlsr   r)   r   s   `                      @@@r3   ro   Shrinker.explain  sO   ""$*<*<*G*GC**

,,CNtCT
  ;;??::ED$6$6$A$A B1 EF @ 
 !))/L
JE3
  ,      ..==  
   O1D"3Z	 r>Oa$77 !uc*A 8D?? + IIt'7'7!  $yyEy:&&tzz2 + "&5/E+,>>N99'&9Q==FNN2.7LC$77 uST{;; ),%++V\\%)  %{{ekk999${{e333${{ekk999 ;;%/EII4D).J!) 22NOO   ..z:;!#$-( 
 E3<(//uZ0PQ![[==gFF}}6 !"2F;FE3<(//uS0IJ (:(::&&55;;===FLL0 ##F++#q(O&#-JN**995#,GI (5
D t!!001Q6#$  & '
%||~'
 !
 sA)+KH$9 b5.3.U0UU.U0UU.""78E#:;""4;;#5#5b#9:	 %: [[55kBF !D$6$6666}},Z ""11&9 ''(A-(+s2[ &&55f= 3 r9   c                 :    U R                  U R                  5        g)a  Run a full set of greedy shrinks (that is, ones that will only ever
move to a better target) and update shrink_target appropriately.

This method iterates to a fixed point and so is idempontent - calling
it twice will have exactly the same effect as calling it once.
N)fixate_shrink_passesr   rI   s    r3   r   Shrinker.greedy_shrinkf  s     	!!$"4"45r9   c                 $    U R                  5         g)a  Performs some preliminary reductions that should not be
repeated as part of the main shrink passes.

The main reason why these can't be included as part of shrink
passes is that they have much more ability to make the test
case "worse". e.g. they might rerandomise part of it, significantly
increasing the value of individual nodes, which works in direct
opposition to the lexical shrinking and will frequently undo
its work.
N)reduce_each_alternativerI   s    r3   r   !Shrinker.initial_coarse_reductiono  s     	$$&r9   c                 6   U R                   R                   Vs/ s H  n/ PM     nn[        U R                  5       H=  u  p4UR                  [        U5      :  d  M   X$R                     R                  U5        M?     [        [        [        U5      5      $ s  snf r-   )	r   r)   	enumerater  r   r6   r  r7   map)rJ   r  r   r  exs        r3   spans_starting_atShrinker.spans_starting_at|  sw    "006676"67tzz*EAxx#f+%xx ''* + S'(( 8s   Bc                     SnU[        U R                  R                  5      :  Ga  U R                  R                  nX!   nUR                  S:X  Ga  UR                  (       Gd  UR
                  S::  Gan  UR                  S   S:X  GaZ  [        UR
                  [        5      (       d   eU R                  USU X!   R                  SS94-   X!S-   S -   5      S   nX@R                  La  Ub  UR                  [        R                  :  a  [        UR                  5      [        U5      :g  nU(       du  [        US-   [        U5      5       HY  nUR                  U   nX&   nUR                  UR                  :w  d'  [        UR
                  UR                  5      (       a  MW  Sn  O   U(       a4  [        UR
                  5       H  n	U R!                  X5      (       d  M    O   US-  nU[        U R                  R                  5      :  a  GM  gg)	a}  This is a pass that is designed to rerandomise use of the
one_of strategy or things that look like it, in order to try
to move from later strategies to earlier ones in the branch
order.

It does this by trying to systematically lower each value it
finds that looks like it might be the branch decision for
one_of, and then attempts to repair any changes in shape that
this causes.
r   integerr   	min_valueNr   r   T)r6   r   r)   r   r   r.   r/   
isinstancerW   r   r   r   r   r   r   r   try_lower_node_as_alternative)
rJ   r  r)   r2   zero_attemptchanged_shapej	zero_node	orig_noder   s
             r3   r    Shrinker.reduce_each_alternative  s    #d((..//&&,,E8D		Y&JJ"$$$[1Q6!$**c2222  $88"1I!!< >>1uwO   !(:(::$0$++v||;$'(:(:$;s5z$IM(!&q1uc%j!9A(4(:(:1(=I(-I )).. @'7$-OOY5J5J(" (" 15 % ": %!&tzz!2A#AA!GG % "3 FA_ #d((..///r9   c                    U R                   R                  nU R                  USU X1   R                  US94-   X1S-   S -   5      (       a  gUSU X1   R                  US94-   nU R                   nU R                  U   n[        S5       H  nU R                  R                  U Vs/ s H  oR                  PM     sn[        U5      S9n	U	R                  [        R                  :  a  M`  U R                  U	5        U H{  n
UR                  U
   nU	R                  U
   nU	R                  UR                  UR                    nU R                  USU U-   X;R                   S -   5        XPR                   Ld  Mz      g   M     gs  snf )zAttempt to lower `self.shrink_target.nodes[i]` to `v`,
while rerandomising and attempting to repair any subsequent
changes to the shape of the test case that this causes.Nr   r   T   r   F)r   r)   r   r   r&  r   rk   r   r.   r6   r   r   r   r   r  r   r   )rJ   r  r   r)   r   rl   r  r  r   random_attemptr/  initial_spanattempt_spancontentss                 r3   r,  &Shrinker.try_lower_node_as_alternative  s    ""(("""1I!4661uwG
 
 rehmmqm9;;$$&&q)qA![[=="()&Q&)#e* > N $$v||3&&~6&}}Q/-33A6)//0B0B\EUEUV''"1I(51A1A1C+DD "4"44     *s   &F
c                      [        [        5      $ r-   )r   r"   rI   s    r3   shrink_pass_choice_trees!Shrinker.shrink_pass_choice_trees  s    :&&r9   random_ordershrink_passr>  c                  ^ U R                   T   nUR                  (       a  gU R                  nU R                  nU R                  n[        U R                  R                  5      nTR                  c   eU R                  R                  TR                  5        U(       a  [        U R                  5      nO[        TR                  5      n UR                  UU4S j5      Tl        T=R                  U R                  U-
  -  sl        T=R                  U R                  U-
  -  sl        T=R                  U R                  U-
  -  sl        T=R                   U[        U R                  R                  5      -
  -  sl        U R                  R#                  5         g! T=R                  U R                  U-
  -  sl        T=R                  U R                  U-
  -  sl        T=R                  U R                  U-
  -  sl        T=R                   U[        U R                  R                  5      -
  -  sl        U R                  R#                  5         f = f)NFc                 &   > TR                  U 5      $ r-   )r?   )chooserr?  s    r3   rt   Shrinker.step.<locals>.<lambda>  s     4 4W =r9   T)r;  	exhaustedrE   rC   rD   r6   r   r   r@   rk   explain_next_call_asr$   r   r#   rB   steprF   clear_call_explanation)	rJ   r?  r>  treeinitial_shrinksr   r   sizeselection_orders	    `       r3   rF  Shrinker.step  s   ,,[9>>,,

!__4%%--.+++(()9)9:4T[[AO4[5L5LMO
	1&*ii='K#
 m!;;""doo8J&JJ"4<</#AA!!TC0B0B0J0J,K%KK!KK..0 m!;;""doo8J&JJ"4<</#AA!!TC0B0B0J0J,K%KK!KK..0s   F B7Ipassesc                    SnU(       Gap  Sn0 nU R                  5       nU R                  nSnU GH#  nU(       a  U R                  5       nU R                  nSn	Sn
X:  a  [        U R                  SU-  U R                  U-
  -   5      U l        U R                  nU R                  nU R                  XyU
S-  :  S9(       d  OFSnXR                  :w  a.  XR                  La  Sn	O[        X`R                  U-
  5      nU	S-  n	X:  a  M  U R                  UL a  SX7'   M  [        U R                  5      [        UR                  5      :  a  SX7'   GM  SX7'   GM&     UR                  UR                  S	9  U(       a  GMo  g
g
)zgRun steps from each pass in ``passes`` until the current shrink target
is a fixed point of all of them.TFr   r      r   r=  r   N)
remove_discardedrC   r   maxr   rF  r6   r   sort__getitem__)rJ   rM  any_ran
reorderingcan_discardcalls_at_loop_startmax_calls_per_failing_stepsp	before_spfailuresmax_failuresprevr   s                r3   r  Shrinker.fixate_shrink_passes  s    GJ //1K"&**
 *+&"&"7"7"9K ..	
 !- &)66::(;;=&DN  --D$(JJM  99R,RSBS6S9T "G
 %

2'9'99'(H9< :JJ<V:6 %MHY -d %%2%&JN&Y->->)??%'JN%&JNE H KKJ22K3q gr9   .c                 .    U R                   R                  $ r-   )r   r)   rI   s    r3   r)   Shrinker.nodesa      !!'''r9   c                 .    U R                   R                  $ r-   )r   r   rI   s    r3   r   Shrinker.choicese  s    !!)))r9   c                 .    U R                   R                  $ r-   )r   r  rI   s    r3   r  Shrinker.spansi  rb  r9   c                     [        [        5      nU R                   H   nXR                     R	                  U5        M"     [        U5      $ )zt
A mapping of labels to a list of spans with that label. Spans in the list
are ordered by their normal index order.
)r   r   r  r  r  dict)rJ   spans_by_labelr%  s      r3   ri  Shrinker.spans_by_labelm  s<     %T***B88$++B/ N##r9   c                 2    [        U R                  [        S9$ )Nr   )r   ri  rV   rI   s    r3   distinct_labelsShrinker.distinct_labelsy  s    d))s33r9   c                   ^ ^^^ UR                  T R                  U 4S j5      nT R                  U   mUR                  [        [	        T5      S-
  5      5      mTT   mTS-   [	        T5      :X  d   TTS-      R
                  TR                  :  a  gT R                  UT5      UUU4S j5       nUR                  US 5      nTR
                  UR
                  ::  d   eTR                  UR                  :  d   eUR                  TR                  :  d   eT R                  T R                  STR
                   T R                  UR
                  UR                   -   T R                  TR                  S -   5        g)ax  Attempt to replace each span with a descendant span.

This is designed to deal with strategies that call themselves
recursively. For example, suppose we had:

binary_tree = st.deferred(
    lambda: st.one_of(
        st.integers(), st.tuples(binary_tree, binary_tree)))

This pass guarantees that we can replace any binary tree with one of
its subtrees - each of those will create an interval that the parent
could validly be replaced with, and this pass will try doing that.

This is pretty expensive - it takes O(len(intervals)^2) - so we run it
late in the process when we've got the number of intervals as far down
as possible.
c                 :   > [        TR                  U    5      S:  $ )Nr   )r6   ri  )lrJ   s    r3   rt   -Shrinker.pass_to_descendant.<locals>.<lambda>  s    C0C0CA0F,G1,Lr9   r   Nc                    > TS-   n [        T5      nU S-   U:  a4  X-   S-  nTU   R                  TR                  :  a  UnOUn U S-   U:  a  M4  TTS-   U  Vs/ s H!  nUR                  TR                  :  d  M  UPM#     sn$ s  snf Nr   r   )r6   r   r   choice_count)lohimidspanancestorr  r  s       r3   descendants0Shrinker.pass_to_descendant.<locals>.descendants  s    QBUBq&2+w1n:##x||3BB q&2+ "!a%"--D$$x'<'<< -  s   B;Bc                      U R                   S:  $ r   )rt  )r%  s    r3   rt   rq    s    BOOa<Or9   )chooserl  ri  r   r6   r   r   r   rt  r   r)   )rJ   rB  r  rz  
descendantry  r  r  s   `    @@@r3   r   Shrinker.pass_to_descendant}  sN   &   "L
 ##E*NN5Ua018q5CJ%A,"4"4"D	UA		 
	 ^^K1OP
~~!1!1111||z~~---&&)>)>>>>JJ'(jj))JNN;<jj()	
r9   c                   ^ ^^^^^ [        T R                  5      S::  a  g/ m[        T R                  5       HF  nT R                  U   nUR                  (       d  UR
                  S:w  a  M5  TR                  U5        MH     T(       d  gT Vs/ s H(  n[        UR                  UR                  S   -
  5      PM*     snm[        T5      nUS:  d   e[        [        T5      5       H  nTU==   U-  ss'   M     T R                  mS mUUUU U4S jm[        R                  " UU4S j5        [        R                  " UU4S	 j5        T R                  5         gs  snf )
a  Sometimes we find ourselves in a situation where changes to one part
of the choice sequence unlock changes to other parts. Sometimes this is
good, but sometimes this can cause us to exhibit exponential slow
downs!

e.g. suppose we had the following:

m = draw(integers(min_value=0))
n = draw(integers(min_value=0))
assert abs(m - n) > 1

If this fails then we'll end up with a loop where on each iteration we
reduce each of m and n by 2 - m can't go lower because of n, then n
can't go lower because of m.

This will take us O(m) iterations to complete, which is exponential in
the data size, as we gradually zig zag our way towards zero.

This can only happen if we're failing to reduce the size of the choice
sequence: The number of iterations that reduce the length of the choice
sequence is bounded by that length.

So what we do is this: We keep track of which nodes are changing, and
then if there's some non-zero common offset to them we try and minimize
them all at once by lowering that offset.

This may not work, and it definitely won't get us out of all possible
exponential slow downs (an example of where it doesn't is where the
shape of the nodes changes as a result of this bouncing behaviour),
but it fails fast when it doesn't work and gets us out of a really
nastily slow case when it does.
r   Nr)  shrink_towardsr   c                 v    U R                   U R                   S-   U R                  U R                  S   U-   S9/4$ )Nr   r  r   )indexr   r/   )r2   r   s     r3   offset_node6Shrinker.lower_common_node_offset.<locals>.offset_node  s@    



Qd&6&67G&H1&LMN r9   c                    > TR                  [        TR                  [        TTSS9 VVs/ s H  u  p#T" X!X-   -  5      PM     snn5      5      $ s  snnf )NFr   )r   r   r)   r  )	r   signr2   r   changedintsr  rJ   sts	       r3   consider3Shrinker.lower_common_node_offset.<locals>.consider  s]    **HH (+7D'G'GGD $D!%.9'G s   Ac                    > T" U S5      $ Nr   rA   r   r  s    r3   rt   3Shrinker.lower_common_node_offset.<locals>.<lambda>  s    !Qr9   c                    > T" U S5      $ )NrP  rA   r  s    r3   rt   r    s    !Rr9   )r6   _Shrinker__changed_nodesr   r)   trivialr   r  absr.   r/   r   r   r   r   r   r   )	rJ   r  r2   offsetr  r  r  r  r  s	   `   @@@@@r3   lower_common_node_offset!Shrinker.lower_common_node_offset  s)   B t##$),,-A::a=D||tyyI5NN4 	 .  NU
MTTC

T--.>??@W
 Tzzs4y!AGvG " 			 		 	v78v89""$A
s   /Ec                 D    U R                   U l        [        5       U l        g r-   )r   "_Shrinker__last_checked_changed_atset_Shrinker__all_changed_nodesrI   s    r3   r   Shrinker.clear_change_tracking  s    )-););&#&5 r9   c                 :    U R                   R                  U5        g r-   )r  add)rJ   r  s     r3   mark_changedShrinker.mark_changed  s      #r9   c           	         U R                   U R                  L a  U R                  $ U R                   nU R                  nXLd   eUR                  nUR                  n[	        UR                  5      [	        UR                  5      :  d   e[        U5      [        U5      :w  d  [        S [        X4SS9 5       5      (       a  [        5       U l        U R                  $ [        U5      [        U5      :X  d   e[        [        X4SS95       Hf  u  nu  pgUR                  UR                  :X  d   e[        UR                  UR                  5      (       a  MK  U R                  R                  U5        Mh     U R                  $ )Nc              3   X   #    U  H   u  pUR                   UR                   :g  v   M"     g 7fr-   )r   )r1   n1n2s      r3   r4   +Shrinker.__changed_nodes.<locals>.<genexpr>  s#      4
-T62BGGrww-Ts   (*Tr   )r  r   r  r)   r8   r6   r   r  r  r#  r   r   r.   r  )rJ   prev_target
new_target
prev_nodes	new_nodesr  r  r  s           r3   __changed_nodesShrinker.__changed_nodes  sJ   ))T-?-??+++44''
,,, &&
$$	
(()H[5F5F,GGGGz?c)n, 4
-0t-T4
 1
 1
 (+uD$ ''' z?c)n444(Z4)PQ8Bww"'')))#BHHbhh77,,003  R
 '''r9   c                    [        U[        5      (       d   eU =R                  S-  sl        [        U R                  U R
                  U R                  -
  S-  5      U l        U R
                  U l        Xl        0 U l        g rs  )	r+  r   rE   rR  r   rC   r   r   ra   )rJ   r  s     r3   r   Shrinker.update_shrink_target(  sk    *&67777 NNTZZ$*C*CCqH
 %)JJ!' "r9   c                   ^  [        U 4S jU 5       5      (       a  g[        T R                  U Vs/ s H,  o3R                  UR                  S-   UR	                  US9/4PM.     sn5      nT R                  U5      S   nUc  gUT R                  L a  T R                  5         gUR                  [        R                  L a  gUR                  [        R                  L a  gUR                  b  UR                  u  pgpT R                  U   nUR                  U:w  a  gUR                  (       a  gUR                  S;   a~  UR                  S   US   ::  a  gT R!                  USUR                   XCR                     R	                  UXCR                     R"                  SUS    S	9/-   XCR                  S -   5      $ [%        T R                  5      [%        UR                  5      -
  n
U
S
::  a  gUS
   R                  nUS   R                  S-   nXU
-   41nT R&                   GH  nUR(                  U:  a  M  UR*                  U::  a  M(  UR                  [%        UR&                  5      :  a  MM  UR&                  UR                     nUR,                   Vs/ s H  nUR(                  U:  d  M  UPM     nnUR,                   Vs/ s H  nUR(                  U:  d  M  UPM     nn[%        U5      [%        U5      :  d  U(       d  M  UR/                  US
   R(                  U[%        U5      *    R(                  45        GM     [1        US SS9 H)  u  nnUSU UUS -   nT R!                  U5      (       d  M)    g   gs  snf s  snf s  snf )a2  Attempts to replace each node in the nodes list with n. Returns
True if it succeeded (which may include some additional modifications
to shrink_target).

In current usage it is expected that each of the nodes currently have
the same value and choice_type, although this is not essential. Note that
n must be < the node at min(nodes) or this is not a valid shrink.

This method will attempt to do some small amount of work to delete data
that occurs after the end of the nodes. This is useful for cases where
there is some size dependency on the value of a node.
c              3   f   >#    U  H&  oR                   [        TR                  5      :  v   M(     g 7fr-   )r  r6   r)   )r1   r2   rJ   s     r3   r4   /Shrinker.try_shrinking_nodes.<locals>.<genexpr>H  s      ?zzS_,s   .1Nr   r   FT>   bytesstringmin_size)with_constraintsr   r   rP  c                     U S   U S   -
  $ r   rA   r   s    r3   rt   .Shrinker.try_shrinking_nodes.<locals>.<lambda>  s    AaD1Q4Kr9   )r   reverse)r   r   r)   r  r   r   r   r  r   r   r  INVALIDmisaligned_atr   r   r/   r   r.   r6   r  r   r   childrenr  r   )rJ   r)   r   r2   initial_attemptr  r  attempt_choice_typeattempt_constraints_attempt_forced
lost_nodesr   r   regions_to_deleter%  r  cin_originalin_replacedur   try_with_deleteds   `                     r3   try_shrinking_nodesShrinker.try_shrinking_nodes8  sZ     ????%JJQVWQVjj$**q.4999+B*CDQVW

 ++O<Q??d((())+ >>V^^+>>V^^+  ,4 %% ME(; ::e$Dyy//yy// ##J/3Fz3RR ! ..#Ldjj1'

388-@'6zz'B'H'H A"5j"A( 9  &jjl3	4  _s7=='99
?aBioo! "#345**Bxx%vv}xx3w}}--!--1K&(kkDkQWW^1kKD&1&:&:M&:aggn1&:KM;3{#33; !!Q%%{C4D3D'E'K'KL- 4 ,2GQUVDAq.r2_QR5HH&&'788 W
 s XH EMs   3M:
M?-M?NNc                 4   U R                   R                  (       a  / nU R                   R                   Hn  nUR                  S:  d  M  UR                  (       d  M(  U(       a  UR
                  US   S   :  d  MG  UR                  UR
                  UR                  45        Mp     U(       d   g[        U R                  5      n[        U5       H	  u  pEX4U2	 M     U R                  [        U5      5      (       d  gU R                   R                  (       a  M  g)a  Try removing all bytes marked as discarded.

This is primarily to deal with data that has been ignored while
doing rejection sampling - e.g. as a result of an integer range, or a
filtered strategy.

Such data will also be handled by the adaptive_example_deletion pass,
but that pass is necessarily more conservative and will try deleting
each interval individually. The common case is that all data drawn and
rejected can just be thrown away immediately in one block, so this pass
will be much faster than trying each one individually when it works.

returns False if there is discarded data and removing it does not work,
otherwise returns True.
r   rP  FT)r   has_discardsr  rt  	discardedr   r  r   r   r)   reversedr   r7   )rJ   r  r%  r  r  r   s         r3   rQ  Shrinker.remove_discarded  s        --I((..OOa'&"((imB6G*G$$bhh%78 /   4::&G +aCL , **5>::/   ---0 r9   c                     [        [        5      nU R                   H5  nXR                  [	        UR
                  5      4   R                  U5        M7     [        UR                  5       5      $ )z5Returns a list of nodes grouped (choice_type, value).)r   r   r)   r   r   r.   r  values)rJ   
duplicatesr2   s      r3   duplicated_nodesShrinker.duplicated_nodes  sS     !&
JJD		:djj#9:;BB4H J%%'((r9   programc                 *   ^ ^ [        UU 4S jST 3S9$ )Nc                 (   > TR                  U T5      $ r-   )_node_program)rB  r  rJ   s    r3   rt   'Shrinker.node_program.<locals>.<lambda>  s    D..w@r9   node_program_rQ   )r=   )rJ   r  s   ``r3   r   Shrinker.node_program  s    @ 	*
 	
r9   c                 F  ^ ^^^^^ [        T5      mUR                  [        [        T R                  5      T-
  S-   5      5      mT R	                  TTT R
                  S9(       d  g UU4S jmT" [        UUU 4S j5      5      mT R
                  m[        UUUU 4S j5        g )Nr   originalc                    > TU T-  -
  $ r-   rA   )r   r  r   s    r3   offset_left+Shrinker._node_program.<locals>.offset_left  s    q1u9r9   c                 F   > TR                  T" U 5      TTR                  S9$ )Nr  )run_node_programr   )r   r  r  rJ   s    r3   rt   (Shrinker._node_program.<locals>.<lambda>  s&    $//NGd6H6H 0 r9   c                 (   > TR                  TTTU S9$ )N)r  repeats)r  )r   r  r  r  rJ   s    r3   rt   r    s    d++AwST+Ur9   )r6   r}  r   r)   r  r   r   )rJ   rB  r  r  r   r  r  s   ` `@@@@r3   r  Shrinker._node_program   s    L NN5TZZ1!4q!89: $$Q$:L:L$M	 
 %%U	
r9   c                     UR                  U R                  5      nU Vs/ s H  o3R                  (       a  M  UPM     nn[        U5      S::  a  gU R	                  U5        gs  snf )a  Find choices that have been duplicated in multiple places and attempt
to minimize all of the duplicates simultaneously.

This lets us handle cases where two values can't be shrunk
independently of each other but can easily be shrunk together.
For example if we had something like:

ls = data.draw(lists(integers()))
y = data.draw(integers())
assert y not in ls

Suppose we drew y = 3 and after shrinking we have ls = [3]. If we were
to replace both 3s with 0, this would be a valid shrink, but if we were
to replace either 3 with 0 on its own the test would start passing.

It is also useful for when that duplication is accidental and the value
of the choices don't matter very much because it allows us to replace
more values at once.
r   N)r}  r  r  r6   minimize_nodes)rJ   rB  r)   r2   s       r3   r   $Shrinker.minimize_duplicated_choices!  sS    ( t445 #(<%$||%<u:?E"	 =s
   A"A"c                   ^ ^^^^^ S mUR                  T R                  U4S j5      mUR                  T R                  UU4S j5      mTR                  mTR                  mS[        S[        4UUUUU 4S jjn[        U5        g)zIf there is a sum of generated numbers that we need their sum
to exceed some bound, lowering one of them requires raising the
other. This pass enables that.c                     U R                   S;   =(       a^    U R                   S:H  =(       aC    [        R                  " U R                  5      =(       d    [	        U R                  5      [
        :  (       + $ )N>   floatr)  r  )r   mathisnanr.   r  r%   r2   s    r3   can_choose_node<Shrinker.redistribute_numeric_pairs.<locals>.can_choose_nodeG  sU     99 44 		W$ WZZ

+Us4::BU/U: r9   c                 D   > T" U 5      =(       a    U R                   (       + $ r-   r  )r2   r  s    r3   rt   5Shrinker.redistribute_numeric_pairs.<locals>.<lambda>T  s    .Ct||3CCr9   c                    > T" U 5      =(       aN    U R                   (       + =(       a6    TR                  U R                  s=:  =(       a    TR                  S-   :*  $ s  $ )N   )r   r  )r2   r  node1s    r3   rt   r  X  sQ    . < OO#< djj;;EKK!O;< <<r9   r   r*   c                   > TR                   S:X  a  TR                  S   OSnU [        TU-
  5      :  a  gTU:  a  U * n  TU -
  nTU -   nTR                   S:X  a  [        U5      [        :  a  gTR                  TR                  S TR                   TR                  US94-   TR                  TR                  S-   TR                   -   TR                  US94-   TR                  TR                  S-   S  -   5      $ ! [         a     gf = f)Nr)  r  r   Fr  r   r   )	r   r/   r  OverflowErrorr%   r   r)   r  r   )	r   r  v1v2mr   r  node2rJ   s	       r3   boost2Shrinker.redistribute_numeric_pairs.<locals>.boostd  s)    8=zzY7N!!"23TU  3q>)** >!BUU zzW$R4G)G**

=U[[):::,./**U[[1_u{{;< :::,./ **U[[1_./	0  !  s    
C< <
D	D	N)r}  r)   r.   rW   boolr   )rJ   rB  r  r  r  r   r  r  s   `  @@@@@r3   r   #Shrinker.redistribute_numeric_pairs>  su    		 JJC
 JJ<	
 "	S "	T "	 "	H 	Ur9   c                   ^ ^^^^ UR                  T R                  S 5      mT R                  UR                  [        TR                  S-   [	        [        T R                  5      TR                  S-   S-   5      5      U 4S j5         mTR                  S   mUUU 4S jm[        UU4S j5        [        UU4S j5        g )	Nc                 P    U R                   S:H  =(       a    U R                  (       + $ Nr)  r   r  r   s    r3   rt   2Shrinker.lower_integers_together.<locals>.<lambda>  s    !&&I"5"Gaii-"Gr9   r   r4  c                    > TR                   U    R                  S:H  =(       a    TR                   U    R                  (       + $ r  )r)   r   r   r  rJ   s    r3   rt   r    s6    $**Q-,,	9 1

10001r9   r  c                 T  > TR                  TR                  S TR                   TR                  TR                  U -
  S94-   TR                  TR                  S-   TR                   -   TR                  TR                  U -
  S94-   TR                  TR                  S-   S  -   5      $ )Nr   r   )r   r)   r  r   r.   )r   r  r  rJ   s    r3   r  2Shrinker.lower_integers_together.<locals>.consider  s    **

=U[[)::q:9;<**U[[1_u{{;< ::q:9;< **U[[1_./	0 r9   c                    > T" TU -
  5      $ r-   rA   r   r  r  s    r3   rt   r    s    x(:;r9   c                    > T" U T-
  5      $ r-   rA   r
  s    r3   rt   r    s    xN(:;r9   )r}  r)   r   r  r   r6   r/   r   )rJ   rB  r  r  r  r  s   ` @@@@r3   r    Shrinker.lower_integers_together  s    JJG
 

NNekkAos3tzz?EKK!Oa<O'PQ1
 **+;<	 	;<;<r9   c                 *  ^ ^^^^^ UR                  T R                  S 5      mT R                  UR                  [        TR                  S-   [	        [        T R                  5      TR                  S-   S-   5      5      UU 4S j5         m[        TR                  5      [        TR                  5      -  nUR                  [        U5      5      mTR                  S   mUU4S jm[        R                  " TR                  T5      UUUU 4S j5        g)	a  
Select two string choices no more than 4 choices apart and simultaneously
lower characters which appear in both strings. This helps cases where the
same character must appear in two strings, but the actual value of the
character is not relevant.

This shrinking pass currently only tries lowering *all* instances of the
duplicated character in both strings. So for instance, given two choices:

    "bbac"
    "abbb"

we would try lowering all five of the b characters simultaneously. This
may fail to shrink some cases where only certain character indices are
correlated, for instance if only the b at index 1 could be lowered
simultaneously and the rest did in fact actually have to be a `b`.

It would be nice to try shrinking that case as well, but we would need good
safeguards because it could get very expensive to try all combinations.
I expect lowering all duplicates to handle most cases in the meantime.
c                 P    U R                   S:H  =(       a    U R                  (       + $ Nr  r  r   s    r3   rt   6Shrinker.lower_duplicated_characters.<locals>.<lambda>  s    !&&H"4"FQYY"Fr9   r   r  c                   > TR                   U    R                  S:H  =(       a]    TR                   U    R                  (       + =(       a8    [        TR                  5      [        TR                   U    R                  5      -  $ r  )r)   r   r  r  r.   )r  r  rJ   s    r3   rt   r    sa    $**Q-,,8 @AAVAV=V @$s4::a=+>+>'??@r9   	intervalsc                 t   > U R                  U R                  R                  TTR                  U5      5      S9$ )Nr   )r   r.   replacechar_in_shrink_order)r2   r   charr  s     r3   	copy_node7Shrinker.lower_duplicated_characters.<locals>.copy_node  s;    
 99::--dI4R4RST4UV   r9   c                   > TR                  TR                  S TR                   T" TU 5      4-   TR                  TR                  S-   TR                   -   T" TU 5      4-   TR                  TR                  S-   S  -   5      $ r  )r   r)   r  )r   r  r  r  rJ   s    r3   rt   r    s    d--

=U[[)UA&()**U[[1_u{{;< UA&() **U[[1_./	0r9   N)r}  r)   r   r  r   r6   r  r.   r   r/   r   r   index_from_char_in_shrink_order)rJ   rB  duplicated_charactersr  r  r  r  r  s   `  @@@@@r3   r   $Shrinker.lower_duplicated_characters  s    , JJF

 

NNekkAos3tzz?EKK!Oa<O'PQ@
 !$EKK 03u{{3C C~~f%:;<%%k2		 	55d;		
r9   c                 X  ^ ^^^^ TS   R                   mTS   R                  mTS   R                  n[        UU4S jT 5       5      (       d   eTS:X  av  US   mT R	                  T[        TT-
  5      5        [        R                  " [        TT-
  5      UU U4S j5        [        R                  " [        TT-
  5      UU U4S j5        g TS:X  af  T R	                  T[        T5      5        [        R                  " [        T5      UU 4S j5        [        R                  " [        T5      UU 4S	 j5        g TS
:X  a  TSL d   eT R	                  TS5        g TS:X  a  [        R                  " TUU 4S jUS   S9  g TS:X  a#  [        R                  " TUU 4S jUS   US   S9  g [        e)Nr   c              3   |   >#    U  H1  nUR                   T:H  =(       a    [        UR                  T5      v   M3     g 7fr-   )r   r   r.   )r1   r2   choice_typer.   s     r3   r4   *Shrinker.minimize_nodes.<locals>.<genexpr>  s4      
 II$Hdjj%)HHs   9<r)  r  c                 .   > TR                  TTU -   5      $ r-   r  r   r)   rJ   r  s    r3   rt   )Shrinker.minimize_nodes.<locals>.<lambda>      $225.1:LMr9   c                 .   > TR                  TTU -
  5      $ r-   r"  r#  s    r3   rt   r$    r%  r9   r  c                 (   > TR                  TU 5      $ r-   r"  valr)   rJ   s    r3   rt   r$  	      D44UC@r9   c                 *   > TR                  TU * 5      $ r-   r"  r(  s    r3   rt   r$    s    D44USDAr9   booleanTFr  c                 (   > TR                  TU 5      $ r-   r"  r(  s    r3   rt   r$    r*  r9   r  )r  r  c                 (   > TR                  TU 5      $ r-   r"  r(  s    r3   rt   r$    r*  r9   r  )r  r  )r   r.   r/   allr  r  r   r   r   r   r!   r  )rJ   r)   r/   r  r  r.   s   `` @@@r3   r  Shrinker.minimize_nodes  s   Ahmma Ah** 

 
 
 	
 

 )#()9:N
 $$UC0F,GHNNNU*+M NNNU*+M G#$$UCJ7LLE
@ LLE
A I%D= =$$UE2G#LL@$Z0
 H$MM@%k2$Z0	 &%r9   c                    UR                  [        [        U R                  5      5      5      nU R                  nU R                  R
                  nU R                  U   nUS UR                   n[        XER                  UR                    Vs/ s HD  nUR                  (       a  UO-UR                  [        SUR                  UR                  5      S9PMF     sn5      nXER                  S  n	U R                  Xh-   U	-   5      S   n
U R                  ULa  g [        U
[         5      (       aI  U
R                  U   nU
R
                  UR                  UR                   nU R#                  Xl-   U	-   5        g g s  snf )Nr   r   r   )r}  r   r6   r  r   r)   r   r7   r   r   r   r   r   r/   r   r+  r   r   )rJ   rB  r  r^  r)   rx  r   r2   r  suffixr  new_spannew_replacements                r3   r   Shrinker.try_trivial_spans$  sU   NN5TZZ12!!""((zz!}|$ "**txx8	 9D  #4Q		4CSCS#T #  9	
 xxz"++F,@6,IJ1MT)g/00}}Q'H%mmHNNX\\JO##F$<v$EF 1#	s   AE5c           	        ^ ^ UR                  T R                  S 5      mT R                  nT R                  T/5        T R                  ULa  gTR                  S:w  a  gT R                  STR
                   TR                  TR                  S-
  S94-   T R                  TR
                  S-   S -   nT R                  U5      S   nUbp  UR                  [        R                  :  dR  [        UR                  5      [        T R                  5      :X  d&  [        UR                  5      TR
                  S-   :X  a  gUT R                  Ld   eT R                  TR
                  5      UU 4S j5       nUR                  SS/5      (       an  T R                  UR                  [        U[        T R                  5      5      U 4S	 j5         nT R!                  USUR"                   X6R$                  S -   5        gT R                  UR                  [        TR
                  S-   [        T R                  5      5      5         mT R!                  USTR
                   UTR
                  S-   S -   5        g)
a  Attempt to minimize each choice in sequence.

This is the pass that ensures that e.g. each integer we draw is a
minimum value. So it's the part that guarantees that if we e.g. do

x = data.draw(integers())
assert x < 10

then in our shrunk example, x = 10 rather than say 97.

If we are unsuccessful at minimizing a choice of interest we then
check if that's because it's changing the size of the test case and,
if so, we also make an attempt to delete parts of the test case to
see if that fixes it.

We handle most of the common cases in try_shrinking_nodes which is
pretty good at clearing out large contiguous blocks of dead space,
but it fails when there is data that has to stay in particular places
in the list.
c                 $    U R                   (       + $ r-   r  r  s    r3   rt   6Shrinker.minimize_individual_choices.<locals>.<lambda>W  s    4<<7Gr9   Nr)  r   r   c                     > Sn [        TR                  5      nU S-   U:  a@  X-   S-  nTR                  U   nUR                  TR                  :  a  UnOUn U S-   U:  a  M@  U$ )Nr   r   r   )r6   r  r   r  )ru  rv  rw  rx  r2   rJ   s       r3   first_span_after_nodeCShrinker.minimize_individual_choices.<locals>.first_span_after_node  sf    BTZZBq&2+w1nzz#::+BB q&2+ Ir9   TFc                 <   > TR                   U    R                  S:  $ r   )r  rt  r  s    r3   rt   r8    s    djjm881<r9   )r}  r)   r   r  r   r  r   r.   r   r   r   r   r6   r   r  r   r   r   r   )rJ   rB  initial_targetloweredr  r:  rx  r2   s   `      @r3   r   $Shrinker.minimize_individual_choicesB  s   * ~~djj*GH++TF#^3  99	!  JJ|$yyDJJNy356jja)*+ 	
 ++G4Q7O~~,7==!S_47==!TZZ!^3  d00000	TZZ	 
	 
!
	  >>4-((::/TZZA<D ##GLdjj$9GHHJ<O$OP::gnnU4::>3tzz?-STUD##GLdjj$9GDJJQRNDT<U$UVr9   c                   ^ ^^^ UR                  T R                  5      nUR                  UR                  5      R                  nUR                   Vs/ s H  oDR                  U:X  d  M  UPM     snm[	        T5      S::  a  gT Vs/ s H  o"R
                  UR                  4PM     snmT R                  m[        R                  " [        [	        T5      5      UU UU4S jUU4S jS9  gs  snf s  snf )a  This pass allows us to reorder the children of each span.

For example, consider the following:

.. code-block:: python

    import hypothesis.strategies as st
    from hypothesis import given


    @given(st.text(), st.text())
    def test_not_equal(x, y):
        assert x != y

Without the ability to reorder x and y this could fail either with
``x=""``, ``y="0"``, or the other way around. With reordering it will
reliably fail with ``x=""``, ``y="0"``.
r   Nc                    > TR                  [        TR                  [        TU SS9 VVVs/ s H4  u  u  pnUUTR                  TU   R                  TU   R
                   4PM6     snnn5      5      $ s  snnnf )NTr   )r   r   r)   r  r   r   )indicesr  r   r  	endpointsrJ   r  r  s       r3   rt   (Shrinker.reorder_spans.<locals>.<lambda>  s|    D33HH *-Y)M *NIFQA	 HHU1X^^eAhllC
 *N
s   ;A4c                 h   > [        TR                  TU    R                  TU    R                   5      $ r-   )r8   r)   r   r   )r  r  r  s    r3   rt   rD    s%    (288E!HNNU1X\\#JKr9   r   )r}  r  r  r  r6   r   r   r   r    r   r   )rJ   rB  rx  r  r  rC  r  r  s   `    @@@r3   r   Shrinker.reorder_spans  s    & ~~djj)t}}-33 MM>MqWW-=M>u:?8=>jj$((+>	#e* L	
 ? ?s   C,*C,!C1c                 p   U[        U5      -   [        UR                  5      :  d  US:  a  g[        UR                  5      n[        U5       HV  n[	        [        [        U5      5      5       H2  u  pxX-   n	U	[        U5      :  a      gUS:X  a  XY	 M%  [        SU< 35      e   MX     U R                  U5      $ )a  Node programs are a mini-DSL for node rewriting, defined as a sequence
of commands that can be run at some index into the nodes

Commands are:

    * "X", delete this node

This method runs the node program in ``program`` at node index
``i`` on the ConjectureData ``original``. If ``repeats > 1`` then it
will attempt to approximate the results of running it that many times.

Returns True if this successfully changes the underlying shrink target,
else False.
r   Fr~   zUnrecognised command )r6   r)   r   r   r  r#  r  r   )
rJ   r  r  r  r  r  r  r   commandr/  s
             r3   r  Shrinker.run_node_program  s     s7|c(..11QUx~~&wA&tIg,>'?@
EG$ c>
-0Eg[.QRR A   &&w//r9   )__all_changed_nodes__allow_transition__derived_values__extend__last_checked_changed_at__predicater   rk   r   r   r   r   r   r   r   rE   )r*   N)r*   r&   )r   )IrH   rR   rS   rT   __doc__rh   r   r   r(   r   r  r   r   r   rg   rW   rC   rD   r   r   r   r7   r   r   r   r   rV   r   r   r   ro   r   r   r&  r   r,  rh  r   r"   r;  r=   rF  r   r  r)   r   r   r   r  ri  rl  r   r  r   r  r  r  r   r  rQ  r  r   r  r   r   r   r   r  r   r   r   r  rX   rA   r9   r3   r]   r]   t   s   DL 6 !&=&"=&  "22=& $d*	=& n'77H$NORVV=& =& =&~   &s & &
 ,C , , <j)<	t%0477	8<03(< 3 3
,   " "6pWr6' ) );z> '$sJ*? ' ' EJ 
 T d <\44
+; \4 \4| (uZ_- ( ( *w|, * * (u ( ( 	$ 	$ 4 48
tN%`)$ (S ( (2# N`(T ) )
C 
J 

B#:JX=@;
z;&zG<bWH-
^0r9   r]   )<r  collectionsr   collections.abcr   r   dataclassesr   typingr   r   r	   r
   r   %hypothesis.internal.conjecture.choicer   r   r   r   r   r   r   #hypothesis.internal.conjecture.datar   r   r   r   r   r   )hypothesis.internal.conjecture.junkdrawerr   r   r   r   (hypothesis.internal.conjecture.shrinkingr   r   r   r    r!   3hypothesis.internal.conjecture.shrinking.choicetreer"   r#   r$   hypothesis.internal.floatsr%   r   r&   %hypothesis.internal.conjecture.enginer'   r  r(   rU   r7   rW   r8   r=   	ExceptionrZ   r]   rA   r9   r3   <module>r]     s     # . !       
 ;F&(88(C'Dd'JK ) KHZ( U3c3h3G-H 0 e$  %&	I 	~0 ~0r9   