B
    >?ð[7 ã               @   sÔ  d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ d
Zeƒ ZG dd„ deƒZdd„ Zdd„ Zdd„ ZeG dd„ deƒƒZdxdd„Ze
eG dd„ deƒƒƒZdydd„Zdzdd„ZeG dd„ deƒƒZeG dd „ d eƒƒZ G d!d"„ d"eƒZ!eG d#d$„ d$e!ƒƒZ"eG d%d&„ d&e!ƒƒZ#eG d'd(„ d(e!ƒƒZ$eG d)d*„ d*e!e ƒƒZ%e#ƒ Z&e"ƒ Z'e$ƒ Z(e%ƒ Z)d+d,„ Z*G d-d.„ d.e+ƒZ,G d/d0„ d0e,ƒZ-G d1d2„ d2e,ƒZ.G d3d4„ d4e,ƒZ/d{d5d6„Z0G d7d8„ d8eƒZ1eG d9d:„ d:e1ƒƒZ2eG d;d<„ d<e2ƒƒZ3e
eG d=d>„ d>e2ƒƒƒZ4G d?d@„ d@e4ƒZ5G dAdB„ dBe4ƒZ6G dCdD„ dDe5ƒZ7G dEdF„ dFe4ƒZ8dGdH„ Z9G dIdJ„ dJe2ƒZ:eG dKdL„ dLe:ƒƒZ;eG dMdN„ dNe:ƒƒZ<G dOdP„ dPe<ƒZ=G dQdR„ dRe<ƒZ>eG dSdT„ dTe2ƒƒZ?eG dUdV„ dVe2ƒƒZ@G dWdX„ dXe@ƒZAG dYdZ„ dZeAƒZBG d[d\„ d\eAƒZCG d]d^„ d^eAƒZDG d_d`„ d`eAƒZEG dadb„ dbe@ƒZFG dcdd„ dde+ƒZGG dedf„ dfeGƒZHG dgdh„ dheGƒZIdidj„ ZJdkdl„ ZKdmdn„ ZLdodp„ ZMdqdr„ ZNdsdt„ ZOdudv„ ZPeQdwkrÐeMƒ  dS )|zV
A version of first order predicate logic, built on
top of the typed lambda calculus.
é    )Úprint_functionÚunicode_literalsN)Údefaultdict)ÚreduceÚtotal_ordering)Ústring_types)ÚTrie)ÚCounter)Úpython_2_unicode_compatibleÚAPPc               @   sè   e Zd ZdZdgZdZdddgZdZddgZdZ	dZ
d	Zd
ZdZdddgZdZdddgZdZddgZdZdddgZdZdddgZdZddgZdZdgZee e e Zee Ze	e
eegZee e e e e e Zdd„ eD ƒZdS )ÚTokensú\ÚexistsZsomeZexistÚallZforallÚ.ú(ú)ú,ú-Únotú!ú&Úandú^ú|Úorz->Zimpliesz=>z<->Ziffz<=>ú=z==z!=c             C   s   g | ]}t  d |¡r|‘qS )z^[-\\.(),!&^|>=<]*$)ÚreÚmatch)Ú.0Úx© r!   ú-lib/python3.7/site-packages/nltk/sem/logic.pyú
<listcomp>G   s    zTokens.<listcomp>N) Ú__name__Ú
__module__Ú__qualname__ÚLAMBDAÚLAMBDA_LISTÚEXISTSÚEXISTS_LISTÚALLÚALL_LISTÚDOTÚOPENÚCLOSEÚCOMMAÚNOTÚNOT_LISTÚANDÚAND_LISTÚORÚOR_LISTÚIMPÚIMP_LISTÚIFFÚIFF_LISTÚEQÚEQ_LISTÚNEQÚNEQ_LISTZBINOPSÚQUANTSZPUNCTÚTOKENSÚSYMBOLSr!   r!   r!   r"   r      s:   




r   c              C   sF   dddddg} x2t | tjtjtjtjtjgƒD ]}td| ƒ q.W dS )z
    Boolean operators
    ZnegationZconjunctionZdisjunctionZimplicationZequivalencez%-15s	%sN)Úzipr   r1   r3   r5   r7   r9   Úprint)ÚnamesÚpairr!   r!   r"   Úboolean_opsJ   s    $rF   c              C   s4   ddg} x&t | tjtjgƒD ]}td| ƒ qW dS )z
    Equality predicates
    ZequalityZ
inequalityz%-15s	%sN)rB   r   r;   r=   rC   )rD   rE   r!   r!   r"   Úequality_predsS   s    rG   c              C   s:   dddg} x*t | tjtjtjgƒD ]}td| ƒ q"W dS )z
    Binding operators
    ZexistentialZ	universalÚlambdaz%-15s	%sN)rB   r   r)   r+   r'   rC   )rD   rE   r!   r!   r"   Úbinding_ops\   s    
rI   c               @   s  e Zd ZdZdFdd„ZdGdd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dHd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(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"dBdC„ Z#dDdE„ Z$dS )IÚLogicParserz$A lambda calculus expression parser.Fc             C   sÎ   t |tƒst‚d| _g | _|| _g | _tdd„ tj	D ƒdd„ tj
D ƒ tdfg dd„ tjtj D ƒ dd„ tjD ƒ dd„ tjD ƒ d	d„ tjD ƒ d
d„ tjD ƒ dd„ tjD ƒ dg ƒ| _tg| _dS )zd
        :param type_check: bool should type checking be performed?
        to their types.
        r   c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#   €   s    z(LogicParser.__init__.<locals>.<listcomp>c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#      s    é   c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#   ƒ   s    c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#   „   s    c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#   …   s    c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#   †   s    c             S   s   g | ]}|d f‘qS )é   r!   )r   r    r!   r!   r"   r#   ‡   s    c             S   s   g | ]}|d f‘qS )é	   r!   )r   r    r!   r!   r"   r#   ˆ   s    )Né
   N)Ú
isinstanceÚboolÚAssertionErrorÚ_currentIndexÚ_bufferÚ
type_checkÚquote_charsÚdictr   r(   r2   r   r<   r>   r?   r4   r6   r8   r:   Úoperator_precedenceÚright_associated_operations)ÚselfrZ   r!   r!   r"   Ú__init__i   s    
šzLogicParser.__init__Nc          
   C   sª   |  ¡ }d| _|  |¡\| _}y.|  d¡}|  d¡rJt| jd |  d¡ƒ‚W nH tk
r” } z*d||d||j	d   f }td|ƒ‚W dd}~X Y nX | j
r¦| |¡ |S )zà
        Parse the expression.

        :param data: str for the input to be parsed
        :param signature: ``dict<str, str>`` that maps variable names to type
        strings
        :returns: a parsed Expression
        r   NrK   z	%s
%s
%s^ú )ÚrstriprX   ÚprocessrY   Úprocess_next_expressionÚinRangeÚUnexpectedTokenExceptionÚtokenÚLogicalExpressionExceptionÚindexrZ   Ú	typecheck)r_   ÚdataÚ	signatureÚmappingÚresultÚeÚmsgr!   r!   r"   Úparse   s    	


zLogicParser.parsec             C   sœ  g }i }t |  ¡ ƒ}d}d}|}x,|t|ƒk rN|}|  ||¡\}	}|	r\|sR|}||	7 }q$|}
|| }d}xD||
kr°||7 }|
| }
t|ƒ| t|ƒkr¬||t|ƒ  }qnP qnW t j|
krþ|rÚ||t|ƒ< | |¡ d}||t|ƒ< | |¡ |t|ƒ7 }q$|| dkr.|rD||t|ƒ< | |¡ d}n|s8|}||| 7 }|d7 }q$W |rl||t|ƒ< | |¡ t|ƒ|t|ƒ< t|ƒd |t|ƒd < ||fS )zSplit the data into tokensÚ r   z 	
rK   )r   Úget_all_symbolsÚlenÚprocess_quoted_tokenZLEAFÚappend)r_   rk   Úoutrm   Z	tokenTrierg   Údata_idxZtoken_start_idxZcur_data_idxZquoted_tokenÚstÚcZsymbolr!   r!   r"   rc   ¨   s\    





zLogicParser.processc       
      C   s   d}|| }|}xæ| j D ]Ü\}}}}	||kr|	r8||7 }|d7 }xŠ|| |krÊ|| |krš|	rj||| 7 }|d7 }t|ƒ|krŒtd d| ƒ‚||| 7 }n||| 7 }|d7 }t|ƒ|krBtd d| ƒ‚qBW |	rÜ||| 7 }|d7 }|sòtd dƒ‚P qW ||fS )Nrr   rK   z:End of input reached.  Escape character [%s] found at end.z%End of input reached.  Expected: [%s]zEmpty quoted token found)r[   rt   rh   )
r_   rx   rk   rg   rz   ÚiÚstartÚendÚescapeZincl_quotesr!   r!   r"   ru   Þ   s>    
z LogicParser.process_quoted_tokenc             C   s   t jS )z#This method exists to be overridden)r   rA   )r_   r!   r!   r"   rs     s    zLogicParser.get_all_symbolsc             C   s   | j | t| jƒk S )z6Return TRUE if the given location is within the buffer)rX   rt   rY   )r_   Úlocationr!   r!   r"   re     s    zLogicParser.inRangec             C   s`   y8|dkr&| j | j }|  jd7  _n| j | j|  }|S  tk
rZ   t| jd ƒ‚Y nX dS )zÃGet the next waiting token.  If a location is given, then
        return the token at currentIndex+location without advancing
        currentIndex; setting it gives lookahead/lookback capability.NrK   )rY   rX   Ú
