U
    e_                    @   sv  d 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Ze	 ZG dd	 d	Zd
d Zdd Zdd ZG dd dZdwddZeG dd dZdxddZdyddZG dd d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G d$d% d%eZ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,d- d-e$Z%G d.d/ d/e$Z&G d0d1 d1e$Z'dzd2d3Z(G d4d5 d5Z)G d6d7 d7e)Z*G d8d9 d9e*Z+eG d:d; d;e*Z,G d<d= d=e,Z-G d>d? d?e,Z.G d@dA dAe-Z/G dBdC dCe,Z0dDdE Z1G dFdG dGe*Z2G dHdI dIe2Z3G dJdK dKe2Z4G dLdM dMe4Z5G dNdO dOe4Z6G dPdQ dQe4Z7G dRdS dSe*Z8G dTdU dUe*Z9G dVdW dWe9Z:G dXdY dYe:Z;G dZd[ d[e:Z<G d\d] d]e:Z=G d^d_ d_e:Z>G d`da dae9Z?G dbdc dce#Z@G ddde dee@ZAG dfdg dge@ZBdhdi ZCdjdk ZDdldm ZEdndo ZFdpdq ZGdrds ZHdtdu ZIeJdvkrreF  dS ){zV
A version of first order predicate logic, built on
top of the typed lambda calculus.
    N)defaultdict)reducetotal_ordering)Counter)Trie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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 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someexistallforalliota.(),-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!   O/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/sem/logic.py
<listcomp>E   s      zTokens.<listcomp>N)"__name__
__module____qualname__LAMBDALAMBDA_LISTEXISTSEXISTS_LISTALLALL_LISTIOTA	IOTA_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   sB   dddddg} t | tjtjtjtjtjgD ]}td|  q,dS )z
    Boolean operators
    ZnegationZconjunctionZdisjunctionZimplicationZequivalence%-15s	%sN)zipr   r3   r5   r7   r9   r;   printnamespairr!   r!   r"   boolean_opsH   s    "rJ   c                  C   s0   ddg} t | tjtjgD ]}td|  qdS )z
    Equality predicates
    ZequalityZ
inequalityrD   N)rE   r   r=   r?   rF   rG   r!   r!   r"   equality_predsQ   s    rK   c                  C   s:   dddg} t | tjtjtjtjgD ]}td|  q$dS )z
    Binding operators
    Zexistential	universallambdarD   N)rE   r   r)   r+   r'   r-   rF   rG   r!   r!   r"   binding_opsZ   s    
rN   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 )z
        :param type_check: should type checking be performed
            to their types?
        :type type_check: bool
        r   c                 S   s   g | ]}|d fqS )   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#      s        c                 S   s   g | ]}|d fqS )   r!   r   r!   r!   r"   r#      s     c                 S   s   g | ]}|d fqS )   r!   r   r!   r!   r"   r#      s     c                 S   s   g | ]}|d fqS )   r!   r   r!   r!   r"   r#      s     c                 S   s   g | ]}|d fqS )   r!   r   r!   r!   r"   r#      s     c                 S   s   g | ]}|d fqS )   r!   r   r!   r!   r"   r#      s     c                 S   s   g | ]}|d fqS )	   r!   r   r!   r!   r"   r#      s     )N
   N)
isinstanceboolAssertionError_currentIndex_buffer
type_checkquote_charsdictr   r(   r4   r   r>   r@   rA   r6   r8   r:   r<   operator_precedenceright_associated_operations)selfr_   r!   r!   r"   __init__f   s6    	zLogicParser.__init__Nc                 C   s   |  }d| _| |\| _}z.| d}| drJt| jd | dW nJ tk
r } z,d	||d||j
d   }td||W 5 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   NrP   z	{}
{}
{}^ )rstripr]   processr^   process_next_expressioninRangeUnexpectedTokenExceptiontokenLogicalExpressionExceptionformatindexr_   	typecheck)rd   data	signaturemappingresultemsgr!   r!   r"   parse   s    	


zLogicParser.parsec                 C   s  g }i }t |  }d}d}|}|t|k rF|}| ||\}	}|	rX|sN|}||	7 }q |}
|| }d}||
kr||7 }|
| }
t|| t|kr||t|  }qhqqht j|
kr|r||t|< || d}||t|< || |t|7 }q || dkr&|r<||t|< || d}n|s0|}||| 7 }|d7 }q |rb||t|< || t||t|< t|d |t|d < ||fS )zSplit the data into tokens r   z 	
rP   )r   get_all_symbolslenprocess_quoted_tokenZLEAFappend)rd   rq   outrs   Z	tokenTrierl   data_idxZtoken_start_idxZcur_data_idxZquoted_tokenstcsymbolr!   r!   r"   rh      s\    





