+
    i}                         R t ^ RIHt  ! R R4      t ! R R4      t ! R R4      tRR	 ltR
 tR tR t	R t
R tR tR tR tR# )aF  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                   <   a  ] tR t^t o RtR tR tR tR tRt	V t
R# )CompoundziA little class to represent an interior node in the tree

This is analogous to SymPy.Basic for non-Atoms
c                    Wn         W n        R # N)opargs)selfr   r   s   &&&N/var/www/html/photoedit/myenv/lib/python3.14/site-packages/sympy/unify/core.py__init__Compound.__init__   s    	    c                    \        V 4      \        V4      J ;'       d;    V P                  VP                  8H  ;'       d    V P                  VP                  8H  # r   )typer   r   r	   others   &&r
   __eq__Compound.__eq__   sJ    T
d5k) ( (dgg.A ( (		UZZ'	)r   c                X    \        \        V 4      V P                  V P                  34      # r   )hashr   r   r   r	   s   &r
   __hash__Compound.__hash__"   s     T$Z$))455r   c                    \        V P                  4      : R RP                  \        \         V P                  4      4      : R2# )[z, ])strr   joinmapr   r   s   &r
   __str__Compound.__str__%   s)    tww<3sDII3F)GHHr   )r   r   N__name__
__module____qualname____firstlineno____doc__r   r   r   r   __static_attributes____classdictcell____classdict__s   @r
   r   r      s%     )6I Ir   r   c                   <   a  ] tR t^(t o RtR tR tR tR tRt	V t
R# )VariablezA Wild token c                    Wn         R # r   arg)r	   r/   s   &&r
   r   Variable.__init__*   s    r   c                p    \        V 4      \        V4      J ;'       d    V P                  VP                  8H  # r   )r   r/   r   s   &&r
   r   Variable.__eq__-   s*    DzT%[(BBTXX-BBr   c                B    \        \        V 4      V P                  34      # r   )r   r   r/   r   s   &r
   r   Variable.__hash__0   s    T$Z*++r   c                :    R \        V P                  4      ,          # )zVariable(%s)r   r/   r   s   &r
   r   Variable.__str__3   s    DHH--r   r.   Nr!   r)   s   @r
   r,   r,   (   s"     C,. .r   r,   c                   <   a  ] tR t^6t o RtR tR tR tR tRt	V t
R# )CondVariablezxA wild token that matches conditionally.

arg   - a wild token.
valid - an additional constraining function on a match.
c                    Wn         W n        R # r   r/   valid)r	   r/   r<   s   &&&r
   r   CondVariable.__init__<   s    
r   c                    \        V 4      \        V4      J ;'       d;    V P                  VP                  8H  ;'       d    V P                  VP                  8H  # r   )r   r/   r<   r   s   &&r
   r   CondVariable.__eq__@   sK    T
d5k) * *EII%* *

ekk)	+r   c                X    \        \        V 4      V P                  V P                  34      # r   )r   r   r/   r<   r   s   &r
   r   CondVariable.__hash__E   s     T$Z4::677r   c                :    R \        V P                  4      ,          # )zCondVariable(%s)r6   r   s   &r
   r   CondVariable.__str__H   s    !CM11r   r;   Nr!   r)   s   @r
   r9   r9   6   s#     
+
82 2r   r9   Nc              +    "   T;'       g    / pW8X  d   Vx  R# \        V \        \        34      '       d   \        WV3/ VB  Rj  xL
  R# \        V\        \        34      '       d   \        WV3/ VB  Rj  xL
  R# \        V \        4      '       Ed   \        V\        4      '       Ed   VP                  RR 4      pVP                  RR 4      p\        V P                  VP                  V3/ VB  EF  pV! V 4      '       Ed#   V! V4      '       Ed   \        V P                  4      \        VP                  4      8  d   W3MW3w  rxV! V 4      '       d1   V! V4      '       d#   \        VP                  VP                  R4      p	M!\        VP                  VP                  R4      p	V	 Fv  w  rV
 Uu. uF"  p\        \	        VP                  V4      4      NK$  	  ppV Uu. uF"  p\        \	        VP                  V4      4      NK$  	  pp\        WV3/ VB  Rj  xL
  Kx  	  EK5  \        V P                  4      \        VP                  4      8X  g   EKe  \        V P                  VP                  V3/ VB  Rj  xL
  EK  	  R# \        V 4      '       d   \        V4      '       d{   \        V 4      \        V4      8X  d`   \        V 4      ^ 8X  d   Vx  R# \        V ^ ,          V^ ,          V3/ VB  F'  p\        V R,          VR,          V3/ VB  Rj  xL
  K)  	  R# R# R# R#  EL ELu upi u upi  EL L L%5i)	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                     R # F xs   &r
   <lambda>unify.<locals>.<lambda>k       Ur   is_associativec                     R # rG   rH   rI   s   &r
   rK   rL   l   rM   r   commutativeassociative:   NN)
