B
    `e                 @   sx  d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZmZ G dd	 d	eZG d
d deZG dd deZG dd deZdd Zdd Zd3ddZdd Z dd Z!dd Z"dd Z#G dd de$Z%d4d d!Z&d"d# Z'G d$d% d%eZ(G d&d' d'eZ)G d(d) d)e$Z*d*d+ Z+d,d- Z,d.d/ Z-d0d1 Z.e/d2krte.  dS )5z;
Module for a resolution-based First Order theorem prover.
    N)defaultdict)reduce)	skolemize)VariableExpressionEqualityExpressionApplicationExpression
ExpressionNegatedExpressionVariableAndExpressionunique_variableOrExpression	is_indvarIndividualVariableExpressionr   )ProverBaseProverCommandc               @   s   e Zd ZdS )ProverParseErrorN)__name__
__module____qualname__ r   r   H/home/dcms/DCMS/lib/python3.7/site-packages/nltk/inference/resolution.pyr   $   s   r   c               @   s&   e Zd ZdZdZd	ddZdd ZdS )
ResolutionProverZANSWERTNFc          
   C   s   |sg }d}yXg }|r&| t|  x|D ]}| t| q,W | |\}}|rbtt| W nR tk
r } z4| jrt|	drd}g }n|rt| n|W dd}~X Y nX ||fS )z
        :param goal: Input expression to prove
        :type goal: sem.Expression
        :param assumptions: Input expressions to use as assumptions in the proof
        :type assumptions: list(sem.Expression)
        Nz maximum recursion depth exceededF)
extendclausify_attempt_proofprintResolutionProverCommand_decorate_clausesRuntimeError_assume_falsestr
startswith)selfgoalassumptionsverboseresultclausesaer   r   r   _prove,   s*    

zResolutionProver._provec             C   s   t t}d}x|t|k r||  s|| r@|| d d }n|d }x|t|k r||kr|r||  s|| | || || }|rx8|D ]0}|d |d f|_|| t|sd|fS qW d}P |d7 }qJW |d7 }qW d|fS )Nr      TF)r   listlenis_tautologyappendunify_parents)r#   r(   Ztriedij
newclauses	newclauser   r   r   r   M   s,    

zResolutionProver._attempt_proof)NNF)r   r   r   
ANSWER_KEYr    r+   r   r   r   r   r   r   (   s   
!r   c               @   s6   e Zd ZdddZdddZdddZed	d
 ZdS )r   Nc             C   s8   |dk	rt |tstnt }t| ||| d| _dS )z
        :param goal: Input expression to prove
        :type goal: sem.Expression
        :param assumptions: Input expressions to use as assumptions in
            the proof.
        :type assumptions: list(sem.Expression)
        N)
isinstancer   AssertionErrorr   __init___clauses)r#   r$   r%   Zproverr   r   r   r;   o   s
    z ResolutionProverCommand.__init__Fc             C   s@   | j dkr:| j|  |  |\| _ }|| _t|| _| j S )zh
        Perform the actual proof.  Store the result to prevent unnecessary
        re-proving.
        N)	Z_resultZ_proverr+   r$   r%   r<   r   r   Z_proof)r#   r&   r(   r   r   r   prove   s    
zResolutionProverCommand.provec             C   sl   |  | t }tttj}xH| jD ]>}x8|D ]0}t|tr0|j	|kr0t|j
ts0||j
 q0W q&W |S )N)r=   setr   r
   r   r8   r<   r9   r   functionargumentr   add)r#   r&   ZanswersZ	answer_exclausetermr   r   r   find_answers   s    



z$ResolutionProverCommand.find_answersc             C   s   d}t dd | D }ttt| }xtt| D ]}d}d}| |  rPd}| | jrht| | j}d|tt| |  d  | }d|tt|d   t|d  }|d|| | ||f 7 }q4W |S )	z,
        Decorate the proof output.
         c             S   s   g | ]}t t|qS r   )r/   r!   ).0rB   r   r   r   
<listcomp>   s    z=ResolutionProverCommand._decorate_clauses.<locals>.<listcomp>AZ	Tautology r-   z[%s] %s %s %s
)maxr/   r!   ranger0   r3   )r(   outZmax_clause_lenZmax_seq_lenr4   parentsZtautseqr   r   r   r      s    
 $z)ResolutionProverCommand._decorate_clauses)NNN)F)F)r   r   r   r;   r=   rD   staticmethodr   r   r   r   r   r   n   s   


r   c               @   sv   e Zd Zdd ZdddZdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )Clausec             C   s   t | | d | _d | _d S )N)r.   r;   _is_tautologyr3   )r#   datar   r   r   r;      s    zClause.__init__NFc             C   s   |dkrt  }|dkrg g f}|dkr.g g f}t|tr@t|}t| ||||t|}g }xVt|D ]J\}}	||krbx8t|D ],\}
}||
kr||
|kr||	|r|||
 q|W qbW g }x*t	t
|D ]}||kr|||  qW |S )a  
        Attempt to unify this Clause with the other, returning a list of
        resulting, unified, Clauses.

        :param other: ``Clause`` with which to unify
        :param bindings: ``BindingDict`` containing bindings that should be used
        during the unification
        :param used: tuple of two lists of atoms.  The first lists the
        atoms from 'self' that were successfully unified with atoms from
        'other'.  The second lists the atoms from 'other' that were successfully
        unified with atoms from 'self'.
        :param skipped: tuple of two ``Clause`` objects.  The first is a list of all
        the atoms from the 'self' Clause that have not been unified with
        anything on the path.  The second is same thing for the 'other' Clause.
        :param debug: bool indicating whether debug statements should print
        :return: list containing all the resulting ``Clause`` objects that could be
        obtained by unification
        N)BindingDictr9   boolDebugObject_iterate_first_complete_unify_path	enumeratesubsumesr1   rK   r/   )r#   otherbindingsusedskippeddebugr6   Zsubsumedr4   Zc1r5   c2r'   r   r   r   r2      s*    
zClause.unifyc             C   s   x| D ]}||krdS qW dS )z
        Return True iff every term in 'self' is a term in 'other'.

        :param other: ``Clause``
        :return: bool
        FTr   )r#   rZ   r)   r   r   r   
isSubsetOf   s    
zClause.isSubsetOfc       	   	   C   sx   g }x0|D ](}t |tr&||j q
||  q
W t|}t }g g f}g g f}td}tt| ||||t	|dkS )z
        Return True iff 'self' subsumes 'other', this is, if there is a
        substitution such that every term in 'self' can be unified with a term
        in 'other'.

        :param other: ``Clause``
        :return: bool
        Fr   )
r9   r	   r1   rC   rP   rS   rU   r/   rV   _subsumes_finalize)	r#   rZ   ZnegatedotheratomZnegatedotherClauser[   r\   r]   r^   r   r   r   rY      s(    	

zClause.subsumesc             C   s   t t| ||S )N)rP   r.   __getslice__)r#   startendr   r   r   rc     s    zClause.__getslice__c                s   t  fdd| D S )Nc                s   g | ]}| kr|qS r   r   )rF   r)   )rZ   r   r   rG     s    z"Clause.__sub__.<locals>.<listcomp>)rP   )r#   rZ   r   )rZ   r   __sub__  s    zClause.__sub__c             C   s   t t| |S )N)rP   r.   __add__)r#   rZ   r   r   r   rg     s    zClause.__add__c             C   s   | j dk	r| j S xt| D ]z\}}t|tst| d }xZ||kr| | }t|trj|j|krd| _ dS nt|tr||jkrd| _ dS |d8 }q:W qW d| _ dS )z
        Self is a tautology if it contains ground terms P and -P.  The ground
        term, P, must be an exact match, ie, not using unification.
        Nr-   TF)rQ   rX   r9   r   r/   r	   rC   )r#   r4   r)   r5   br   r   r   r0      s$    






zClause.is_tautologyc             C   s   t tjdd | D S )Nc             s   s   | ]}|  | B V  qd S )N)free	constants)rF   rb   r   r   r   	<genexpr>9  s    zClause.free.<locals>.<genexpr>)r   operatoror_)r#   r   r   r   ri   8  s    zClause.freec                s   t  fdd| D S )z
        Replace every instance of variable with expression across every atom
        in the clause

        :param variable: ``Variable``
        :param expression: ``Expression``
        c                s   g | ]}|  qS r   )replace)rF   rb   )
expressionvariabler   r   rG   C  s    z"Clause.replace.<locals>.<listcomp>)rP   )r#   rp   ro   r   )ro   rp   r   rn   ;  s    zClause.replacec                s   t  fdd| D S )z
        Replace every binding

        :param bindings: A list of tuples mapping Variable Expressions to the
        Expressions to which they are bound
        :return: ``Clause``
        c                s   g | ]}|  qS r   )substitute_bindings)rF   rb   )r[   r   r   rG   M  s    z.Clause.substitute_bindings.<locals>.<listcomp>)rP   )r#   r[   r   )r[   r   rq   E  s    zClause.substitute_bindingsc             C   s   dd dd | D  d S )N{z, c             s   s   | ]}d | V  qdS )z%sNr   )rF   itemr   r   r   rk   P  s    z!Clause.__str__.<locals>.<genexpr>})join)r#   r   r   r   __str__O  s    zClause.__str__c             C   s   d|  S )Nz%sr   )r#   r   r   r   __repr__R  s    zClause.__repr__)NNNF)r   r   r   r;   r2   r`   rY   rc   rf   rg   r0   ri   rn   rq   rv   rw   r   r   r   r   rP      s   