IndexErrorÚExpectedMoreTokensException)r_   r   Útokr!   r!   r"   rg   
  s    zLogicParser.tokenc             C   s
   |t jkS )N)r   r@   )r_   r‚   r!   r!   r"   Ú
isvariable  s    zLogicParser.isvariablec             C   s`   y|   ¡ }W n& tk
r2   t| jd dd‚Y nX |  ||¡}|sTt| j|dd‚|  ||¡S )zAParse the next complete expression from the stream and return it.rK   zExpression expected.)Úmessage)rg   r   rX   Úhandlerf   Úattempt_adjuncts)r_   Úcontextr‚   Úaccumr!   r!   r"   rd     s    z#LogicParser.process_next_expressionc             C   sr   |   |¡r|  ||¡S |tjkr,|  ||¡S |tjkrB|  ||¡S |tjkrX|  ||¡S |tj	krn|  
||¡S dS )zgThis method is intended to be overridden for logics that
        use different operators or expressionsN)rƒ   Úhandle_variabler   r2   Úhandle_negationr(   Úhandle_lambdar?   Úhandle_quantr.   Úhandle_open)r_   r‚   r‡   r!   r!   r"   r…   -  s    




zLogicParser.handlec             C   sB   d }x8|| j kr<| j }|  ||¡}|  ||¡}|  ||¡}qW |S )N)rX   Úattempt_EqualityExpressionÚattempt_ApplicationExpressionÚattempt_BooleanExpression)r_   Ú
expressionr‡   Zcur_idxr!   r!   r"   r†   ?  s    zLogicParser.attempt_adjunctsc             C   s   |   |  tj¡¡S )N)Úmake_NegatedExpressionrd   r   r1   )r_   r‚   r‡   r!   r!   r"   rŠ   H  s    zLogicParser.handle_negationc             C   s   t |ƒS )N)ÚNegatedExpression)r_   r‘   r!   r!   r"   r’   K  s    z"LogicParser.make_NegatedExpressionc             C   s¬   |   |¡}|  d¡r¨|  d¡tjkr¨t|tƒsHt|tƒsHt| j	d| ƒ‚|  ¡  |  
||  t¡¡}x8|  d¡rš|  d¡tjkrš|  ¡  |  
||  t¡¡}qdW |  tj¡ |S )Nr   zW'%s' is an illegal predicate name.  Individual variables may not be used as predicates.)Úmake_VariableExpressionre   rg   r   r.   rU   ÚFunctionVariableExpressionÚConstantExpressionrh   rX   Úmake_ApplicationExpressionrd   r   r0   ÚassertNextTokenr/   )r_   r‚   r‡   rˆ   r!   r!   r"   r‰   N  s"    
zLogicParser.handle_variablec          
   C   sh   y|   ¡ }W n. tk
r: } zt|jdƒ‚W d d }~X Y nX t|  |¡tƒr`t| jd||f ƒ‚t|ƒS )NzVariable expected.z;'%s' is an illegal variable name.  Constants may not be %s.)	rg   r   ri   rU   r”   r–   rh   rX   ÚVariable)r_   Zdescriptionr‚   ro   r!   r!   r"   Úget_next_token_variablel  s    z#LogicParser.get_next_token_variablec             C   sÐ   |   d¡st| jd dd‚|  d¡g}x\|   d¡rN|  d¡tjkr`|   d¡s`t| jd dd‚|  |  d¡¡srP | |  d¡¡ q*W |   d¡r¨|  d¡tjkr¨|  ¡  |  	|¡}x|rÊ|  
| ¡ |¡}q´W |S )Nr   rL   z;Variable and Expression expected following lambda operator.)r„   Z
abstractedrK   zExpression expected.)re   r   rX   rš   rg   r   r-   rƒ   rv   rd   Úmake_LambdaExpressionÚpop)r_   r‚   r‡   Úvarsrˆ   r!   r!   r"   r‹   y  s&    


zLogicParser.handle_lambdac             C   sà   |   |¡}|  d¡s*t| jd d| d‚|  d¡g}x\|  d¡r\|  d¡tjkrn|  d¡snt| jd dd‚|  |  d¡¡s€P | 	|  d¡¡ q8W |  d¡r¶|  d¡tjkr¶|  ¡  |  
|¡}x|rÚ|  || ¡ |¡}qÂW |S )Nr   rL   z;Variable and Expression expected following quantifier '%s'.)r„   Z
quantifiedrK   zExpression expected.)Ú get_QuantifiedExpression_factoryre   r   rX   rš   rg   r   r-   rƒ   rv   rd   Úmake_QuanifiedExpressionrœ   )r_   r‚   r‡   Úfactoryr   rˆ   r!   r!   r"   rŒ   ”  s*    




zLogicParser.handle_quantc             C   s.   |t jkrtS |t jkrtS |  |t j¡ dS )z\This method serves as a hook for other logic parsers that
        have different quantifiersN)r   r*   ÚExistsExpressionr,   ÚAllExpressionÚassertTokenr?   )r_   r‚   r!   r!   r"   rž   ²  s
    

z,LogicParser.get_QuantifiedExpression_factoryc             C   s
   |||ƒS )Nr!   )r_   r    ÚvariableÚtermr!   r!   r"   rŸ   ¼  s    z$LogicParser.make_QuanifiedExpressionc             C   s   |   d ¡}|  tj¡ |S )N)rd   r˜   r   r/   )r_   r‚   r‡   rˆ   r!   r!   r"   r   ¿  s    
zLogicParser.handle_openc             C   sb   |   d¡r^|  d¡}|tjtj kr^|  ||¡r^|  ¡  |  ||  |¡¡}|tjkr^|  |¡}|S )z»Attempt to make an equality expression.  If the next token is an
        equality operator, then an EqualityExpression will be returned.
        Otherwise, the parameter will be returned.r   )	re   rg   r   r<   r>   Úhas_priorityÚmake_EqualityExpressionrd   r’   )r_   r‘   r‡   r‚   r!   r!   r"   rŽ   Å  s    



z&LogicParser.attempt_EqualityExpressionc             C   s
   t ||ƒS )zlThis method serves as a hook for other logic parsers that
        have different equality expression classes)ÚEqualityExpression)r_   ÚfirstÚsecondr!   r!   r"   r§   Ö  s    z#LogicParser.make_EqualityExpressionc             C   sX   xR|   d¡rR|  d¡}|  |¡}|rN|  ||¡rN|  ¡  |  |||  |¡¡}qP qW |S )z¶Attempt to make a boolean expression.  If the next token is a boolean
        operator, then a BooleanExpression will be returned.  Otherwise, the
        parameter will be returned.r   )re   rg   Úget_BooleanExpression_factoryr¦   Úmake_BooleanExpressionrd   )r_   r‘   r‡   r‚   r    r!   r!   r"   r   Û  s    

z%LogicParser.attempt_BooleanExpressionc             C   s@   |t jkrtS |t jkrtS |t jkr*tS |t jkr8tS dS dS )zbThis method serves as a hook for other logic parsers that
        have different boolean operatorsN)	r   r4   ÚAndExpressionr6   ÚOrExpressionr8   ÚImpExpressionr:   ÚIffExpression)r_   r‚   r!   r!   r"   r«   ë  s    



z)LogicParser.get_BooleanExpression_factoryc             C   s
   |||ƒS )Nr!   )r_   r    r©   rª   r!   r!   r"   r¬   ù  s    z"LogicParser.make_BooleanExpressionc             C   sÊ   |   t|¡rÆ|  d¡rÆ|  d¡tjkrÆt|tƒsbt|tƒsbt|t	ƒsbt|t
ƒsbt| jd| d ƒ‚|  ¡  |  ||  t¡¡}x8|  d¡r´|  d¡tjkr´|  ¡  |  ||  t¡¡}q~W |  tj¡ |S |S )zíAttempt to make an application expression.  The next tokens are
        a list of arguments in parens, then the argument expression is a
        function being applied to the arguments.  Otherwise, return the
        argument expression.r   zThe function '%szq' is not a Lambda Expression, an Application Expression, or a functional predicate, so it may not take arguments.)r¦   r   re   rg   r   r.   rU   ÚLambdaExpressionÚApplicationExpressionr•   r–   rh   rX   r—   rd   r0   r˜   r/   )r_   r‘   r‡   rˆ   r!   r!   r"   r   ü  s(    



z)LogicParser.attempt_ApplicationExpressionc             C   s
   t ||ƒS )N)r²   )r_   ÚfunctionÚargumentr!   r!   r"   r—     s    z&LogicParser.make_ApplicationExpressionc             C   s   t t|ƒƒS )N)ÚVariableExpressionr™   )r_   Únamer!   r!   r"   r”   "  s    z#LogicParser.make_VariableExpressionc             C   s
   t ||ƒS )N)r±   )r_   r¤   r¥   r!   r!   r"   r›   %  s    z!LogicParser.make_LambdaExpressionc             C   s2   | j | | j | k p0|| jko0| j | | j | kS )N)r]   r^   )r_   Z	operationr‡   r!   r!   r"   r¦   (  s    
zLogicParser.has_priorityc          
   C   s~   y|   ¡ }W n4 tk
r@ } zt|jd| d‚W d d }~X Y nX t|tƒrd||krzt| j||ƒ‚n||krzt| j||ƒ‚d S )NzExpected token '%s'.)r„   )rg   r   ri   rU   Úlistrf   rX   )r_   Úexpectedr‚   ro   r!   r!   r"   r˜   0  s    "
zLogicParser.assertNextTokenc             C   s<   t |tƒr"||kr8t| j||ƒ‚n||kr8t| j||ƒ‚d S )N)rU   r·   rf   rX   )r_   r‚   r¸   r!   r!   r"   r£   ?  s
    
