Ë
    7^(h}  ã                   óˆ   — d Z ddlmZ  G d„ d«      Z G d„ d«      Z G d„ d«      Zdd
„Zd„ Zd„ Zd„ Z	d„ Z
d„ Zd„ Zd„ Zd„ Zy	)aG   Generic Unification algorithm for expression trees with lists of children

This implementation is a direct translation of

Artificial Intelligence: A Modern Approach by Stuart Russel and Peter Norvig
Second edition, section 9.2, page 276

It is modified in the following ways:

1.  We allow associative and commutative Compound expressions. This results in
    combinatorial blowup.
2.  We explore the tree lazily.
3.  We provide generic interfaces to symbolic algebra libraries in Python.

A more traditional version can be found here
http://aima.cs.berkeley.edu/python/logic.html
é    )Úkbinsc                   ó(   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zy)ÚCompoundzr A little class to represent an interior node in the tree

    This is analogous to SymPy.Basic for non-Atoms
    c                 ó    — || _         || _        y ©N)ÚopÚargs)Úselfr   r	   s      úN/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/unify/core.pyÚ__init__zCompound.__init__   s   € ØˆŒØˆ�	ó    c                 óš   — t        | «      t        |«      u xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )Útyper   r	   ©r
   Úothers     r   Ú__eq__zCompound.__eq__   s@   € Ü�T“
œd 5›kÐ)ò (¨d¯g©g¸¿¹Ñ.Aò (Ø—	‘	˜UŸZ™ZÑ'ð	)r   c                 óX   — t        t        | «      | j                  | j                  f«      S r   )Úhashr   r   r	   ©r
   s    r   Ú__hash__zCompound.__hash__"   s    € Ü”T˜$“Z §¡¨$¯)©)Ð4Ó5Ð5r   c                 ó†   — t        | j                  «      ›ddj                  t        t         | j                  «      «      ›d�S )Nú[z, ú])Ústrr   ÚjoinÚmapr	   r   s    r   Ú__str__zCompound.__str__%   s)   € Ü˜tŸw™w�<¨¯©´3´s¸D¿I¹IÓ3FÕ)GÐHÐHr   N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   © r   r   r   r      s   „ ñòò)ò6óIr   r   c                   ó(   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zy)ÚVariablez A Wild token c                 ó   — || _         y r   )Úarg)r
   r'   s     r   r   zVariable.__init__*   s	   € Øˆ�r   c                 ód   — t        | «      t        |«      u xr | j                  |j                  k(  S r   )r   r'   r   s     r   r   zVariable.__eq__-   s'   € Ü�D‹zœT %›[Ð(ÒB¨T¯X©X¸¿¹Ñ-BÐBr   c                 óB   — t        t        | «      | j                  f«      S r   )r   r   r'   r   s    r   r   zVariable.__hash__0   s   € Ü”T˜$“Z §¡Ð*Ó+Ð+r   c                 ó2   — dt        | j                  «      z  S )NzVariable(%s)©r   r'   r   s    r   r   zVariable.__str__3   s   € Ø¤ D§H¡H£Ñ-Ð-r   Nr   r#   r   r   r%   r%   (   s   „ ÙòòCò,ó.r   r%   c                   ó(   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zy)ÚCondVariablez… A wild token that matches conditionally.

    arg   - a wild token.
    valid - an additional constraining function on a match.
    c                 ó    — || _         || _        y r   )r'   Úvalid)r
   r'   r/   s      r   r   zCondVariable.__init__<   s   € ØˆŒØˆ�
r   c                 óš   — t        | «      t        |«      u xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r   r'   r/   r   s     r   r   zCondVariable.__eq__@   sA   € Ü�T“
œd 5›kÐ)ò *Ø—‘˜EŸI™IÑ%ò*à—
‘
˜eŸk™kÑ)ð	+r   c                 óX   — t        t        | «      | j                  | j                  f«      S r   )r   r   r'   r/   r   s    r   r   zCondVariable.__hash__E   s    € Ü”T˜$“Z §¡¨4¯:©:Ð6Ó7Ð7r   c                 ó2   — dt        | j                  «      z  S )NzCondVariable(%s)r+   r   s    r   r   zCondVariable.__str__H   s   € Ø!¤C¨¯©£MÑ1Ð1r   Nr   r#   r   r   r-   r-   6   s   „ ñò
ò+ò
8ó2r   r-   Nc              +   ó¸  K  — |xs i }| |k(  r|–— yt        | t        t        f«      rt        | ||fi |¤ŽE d{  –—†  yt        |t        t        f«      rt        || |fi |¤ŽE d{  –—†  yt        | t        «      �rÍt        |t        «      �r¼|j                  dd„ «      }|j                  dd„ «      }t        | j                  |j                  |fi |¤ŽD �]o  } || «      �r ||«      �rt        | j                  «      t        |j                  «      k  r| |fn|| f\  }} || «      r* ||«      r"t        |j                  |j                  d«      }	n!t        |j                  |j                  d«      }	|	D ]s  \  }
}|
D �cg c]!  }t        t	        |j                  |«      «      ‘Œ# }}|D �cg c]!  }t        t	        |j                  |«      «      ‘Œ# }}t        |||fi |¤ŽE d{  –—†  Œu �Œt        | j                  «      t        |j                  «      k(  s�ŒFt        | j                  |j                  |fi |¤ŽE d{  –—†  �Œr yt        | «      rmt        |«      rat        | «      t        |«      k(  rIt        | «      dk(  r|–— yt        | d   |d   |fi |¤ŽD ]  }t        | d	d |d	d |fi |¤ŽE d{  –—†  Œ  yyyy7 �ŒŠ7 �Œ`c c}w c c}w 7 Œì7 Œ“7 Œ­w)
a
   Unify two expressions.

    Parameters
    ==========

        x, y - expression trees containing leaves, Compounds and Variables.
        s    - a mapping of variables to subtrees.

    Returns
    =======

        lazy sequence of mappings {Variable: subtree}

    Examples
    ========

    >>> from sympy.unify.core import unify, Compound, Variable
    >>> expr    = Compound("Add", ("x", "y"))
    >>> pattern = Compound("Add", ("x", Variable("a")))
    >>> next(unify(expr, pattern, {}))
    {Variable(a): 'y'}
    NÚis_commutativec                  ó   — y©NFr#   ©Úxs    r   ú<lambda>zunify.<locals>.<lambda>k   ó   � r   Úis_associativec                  ó   — yr6   r#   r7   s    r   r9   zunify.<locals>.<lambda>l   r:   r   ÚcommutativeÚassociativer   é   )Ú
