o
    ‡ZŽhV  ã                   @   s†   d dl mZmZ d dlmZmZ d dlmZmZ d dl	m
Z
 G dd„ dƒZG dd„ deƒZi Zd	d
„ Zdd„ Zefdd„Zdd„ ZdS )é   )ÚreifyÚunify)ÚfirstÚgroupby)Ú	_toposortÚfreeze©Úisvarc                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
Dispatcherc                 C   s   || _ i | _g | _d S ©N)ÚnameÚfuncsÚordering)Úselfr   © r   úV/var/www/auris/lib/python3.10/site-packages/torch/fx/experimental/unification/match.pyÚ__init__	   s   
zDispatcher.__init__c                 C   s   || j t|ƒ< t| j ƒ| _d S r   )r   r   r   )r   Ú	signatureÚfuncr   r   r   Úadd   s   zDispatcher.addc                 O   s   |   |¡\}}||i |¤ŽS r   )Úresolve)r   ÚargsÚkwargsr   Ú_r   r   r   Ú__call__   s   zDispatcher.__call__c                 C   sl   t |ƒ}| jD ]}t |ƒ|krqtt|ƒ|ƒ}|dur&| j| }||f  S qtdt| jƒ d t|ƒ ƒ‚)NFz No match found. 
Known matches: z
Input: )Úlenr   r   r   r   ÚNotImplementedErrorÚstr)r   r   Únr   ÚsÚresultr   r   r   r      s$   

þÿþýÿzDispatcher.resolvec                    s   ‡ ‡fdd„}|S )Nc                    s   ˆ   ˆ| ¡ ˆ S r   )r   )r   ©r   r   r   r   r   '   s   zDispatcher.register.<locals>._r   )r   r   r   r   r!   r   Úregister&   s   zDispatcher.registerN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r"   r   r   r   r   r
      s    r
   c                   @   s   e Zd ZdZdd„ ZdS )ÚVarDispatcheraX  A dispatcher that calls functions with variable names
    >>> # xdoctest: +SKIP
    >>> d = VarDispatcher("d")
    >>> x = var("x")
    >>> @d.register("inc", x)
    ... def f(x):
    ...     return x + 1
    >>> @d.register("double", x)
    ... def f(x):
    ...     return x * 2
    >>> d("inc", 10)
    11
    >>> d("double", 10)
    20
    c                 O   s.   |   |¡\}}dd„ | ¡ D ƒ}|di |¤ŽS )Nc                 S   s   i | ]\}}|j |“qS r   )Útoken©Ú.0ÚkÚvr   r   r   Ú
<dictcomp>A   s    z*VarDispatcher.__call__.<locals>.<dictcomp>r   )r   Úitems)r   r   r   r   r   Údr   r   r   r   ?   s   zVarDispatcher.__call__N)r#   r$   r%   Ú__doc__r   r   r   r   r   r&   .   s    r&   c                     s,   |  dt¡‰|  dt¡‰ ‡ ‡‡fdd„}|S )NÚ	namespacer
   c                    s2   | j }|ˆvrˆ |ƒˆ|< ˆ| }| ˆ| ¡ |S r   )r#   r   )r   r   r.   ©Ú
dispatcherr0   r   r   r   r   L   s   zmatch.<locals>._)ÚgetÚglobal_namespacer
   )r   r   r   r   r1   r   ÚmatchH   s   r5   c                 C   sd   t |ƒr
t | ƒs
dS t| |ƒ}|du rdS dd„ | ¡ D ƒ}t| |ƒ| kr'dS t||ƒ|kr0dS dS )z)``a`` is a more specific match than ``b``TFc                 S   s&   i | ]\}}t |ƒrt |ƒs||“qS r   r   r(   r   r   r   r,   a   s   & zsupercedes.<locals>.<dictcomp>N)r	   r   r-   r   )ÚaÚbr   r   r   r   Ú
supercedesZ   s   
ÿr8   c                 C   s,   t | |ƒrt || ƒr|| ƒ||ƒkS dS dS )zUA should be checked before B
    Tie broken by tie_breaker, defaults to ``hash``
    TF)r8   )r6   r7   Ztie_breakerr   r   r   Úedgei   s
   

r9   c                    s^   t ttˆ ƒƒ‰ ‡ fdd„ˆ D ƒ}tt|ƒ}ˆ D ]
}||vr!g ||< qdd„ | ¡ D ƒ}t|ƒS )z€A sane ordering of signatures to check, first to last
    Topological sort of edges as given by ``edge`` and ``supercedes``
    c                    s(   g | ]}ˆ D ]}t ||ƒr||f‘qqS r   )r9   ©r)   r6   r7   ©Ú
signaturesr   r   Ú
<listcomp>{   s   ( zordering.<locals>.<listcomp>c                 S   s    i | ]\}}|d d„ |D ƒ“qS )c                 S   s   g | ]\}}|‘qS r   r   r:   r   r   r   r=   €   s    z'ordering.<locals>.<dictcomp>.<listcomp>r   r(   r   r   r   r,   €   s     zordering.<locals>.<dictcomp>)ÚlistÚmapÚtupler   r   r-   r   )r<   Úedgesr   r   r;   r   r   v   s   
€r   N)Úcorer   r   Zunification_toolsr   r   Úutilsr   r   Úvariabler	   r
   r&   r4   r5   r8   Úhashr9   r   r   r   r   r   Ú<module>   s   &