/&

rP   c          
   C   s$  | d| ||f  t| r$t|s6|| |||||S t| ||||||d }|d | d g |d f}|t| dd ||||||d 7 }yxt| d |d ||\}	}
}| dd |d  |d  }|dd |d  |d  }|t|||	|
g g f||d 7 }W n tk
r   Y nX |S dS )zF
    This method facilitates movement through the terms of 'self'
    zunify(%s,%s) %sr-   r   N)liner/   _iterate_secondrV   _unify_termsBindingException)firstsecondr[   r\   r]   finalize_methodr^   r'   
newskippednewbindingsnewusedunusednewfirst	newsecondr   r   r   rV   V  s0     rV   c          
   C   s  | d| ||f  t| r$t|s6|| |||||S |d |d |d g f}t| |dd |||||d }yxt| d |d ||\}	}
}| dd |d  |d  }|dd |d  |d  }|t|||	|
g g f||d 7 }W n tk
r   Y nX |S dS )zG
    This method facilitates movement through the terms of 'other'
    zunify(%s,%s) %sr   r-   N)rx   r/   ry   rz   r{   )r|   r}   r[   r\   r]   r~   r^   r   r'   r   r   r   r   r   r   r   r   ry     s,    ry   c             C   sd  t | tstt |tst|dkr*t }|dkr:g g f}t | trt |trt| j||}|d | g |d |g f}g g f}nt | trt |trt| |j|}|d | g |d |g f}g g f}nt | trt| j	j