isinstancer,   r9   	unify_varr   getunifyr   lenr   allcombinationsunpackis_args)rJ   ysfnsrE   rN   sopabcombsaaargsbbargsr/   aabbsheads   &&&,            r
   rV   rV   K   sw    . 	
RAv	A,/	0	0Q1,,,,	A,/	0	0Q1,,,,	Ax	 	 Z8%<%<!1?C!1?CqttQ.#.Ca  ^A%6%6!$QVVs166{!:v!!$$):):+AFFAFFMJE+AFFAFFMJE&+NFAGH#&!$$!45BHAGH#&!$$!45BH$RS8C888 ', QVVAFF+ <<<< / 


s1vQ'7q6Q;GqtQqT144 2"u<<<< 5	 (8
) 	-, IH8< =s   AML-2M:L0;BMAM*AM8(L3 M&(L8M L=!5M%MM BMMM0M3M MMc              +   D  "   W9   d   \        W ,          W3/ VB  R j  xL
  R # \        W4      '       d   R # \        V \        4      '       d(   V P	                  V4      '       d   \        W V4      x  R # \        V \        4      '       d   \        W V4      x  R # R #  L~5ir   )rV   occur_checkrS   r9   r<   assocr,   )varrJ   r\   r]   s   &&&,r
   rT   rT      sy     
x----	S			C	&	&399Q<<AA	C	"	"AA 
# 	.s   B BAB &9B c                  a  S V8X  d   R# \        V\        4      '       d   \        S VP                  4      # \	        V4      '       dC   \
        ;QJ d    V 3R lV 4       F  '       g   K   RM	  RM! V 3R lV 4       4      '       d   R# R# )z"var occurs in subtree owned by x? Tc              3   <   <"   T F  p\        SV4      x  K  	  R # 5ir   )rh   ).0xirj   s   & r
   	<genexpr>occur_check.<locals>.<genexpr>   s     0a{3##as   F)rS   r   rh   r   rZ   any)rj   rJ   s   f&r
   rh   rh      sZ    
ax	Ax	 	 3''	30a03330a000r   c                .    V P                  4       p W V&   V # )z,Return copy of d with key associated to val )copy)dkeyvals   &&&r
   ri   ri      s    	AcFHr   c                <    \        V 4      \        \        \        39   # )zIs x a traditional iterable? )r   tuplelistsetrI   s   &r
   rZ   rZ      s    7udC(((r   c                     \        V \        4      '       d.   \        V P                  4      ^8X  d   V P                  ^ ,          # V # )rR   )rS   r   rW   r   rI   s   &r
   rY   rY      s1    !X3qvv;!#3vvayr   c           	   #    "   VR8X  d   ^pVR8X  d   Rp\        V 4      \        V4      8  d   W3MW3w  r4\        \        \        \        V4      4      4      \        V4      VR7       F}  pWA8X  d<   \        ;QJ d    . R V  4       F  NK  	  5M! R V  4       4      \        W4      3x  KD  \        W4      \        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      3x  K  	  R# 5i)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,)))
rP   rQ   N)orderedc              3   &   "   T F  q3x  K	  	  R # 5ir   rH   )rm   r_   s   & r
   ro   "allcombinations.<locals>.<genexpr>   s     (aa   c              3   &   "   T F  q3x  K	  	  R # 5ir   rH   )rm   r`   s   & r
   ro   r      s     +<!QD!r   )rW   r   ry   rangerx   	partition)ABr}   smbgparts   &&&   r
   rX   rX      s     F --1vAaVQFFBd5R>*CGWE7%(a(%%(a(()A*<<<A$ee+<!+<ee+<!+<&<<<	 Fs   A9C+<A/C+c           	     `    \        V 4      ! V Uu. uF  p\        W4      NK  	  up4      # u upi )zPartition 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)itr   inds   && r
   r   r      s)     8t4tU2^t4554s   +c                Z    \        V 4      ! V Uu. uF  q V,          NK  	  up4      # u upi )zFancy 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   )r   r   is   && r
   r   r      s)     8C(CqUUC())(s   (r   )r&   sympy.utilities.iterablesr   r   r,   r9   rV   rT   rh   ri   rZ   rY   rX   r   r   rH   r   r
   <module>r      s^   $ ,I I&. .2 2*5=n),=\
6
*r   