zLogicParser.assertTokenc             C   s6   |   d¡rd|  d¡ }nd}d| jj d | d S )Nr   zNext token: zNo more tokensú<z: ú>)re   rg   Ú	__class__r$   )r_   rp   r!   r!   r"   Ú__repr__G  s    
zLogicParser.__repr__)F)N)N)%r$   r%   r&   Ú__doc__r`   rq   rc   ru   rs   re   rg   rƒ   rd   r…   r†   rŠ   r’   r‰   rš   r‹   rŒ   rž   rŸ   r   rŽ   r§   r   r«   r¬   r   r—   r”   r›   r¦   r˜   r£   r¼   r!   r!   r!   r"   rJ   e   sD   
$
6$
	
#rJ   c          	   C   s˜   |dk	r|   |¡} |dkr tƒ }g }xnt|  ¡ ƒD ]^\}}| ¡ }| d¡s2|dkrVq2y| | |¡¡ W q2 tk
rŽ   t	d||f ƒ‚Y q2X q2W |S )až  
    Convert a file of First Order Formulas into a list of {Expression}s.

    :param s: the contents of the file
    :type s: str
    :param logic_parser: The parser to be used to parse the logical expression
    :type logic_parser: LogicParser
    :param encoding: the encoding of the input string, if it is binary
    :type encoding: str
    :return: a list of parsed formulas.
    :rtype: list(Expression)
    Nú#rr   zUnable to parse line %s: %s)
ÚdecoderJ   Ú	enumerateÚ
splitlinesÚstripÚ
startswithrv   rq   rh   Ú
ValueError)ÚsZlogic_parserÚencodingZ
statementsZlinenumÚliner!   r!   r"   Ú
read_logicO  s    
rÈ   c               @   sL   e Z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 )r™   c             C   s    t |tƒstd| ƒ‚|| _dS )z7
        :param name: the name of the variable
        z%s is not a stringN)rU   r   rW   r¶   )r_   r¶   r!   r!   r"   r`   p  s    zVariable.__init__c             C   s   t |tƒo| j|jkS )N)rU   r™   r¶   )r_   Úotherr!   r!   r"   Ú__eq__w  s    zVariable.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   Ú__ne__z  s    zVariable.__ne__c             C   s   t |tƒst‚| j|jk S )N)rU   r™   Ú	TypeErrorr¶   )r_   rÉ   r!   r!   r"   Ú__lt__}  s    
zVariable.__lt__c             C   s   |  | | ¡S )N)Úget)r_   Úbindingsr!   r!   r"   Úsubstitute_bindings‚  s    zVariable.substitute_bindingsc             C   s
   t | jƒS )N)Úhashr¶   )r_   r!   r!   r"   Ú__hash__…  s    zVariable.__hash__c             C   s   | j S )N)r¶   )r_   r!   r!   r"   Ú__str__ˆ  s    zVariable.__str__c             C   s
   d| j  S )NzVariable('%s'))r¶   )r_   r!   r!   r"   r¼   ‹  s    zVariable.__repr__N)r$   r%   r&   r`   rÊ   rË   rÍ   rÐ   rÒ   rÓ   r¼   r!   r!   r!   r"   r™   m  s   r™   c             C   sŒ   | dk	rFt | jƒrd}qJt| jƒr(d}qJt| jƒr8d}qJdsJtdƒ‚nd}td|t ¡ f ƒ}x(|dk	r†||kr†td|t ¡ f ƒ}q`W |S )a  
    Return a new, unique variable.

    :param pattern: ``Variable`` that is being replaced.  The new variable must
        be the same type.
    :param term: a set of ``Variable`` objects that should not be returned from
        this function.
    :rtype: Variable
    NÚzÚFZe0Fz!Cannot generate a unique constantz%s%s)Ú	is_indvarr¶   Ú
is_funcvarÚis_eventvarrW   r™   Ú_counterrÎ   )ÚpatternÚignoreÚprefixÚvr!   r!   r"   Úunique_variable  s    



rÞ   c             C   s:   t tdt ¡  ƒƒ}| r6xt| ƒD ]}|t |ƒƒ}q"W |S )zX
    Return a skolem function over the variables in univ_scope
    param univ_scope
    zF%s)rµ   r™   rÙ   rÎ   r·   )Z
univ_scopeZskolemrÝ   r!   r!   r"   Úskolem_function«  s
    rß   c               @   s(   e Zd Zdd„ Zdd„ Zedd„ ƒZdS )ÚTypec             C   s   d|  S )Nz%sr!   )r_   r!   r!   r"   r¼   ¹  s    zType.__repr__c             C   s   t d|  ƒS )Nz%s)rÑ   )r_   r!   r!   r"   rÒ   ¼  s    zType.__hash__c             C   s   t |ƒS )N)Ú	read_type)ÚclsrÅ   r!   r!   r"   Ú
fromstring¿  s    zType.fromstringN)r$   r%   r&   r¼   rÒ   Úclassmethodrã   r!   r!   r!   r"   rà   ·  s   rà   c               @   sJ   e Zd Zdd„ Zdd„ Zdd„ ZejZdd„ Zd	d
„ Z	dd„ Z
dd„ ZdS )ÚComplexTypec             C   s<   t |tƒstd| ƒ‚t |tƒs,td| ƒ‚|| _|| _d S )Nz%s is not a Type)rU   rà   rW   r©   rª   )r_   r©   rª   r!   r!   r"   r`   Æ  s    zComplexType.__init__c             C   s"   t |tƒo | j|jko | j|jkS )N)rU   rå   r©   rª   )r_   rÉ   r!   r!   r"   rÊ   Ì  s    
zComplexType.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   Ó  s    zComplexType.__ne__c             C   s2   t |tƒr&| j |j¡o$| j |j¡S | tkS d S )N)rU   rå   r©   Úmatchesrª   ÚANY_TYPE)r_   rÉ   r!   r!   r"   ræ   Ø  s    
zComplexType.matchesc             C   s^   |t kr| S t|tƒrJ| j |j¡}| j |j¡}|rD|rDt||ƒS d S n| t krV|S d S d S )N)rç   rU   rå   r©   Úresolverª   )r_   rÉ   ÚfrÅ   r!   r!   r"   rè   Þ  s    

zComplexType.resolvec             C   s$   | t krdt  S d| j| jf S d S )Nz%sz<%s,%s>)rç   r©   rª   )r_   r!   r!   r"   rÓ   í  s    zComplexType.__str__c             C   s,   | t krt  ¡ S d| j ¡ | j ¡ f S d S )Nz
(%s -> %s))rç   Ústrr©   rª   )r_   r!   r!   r"   rê   ó  s    zComplexType.strN)r$   r%   r&   r`   rÊ   rË   rà   rÒ   ræ   rè   rÓ   rê   r!   r!   r!   r"   rå   Ä  s   rå   c               @   s2   e Zd Zdd„ Zdd„ ZejZdd„ Zdd„ Zd	S )
Ú	BasicTypec             C   s   t |tƒod|  d| kS )Nz%s)rU   rë   )r_   rÉ   r!   r!   r"   rÊ   û  s    zBasicType.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   þ  s    zBasicType.__ne__c             C   s   |t kp| |kS )N)rç   )r_   rÉ   r!   r!   r"   ræ     s    zBasicType.matchesc             C   s   |   |¡r| S d S d S )N)ræ   )r_   rÉ   r!   r!   r"   rè     s    
zBasicType.resolveN)	r$   r%   r&   rÊ   rË   rà   rÒ   ræ   rè   r!   r!   r!   r"   rë   ú  s
   rë   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú
EntityTypec             C   s   dS )Nro   r!   )r_   r!   r!   r"   rÓ     s    zEntityType.__str__c             C   s   dS )NZINDr!   )r_   r!   r!   r"   rê     s    zEntityType.strN)r$   r%   r&   rÓ   rê   r!   r!   r!   r"   rì     s   rì   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚTruthValueTypec             C   s   dS )NÚtr!   )r_   r!   r!   r"   rÓ     s    zTruthValueType.__str__c             C   s   dS )NZBOOLr!   )r_   r!   r!   r"   rê     s    zTruthValueType.strN)r$   r%   r&   rÓ   rê   r!   r!   r!   r"   rí     s   rí   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú	EventTypec             C   s   dS )NrÝ   r!   )r_   r!   r!   r"   rÓ   !  s    zEventType.__str__c             C   s   dS )NZEVENTr!   )r_   r!   r!   r"   rê   $  s    zEventType.strN)r$   r%   r&   rÓ   rê   r!   r!   r!   r"   rï     s   rï   c               @   sb   e Zd Zdd„ Zedd„ ƒZedd„ ƒZdd„ Zd	d
„ Ze	j
Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚAnyTypec             C   s   d S )Nr!   )r_   r!   r!   r"   r`   *  s    zAnyType.__init__c             C   s   | S )Nr!   )r_   r!   r!   r"   r©   -  s    zAnyType.firstc             C   s   | S )Nr!   )r_   r!   r!   r"   rª   1  s    zAnyType.secondc             C   s   t |tƒp| | ¡S )N)rU   rð   rÊ   )r_   rÉ   r!   r!   r"   rÊ   5  s    zAnyType.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   8  s    zAnyType.__ne__c             C   s   dS )NTr!   )r_   rÉ   r!   r!   r"   ræ   =  s    zAnyType.matchesc             C   s   |S )Nr!   )r_   rÉ   r!   r!   r"   rè   @  s    zAnyType.resolvec             C   s   dS )Nú?r!   )r_   r!   r!   r"   rÓ   C  s    zAnyType.__str__c             C   s   dS )NÚANYr!   )r_   r!   r!   r"   rê   F  s    zAnyType.strN)r$   r%   r&   r`   Úpropertyr©   rª   rÊ   rË   rà   rÒ   ræ   rè   rÓ   rê   r!   r!   r!   r"   rð   (  s   rð   c             C   s
  t | tƒst‚|  dd¡} | d dkr¸| d dks6t‚d}xVt| ƒD ]J\}}|dkr^|d7 }qD|dkr||d8 }|dksŽt‚qD|dkrD|dkrDP qDW tt| d|… ƒt| |d d… ƒƒS | d d	t krÌtS | d d	t kràtS | d d	t	 krôt	S t
d d
| d  ƒ‚d S )Nra   rr   r   r¹   éÿÿÿÿrº   rK   r   z%szUnexpected character: '%s'.)rU   r   rW   ÚreplacerÀ   rå   rá   ÚENTITY_TYPEÚ
TRUTH_TYPErç   rh   )Ztype_stringZparen_countr{   Úcharr!   r!   r"   rá   P  s.    
$rá   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚTypeExceptionc                s   t t| ƒ |¡ d S )N)Úsuperrù   r`   )r_   rp   )r»   r!   r"   r`   n  s    zTypeException.__init__)r$   r%   r&   r`   Ú__classcell__r!   r!   )r»   r"   rù   m  s   rù   c                   s   e Zd Zd‡ fdd„	Z‡  ZS )Ú"InconsistentTypeHierarchyExceptionNc                s.   |rd||f }nd| }t t| ƒ |¡ d S )NzLThe variable '%s' was found in multiple places with different types in '%s'.zDThe variable '%s' was found in multiple places with different types.)rú   rü   r`   )r_   r¤   r‘   rp   )r»   r!   r"   r`   s  s    z+InconsistentTypeHierarchyException.__init__)N)r$   r%   r&   r`   rû   r!   r!   )r»   r"   rü   r  s   rü   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚTypeResolutionExceptionc                s    t t| ƒ d||j|f ¡ d S )Nz9The type of '%s', '%s', cannot be resolved with type '%s')rú   rý   r`   Útype)r_   r‘   Ú
other_type)r»   r!   r"   r`   ‚  s    
z TypeResolutionException.__init__)r$   r%   r&   r`   rû   r!   r!   )r»   r"   rý     s   rý   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚIllegalTypeExceptionc                s$   t t| ƒ d|jj|||f ¡ d S )Nz9Cannot set type of %s '%s' to '%s'; must match type '%s'.)rú   r   r`   r»   r$   )r_   r‘   rÿ   Zallowed_type)r»   r!   r"   r`   Š  s    
zIllegalTypeException.__init__)r$   r%   r&   r`   rû   r!   r!   )r»   r"   r   ‰  s   r   c             C   s<   x| D ]}|  |¡}qW x| dd… D ]}|  |¡ q&W |S )zè
    Ensure correct typing across a collection of ``Expression`` objects.
    :param expressions: a collection of expressions
    :param signature: dict that maps variable names to types (or string
    representations of types)
    Nrô   )rj   )Zexpressionsrl   r‘   r!   r!   r"   rj   ‘  s
    
rj   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSubstituteBindingsIzT
    An interface for classes that can perform substitutions for
    variables.
    c             C   s
   t ƒ ‚dS )zÍ
        :return: The object that is obtained by replacing
            each variable bound by ``bindings`` with its values.
            Aliases are already resolved. (maybe?)
        :rtype: (any)
        N)ÚNotImplementedError)r_   rÏ   r!   r!   r"   rÐ   §  s    z'SubstituteBindingsI.substitute_bindingsc             C   s
   t ƒ ‚dS )zB
        :return: A list of all variables in this object.
        N)r  )r_   r!   r!   r"   Ú	variables°  s    zSubstituteBindingsI.variablesN)r$   r%   r&   r½   rÐ   r  r!   r!   r!   r"   r  ¡  s   	r  c               @   s  e Zd ZdZeƒ ZeddZe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dAdd„Zdd„ Zd d!„ ZdBd"d#„Zd$d%„ Zedfd&d'„ZdCd(d)„ZdDd*d+„Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Z d6d7„ Z!d8d9„ Z"d:d;„ Z#d<d=„ Z$d>d?„ Z%dS )EÚ
Expressionz<This is the base abstract object for all logical expressionsT)rZ   FNc             C   s$   |r| j  ||¡S | j ||¡S d S )N)Ú_type_checking_logic_parserrq   Ú_logic_parser)râ   rÅ   rZ   rl   r!   r!   r"   rã   ¾  s    zExpression.fromstringc             G   s$   |   |¡}x|D ]}||ƒ}qW |S )N)Úapplyto)r_   rÉ   Z
additionalrˆ   Úar!   r!   r"   Ú__call__Å  s    

zExpression.__call__c             C   s    t |tƒstd| ƒ‚t| |ƒS )Nz%s is not an Expression)rU   r  rW   r²   )r_   rÉ   r!   r!   r"   r  Ë  s    zExpression.applytoc             C   s   t | ƒS )N)r“   )r_   r!   r!   r"   Ú__neg__Ï  s    zExpression.__neg__c             C   s   |  S )zWIf this is a negated expression, remove the negation.
        Otherwise add a negation.r!   )r_   r!   r!   r"   ÚnegateÒ  s    zExpression.negatec             C   s    t |tƒstd| ƒ‚t| |ƒS )Nz%s is not an Expression)rU   r  rÌ   r­   )r_   rÉ   r!   r!   r"   Ú__and__×  s    
zExpression.__and__c             C   s    t |tƒstd| ƒ‚t| |ƒS )Nz%s is not an Expression)rU   r  rÌ   r®   )r_   rÉ   r!   r!   r"   Ú__or__Ü  s    
zExpression.__or__c             C   s    t |tƒstd| ƒ‚t| |ƒS )Nz%s is not an Expression)rU   r  rÌ   r¯   )r_   rÉ   r!   r!   r"   Ú__gt__á  s    
zExpression.__gt__c             C   s    t |tƒstd| ƒ‚t| |ƒS )Nz%s is not an Expression)rU   r  rÌ   r°   )r_   rÉ   r!   r!   r"   rÍ   æ  s    
zExpression.__lt__c             C   s
   t ƒ ‚d S )N)r  )r_   rÉ   r!   r!   r"   rÊ   ë  s    zExpression.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   î  s    zExpression.__ne__c             C   sL   t |tƒstd| ƒ‚|dkr0ddlm} |ƒ }t|  ¡ | ¡ ƒ}| |¡S )a9  
        Check for logical equivalence.
        Pass the expression (self <-> other) to the theorem prover.
        If the prover says it is valid, then the self and other are equal.

        :param other: an ``Expression`` to check equality against
        :param prover: a ``nltk.inference.api.Prover``
        z%s is not an ExpressionNr   )ÚProver9)rU   r  rW   Znltk.inferencer  r°   ÚsimplifyZprove)r_   rÉ   Zproverr  Zbicondr!   r!   r"   Úequivñ  s    	zExpression.equivc             C   s   t t| ƒƒS )N)rÑ   Úrepr)r_   r!   r!   r"   rÒ     s    zExpression.__hash__c             C   sr   | }xd|  ¡ D ]X}||kr|| }t|tƒr8|  |¡}nt|tƒsPtd|f ƒ‚| |¡}| ||¡}qW | ¡ S )Nz@Can not substitute a non-expression value into an expression: %r)	r  rU   r™   r”   r  rÄ   rÐ   rõ   r  )r_   rÏ   ÚexprÚvarÚvalr!   r!   r"   rÐ     s    



zExpression.substitute_bindingsc                sz   t tƒ‰ |rXxJ|D ]B}|| }tt|ƒƒ}t|tƒr<||_n
t|ƒ|_ˆ |  |¡ qW | j	ˆ d t
‡ fdd„ˆ D ƒƒS )zý
        Infer and check types.  Raise exceptions if necessary.

        :param signature: dict that maps variable names to types (or string
            representations of types)
        :return: the signature, plus any additional type mappings
        )rl   c             3   s    | ]}|ˆ | d  j fV  qdS )r   N)rþ   )r   Úkey)Úsigr!   r"   ú	<genexpr>-  s    z'Expression.typecheck.<locals>.<genexpr>)r   r·   rµ   r™   rU   rà   rþ   rá   rv   Ú	_set_typer\   )r_   rl   r  r  ÚvarExr!   )r  r"   rj     s    


zExpression.typecheckc             C   s
   t ƒ ‚dS )zÉ
        Find the type of the given variable as it is used in this expression.
        For example, finding the type of "P" in "P(x) & Q(x,y)" yields "<e,t>"

        :param variable: Variable
        N)r  )r_   r¤   r!   r!   r"   Úfindtype/  s    zExpression.findtypec             C   s
   t ƒ ‚dS )zá
        Set the type of this expression to be the given type.  Raise type
        exceptions where applicable.

        :param other_type: Type
        :param signature: dict(str -> list(AbstractVariableExpression))
        N)r  )r_   rÿ   rl   r!   r!   r"   r  8  s    zExpression._set_typec                sH   t ˆtƒstdˆ ƒ‚t ˆtƒs,tdˆ ƒ‚|  ‡ ‡‡‡fdd„| j¡S )au  
        Replace every instance of 'variable' with 'expression'
        :param variable: ``Variable`` The variable to replace
        :param expression: ``Expression`` The expression with which to replace it
        :param replace_bound: bool Should bound variables be replaced?
        :param alpha_convert: bool Alpha convert automatically to avoid name clashes?
        z%s is not a Variablez%s is not an Expressionc                s   |   ˆˆˆˆ ¡S )N)rõ   )ro   )Úalpha_convertr‘   Úreplace_boundr¤   r!   r"   Ú<lambda>P  s    z$Expression.replace.<locals>.<lambda>)rU   r™   rW   r  Úvisit_structuredr»   )r_   r¤   r‘   r  r  r!   )r  r‘   r  r¤   r"   rõ   B  s    
zExpression.replacec                s’   ‡ fdd„‰ | }x|t tˆ | ƒdd„ dƒD ]`\}}t|tƒrT| td|d  ƒ¡}n&t|tƒrv| td|d  ƒ¡}n|}| |j|d	¡}q*W |S )
z&Rename auto-generated unique variablesc                s8   t | tƒrt| gƒS t | tƒr$tƒ S |  ˆ dd„ ¡S d S )Nc             S   s   t tj| tƒ ƒS )N)r   ÚoperatorÚor_Úset)Úpartsr!   r!   r"   r  ^  s    z>Expression.normalize.<locals>.get_indiv_vars.<locals>.<lambda>)rU   ÚIndividualVariableExpressionr"  ÚAbstractVariableExpressionÚvisit)ro   )Úget_indiv_varsr!   r"   r'  W  s    