zLogicParser.processc           
      C   s   d}|| }|}| j D ]\}}}}	||kr|	r6||7 }|d7 }|| |kr|| |kr|	rf||| 7 }|d7 }t||krtd d| ||| 7 }n||| 7 }|d7 }t||kr>td d| q>|	r||| 7 }|d7 }|std d qq||fS )Nrx   rP   z:End of input reached.  Escape character [%s] found at end.z%End of input reached.  Expected: [%s]zEmpty quoted token found)r`   rz   rm   )
rd   r~   rq   rl   r   istartendescapeZincl_quotesr!   r!   r"   r{      sF     
z LogicParser.process_quoted_tokenc                 C   s   t jS )z#This method exists to be overridden)r   rC   rd   r!   r!   r"   ry      s    zLogicParser.get_all_symbolsc                 C   s   | j | t| jk S )z6Return TRUE if the given location is within the buffer)r]   rz   r^   )rd   locationr!   r!   r"   rj     s    zLogicParser.inRangec              
   C   sr   z:|dkr&| j | j }|  jd7  _n| j | j|  }|W S  tk
rl } zt| jd |W 5 d}~X Y nX dS )zGet 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.NrP   )r^   r]   
IndexErrorExpectedMoreTokensException)rd   r   tokru   r!   r!   r"   rl     s    zLogicParser.tokenc                 C   s
   |t jkS N)r   rB   rd   r   r!   r!   r"   
isvariable  s    zLogicParser.isvariablec              
   C   sp   z|   }W n6 tk
rB } zt| jd dd|W 5 d}~X Y nX | ||}|sdt| j|dd| ||S )zAParse the next complete expression from the stream and return it.rP   Expression expected.messageN)rl   r   r]   handlerk   attempt_adjuncts)rd   contextr   ru   accumr!   r!   r"   ri     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   r4   handle_negationr(   handle_lambdarA   handle_quantr0   handle_openrd   r   r   r!   r!   r"   r   +  s    




zLogicParser.handlec                 C   s>   d }|| j kr:| j }| ||}| ||}| ||}q|S r   )r]   attempt_EqualityExpressionattempt_ApplicationExpressionattempt_BooleanExpression)rd   
expressionr   Zcur_idxr!   r!   r"   r   =  s    
zLogicParser.attempt_adjunctsc                 C   s   |  | tjS r   )make_NegatedExpressionri   r   r3   r   r!   r!   r"   r   F  s    zLogicParser.handle_negationc                 C   s   t |S r   NegatedExpression)rd   r   r!   r!   r"   r   I  s    z"LogicParser.make_NegatedExpressionc                 C   s   |  |}| dr| dtjkrt|tsHt|tsHt| j	d| |   | 
|| t}| dr| dtjkr|   | 
|| t}qb| tj |S )Nr   zW'%s' is an illegal predicate name.  Individual variables may not be used as predicates.)make_VariableExpressionrj   rl   r   r0   rZ   FunctionVariableExpressionConstantExpressionrm   r]   make_ApplicationExpressionri   r   r2   assertNextTokenr1   rd   r   r   r   r!   r!   r"   r   L  s2    
   zLogicParser.handle_variablec              
   C   sj   z|   }W n0 tk
r< } zt|jd|W 5 d }~X Y nX t| |trbt| jd||f t|S )NzVariable expected.z;'%s' is an illegal variable name.  Constants may not be %s.)	rl   r   ro   rZ   r   r   rm   r]   Variable)rd   descriptionr   ru   r!   r!   r"   get_next_token_variablej  s     z#LogicParser.get_next_token_variablec                 C   s   |  dst| jd dd| dg}|  drL| dtjkr^|  ds^t| jd dd| | dspq|| d q(|  dr| dtjkr|   | 	|}|r| 
| |}q|S )Nr   rQ   z;Variable and Expression expected following lambda operator.r   Z
abstractedrP   r   )rj   r   r]   r   rl   r   r/   r   r|   ri   make_LambdaExpressionpop)rd   r   r   varsr   r!   r!   r"   r   w  s0    

 
zLogicParser.handle_lambdac                 C   s   |  |}| ds*t| jd d| d| dg}| drZ| dtjkrl| dslt| jd dd| | ds~q|	| d q6| dr| dtjkr|   | 
|}|r| || |}q|S )Nr   rQ   z;Variable and Expression expected following quantifier '%s'.r   Z
quantifiedrP   r   ) get_QuantifiedExpression_factoryrj   r   r]   r   rl   r   r/   r   r|   ri   make_QuanifiedExpressionr   )rd   r   r   factoryr   r   r!   r!   r"   r     s6    


 
zLogicParser.handle_quantc                 C   s<   |t jkrt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,   AllExpressionr.   IotaExpressionassertTokenrA   r   r!   r!   r"   r     s    


z,LogicParser.get_QuantifiedExpression_factoryc                 C   s
   |||S r   r!   )rd   r   variabletermr!   r!   r"   r     s    z$LogicParser.make_QuanifiedExpressionc                 C   s   |  d }| tj |S r   )ri   r   r   r1   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 )zAttempt 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   )	rj   rl   r   r>   r@   has_prioritymake_EqualityExpressionri   r   )rd   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rd   firstsecondr!   r!   r"   r     s    z#LogicParser.make_EqualityExpressionc                 C   sT   |  drP| d}| |}|rP| ||rP|   | ||| |}q qPq |S )zAttempt 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   )rj   rl   get_BooleanExpression_factoryr   make_BooleanExpressionri   )rd   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   r6   AndExpressionr8   OrExpressionr:   ImpExpressionr<   IffExpressionr   r!   r!   r"   r     s    



z)LogicParser.get_BooleanExpression_factoryc                 C   s
   |||S r   r!   )rd   r   r   r   r!   r!   r"   r     s    z"LogicParser.make_BooleanExpressionc                 C   s   |  t|r| dr| dtjkrt|tsbt|tsbt|t	sbt|t
sbt| jd| d |   | || t}| dr| dtjkr|   | || t}q|| tj |S |S )zAttempt 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   rj   rl   r   r0   rZ   LambdaExpressionApplicationExpressionr   r   rm   r]   r   ri   r2   r   r1   )rd   r   r   r   r!   r!   r"   r     s<      z)LogicParser.attempt_ApplicationExpressionc                 C   s
   t ||S r   )r   rd   functionargumentr!   r!   r"   r     s    z&LogicParser.make_ApplicationExpressionc                 C   s   t t|S r   )VariableExpressionr   rd   namer!   r!   r"   r   "  s    z#LogicParser.make_VariableExpressionc                 C   s
   t ||S r   )r   rd   r   r   r!   r!   r"   r   %  s    z!LogicParser.make_LambdaExpressionc                 C   s2   | j | | j | k p0|| jko0| j | | j | kS r   )rb   rc   )rd   Z	operationr   r!   r!   r"   r   (  s    
zLogicParser.has_priorityc              
   C   s   z|   }W n6 tk
rB } zt|jd| d|W 5 d }~X Y nX t|trf||kr|t| j||n||kr|t| j||d S )NExpected token '%s'.r   )rl   r   ro   rZ   listrk   r]   )rd   expectedr   ru   r!   r!   r"   r   0  s     
zLogicParser.assertNextTokenc                 C   s<   t |tr"||kr8t| j||n||kr8t| j||d S r   )rZ   r   rk   r]   )rd   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<: >)rj   rl   	__class__r$   rd   rv   r!   r!   r"   __repr__G  s    
zLogicParser.__repr__)F)N)N)%r$   r%   r&   __doc__re   rw   rh   r{   ry   rj   rl   r   ri   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"   rO   c   sD   
%
6$
	#rO   c                 C   s   |dk	r|  |} |dkr t }g }t|  D ]r\}}| }|ds0|dkrTq0z||| W q0 tk
r } zt	d| d| |W 5 d}~X Y q0X q0|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#rx   zUnable to parse line r   )
decoderO   	enumerate
splitlinesstrip
startswithr|   rw   rm   
ValueError)sZlogic_parserencodingZ
statementsZlinenumlineru   r!   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
        %s is not a stringN)rZ   strr\   r   r   r!   r!   r"   re   o  s    zVariable.__init__c                 C   s   t |to| j|jkS r   )rZ   r   r   rd   otherr!   r!   r"   __eq__v  s    zVariable.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   __ne__y  s    zVariable.__ne__c                 C   s   t |tst| j|jk S r   )rZ   r   	TypeErrorr   r   r!   r!   r"   __lt__|  s    
zVariable.__lt__c                 C   s   | | | S r   )getrd   bindingsr!   r!   r"   substitute_bindings  s    zVariable.substitute_bindingsc                 C   s
   t | jS r   )hashr   r   r!   r!   r"   __hash__  s    zVariable.__hash__c                 C   s   | j S r   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&   re   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| t  }|dk	r||krt| t  }q^|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 constant)	is_indvarr   
is_funcvaris_eventvarr\   r   _counterr   )patternignoreprefixvr!   r!   r"   unique_variable  s    



r   c                 C   s6   t tdt  }| r2t| D ]}|t |}q |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 N%sr!   r   r!   r!   r"   r     s    zType.__repr__c                 C   s   t d|  S r   )r   r   r!   r!   r"   r     s    zType.__hash__c                 C   s   t |S r   )	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)rZ   r   r\   r   r   r   r!   r!   r"   re     s    zComplexType.__init__c                 C   s"   t |to | j|jko | j|jkS r   )rZ   r  r   r   r   r!   r!   r"   r     s
    


zComplexType.__eq__c                 C   s
   | |k S r   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 r   )rZ   r  r   matchesr   ANY_TYPEr   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 r   )r  rZ   r  r   resolver   )rd   r   fr   r!   r!   r"   r    s    

zComplexType.resolvec                 C   s*   | t krdt  S d| j d| j dS d S )Nr   r   r   r   )r  r   r   r   r!   r!   r"   r     s    zComplexType.__str__c                 C   s2   | t krt  S d| j  d| j  dS d S )Nr   z -> r   )r  r   r   r   r   r!   r!   r"   r     s    zComplexType.strN)r$   r%   r&   re   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 r   )rZ   r	  r   r!   r!   r"   r     s    zBasicType.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r     s    zBasicType.__ne__c                 C   s   |t kp| |kS r   )r  r   r!   r!   r"   r     s    zBasicType.matchesc                 C   s   |  |r| S d S d S 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 )Nru   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.strNr  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.strNr  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 r   r!   r   r!   r!   r"   re   #  s    zAnyType.__init__c                 C   s   | S r   r!   r   r!   r!   r"   r   &  s    zAnyType.firstc                 C   s   | S r   r!   r   r!   r!   r"   r   *  s    zAnyType.secondc                 C   s   t |tp|| S r   )rZ   r  r   r   r!   r!   r"   r   .  s    zAnyType.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r   1  s    zAnyType.__ne__c                 C   s   dS )NTr!   r   r!   r!   r"   r  6  s    zAnyType.matchesc                 C   s   |S r   r!   r   r!   r!   r"   r  9  s    zAnyType.resolvec                 C   s   dS )N?r!   r   r!   r!   r"   r   <  s    zAnyType.__str__c                 C   s   dS )NANYr!   r   r!   r!   r"   r   ?  s    zAnyType.strN)r$   r%   r&   re   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}t| D ]L\}}|dkr\|d7 }qB|dkrz|d8 }|dkstqB|dkrB|dkrB qqBtt| d| t| |d d S | d d	t krtS | d d	t krtS | d d	t	 krt	S t
d d
| d  d S )Nrf   rx   r   r   r   rP   r   r   zUnexpected character: '%s'.)rZ   r   r\   replacer   r  r   ENTITY_TYPE
TRUTH_TYPEr  rm   )Ztype_stringZparen_countr   charr!   r!   r"   r   I  s8    
  
r   c                       s   e Zd Z fddZ  ZS )TypeExceptionc                    s   t  | d S r   superre   r   r   r!   r"   re   i  s    zTypeException.__init__r$   r%   r&   re   __classcell__r!   r!   r  r"   r  h  s   r  c                       s   e Zd Zd fdd	Z  ZS )"InconsistentTypeHierarchyExceptionNc                    s*   |rd||f }nd| }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  )rd   r   r   rv   r  r!   r"   re   n  s    z+InconsistentTypeHierarchyException.__init__)Nr  r!   r!   r  r"   r  m  s   r  c                       s   e Zd Z fddZ  ZS )TypeResolutionExceptionc                    s   t  d||j|f  d S )Nz9The type of '%s', '%s', cannot be resolved with type '%s')r  re   type)rd   r   
other_typer  r!   r"   re   }  s
    
z TypeResolutionException.__init__r  r!   r!   r  r"   r  |  s   r  c                       s   e Zd Z fddZ  ZS )IllegalTypeExceptionc                    s    t  d|jj|||f  d S )Nz9Cannot set type of %s '%s' to '%s'; must match type '%s'.)r  re   r   r$   )rd   r   r!  Zallowed_typer  r!   r"   re     s
    zIllegalTypeException.__init__r  r!   r!   r  r"   r"    s   r"  c                 C   s4   | D ]}| |}q| dd D ]}| | q |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  )rp   )Zexpressionsrr   r   r!   r!   r"   rp     s
    rp   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NotImplementedErrorr   r!   r!   r"   r     s    z'SubstituteBindingsI.substitute_bindingsc                 C   s
   t  dS )zB
        :return: A list of all variables in this object.
        Nr$  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)r_   FNc                 C   s$   |r| j ||S | j||S d S r   )_type_checking_logic_parserrw   _logic_parser)r  r   r_   rr   r!   r!   r"   r    s    zExpression.fromstringc                 G   s    |  |}|D ]}||}q|S r   )applyto)rd   r   
additionalr   ar!   r!   r"   __call__  s    

zExpression.__call__c                 C   s    t |tstd| t| |S N%s is not an Expression)rZ   r'  r\   r   r   r!   r!   r"   r*    s    zExpression.applytoc                 C   s   t | S r   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 r.  )rZ   r'  r   r   r   r!   r!   r"   __and__  s    
zExpression.__and__c                 C   s    t |tstd| t| |S r.  )rZ   r'  r   r   r   r!   r!   r"   __or__  s    
zExpression.__or__c                 C   s    t |tstd| t| |S r.  )rZ   r'  r   r   r   r!   r!   r"   __gt__  s    
zExpression.__gt__c                 C   s    t |tstd| t| |S r.  )rZ   r'  r   r   r   r!   r!   r"   r     s    
zExpression.__lt__c                 C   s   t S r   )NotImplementedr   r!   r!   r"   r     s    zExpression.__eq__c                 C   s
   | |k S r   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``
        r/  Nr   )Prover9)rZ   r'  r\   Znltk.inferencer6  r   simplifyZprove)rd   r   Zproverr6  Zbicondr!   r!   r"   equiv  s    	zExpression.equivc                 C   s   t t| S r   )r   reprr   r!   r!   r"   r     s    zExpression.__hash__c                 C   sn   | }|  D ]X}||kr|| }t|tr6| |}nt|tsNtd|f ||}|||}q| S )Nz@Can not substitute a non-expression value into an expression: %r)	r&  rZ   r   r   r'  r   r   r  r7  )rd   r   exprvarvalr!   r!   r"   r      s    


zExpression.substitute_bindingsc                    sr   t t |rT|D ]B}|| }tt|}t|tr:||_n
t||_ | | q| j	 d  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
        )rr   c                    s   i | ]}| | d  j qS )r   )r   )r   keysigr!   r"   
<dictcomp>'  s      z(Expression.typecheck.<locals>.<dictcomp>)
r   r   r   r   rZ   r   r   r   r|   	_set_type)rd   rr   r=  r<  varExr!   r>  r"   rp     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
        Nr$  rd   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))
        Nr$  rd   r!  rr   r!   r!   r"   rA  2  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?
        %s is not a Variabler/  c                    s   |   S r   )r  ru   alpha_convertr   replace_boundr   r!   r"   <lambda>J      z$Expression.replace.<locals>.<lambda>)rZ   r   r\   r'  visit_structuredr   rd   r   r   rJ  rI  r!   rH  r"   r  <  s    zExpression.replacec                    s    fdd | }t t | dd dD ]`\}}t|trR|td|d  }n&t|trt|td|d  }n|}||j|d	}q(|S )