| jfg}|d | g |d f}g |gf}nNt |trNt|j	j
|jfg}|d |d |g f}| gg f}nt| |f|||fS )a  
    This method attempts to unify two terms.  Two expressions are unifiable
    if there exists a substitution function S such that S(a) == S(-b).

    :param a: ``Expression``
    :param b: ``Expression``
    :param bindings: ``BindingDict`` a starting set of bindings with which
    the unification must be consistent
    :return: ``BindingDict`` A dictionary of the bindings required to unify
    :raise ``BindingException``: If the terms cannot be unified
    Nr   r-   )r9   r   r:   rS   r	   r   most_general_unificationrC   r   r|   rp   r}   r{   )r)   rh   r[   r\   r   r   r   r   r   r   rz     s0    

rz   c             C   sX   |d s|d rFt |d |d  |  | }|d|  ||gS |d g S d S )Nr   r-   z  -> New Clause: %sz  -> End)rP   rx   rq   )r|   r}   r[   r\   r]   r^   r7   r   r   r   rW     s    
rW   c             C   s"   t |d st | sdgS g S d S )Nr   T)r/   )r|   r}   r[   r\   r]   r^   r   r   r   ra     s    ra   c             C   sZ   g }xPt t| D ]@}x0| D ]$}t|jr tt }|||}q W || qW |S )zC
    Skolemize, clausify, and standardize the variables apart.
    )		_clausifyr   ri   r   namer   r   rn   r1   )ro   Zclause_listrB   ri   Znewvarr   r   r   r     s    