z,Expression.normalize.<locals>.get_indiv_varsc             S   s   | j S )N)r¤   )ro   r!   r!   r"   r  b  s    z&Expression.normalize.<locals>.<lambda>)r  ze0%srK   zz%sT)	rÀ   ÚsortedrU   ÚEventVariableExpressionr»   r™   r$  rõ   r¤   )r_   Znewvarsrn   r{   ro   ZnewVarr!   )r'  r"   Ú	normalizeT  s    
"

zExpression.normalizec             C   s
   t ƒ ‚dS )aR  
        Recursively visit subexpressions.  Apply 'function' to each
        subexpression and pass the result of each function application
        to the 'combinator' for aggregation:

            return combinator(map(function, self.subexpressions))

        Bound variables are neither applied upon by the function nor given to
        the combinator.
        :param function: ``Function<Expression,T>`` to call on each subexpression
        :param combinator: ``Function<list<T>,R>`` to combine the results of the
        function calls
        :return: result of combination ``R``
        N)r  )r_   r³   Ú
combinatorr!   r!   r"   r&  l  s    zExpression.visitc                s   |   |‡ fdd„¡S )af  
        Recursively visit subexpressions.  Apply 'function' to each
        subexpression and pass the result of each function application
        to the 'combinator' for aggregation.  The combinator must have
        the same signature as the constructor.  The function is not
        applied to bound variables, but they are passed to the
        combinator.
        :param function: ``Function`` to call on each subexpression
        :param combinator: ``Function`` with the same signature as the
        constructor, to combine the results of the function calls
        :return: result of combination
        c                s   ˆ | Ž S )Nr!   )r#  )r+  r!   r"   r  Š  s    z-Expression.visit_structured.<locals>.<lambda>)r&  )r_   r³   r+  r!   )r+  r"   r  }  s    zExpression.visit_structuredc             C   s   d| j j| f S )Nz<%s %s>)r»   r$   )r_   r!   r!   r"   r¼   Œ  s    zExpression.__repr__c             C   s   |   ¡ S )N)rê   )r_   r!   r!   r"   rÓ     s    zExpression.__str__c             C   s&   |   ¡ tdd„ |  ¡ |  ¡ B D ƒƒB S )zþ
        Return a set of all the variables for binding substitution.
        The variables returned include all free (non-bound) individual
        variables and any variable starting with '?' or '@'.
        :return: set of ``Variable`` objects
        c             s   s    | ]}t  d |j¡r|V  qdS )z^[?@]N)r   r   r¶   )r   Úpr!   r!   r"   r  š  s    z'Expression.variables.<locals>.<genexpr>)Úfreer"  Ú
predicatesÚ	constants)r_   r!   r!   r"   r  ’  s    zExpression.variablesc             C   s   |   dd„ dd„ ¡S )zÅ
        Return a set of all the free (non-bound) variables.  This includes
        both individual and predicate variables, but not constants.
        :return: set of ``Variable`` objects
        c             S   s   |   ¡ S )N)r-  )ro   r!   r!   r"   r  ¤  s    z!Expression.free.<locals>.<lambda>c             S   s   t tj| tƒ ƒS )N)r   r   r!  r"  )r#  r!   r!   r"   r  ¤  s    )r&  )r_   r!   r!   r"   r-    s    zExpression.freec             C   s   |   dd„ dd„ ¡S )zu
        Return a set of individual constants (non-predicates).
        :return: set of ``Variable`` objects
        c             S   s   |   ¡ S )N)r/  )ro   r!   r!   r"   r  ­  s    z&Expression.constants.<locals>.<lambda>c             S   s   t tj| tƒ ƒS )N)r   r   r!  r"  )r#  r!   r!   r"   r  ­  s    )r&  )r_   r!   r!   r"   r/  §  s    zExpression.constantsc             C   s   |   dd„ dd„ ¡S )zu
        Return a set of predicates (constants, not variables).
        :return: set of ``Variable`` objects
        c             S   s   |   ¡ S )N)r.  )ro   r!   r!   r"   r  ¶  s    z'Expression.predicates.<locals>.<lambda>c             S   s   t tj| tƒ ƒS )N)r   r   r!  r"  )r#  r!   r!   r"   r  ¶  s    )r&  )r_   r!   r!   r"   r.  °  s    zExpression.predicatesc             C   s   |   dd„ | j¡S )zD
        :return: beta-converted version of this expression
        c             S   s   |   ¡ S )N)r  )ro   r!   r!   r"   r  ½  s    z%Expression.simplify.<locals>.<lambda>)r  r»   )r_   r!   r!   r"   r  ¹  s    zExpression.simplifyc             C   s   t |ƒS )N)rµ   )r_   r¤   r!   r!   r"   r”   ¿  s    z"Expression.make_VariableExpression)FN)N)N)FT)N)&r$   r%   r&   r½   rJ   r  r  rä   rã   r	  r  r
  r  r  r  r  rÍ   rÊ   rË   r  rÒ   rÐ   rj   r  rç   r  rõ   r*  r&  r  r¼   rÓ   r  r-  r/  r.  r  r”   r!   r!   r!   r"   r  ·  sB   


	



		r  c               @   s    e Zd ZdZdd„ Zdd„ Zedd„ ƒZedfd	d
„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZejZdd„ Zdd„ Zedd„ ƒZedd„ ƒZdd „ ZdS )!r²   a`  
    This class is used to represent two related types of logical expressions.

    The first is a Predicate Expression, such as "P(x,y)".  A predicate
    expression is comprised of a ``FunctionVariableExpression`` or
    ``ConstantExpression`` as the predicate and a list of Expressions as the
    arguments.

    The second is a an application of one expression to another, such as
    "(\x.dog(x))(fido)".

    The reason Predicate Expressions are treated as Application Expressions is
    that the Variable Expression predicate of the expression may be replaced
    with another Expression, such as a LambdaExpression, which would mean that
    the Predicate should be thought of as being applied to the arguments.

    The logical expression reader will always curry arguments in a application expression.
    So, "\x y.see(x,y)(john,mary)" will be represented internally as
    "((\x y.(see(x))(y))(john))(mary)".  This simplifies the internals since
    there will always be exactly one argument in an application.

    The str() method will usually print the curried forms of application
    expressions.  The one exception is when the the application expression is
    really a predicate expression (ie, underlying function is an
    ``AbstractVariableExpression``).  This means that the example from above
    will be returned as "(\x y.see(x,y)(john))(mary)".
    c             C   s<   t |tƒstd| ƒ‚t |tƒs,td| ƒ‚|| _|| _dS )zˆ
        :param function: ``Expression``, for the function expression
        :param argument: ``Expression``, for the argument
        z%s is not an ExpressionN)rU   r  rW   r³   r´   )r_   r³   r´   r!   r!   r"   r`   á  s    zApplicationExpression.__init__c             C   sB   | j  ¡ }| j ¡ }t|tƒr2|j |j|¡ ¡ S |  ||¡S d S )N)	r³   r  r´   rU   r±   r¥   rõ   r¤   r»   )r_   r³   r´   r!   r!   r"   r  ë  s
    


zApplicationExpression.simplifyc             C   s    t | jjtƒr| jjjS tS d S )N)rU   r³   rþ   rå   rª   rç   )r_   r!   r!   r"   rþ   ó  s    
zApplicationExpression.typeNc          
   C   sŠ   t |tƒst‚|dkrttƒ}| j t|¡ y| j t	| jj
