U
    yh                     @   sn   d dl mZmZ d dlmZ dd Zdd Zdd Zd	d
 Zeee	dd Z
dd Zeeee	dd ZdS )   )unifyreify)dispatchc                 C   s&   t | | tft t| tft | S )a   Register standard unify and reify operations on class
    This uses the type and __dict__ or __slots__ attributes to define the
    nature of the term
    See Also:
    >>> # xdoctest: +SKIP
    >>> class A(object):
    ...     def __init__(self, a, b):
    ...         self.a = a
    ...         self.b = b
    >>> unifiable(A)
    <class 'unification.more.A'>
    >>> x = var('x')
    >>> a = A(1, 2)
    >>> b = A(1, x)
    >>> unify(a, b, {})
    {~x: 2}
    )_unifyadddictunify_object_reifyreify_object)cls r   X/var/www/html/venv/lib/python3.8/site-packages/torch/fx/experimental/unification/more.py	unifiable   s    r   c                 C   s"   t | drt| |S t| |S dS )a   Reify a Python object with a substitution
    >>> # xdoctest: +SKIP
    >>> class Foo(object):
    ...     def __init__(self, a, b):
    ...         self.a = a
    ...         self.b = b
    ...     def __str__(self):
    ...         return "Foo(%s, %s)"%(str(self.a), str(self.b))
    >>> x = var('x')
    >>> f = Foo(1, x)
    >>> print(f)
    Foo(1, ~x)
    >>> print(reify_object(f, {x: 2}))
    Foo(1, 2)
    	__slots__N)hasattr_reify_object_slots_reify_object_dictosr   r   r   r
   #   s    

r
   c                 C   s8   t t| }t| j|}|| jkr(| S |j| |S )N)object__new__typer   __dict__update)r   r   objdr   r   r   r   9   s    
r   c                    sb    fdd j D }t||}||kr* S tt }t j |D ]\}}t||| qD|S d S )Nc                    s   g | ]}t  |qS r   getattr).0attrr   r   r   
<listcomp>C   s     z'_reify_object_slots.<locals>.<listcomp>)r   r   r   r   r   zipsetattr)r   r   attrsZ	new_attrsZnewobjslotr    r   r!   r   r   B   s    
r   c                 C   s   t t| j| j| jf| S )z! Reify a Python ``slice`` object )slicer   startstopstepr   r   r   r   r	   N   s    r	   c                    s^   t  t krdS t drJt fdd jD fddjD |S t jj|S dS )a   Unify two Python objects
    Unifies their type and ``__dict__`` attributes
    >>> # xdoctest: +SKIP
    >>> class Foo(object):
    ...     def __init__(self, a, b):
    ...         self.a = a
    ...         self.b = b
    ...     def __str__(self):
    ...         return "Foo(%s, %s)"%(str(self.a), str(self.b))
    >>> x = var('x')
    >>> f = Foo(1, x)
    >>> g = Foo(1, 2)
    >>> unify_object(f, g, {})
    {~x: 2}
    Fr   c                    s   g | ]}t  |qS r   r   r   r&   )ur   r   r"   l   s     z unify_object.<locals>.<listcomp>c                    s   g | ]}t  |qS r   r   r+   )vr   r   r"   m   s     N)r   r   r   r   r   r,   r-   r   r   )r,   r-   r   r   Y   s    
r   c                 C   s$   t | j| j| jf|j|j|jf|S )z! Unify a Python ``slice`` object )r   r(   r)   r*   r.   r   r   r   r   s   s    r   N)corer   r   r   r   r
   r   r   r'   r   r	   r   r   r   r   r   r   <module>   s   	