z&Rename auto-generated unique variablesc                    s4   t | tr| hS t | tr t S |  dd S d S )Nc                 S   s   t tj| t S r   r   operatoror_setpartsr!   r!   r"   rK  X  rL  z>Expression.normalize.<locals>.get_indiv_vars.<locals>.<lambda>)rZ   IndividualVariableExpressionAbstractVariableExpressionrR  visitrG  get_indiv_varsr!   r"   rY  Q  s    

 z,Expression.normalize.<locals>.get_indiv_varsc                 S   s   | j S r   r   rG  r!   r!   r"   rK  \  rL  z&Expression.normalize.<locals>.<lambda>)r=  ze0%srP   zz%sT)	r   sortedrZ   EventVariableExpressionr   r   rU  r  r   )rd   Znewvarsrt   r   ru   ZnewVarr!   rX  r"   	normalizeN  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``
        Nr$  rd   r   
combinatorr!   r!   r"   rW  f  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 r   r!   rS  r_  r!   r"   rK    rL  z-Expression.visit_structured.<locals>.<lambda>rW  r^  r!   r`  r"   rM  w  s    zExpression.visit_structuredc                 C   s   d| j j d|  dS )Nr   rf   r   )r   r$   r   r!   r!   r"   r     s    zExpression.__repr__c                 C   s   |   S r   )r   r   r!   r!   r"   r     s    zExpression.__str__c                 C   s"   |   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   h | ]}t d |jr|qS )z^[?@])r   r   r   )r   pr!   r!   r"   	<setcomp>  s     z'Expression.variables.<locals>.<setcomp>)free
predicates	constantsr   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 r   )rd  rG  r!   r!   r"   rK    rL  z!Expression.free.<locals>.<lambda>c                 S   s   t tj| t S r   rO  rS  r!   r!   r"   rK    rL  ra  r   r!   r!   r"   rd    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 r   )rf  rG  r!   r!   r"   rK    rL  z&Expression.constants.<locals>.<lambda>c                 S   s   t tj| t S r   rO  rS  r!   r!   r"   rK    rL  ra  r   r!   r!   r"   rf    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 r   )re  rG  r!   r!   r"   rK    rL  z'Expression.predicates.<locals>.<lambda>c                 S   s   t tj| t S r   rO  rS  r!   r!   r"   rK    rL  ra  r   r!   r!   r"   re    s     zExpression.predicatesc                 C   s   |  dd | jS )zD
        :return: beta-converted version of this expression
        c                 S   s   |   S r   )r7  rG  r!   r!   r"   rK    rL  z%Expression.simplify.<locals>.<lambda>)rM  r   r   r!   r!   r"   r7    s    zExpression.simplifyc                 C   s   t |S r   )r   rC  r!   r!   r"   r     s    z"Expression.make_VariableExpression)FN)N)N)FT)N)&r$   r%   r&   r   rO   r)  r(  r  r  r-  r*  r0  r1  r2  r3  r4  r   r   r   r8  r   r   rp   rD  r  rA  r  r]  rW  rM  r   r   r&  rd  rf  re  r7  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
        r/  N)rZ   r'  r\   r   r   r   r!   r!   r"   re     s    zApplicationExpression.__init__c                 C   sB   | j  }| j }t|tr2|j|j| S | ||S d S r   )	r   r7  r   rZ   r   r   r  r   r   r   r!   r!   r"   r7    s
    


zApplicationExpression.simplifyc                 C   s    t | jjtr| jjjS tS d S r   )rZ   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| z| jt	| jj
|| W nL tk
r } z.td| j| jj
| j| jj
| jj
jf |W 5 d}~X Y nX dS ):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'.)rZ   r   r\   r   r   r   rA  r  r   r  r   r  r  r   )rd   r!  rr   ru   r!   r!   r"   rA    s,     
zApplicationExpression._set_typec                    s   t  tstd  |  r,|  \}}n| j}| jg} fdd|g| D }g }|D ]4}|tkrZ|r|D ]}||rn qqnqZ|	| qZt
|dkrt|d S tS dS ):see Expression.findtype()rF  c                    s   g | ]}|  qS r!   )rD  r   argrZ  r!   r"   r#     s     z2ApplicationExpression.findtype.<locals>.<listcomp>rP   r   N)rZ   r   r\   is_atomuncurryr   r   r  r  r|   rz   r   )rd   r   r   argsfounduniquer  ur!   rZ  r"   rD    s"    
zApplicationExpression.findtypec                 C   s,   t | jtrt }n
| j }|| j B S z:see: Expression.constants())rZ   r   rV  rR  rf  r   )rd   Zfunction_constantsr!   r!   r"   rf  '  s    
zApplicationExpression.constantsc                 C   s0   t | jtr| jjh}n
| j }|| j B S z:see: Expression.predicates())rZ   r   r   r   re  r   )rd   Zfunction_predsr!   r!   r"   re  /  s    
z ApplicationExpression.predicatesc                 C   s   ||| j || jgS z:see: Expression.visit())r   r   r^  r!   r!   r"   rW  7  s    zApplicationExpression.visitc                 C   s"   t |to | j|jko | j|jkS r   )rZ   r   r   r   r   r!   r!   r"   r   ;  s
    


zApplicationExpression.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r   B  s    zApplicationExpression.__ne__c                 C   s   |   r*|  \}}ddd |D }n| j}d| j }d| }d}t|trt|jtrpt|jjt	sd}qt|jt
sd}nt|trd}|rtj| tj }|tj | tj S )Nr   c                 s   s   | ]}d | V  qdS r   Nr!   ri  r!   r!   r"   	<genexpr>K  s     z0ApplicationExpression.__str__.<locals>.<genexpr>r   FT)rk  rl  joinr   r   rZ   r   r   r   rV  BooleanExpressionr   r0   r1   )rd   r   rm  Zarg_strZfunction_strZparenthesize_functionr!   r!   r"   r   G  s$    


zApplicationExpression.__str__c                 C   s6   | j }| jg}t|tr.|d|j |j }q||fS )zh
        Uncurry this application expression

        return: A tuple (base-function, arg-list)
        r   )r   r   rZ   r   insert)rd   r   rm  r!   r!   r"   rl  a  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   rl  r   r!   r!   r"   predo  s    zApplicationExpression.predc                 C   s   |   d S )z+
        Return uncurried arg-list
        rP   ry  r   r!   r!   r"   rm  x  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)?
        )rZ   rz  rV  r   r!   r!   r"   rk    s    zApplicationExpression.is_atom)r$   r%   r&   r   re   r7  r  r   r  rA  rD  rf  re  rW  r   r   r'  r   r   rl  rz  rm  rk  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 )rV  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
        rF  N)rZ   r   r\   r   rC  r!   r!   r"   re     s    z#AbstractVariableExpression.__init__c                 C   s   | S r   r!   r   r!   r!   r"   r7    s    z#AbstractVariableExpression.simplifyFTc                 C   sB   t |tstd| t |ts,td| | j|kr:|S | S dS ):see: Expression.replace()z%s is not an Variabler/  N)rZ   r   r\   r'  r   rN  r!   r!   r"   r    s    
z"AbstractVariableExpression.replaceNc                 C   sz   t |tst|dkrtt}|}|| jj D ]}|j|}|s.t	| q.|| jj 
|  || jj D ]
}||_qjdS rg  N)rZ   r   r\   r   r   r   r   r   r  r  r|   rd   r!  rr   
resolutionrB  r!   r!   r"   rA    s    
z$AbstractVariableExpression._set_typec                 C   s.   t |tstd| | j|kr&| jS tS dS rh  rF  N)rZ   r   r\   r   r   r  rC  r!   r!   r"   rD    s    
z#AbstractVariableExpression.findtypec                 C   s   t  S rr  rR  r   r!   r!   r"   re    s    z%AbstractVariableExpression.predicatesc                 C   s   t |to| j|jkS )zTAllow equality between instances of ``AbstractVariableExpression``
        subtypes.)rZ   rV  r   r   r!   r!   r"   r     s    

z!AbstractVariableExpression.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r     s    z!AbstractVariableExpression.__ne__c                 C   s   t |tst| j|jk S r   )rZ   rV  r   r   r   r!   r!   r"   r     s    
z!AbstractVariableExpression.__lt__c                 C   s
   d| j  S r   rZ  r   r!   r!   r"   r     s    z"AbstractVariableExpression.__str__)FT)r$   r%   r&   r   re   r7  r  r  rA  rD  re  r   r   r   r'  r   r   r!   r!   r!   r"   rV    s   
rV  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 )rU  zThis 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 r|  )rZ   r   r\   r   r   r  r  r"  r   r   r|   rE  r!   r!   r"   rA    s    
z&IndividualVariableExpression._set_typec                 C   s   t S r   )r  r   r!   r!   r"   	_get_type  s    z&IndividualVariableExpression._get_typec                 C   s   | j hS z:see: Expression.free()rZ  r   r!   r!   r"   rd    s    z!IndividualVariableExpression.freec                 C   s   t  S rq  r  r   r!   r!   r"   rf    s    z&IndividualVariableExpression.constants)r$   r%   r&   r   r  rA  r  r  r   rd  rf  r!   r!   r!   r"   rU    s   
rU  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   | j hS r  rZ  r   r!   r!   r"   rd    s    zFunctionVariableExpression.freec                 C   s   t  S rq  r  r   r!   r!   r"   rf    s    z$FunctionVariableExpression.constantsN)r$   r%   r&   r   r  r   rd  rf  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}|| j	j
 D ]}|j|}|sRt| qR|| j	j
 |  || j	j
 D ]
}||_qdS r|  )rZ   r   r\   r   r   r  r  r   r  r   r   r  r|   r}  r!   r!   r"   rA    s    

zConstantExpression._set_typec                 C   s   t  S r  r  r   r!   r!   r"   rd  %  s    zConstantExpression.freec                 C   s   | j hS rq  rZ  r   r!   r!   r"   rf  )  s    zConstantExpression.constants)
r$   r%   r&   r   r  r   r  rA  rd  rf  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.
    rF  N)rZ   r   r\   r   r   rU  r   r   r   r\  r   rZ  r!   r!   r"   r   .  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 )VariableBinderExpressionzThis 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
        rF  r/  N)rZ   r   r\   r'  r   r   r   r!   r!   r"   re   B  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 )r{  rF  r/  z&%s is not a AbstractVariableExpressionT)r   N)rZ   r   r\   r'  r   rV  r   r   r  rd  rI  r   rN  r!   r!   r"   r  L  s(    
z VariableBinderExpression.replacec                 C   s4   t |tstd| | || j| jt|dS )zRename all occurrences of the variable introduced by this variable
        binder in the expression to ``newvar``.
        :param newvar: ``Variable``, for the new variable
        rF  T)rZ   r   r\   r   r   r  r   r   )rd   Znewvarr!   r!   r"   rI  j  s
     z&VariableBinderExpression.alpha_convertc                 C   s   | j  | jh S r  )r   rd  r   r   r!   r!   r"   rd  t  s    zVariableBinderExpression.freec                 C   s4   t |tstd| || jkr$tS | j|S dS r  )rZ   r   r\   r   r  r   rD  rC  r!   r!   r"   rD  x  s    
z!VariableBinderExpression.findtypec                 C   s   ||| j gS rs  r   r^  r!   r!   r"   rW    s    zVariableBinderExpression.visitc                 C   s   || j || jS )z#:see: Expression.visit_structured())r   r   r^  r!   r!   r"   rM    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)rZ   r   r   r   r   r  )rd   r   Zvarexr!   r!   r"   r     s    
zVariableBinderExpression.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r     s    zVariableBinderExpression.__ne__N)FT)r$   r%   r&   r   re   r  rI  rd  rD  rW  rM  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 )r   c                 C   s   t | j| j| jjS r   )r  r   rD  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 r|  )rZ   r   r\   r   r   r   rA  r   r   r  r  rE  r!   r!   r"   rA    s    zLambdaExpression._set_typec                 C   sV   | j g}| j}|j| jkr.||j  |j}qtjddd |D  tj d|  S )Nrf   c                 s   s   | ]}d | V  qdS rt  r!   r   r   r!   r!   r"   ru    s     z+LambdaExpression.__str__.<locals>.<genexpr>r   )r   r   r   r|   r   r'   rv  r/   rd   r&  r   r!   r!   r"   r     s    zLambdaExpression.__str__r$   r%   r&   r  r   r  rA  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 r   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 r|  
rZ   r   r\   r   r   r  r  r"  r   rA  rE  r!   r!   r"   rA    s    
zQuantifiedExpression._set_typec                 C   s\   | j g}| j}|j| jkr.||j  |j}q|  d ddd |D  tj d|  S )Nrf   c                 s   s   | ]}d | V  qdS rt  r!   r  r!   r!   r"   ru    s     z/QuantifiedExpression.__str__.<locals>.<genexpr>r   )r   r   r   r|   getQuantifierrv  r   r/   r  r!   r!   r"   r     s    zQuantifiedExpression.__str__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 r   )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 r   )r   r+   r   r!   r!   r"   r    s    zAllExpression.getQuantifierNr  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 r   )r   r-   r   r!   r!   r"   r    s    zIotaExpression.getQuantifierNr  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 r.  )rZ   r'  r\   r   )rd   r   r!   r!   r"   re     s    zNegatedExpression.__init__c                 C   s   t S r   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 r|  r  rE  r!   r!   r"   rA    s    
zNegatedExpression._set_typec                 C   s"   t |tstd| | j|S )NrF  )rZ   r   r\   r   rD  rC  r!   r!   r"   rD    s    zNegatedExpression.findtypec                 C   s   ||| j gS rs  r  r^  r!   r!   r"   rW    s    zNegatedExpression.visitc                 C   s   | j S )z:see: Expression.negate()r  r   r!   r!   r"   r1    s    zNegatedExpression.negatec                 C   s   t |to| j|jkS r   )rZ   r   r   r   r!   r!   r"   r     s    zNegatedExpression.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r   
  s    zNegatedExpression.__ne__c                 C   s   t jd| j  S r   )r   r3   r   r   r!   r!   r"   r     s    zNegatedExpression.__str__)r$   r%   r&   re   r  r   r  rA  rD  rW  r1  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 r.  )rZ   r'  r\   r   r   r   r!   r!   r"   re     s    zBinaryExpression.__init__c                 C   s   t S r   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 r  )rZ   r   r\   r   rD  r   r  )rd   r   r  r   r!   r!   r"   rD    s    zBinaryExpression.findtypec                 C   s   ||| j || jgS rs  )r   r   r^  r!   r!   r"   rW  *  s    zBinaryExpression.visitc                 C   s0   t | |jst || jo.| j|jko.| j|jkS r   )rZ   r   r   r   r   r!   r!   r"   r   .  s
    

zBinaryExpression.__eq__c                 C   s
   | |k S r   r!   r   r!   r!   r"   r   5  s    zBinaryExpression.__ne__c                 C   s<   |  | j}|  | j}tj| d |   d | tj S )Nrf   )
_str_subexr   r   r   r0   getOpr1   r   r!   r!   r"   r   :  s    zBinaryExpression.__str__c                 C   s   d| S r   r!   )rd   subexr!   r!   r"   r  ?  s    zBinaryExpression._str_subexN)r$   r%   r&   re   r  r   rD  rW  r   r   r'  r   r   r  r!   r!   r!   r"   r    s   
r  c                   @   s   e Zd ZedfddZdS )rw  Nc                 C   sT   t |tst|dkrtt}|ts4t| |t| j	t| | j
	t| dS r|  )rZ   r   r\   r   r   r  r  r"  r   rA  r   rE  r!   r!   r"   rA  D  s    
zBooleanExpression._set_type)r$   r%   r&   r  rA  r!   r!   r!   r"   rw  C  s   rw  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 r   )r   r5   r   r!   r!   r"   r  T  s    zAndExpression.getOpc                 C   s"   d| }t |tr|dd S |S Nr   rP   r  )rZ   r   rd   r  r   r!   r!   r"   r  W  s    
zAndExpression._str_subexNr$   r%   r&   r   r  r  r!   r!   r!   r"   r   Q  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 r   )r   r7   r   r!   r!   r"   r  a  s    zOrExpression.getOpc                 C   s"   d| }t |tr|dd S |S r  )rZ   r   r  r!   r!   r"   r  d  s    
zOrExpression._str_subexNr  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 implicationsc                 C   s   t jS r   )r   r9   r   r!   r!   r"   r  n  s    zImpExpression.getOpNr$   r%   r&   r   r  r!   r!   r!   r"   r   k  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 r   )r   r;   r   r!   r!   r"   r  u  s    zIffExpression.getOpNr  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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 r|  )rZ   r   r\   r   r   r  r  r"  r   rA  r  r   rE  r!   r!   r"   rA  |  s    
zEqualityExpression._set_typec                 C   s   t jS r   )r   r=   r   r!   r!   r"   r    s    zEqualityExpression.getOp)r$   r%   r&   r   r  rA  r  r!   r!   r!   r"   r   y  s   r   c                   @   s   e Zd Zdd ZdS )rm   c                 C   s   || _ t| | d S r   )ro   	Exceptionre   rd   ro   r   r!   r!   r"   re     s    z#LogicalExpressionException.__init__Nr$   r%   r&   re   r!   r!   r!   r"   rm     s   rm   c                   @   s   e Zd ZdddZdS )rk   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  r   rm   re   )rd   ro   Z
unexpectedr   r   rv   r!   r!   r"   re     s    z!UnexpectedTokenException.__init__)NNNr  r!   r!   r!   r"   rk     s   rk   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.  r  r  r!   r!   r"   re     s      z$ExpectedMoreTokensException.__init__)Nr  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
    r   z^[a-df-z]\d*$NrZ   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
    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
    r   z
^[A-Z]\d*$Nr  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
    r   z^e\d*$Nr  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  rF   r7  rI  r   )Zlexpre1e2r!   r!   r"   demo  s8    r  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)rF   demoExceptionr!   r!   r!   r"   demo_errors  s    r  c              
   C   sL   zt |  W n8 tk
rF } zt|jj d|  W 5 d }~X Y nX d S )Nr   )r'  r  rm   rF   r   r$   )r   ru   r!   r!   r"   r    s    r  c                 C   s   t |   d| j  d S )Nz : )rF   r   r   )exr!   r!   r"   	printtype  s    r  __main__)NN)NN)N)N)Kr   rP  r   collectionsr   	functoolsr   r   Znltk.internalsr   Z	nltk.utilr   r   r   r   rJ   rK   rN   rO   r   r   r   r   r   r  r	  r
  r  r  r  r  r  r  r  r   r  r  r  r  r"  rp   r#  r'  r   rV  rU  r   r\  r   r   r  r   r  r   r   r   r   r  rw  r   r   r   r   r   rm   rk   r   r   r   r   r  r  r  r  r$   r!   r!   r!   r"   <module>	   s   -			   o
 

5!
   KK'],0	!