|ƒ|¡ W n< tk
r„   td| j| jj
| j| jj
| jj
jf ƒ‚Y nX dS )z:see Expression._set_type()NzqThe function '%s' is of type '%s' and cannot be applied to '%s' of type '%s'.  Its argument must match type '%s'.)rU   rà   rW   r   r·   r´   r  rç   r³   rå   rþ   rý   rù   r©   )r_   rÿ   rl   r!   r!   r"   r  ú  s    zApplicationExpression._set_typec                s¶   t ˆ tƒstdˆ  ƒ‚|  ¡ r,|  ¡ \}}n| j}| jg}‡ fdd„|g| D ƒ}g }x>|D ]6}|tkr\|rˆx$|D ]}| |¡rrP qrW q\| 	|¡ q\W t
|ƒdkr®t|ƒd S tS dS )z:see Expression.findtype()z%s is not a Variablec                s   g | ]}|  ˆ ¡‘qS r!   )r  )r   Úarg)r¤   r!   r"   r#     s    z2ApplicationExpression.findtype.<locals>.<listcomp>rK   r   N)rU   r™   rW   Úis_atomÚuncurryr³   r´   rç   ræ   rv   rt   r·   )r_   r¤   r³   ÚargsÚfoundÚuniqueré   Úur!   )r¤   r"   r    s"    


zApplicationExpression.findtypec             C   s,   t | jtƒrtƒ }n
| j ¡ }|| j ¡ B S )z:see: Expression.constants())rU   r³   r%  r"  r/  r´   )r_   Zfunction_constantsr!   r!   r"   r/  .  s    
zApplicationExpression.constantsc             C   s4   t | jtƒrt| jjgƒ}n
| j ¡ }|| j ¡ B S )z:see: Expression.predicates())rU   r³   r–   r"  r¤   r.  r´   )r_   Zfunction_predsr!   r!   r"   r.  6  s    
z ApplicationExpression.predicatesc             C   s   ||| j ƒ|| jƒgƒS )z:see: Expression.visit())r³   r´   )r_   r³   r+  r!   r!   r"   r&  >  s    zApplicationExpression.visitc             C   s"   t |tƒo | j|jko | j|jkS )N)rU   r²   r³   r´   )r_   rÉ   r!   r!   r"   rÊ   B  s    
zApplicationExpression.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   I  s    zApplicationExpression.__ne__c             C   s¸   |   ¡ r*|  ¡ \}}d dd„ |D ƒ¡}n| j}d| j }d| }d}t|tƒr‚t|jtƒrpt|jjt	ƒs€d}qt|jt
ƒsd}nt|tƒrd}|r¤tj| tj }|tj | tj S )Nr   c             s   s   | ]}d | V  qdS )z%sNr!   )r   r0  r!   r!   r"   r  R  s    z0ApplicationExpression.__str__.<locals>.<genexpr>z%sFT)r1  r2  Újoinr³   r´   rU   r±   r¥   r²   r%  ÚBooleanExpressionr   r.   r/   )r_   r³   r3  Zarg_strZfunction_strZparenthesize_functionr!   r!   r"   rÓ   N  s$    


zApplicationExpression.__str__c             C   s:   | j }| jg}x"t|tƒr0| d|j¡ |j }qW ||fS )zh
        Uncurry this application expression

        return: A tuple (base-function, arg-list)
        r   )r³   r´   rU   r²   Úinsert)r_   r³   r3  r!   r!   r"   r2  h  s    
zApplicationExpression.uncurryc             C   s   |   ¡ d S )z¯
        Return uncurried base-function.
        If this is an atom, then the result will be a variable expression.
        Otherwise, it will be a lambda expression.
        r   )r2  )r_   r!   r!   r"   Úpredv  s    zApplicationExpression.predc             C   s   |   ¡ d S )z+
        Return uncurried arg-list
        rK   )r2  )r_   r!   r!   r"   r3    s    zApplicationExpression.argsc             C   s   t | jtƒS )zk
        Is this expression an atom (as opposed to a lambda expression applied
        to a term)?
        )rU   r:  r%  )r_   r!   r!   r"   r1  †  s    zApplicationExpression.is_atom)r$   r%   r&   r½   r`   r  ró   rþ   rç   r  r  r/  r.  r&  rÊ   rË   r  rÒ   rÓ   r2  r:  r3  r1  r!   r!   r!   r"   r²   Ã  s"   
	r²   c               @   sn   e Zd ZdZdd„ Zdd„ Zddd	„Zed
fdd„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZejZdd„ Zd
S )r%  zDThis class represents a variable to be used as a predicate or entityc             C   s    t |tƒstd| ƒ‚|| _dS )zA
        :param variable: ``Variable``, for the variable
        z%s is not a VariableN)rU   r™   rW   r¤   )r_   r¤   r!   r!   r"   r`   “  s    z#AbstractVariableExpression.__init__c             C   s   | S )Nr!   )r_   r!   r!   r"   r  š  s    z#AbstractVariableExpression.simplifyFTc             C   sB   t |tƒstd| ƒ‚t |tƒs,td| ƒ‚| j|kr:|S | S dS )z:see: Expression.replace()z%s is not an Variablez%s is not an ExpressionN)rU   r™   rW   r  r¤   )r_   r¤   r‘   r  r  r!   r!   r"   rõ     s    

z"AbstractVariableExpression.replaceNc             C   s‚   t |tƒst‚|dkrttƒ}|}x,|| jj D ]}|j |¡}|s0t	| ƒ‚q0W || jj  
| ¡ x|| jj D ]
}||_qpW dS )z:see Expression._set_type()N)rU   rà   rW   r   r·   r¤   r¶   rþ   rè   rü   rv   )r_   rÿ   rl   Ú
resolutionr  r!   r!   r"   r  ¨  s    z$AbstractVariableExpression._set_typec             C   s.   t |tƒstd| ƒ‚| j|kr&| jS tS dS )z:see Expression.findtype()z%s is not a VariableN)rU   r™   rW   r¤   rþ   rç   )r_   r¤   r!   r!   r"   r  ¹  s    
z#AbstractVariableExpression.findtypec             C   s   t ƒ S )z:see: Expression.predicates())r"  )r_   r!   r!   r"   r.  Á  s    z%AbstractVariableExpression.predicatesc             C   s   t |tƒo| j|jkS )zTAllow equality between instances of ``AbstractVariableExpression``
        subtypes.)rU   r%  r¤   )r_   rÉ   r!   r!   r"   rÊ   Å  s    
z!AbstractVariableExpression.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   Í  s    z!AbstractVariableExpression.__ne__c             C   s   t |tƒst‚| j|jk S )N)rU   r%  rÌ   r¤   )r_   rÉ   r!   r!   r"   rÍ   Ð  s    
z!AbstractVariableExpression.__lt__c             C   s
   d| j  S )Nz%s)r¤   )r_   r!   r!   r"   rÓ   ×  s    z"AbstractVariableExpression.__str__)FT)r$   r%   r&   r½   r`   r  rõ   rç   r  r  r.  rÊ   rË   rÍ   r  rÒ   rÓ   r!   r!   r!   r"   r%  Ž  s   
r%  c               @   s@   e Zd ZdZedfdd„Zdd„ ZeeeƒZdd„ Z	d	d
„ Z
dS )r$  zˆThis class represents variables that take the form of a single lowercase
    character (other than 'e') followed by zero or more digits.Nc             C   sJ   t |tƒst‚|dkrttƒ}| t¡s4t| |tƒ‚|| jj	  
| ¡ dS )z:see Expression._set_type()N)rU   rà   rW   r   r·   ræ   rö   r   r¤   r¶   rv   )r_   rÿ   rl   r!   r!   r"   r  ß  s    
z&IndividualVariableExpression._set_typec             C   s   t S )N)rö   )r_   r!   r!   r"   Ú	_get_typeë  s    z&IndividualVariableExpression._get_typec             C   s   t | jgƒS )z:see: Expression.free())r"  r¤   )r_   r!   r!   r"   r-  ð  s    z!IndividualVariableExpression.freec             C   s   t ƒ S )z:see: Expression.constants())r"  )r_   r!   r!   r"   r/  ô  s    z&IndividualVariableExpression.constants)r$   r%   r&   r½   rç   r  r<  ró   rþ   r-  r/  r!   r!   r!   r"   r$  Û  s   
r$  c               @   s$   e Zd ZdZeZdd„ Zdd„ ZdS )r•   zwThis class represents variables that take the form of a single uppercase
    character followed by zero or more digits.c             C   s   t | jgƒS )z:see: Expression.free())r"  r¤   )r_   r!   r!   r"   r-  ÿ  s    zFunctionVariableExpression.freec             C   s   t ƒ S )z:see: Expression.constants())r"  )r_   r!   r!   r"   r/    s    z$FunctionVariableExpression.constantsN)r$   r%   r&   r½   rç   rþ   r-  r/  r!   r!   r!   r"   r•   ù  s   r•   c               @   s   e Zd ZdZeZdS )r)  z{This class represents variables that take the form of a single lowercase
    'e' character followed by zero or more digits.N)r$   r%   r&   r½   Ú
EVENT_TYPErþ   r!   r!   r!   r"   r)    s   r)  c               @   s2   e Zd ZdZeZedfdd„Zdd„ Zdd„ Z	dS )	r–   ztThis class represents variables that do not take the form of a single
    character followed by zero or more digits.Nc             C   s¦   t |tƒst‚|dkrttƒ}|tkr,t}n|}| jtkrF| | j¡}x,|| j	j
 D ]}|j |¡}|sTt| ƒ‚qTW || j	j
  | ¡ x|| j	j
 D ]
}||_q”W dS )z:see Expression._set_type()N)rU   rà   rW   r   r·   rç   rö   rþ   rè   r¤   r¶   rü   rv   )r_   rÿ   rl   r;  r  r!   r!   r"   r    s    
zConstantExpression._set_typec             C   s   t ƒ S )z:see: Expression.free())r"  )r_   r!   r!   r"   r-  -  s    zConstantExpression.freec             C   s   t | jgƒS )z:see: Expression.constants())r"  r¤   )r_   r!   r!   r"   r/  1  s    zConstantExpression.constants)
r$   r%   r&   r½   rö   rþ   rç   r  r-  r/  r!   r!   r!   r"   r–     s
   r–   c             C   sX   t | tƒstd|  ƒ‚t| jƒr(t| ƒS t| jƒr:t| ƒS t| jƒrLt	| ƒS t
| ƒS dS )z”
    This is a factory method that instantiates and returns a subtype of
    ``AbstractVariableExpression`` appropriate for the given variable.
    z%s is not a VariableN)rU   r™   rW   rÖ   r¶   r$  r×   r•   rØ   r)  r–   )r¤   r!   r!   r"   rµ   6  s    


rµ   c               @   s`   e Zd 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ejZdS )ÚVariableBinderExpressionz‘This an abstract class for any Expression that binds a variable in an
    Expression.  This includes LambdaExpressions and Quantified Expressionsc             C   s<   t |tƒstd| ƒ‚t |tƒs,td| ƒ‚|| _|| _dS )zs
        :param variable: ``Variable``, for the variable
        :param term: ``Expression``, for the term
        z%s is not a Variablez%s is not an ExpressionN)rU   r™   rW   r  r¤   r¥   )r_   r¤   r¥   r!   r!   r"   r`   J  s    z!VariableBinderExpression.__init__FTc          	   C   s¶   t |tƒstd| ƒ‚t |tƒs,td| ƒ‚| j|krr|rlt |tƒsPtd| ƒ‚|  |j| j ||d|¡¡S | S n@|r–| j| 	¡ kr–|  