isinstancer%   r-   Ú	unify_varr   ÚgetÚunifyr   Úlenr	   ÚallcombinationsÚunpackÚis_args)r8   ÚyÚsÚfnsr4   r;   ÚsopÚaÚbÚcombsÚaaargsÚbbargsr'   ÚaaÚbbÚsheads                   r   rC   rC   K   s~  è ø€ ð. 	
ŠˆR€AàˆA‚vØ‹Ü	�Aœ¤,Ð/Ô	0Ü˜Q  1Ñ,¨Ñ,×,Ñ,Ü	�Aœ¤,Ð/Ô	0Ü˜Q  1Ñ,¨Ñ,×,Ñ,Ü	�A”xÕ	 ¤Z°´8Õ%<ØŸ™Ð!1±?ÓCˆØŸ™Ð!1±?ÓCˆÜ˜Ÿ™˜qŸt™t QÑ.¨#Ñ.ó 	=ˆCÙ˜aÕ ¡^°AÕ%6Ü!$ Q§V¡V£¬s°1·6±6«{Ò!:˜˜1‘vÀÀAÀ‘��1Ù! !Ô$©¸Ô):Ü+¨A¯F©F°A·F±F¸MÓJ‘Eä+¨A¯F©F°A·F±F¸MÓJ�EØ&+ò 9‘N�F˜FØAGÖH¸#œ&¤¨!¯$©$°Ó!4Õ5ÐH�BÐHØAGÖH¸#œ&¤¨!¯$©$°Ó!4Õ5ÐH�BÐHÜ$ R¨¨SÑ8°CÑ8×8Ñ8ò9ô �Q—V‘V“¤ A§F¡F£Ô+Ü  §¡¨¯©°Ñ<¸Ñ<×<Ò<ñ	=ô 
�Œœ œ
¤s¨1£v´°Q³Ò'7Üˆq‹6�QŠ;Ø‹Gä˜q ™t Q q¡T¨1Ñ4°Ñ4ò =�Ü   1 2 ¨¨!¨"¨¨uÑ<¸Ñ<×<Ñ<ñ=ð	 (8˜
ˆð) 	-ùà,ûò IùÚHØ8øà<øð =úsw   ‚8KºK»,KÁ'KÁ(DKÆ&K
Æ(KÆ.&KÇKÇ'KÇ(3KÈ%KÉKÉA6KÊ9KÊ:KËKË
KËKËKc              +   ó  K  — | |v rt        ||    ||fi |¤ŽE d {  –—†  y t        | |«      ry t        | t        «      r!| j	                  |«      rt        || |«      –— y t        | t        «      rt        || |«      –— y y 7 Œd­wr   )rC   Úoccur_checkr@   r-   r/   Úassocr%   )Úvarr8   rI   rJ   s       r   rA   rA   ‚   s{   è ø€ Ø
ˆa�xÜ˜˜3™  AÑ-¨Ñ-×-Ñ-Ü	�S˜!Ô	ØÜ	�CœÔ	&¨3¯9©9°Q¬<Ü�A�s˜AÓÓÜ	�CœÔ	"Ü�A�s˜AÓÓð 
#ð 	.ús   ‚B›B œA%Bc                 óž   ‡ — ‰ |k(  ryt        |t        «      rt        ‰ |j                  «      S t	        |«      rt        ˆ fd„|D «       «      ryy)z# var occurs in subtree owned by x? Tc              3   ó6   •K  — | ]  }t        ‰|«      –— Œ y ­wr   )rU   )Ú.0ÚxirW   s     €r   ú	<genexpr>zoccur_check.<locals>.<genexpr>“   s   øè ø€ Ò0¨Œ{˜3 ×#Ñ0ùs   ƒF)r@   r   rU   r	   rG   Úany)rW   r8   s   ` r   rU   rU   Œ   sB   ø€ à
ˆa‚xØÜ	�A”xÔ	 Ü˜3 §¡Ó'Ð'Ü	�ŒÜÓ0¨aÔ0Ô0¸Ør   c                 ó0   — | j                  «       } || |<   | S )z- Return copy of d with key associated to val )Úcopy)ÚdÚkeyÚvals      r   rV   rV   –   s   € à	�‰‹€AØ€A€c�FØ€Hr   c                 ó:   — t        | «      t        t        t        fv S )z Is x a traditional iterable? )r   ÚtupleÚlistÚsetr7   s    r   rG   rG   œ   s   € ä�‹7”uœd¤CÐ(Ð(Ð(r   c                 ót   — t        | t        «      r't        | j                  «      dk(  r| j                  d   S | S )Nr?   r   )r@   r   rD   r	   r7   s    r   rF   rF       s.   € Ü�!”XÔ¤3 q§v¡v£;°!Ò#3Ø�v‰v�a‰yÐàˆr   c           	   #   óf  K  — |dk(  rd}|dk(  rd}t        | «      t        |«      k  r| |fn|| f\  }}t        t        t        t        |«      «      «      t        |«      |¬«      D ]H  }||k(  r!t	        d„ | D «       «      t        ||«      f–— Œ)t        | |«      t	        d„ |D «       «      f–— ŒJ y­w)a  
    Restructure A and B to have the same number of elements.

    Parameters
    ==========

    ordered must be either 'commutative' or 'associative'.

    A and B can be rearranged so that the larger of the two lists is
    reorganized into smaller sublists.

    Examples
    ========

    >>> from sympy.unify.core import allcombinations
    >>> for x in allcombinations((1, 2, 3), (5, 6), 'associative'): print(x)
    (((1,), (2, 3)), ((5,), (6,)))
    (((1, 2), (3,)), ((5,), (6,)))

    >>> for x in allcombinations((1, 2, 3), (5, 6), 'commutative'): print(x)
        (((1,), (2, 3)), ((5,), (6,)))
        (((1, 2), (3,)), ((5,), (6,)))
        (((1,), (3, 2)), ((5,), (6,)))
        (((1, 3), (2,)), ((5,), (6,)))
        (((2,), (1, 3)), ((5,), (6,)))
        (((2, 1), (3,)), ((5,), (6,)))
        (((2,), (3, 1)), ((5,), (6,)))
        (((2, 3), (1,)), ((5,), (6,)))
        (((3,), (1, 2)), ((5,), (6,)))
        (((3, 1), (2,)), ((5,), (6,)))
        (((3,), (2, 1)), ((5,), (6,)))
        (((3, 2), (1,)), ((5,), (6,)))
    r=   é   r>   N)Úorderedc              3   ó"   K  — | ]  }|f–— Œ	 y ­wr   r#   )rZ   rL   s     r   r\   z"allcombinations.<locals>.<genexpr>Ð   s   è ø€ Ò( ˜œÑ(ùó   ‚c              3   ó"   K  — | ]  }|f–— Œ	 y ­wr   r#   )rZ   rM   s     r   r\   z"allcombinations.<locals>.<genexpr>Ò   s   è ø€ Ò+<°Q¨Q¬DÑ+<ùrl   )rD   r   re   Úrangerd   Ú	partition)ÚAÚBrj   ÚsmÚbgÚparts         r   rE   rE   ¦   s®   è ø€ ðF �-ÒØˆØ�-ÒØˆÜ˜1“v¤ A£’ˆa�‰V¨Q°¨F�F€BˆÜ”dœ5¤ R£›>Ó*¬C°«G¸WÔEò =ˆØ�Š7ÜÑ( aÔ(Ó(¬)°A°tÓ*<Ð<Ó<ä˜A˜tÓ$¤eÑ+<¸!Ô+<Ó&<Ð<Ó<ñ	=ùs   ‚B/B1c           	      ó^   —  t        | «      |D �cg c]  }t        | |«      ‘Œ c}«      S c c}w )z× Partition a tuple/list into pieces defined by indices.

    Examples
    ========

    >>> from sympy.unify.core import partition
    >>> partition((10, 20, 30, 40), [[0, 1, 2], [3]])
    ((10, 20, 30), (40,))
    )r   Úindex)Úitrt   Úinds      r   ro   ro   Ô   s)   € ð Œ4�‹8¨tÖ4¨”U˜2˜s•^Ò4Ó5Ð5ùÒ4s   �*c                 óP   —  t        | «      |D �cg c]  }| |   ‘Œ	 c}«      S c c}w )z½ Fancy indexing into an indexable iterable (tuple, list).

    Examples
    ========

    >>> from sympy.unify.core import index
    >>> index([10, 20, 30], (1, 2, 0))
    [20, 30, 10]
    )r   )rw   rx   Úis      r   rv   rv   à   s'   € ð Œ4�‹8 CÖ(˜q�R˜“UÒ(Ó)Ð)ùÒ(s   �#r   )r"   Úsympy.utilities.iterablesr   r   r%   r-   rC   rA   rU   rV   rG   rF   rE   ro   rv   r#   r   r   ú<module>r|      s^   ðñõ$ ,÷Iñ I÷&.ñ .÷2ñ 2ó*5=ònòòò)òò,=ò\
6ó
*r   