r   c             C   s   t | trt| jt| j S t | trnt| j}t| j}t|dksLtt|dks\t|d |d  gS t | trt	| ggS t | t
rt	| ggS t | trt | jt
rt	| ggS t | jtrt	| ggS t dS )z;
    :param expression: a skolemized expression in CNF
    r-   r   N)r9   r   r   r|   r}   r   r/   r:   r   rP   r   r	   rC   r   )ro   r|   r}   r   r   r   r     s$    






r   c               @   sN   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dS )rS   Nc             C   s(   i | _ |r$x|D ]\}}|| |< qW dS )z
        :param binding_list: list of (``AbstractVariableExpression``, ``AtomicExpression``) to initialize the dictionary
        N)d)r#   Zbinding_listvrh   r   r   r   r;     s    zBindingDict.__init__c             C   s   t |tstt |tsty| | }W n tk
r@   d}Y nX |rN||krZ|| j|< nnt |try| |j }W n tk
r   d}Y nX t|}|r||kr|| j|j< qt	d| nt	d| dS )a  
        A binding is consistent with the dict if its variable is not already bound, OR if its
        variable is already bound to its argument.

        :param variable: ``Variable`` The variable to bind
        :param binding: ``Expression`` The atomic to which 'variable' should be bound
        :raise BindingException: If the variable cannot be bound in this dictionary
        Nz*Variable %s already bound to another value)
r9   r
   r:   r   KeyErrorr   r   rp   r   r{   )r#   rp   ZbindingexistingZbinding2r   r   r   __setitem__  s(    	


zBindingDict.__setitem__c             C   sJ   t |tst| j| }x,|rDy| j| }W q tk
r@   |S X qW dS )zD
        Return the expression to which 'variable' is bound
        N)r9   r
   r:   r   r   )r#   rp   Zintermediater   r   r   __getitem__E  s    
zBindingDict.__getitem__c             C   s
   || j kS )N)r   )r#   rs   r   r   r   __contains__R  s    zBindingDict.__contains__c             C   sp   yFt  }x| jD ]}| j| ||< qW x|jD ]}|j| ||< q.W |S  tk
rj   td| |f Y nX dS )a  
        :param other: ``BindingDict`` The dict with which to combine self
        :return: ``BindingDict`` A new dict containing all the elements of both parameters
        :raise BindingException: If the parameter dictionaries are not consistent with each other
        z?Attempting to add two contradicting BindingDicts: '%s' and '%s'N)rS   r   r{   )r#   rZ   Zcombinedr   r   r   r   rg   U  s    zBindingDict.__add__c             C   s
   t | jS )N)r/   r   )r#   r   r   r   __len__h  s    zBindingDict.__len__c                s.   d  fddt j D }d| d S )Nz, c             3   s    | ]}d | j | f V  qdS )z%s: %sN)r   )rF   r   )r#   r   r   rk   l  s    z&BindingDict.__str__.<locals>.<genexpr>rr   rt   )ru   sortedr   keys)r#   Zdata_strr   )r#   r   rv   k  s    "zBindingDict.__str__c             C   s   d|  S )Nz%sr   )r#   r   r   r   rw   o  s    zBindingDict.__repr__)N)r   r   r   r;   r   r   r   rg   r   rv   rw   r   r   r   r   rS     s   