t| jd¡} |  | j| j ||||¡¡S dS )z:see: Expression.replace()z%s is not a Variablez%s is not an Expressionz&%s is not a AbstractVariableExpressionT)rÚ   N)rU   r™   rW   r  r¤   r%  r»   r¥   rõ   r-  r  rÞ   )r_   r¤   r‘   r  r  r!   r!   r"   rõ   T  s     


z VariableBinderExpression.replacec             C   s4   t |tƒstd| ƒ‚|  || j | jt|ƒd¡¡S )zµRename all occurrences of the variable introduced by this variable
        binder in the expression to ``newvar``.
        :param newvar: ``Variable``, for the new variable
        z%s is not a VariableT)rU   r™   rW   r»   r¥   rõ   r¤   rµ   )r_   Znewvarr!   r!   r"   r  r  s    z&VariableBinderExpression.alpha_convertc             C   s   | j  ¡ t| jgƒ S )z:see: Expression.free())r¥   r-  r"  r¤   )r_   r!   r!   r"   r-  |  s    zVariableBinderExpression.freec             C   s4   t |tƒstd| ƒ‚|| jkr$tS | j |¡S dS )z:see Expression.findtype()z%s is not a VariableN)rU   r™   rW   r¤   rç   r¥   r  )r_   r¤   r!   r!   r"   r  €  s    
z!VariableBinderExpression.findtypec             C   s   ||| j ƒgƒS )z:see: Expression.visit())r¥   )r_   r³   r+  r!   r!   r"   r&  ˆ  s    zVariableBinderExpression.visitc             C   s   || j || jƒƒS )z#:see: Expression.visit_structured())r¤   r¥   )r_   r³   r+  r!   r!   r"   r  Œ  s    z)VariableBinderExpression.visit_structuredc             C   sZ   t | |jƒst || jƒrR| j|jkr0| j|jkS t| jƒ}| j|j |j|¡kS ndS dS )z~Defines equality modulo alphabetic variance.  If we are comparing
        \x.M  and \y.N, then check equality of M and N[x/y].FN)rU   r»   r¤   r¥   rµ   rõ   )r_   rÉ   Zvarexr!   r!   r"   rÊ     s    
zVariableBinderExpression.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË     s    zVariableBinderExpression.__ne__N)FT)r$   r%   r&   r½   r`   rõ   r  r-  r  r&  r  rÊ   rË   r  rÒ   r!   r!   r!   r"   r>  F  s   


r>  c               @   s.   e Zd Zedd„ ƒZedfdd„Zdd„ ZdS )r±   c             C   s   t | j | j¡| jjƒS )N)rå   r¥   r  r¤   rþ   )r_   r!   r!   r"   rþ   ¥  s    zLambdaExpression.typeNc             C   sH   t |tƒst‚|dkrttƒ}| j |j|¡ | j 	|¡sDt
| |ƒ‚dS )z:see Expression._set_type()N)rU   rà   rW   r   r·   r¥   r  rª   rþ   rè   rý   )r_   rÿ   rl   r!   r!   r"   r  ©  s    zLambdaExpression._set_typec             C   sZ   | j g}| j}x"|j| jkr0| |j ¡ |j}qW tjd dd„ |D ƒ¡ tj d|  S )Nra   c             s   s   | ]}d | V  qdS )z%sNr!   )r   rÝ   r!   r!   r"   r  ¼  s    z+LambdaExpression.__str__.<locals>.<genexpr>z%s)r¤   r¥   r»   rv   r   r'   r7  r-   )r_   r  r¥   r!   r!   r"   rÓ   ´  s    
zLambdaExpression.__str__)r$   r%   r&   ró   rþ   rç   r  rÓ   r!   r!   r!   r"   r±   £  s   r±   c               @   s.   e Zd Zedd„ ƒZedfdd„Zdd„ ZdS )ÚQuantifiedExpressionc             C   s   t S )N)r÷   )r_   r!   r!   r"   rþ   Ä  s    zQuantifiedExpression.typeNc             C   sF   t |tƒst‚|dkrttƒ}| t¡s4t| |tƒ‚| j 	t|¡ dS )z:see Expression._set_type()N)
rU   rà   rW   r   r·   ræ   r÷   r   r¥   r  )r_   rÿ   rl   r!   r!   r"   r  È  s    
zQuantifiedExpression._set_typec             C   s`   | j g}| j}x"|j| jkr0| |j ¡ |j}qW |  ¡ d d dd„ |D ƒ¡ tj d|  S )Nra   c             s   s   | ]}d | V  qdS )z%sNr!   )r   rÝ   r!   r!   r"   r  Ü  s    z/QuantifiedExpression.__str__.<locals>.<genexpr>z%s)r¤   r¥   r»   rv   ÚgetQuantifierr7  r   r-   )r_   r  r¥   r!   r!   r"   rÓ   Ó  s    
zQuantifiedExpression.__str__)r$   r%   r&   ró   rþ   rç   r  rÓ   r!   r!   r!   r"   r?  Â  s   r?  c               @   s   e Zd Zdd„ ZdS )r¡   c             C   s   t jS )N)r   r)   )r_   r!   r!   r"   r@  ã  s    zExistsExpression.getQuantifierN)r$   r%   r&   r@  r!   r!   r!   r"   r¡   â  s   r¡   c               @   s   e Zd Zdd„ ZdS )r¢   c             C   s   t jS )N)r   r+   )r_   r!   r!   r"   r@  è  s    zAllExpression.getQuantifierN)r$   r%   r&   r@  r!   r!   r!   r"   r¢   ç  s   r¢   c               @   sd   e Zd Zdd„ Zedd„ ƒZedfdd„Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ ZejZdd„ ZdS )r“   c             C   s    t |tƒstd| ƒ‚|| _d S )Nz%s is not an Expression)rU   r  rW   r¥   )r_   r¥   r!   r!   r"   r`   î  s    zNegatedExpression.__init__c             C   s   t S )N)r÷   )r_   r!   r!   r"   rþ   ò  s    zNegatedExpression.typeNc             C   sF   t |tƒst‚|dkrttƒ}| t¡s4t| |tƒ‚| j 	t|¡ dS )z:see Expression._set_type()N)
rU   rà   rW   r   r·   ræ   r÷   r   r¥   r  )r_   rÿ   rl   r!   r!   r"   r  ö  s    
zNegatedExpression._set_typec             C   s"   t |tƒstd| ƒ‚| j |¡S )Nz%s is not a Variable)rU   r™   rW   r¥   r  )r_   r¤   r!   r!   r"   r    s    zNegatedExpression.findtypec             C   s   ||| j ƒgƒS )z:see: Expression.visit())r¥   )r_   r³   r+  r!   r!   r"   r&    s    zNegatedExpression.visitc             C   s   | j S )z:see: Expression.negate())r¥   )r_   r!   r!   r"   r  	  s    zNegatedExpression.negatec             C   s   t |tƒo| j|jkS )N)rU   r“   r¥   )r_   rÉ   r!   r!   r"   rÊ     s    zNegatedExpression.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË     s    zNegatedExpression.__ne__c             C   s   t jd| j  S )Nz%s)r   r1   r¥   )r_   r!   r!   r"   rÓ     s    zNegatedExpression.__str__)r$   r%   r&   r`   ró   rþ   rç   r  r  r&  r  rÊ   rË   r  rÒ   rÓ   r!   r!   r!   r"   r“   ì  s   r“   c               @   sV   e Zd Zdd„ Zedd„ ƒZdd„ Zdd„ Zd	d
„ Zdd„ Z	e
jZdd„ Zdd„ ZdS )ÚBinaryExpressionc             C   s<   t |tƒstd| ƒ‚t |tƒs,td| ƒ‚|| _|| _d S )Nz%s is not an Expression)rU   r  rW   r©   rª   )r_   r©   rª   r!   r!   r"   r`     s    zBinaryExpression.__init__c             C   s   t S )N)r÷   )r_   r!   r!   r"   rþ   !  s    zBinaryExpression.typec             C   sV   t |tƒstd| ƒ‚| j |¡}| j |¡}||ks>|tkrB|S |tkrN|S tS dS )z:see Expression.findtype()z%s is not a VariableN)rU   r™   rW   r©   r  rª   rç   )r_   r¤   ré   rÅ   r!   r!   r"   r  %  s    zBinaryExpression.findtypec             C   s   ||| j ƒ|| jƒgƒS )z:see: Expression.visit())r©   rª   )r_   r³   r+  r!   r!   r"   r&  1  s    zBinaryExpression.visitc             C   s0   t | |jƒst || jƒo.| j|jko.| j|jkS )N)rU   r»   r©   rª   )r_   rÉ   r!   r!   r"   rÊ   5  s    zBinaryExpression.__eq__c             C   s
   | |k S )Nr!   )r_   rÉ   r!   r!   r"   rË   <  s    zBinaryExpression.__ne__c             C   s<   |   | j¡}|   | j¡}tj| d |  ¡  d | tj S )Nra   )Ú