'rS   c             C   s   |dkrt  }| |kr|S t| tr0t| ||S t|trFt|| |S t| trzt|trzt| j|j|t| j|j| S t| |fdS )ah  
    Find the most general unification of the two given expressions

    :param a: ``Expression``
    :param b: ``Expression``
    :param bindings: ``BindingDict`` a starting set of bindings with which the
                     unification must be consistent
    :return: a list of bindings
    :raise BindingException: if the Expressions cannot be unified
    N)	rS   r9   r   _mgu_varr   r   r?   r@   r{   )r)   rh   r[   r   r   r   r   s  s    

r   c             C   s<   | j | | B kr$t| |fnt| j |fg| S d S )N)rp   ri   rj   r{   rS   )varro   r[   r   r   r   r     s    r   c               @   s   e Zd Zdd ZdS )r{   c             C   s,   t |trt| d|  nt| | d S )Nz'%s' cannot be bound to '%s')r9   tuple	Exceptionr;   )r#   argr   r   r   r;     s    
zBindingException.__init__N)r   r   r   r;   r   r   r   r   r{     s   r{   c               @   s   e Zd Zdd ZdS )UnificationExceptionc             C   s   t | d||f  d S )Nz'%s' cannot unify with '%s')r   r;   )r#   r)   rh   r   r   r   r;     s    zUnificationException.__init__N)r   r   r   r;   r   r   r   r   r     s   r   c               @   s&   e Zd Zd
ddZdd Zdd Zd	S )rU   Tr   c             C   s   || _ || _d S )N)enabledindent)r#   r   r   r   r   r   r;     s    zDebugObject.__init__c             C   s   t | j| j| S )N)rU   r   r   )r#   r4   r   r   r   rg     s    zDebugObject.__add__c             C   s   | j rtd| j |  d S )Nz    )r   r   r   )r#   rx   r   r   r   rx     s    zDebugObject.lineN)Tr   )r   r   r   r;   rg   rx   r   r   r   r   rU     s   
rU   c           
   C   s&  t d t d t d t d t d t d t d t d t d t d t d t d t d	 t d
 td} td}td}td| ||t || |gf  td} td}td}td| ||t || |gf  td}td}td||t ||gf  d S )Nzman(x)z(man(x) -> man(x))z(man(x) -> --man(x))z-(man(x) and -man(x))z(man(x) or -man(x))z(man(x) iff man(x))z-(man(x) iff -man(x))zall x.man(x)z--all x.some y.F(x,y) & some x.all y.(-F(x,y))zsome x.all y.sees(x,y)zall x.(man(x) -> mortal(x))zman(Socrates)zmortal(Socrates)z%s, %s |- %s: %szall x.(man(x) -> walks(x))z	man(John)zsome y.walks(y)z5some e1.some e2.(believe(e1,john,e2) & walk(e2,mary))zsome e0.walk(e0,mary)z%s |- %s: %s)resolution_testr   
fromstringr   r   r=   )p1p2cpr   r   r   testResolutionProver  s2    


"


"

r   c             C   s*   t | }t |}td||f  d S )Nz	|- %s: %s)r   r   r   r=   r   )r*   ftr   r   r   r     s    
r   c              C   s  t j} tt| d tt| d tt| d tt| d tt| d tt| d tt| d tt| d tt| d	 tt| d
 tt| d tt| d tt| d tt| d tt| d tt| d tt| d d S )NzP(x) | Q(x)z(P(x) & Q(x)) | R(x)zP(x) | (Q(x) & R(x))z(P(x) & Q(x)) | (R(x) & S(x))zP(x) | Q(x) | R(x)zP(x) | (Q(x) & R(x)) | S(x)zexists x.P(x) | Q(x)z-(-P(x) & Q(x))zP(x) <-> Q(x)z-(P(x) <-> Q(x))z-(all x.P(x))z-(some x.P(x))zsome x.P(x)zsome x.all y.P(x,y)zall y.some x.P(x,y)zall z.all y.some x.P(x,y,z)z1all x.(all y.P(x,y) -> -all y.(Q(x,y) -> R(x,y))))r   r   r   r   )Zlexprr   r   r   test_clausify  s$    r   c              C   s:   t   t  t  t  td} tt| | g  d S )Nzman(x))r   r   r   r   r   r   r=   )r   r   r   r   demo  s    
r   __main__)NN)N)0__doc__rl   collectionsr   	functoolsr   Znltk.semr   Znltk.sem.logicr   r   r   r   r	   r
   r   r   r   r   r   Znltk.inference.apir   r   r   r   r   r   r.   rP   rV   ry   rz   rW   ra   r   r   objectrS   r   r   r{   r   rU   r   r   r   r   r   r   r   r   r   <module>   s<   8FB ','
.
`