_str_subexr©   rª   r   r.   ÚgetOpr/   )r_   r©   rª   r!   r!   r"   rÓ   A  s    zBinaryExpression.__str__c             C   s   d| S )Nz%sr!   )r_   Úsubexr!   r!   r"   rB  F  s    zBinaryExpression._str_subexN)r$   r%   r&   r`   ró   rþ   r  r&  rÊ   rË   r  rÒ   rÓ   rB  r!   r!   r!   r"   rA    s   rA  c               @   s   e Zd Zedfdd„ZdS )r8  Nc             C   sT   t |tƒst‚|dkrttƒ}| t¡s4t| |tƒ‚| j 	t|¡ | j
 	t|¡ dS )z:see Expression._set_type()N)rU   rà   rW   r   r·   ræ   r÷   r   r©   r  rª   )r_   rÿ   rl   r!   r!   r"   r  K  s    
zBooleanExpression._set_type)r$   r%   r&   rç   r  r!   r!   r!   r"   r8  J  s   r8  c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r­   z"This class represents conjunctionsc             C   s   t jS )N)r   r3   )r_   r!   r!   r"   rC  [  s    zAndExpression.getOpc             C   s"   d| }t |tƒr|dd… S |S )Nz%srK   rô   )rU   r­   )r_   rD  rÅ   r!   r!   r"   rB  ^  s    
zAndExpression._str_subexN)r$   r%   r&   r½   rC  rB  r!   r!   r!   r"   r­   X  s   r­   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r®   z"This class represents disjunctionsc             C   s   t jS )N)r   r5   )r_   r!   r!   r"   rC  h  s    zOrExpression.getOpc             C   s"   d| }t |tƒr|dd… S |S )Nz%srK   rô   )rU   r®   )r_   rD  rÅ   r!   r!   r"   rB  k  s    
zOrExpression._str_subexN)r$   r%   r&   r½   rC  rB  r!   r!   r!   r"   r®   e  s   r®   c               @   s   e Zd ZdZdd„ ZdS )r¯   z"This class represents implicationsc             C   s   t jS )N)r   r7   )r_   r!   r!   r"   rC  u  s    zImpExpression.getOpN)r$   r%   r&   r½   rC  r!   r!   r!   r"   r¯   r  s   r¯   c               @   s   e Zd ZdZdd„ ZdS )r°   z$This class represents biconditionalsc             C   s   t jS )N)r   r9   )r_   r!   r!   r"   rC  |  s    zIffExpression.getOpN)r$   r%   r&   r½   rC  r!   r!   r!   r"   r°   y  s   r°   c               @   s&   e Zd ZdZedfdd„Zdd„ ZdS )r¨   z:This class represents equality expressions like "(x = y)".Nc             C   sT   t |tƒst‚|dkrttƒ}| t¡s4t| |tƒ‚| j 	t
|¡ | j 	t
|¡ dS )z:see Expression._set_type()N)rU   rà   rW   r   r·   ræ   r÷   r   r©   r  rö   rª   )r_   rÿ   rl   r!   r!   r"   r  ƒ  s    
zEqualityExpression._set_typec             C   s   t jS )N)r   r;   )r_   r!   r!   r"   rC    s    zEqualityExpression.getOp)r$   r%   r&   r½   rç   r  rC  r!   r!   r!   r"   r¨   €  s   r¨   c               @   s   e Zd Zdd„ ZdS )rh   c             C   s   || _ t | |¡ d S )N)ri   Ú	Exceptionr`   )r_   ri   r„   r!   r!   r"   r`   —  s    z#LogicalExpressionException.__init__N)r$   r%   r&   r`   r!   r!   r!   r"   rh   –  s   rh   c               @   s   e Zd Zddd„ZdS )rf   Nc             C   sN   |r|rd||f }n&|r4d| }|r<|d| 7 }nd| }t  | ||¡ d S )Nz-Unexpected token: '%s'.  Expected token '%s'.zUnexpected token: '%s'.z  zExpected token '%s'.)rh   r`   )r_   ri   Z
unexpectedr¸   r„   rp   r!   r!   r"   r`     s    
z!UnexpectedTokenException.__init__)NNN)r$   r%   r&   r`   r!   r!   r!   r"   rf   œ  s   rf   c               @   s   e Zd Zddd„ZdS )r   Nc             C   s   |sd}t  | |d| ¡ d S )NzMore tokens expected.zEnd of input found.  )rh   r`   )r_   ri   r„   r!   r!   r"   r`   ­  s    z$ExpectedMoreTokensException.__init__)N)r$   r%   r&   r`   r!   r!   r!   r"   r   ¬  s   r   c             C   s&   t | tƒstd|  ƒ‚t d| ¡dk	S )zÆ
    An individual variable must be a single lowercase character other than 'e',
    followed by zero or more digits.

    :param expr: str
    :return: bool True if expr is of the correct form
    z%s is not a stringz^[a-df-z]\d*$N)rU   r   rW   r   r   )r  r!   r!   r"   rÖ   µ  s    rÖ   c             C   s&   t | tƒstd|  ƒ‚t d| ¡dk	S )z³
    A function variable must be a single uppercase character followed by
    zero or more digits.

    :param expr: str
    :return: bool True if expr is of the correct form
    z%s is not a stringz
^[A-Z]\d*$N)rU   r   rW   r   r   )r  r!   r!   r"   r×   Á  s    r×   c             C   s&   t | tƒstd|  ƒ‚t d| ¡dk	S )zµ
    An event variable must be a single lowercase 'e' character followed by
    zero or more digits.

    :param expr: str
    :return: bool True if expr is of the correct form
    z%s is not a stringz^e\d*$N)rU   r   rW   r   r   )r  r!   r!   r"   rØ   Í  s    rØ   c              C   sH  t j} 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| dƒ ¡ ƒ t| dƒ ¡ ƒ t| dƒ ¡ ƒ tdƒ | dƒ}t|ƒ | tdƒ¡}t|ƒ t||kƒ d S )Nz3====================Test reader====================Zjohnzman(x)z-man(x)z(man(x) & tall(x) & walks(x))z&exists x.(man(x) & tall(x) & walks(x))z	\x.man(x)z\x.man(x)(john)z\x y.sees(x,y)z\x y.sees(x,y)(a,b)z(\x.exists y.walks(x,y))(x)zexists x.x = yzexists x.(x = y)zP(x) & x=y & P(y)z\P Q.exists x.(P(x) & Q(x))zman(x) <-> tall(x)z5====================Test simplify====================z\x.\y.sees(x,y)(john)(mary)z\x.\y.sees(x,y)(john, mary)z,all x.(man(x) & (\x.exists y.walks(x,y))(x))z5(\P.\Q.exists x.(P(x) & Q(x)))(\x.dog(x))(\x.bark(x))z\====================Test alpha conversion and binder expression equality====================zexists x.P(x)rÔ   )r  rã   rC   r  r  r™   )ZlexprZe1Ze2r!   r!   r"   ÚdemoÙ  s8    rF  c               C   st   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ƒ d S )Nz:====================Test reader errors====================z(P(x) & Q(x)z((P(x) &) & Q(x))zP(x) -> zP(xzP(x,zP(x,)r   z	exists x.r   z\ x y.zP(x)Q(x)z	(P(x)Q(x)zexists x -> y)rC   ÚdemoExceptionr!   r!   r!   r"   Údemo_errorsú  s    rH  c          
   C   sJ   yt  | ¡ W n6 tk
rD } ztd|jj|f ƒ W d d }~X Y nX d S )Nz%s: %s)r  rã   rh   rC   r»   r$   )rÅ   ro   r!   r!   r"   rG    s    rG  c             C   s   t d|  ¡ | jf ƒ d S )Nz%s : %s)rC   rê   rþ   )Úexr!   r!   r"   Ú	printtype  s    rJ  Ú__main__)NN)NN)N)N)Rr½   Z
__future__r   r   r   r   Úcollectionsr   Ú	functoolsr   r   Zsixr   Z	nltk.utilr   Znltk.internalsr	   Znltk.compatr
   r   rÙ   Úobjectr   rF   rG   rI   rJ   rÈ   r™   rÞ   rß   rà   rå   rë   rì   rí   rï   rð   r÷   rö   r=  rç   rá   rE  rù   rü   rý   r   rj   r  r  r²   r%  r$  r•   r)  r–   rµ   r>  r±   r?  r¡   r¢   r“   rA  r8  r­   r®   r¯   r°   r¨   rh   rf   r   rÖ   r×   rØ   rF  rH  rG  rJ  r$   r!   r!   r!   r"   Ú<module>   s¼   +			   l

 

5
!
   K
K'],0	!
