
    /hN
                       S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  Sr\	" 5       r " S S	5      rS
 rS rS r " S S5      rSgS jr\ " S S5      5       rSgS jrShS jr " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S  S!\\5      r\" 5       r\" 5       r\" 5       r \" 5       r!S" r" " S# S$\#5      r$ " S% S&\$5      r% " S' S(\$5      r& " S) S*\$5      r'ShS+ jr( " S, S-5      r) " S. S/\)5      r* " S0 S1\*5      r+\ " S2 S3\*5      5       r, " S4 S5\,5      r- " S6 S7\,5      r. " S8 S9\-5      r/ " S: S;\,5      r0S< r1 " S= S>\*5      r2 " S? S@\25      r3 " SA SB\25      r4 " SC SD\45      r5 " SE SF\45      r6 " SG SH\45      r7 " SI SJ\*5      r8 " SK SL\*5      r9 " SM SN\95      r: " SO SP\:5      r; " SQ SR\:5      r< " SS ST\:5      r= " SU SV\:5      r> " SW SX\95      r? " SY SZ\#5      r@ " S[ S\\@5      rA " S] S^\@5      rBS_ rCS` rDSa rESb rFSc rGSd rHSe rI\JSf:X  a  \F" 5         gg)izV
A version of first order predicate logic, built on
top of the typed lambda calculus.
    N)defaultdict)reducetotal_ordering)Counter)TrieAPPc            	       t   \ rS rSrSrS/rSr/ SQrSrSS/r	Sr
S/rSrS	rS
rSrSr/ SQrSr/ SQrSrSS/rSr/ SQrSr/ SQrSrSS/rSrS/r\\-   \-   \-   r\\	-   \-   r\\\\/r \\-   \-   \-   \-   \ -   \-   r!\! V VVs/ s H#  n[D        RF                  " SU5      (       d  M!  UPM%     snnn r$Sr%gs  snnn f )Tokens   \exists)somer   existallforalliota.(),-)notr   !&)andr   ^|or->)impliesr   z=><->)iffr!   z<=>=z==z!=z^[-\\.(),!&^|>=<]*$ N)&__name__
__module____qualname____firstlineno__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_LISTBINOPSQUANTSPUNCTTOKENSrematchSYMBOLS__static_attributes__).0xrG   s   000F/var/www/auris/envauris/lib/python3.13/site-packages/nltk/sem/logic.pyr
   r
      s   F&K F-K
Cx HDI CDEE C H
C H	BSkG
C&H
C$H	BDkG
CvH (*X5F8#i/F$u%Eg(61K?%G(RF !H&QBHH-CQ$Gq&HGHs    B3%B3r
   c                      / SQn [        U [        R                  [        R                  [        R                  [        R
                  [        R                  /5       H  n[        SU-  5        M     g)z
Boolean operators
)negationconjunctiondisjunctionimplicationequivalence%-15s	%sN)zipr
   r5   r7   r9   r;   r=   printnamespairs     rM   boolean_opsrZ   H   sH     UEEFJJ

FIIvzz6::VWkD ! X    c                      SS/n [        U [        R                  [        R                  /5       H  n[	        SU-  5        M     g)z
Equality predicates
equality
inequalityrT   N)rU   r
   r?   rA   rV   rW   s     rM   equality_predsr_   Q   s9     &EEFIIvzz23kD ! 4r[   c                      / SQn [        U [        R                  [        R                  [        R                  [        R
                  /5       H  n[        SU-  5        M     g)z
Binding operators
)existential	universallambdarT   N)rU   r
   r+   r-   r)   r/   rV   rW   s     rM   binding_opsrd   Z   sA     3EEFMM6::v}}fkkRSkD ! Tr[   c                       \ rS rSrSrS&S jrS'S jrS rS rS r	S	 r
S'S
 jrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S  r!S! r"S" r#S# r$S$ r%S%r&g)(LogicParserc   z$A lambda calculus expression parser.c                 Z   [        U[        5      (       d   eSU l        / U l        Xl         / U l        [        [        R                   Vs/ s H  o"S4PM     sn[        R                   Vs/ s H  o"S4PM     sn-   [        S4/-   [        R                  [        R                  -    Vs/ s H  o"S4PM     sn-   [        R                   Vs/ s H  o"S4PM     sn-   [        R                   Vs/ s H  o"S4PM     sn-   [        R                   Vs/ s H  o"S4PM     sn-   [        R                    Vs/ s H  o"S	4PM     sn-   [        R"                   Vs/ s H  o"S
4PM     sn-   S/-   5      U l        [        /U l        gs  snf s  snf s  snf s  snf s  snf s  snf s  snf s  snf )za
:param type_check: should type checking be performed
    to their types?
:type type_check: bool
r                           	   )N
   N)
isinstancebool_currentIndex_buffer
type_checkquote_charsdictr
   r*   r6   r   r@   rB   rD   r8   r:   r<   r>   operator_precedenceright_associated_operations)selfrw   rL   s      rM   __init__LogicParser.__init__f   s    *d++++$	/ #'#//0/V/0%/!1v/0Qxj  &~~?@?!1v?@A  &}}-}!1v}-	.
  &/!1v/0  &~~.~!1v~./  &/!1v/0  &/!1v/0 l	$
  -05( 1/@-/.//s0   F,F

*F
F
0F
F
6F#
F(
Nc           	         UR                  5       nSU l        U R                  U5      u  U l        n U R	                  S5      nU R                  S5      (       a(  [        U R                  S-   U R                  S5      5      e U R                  (       a  UR                  U5        U$ ! [         a6  nSR                  XQSX5R                  S-
     -  5      n[        SU5      UeSnAff = f)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   Nri   z	{}
{}
{}^ )rstripru   processrv   process_next_expressioninRangeUnexpectedTokenExceptiontokenLogicalExpressionExceptionformatindexrw   	typecheck)r|   data	signaturemappingresultemsgs          rM   parseLogicParser.parse   s     {{} $T 2g	?11$7F||A.t/A/AA/EtzzRS}UU  ??Y' * 	?&&qgggk6J0JKC,T37Q>	?s   AB& &
C&01C!!C&c                    / n0 n[        U R                  5       5      nSnSnUnU[        U5      :  Ga3  UnU R                  Xa5      u  pU	(       a  U(       d  UnXY-  nM;  Un
X   nSnX;   a<  X-  nX   n
[        U5      U-
  [        U5      :  a  X[        U5      -      nOOX;   a  M<  [         R                  U
;   aT  U(       a   Xs[        U5      '   UR                  U5        SnXc[        U5      '   UR                  U5        U[        U5      -  nOEX   S;   a(  U(       a   Xs[        U5      '   UR                  U5        SnOU(       d  UnXQU   -  nUS-  nU[        U5      :  a  GM3  U(       a  Xs[        U5      '   UR                  U5        [        U5      U[        U5      '   [        U5      S-   U[        U5      S-   '   X#4$ )zSplit the data into tokens r   z 	
ri   )r   get_all_symbolslenprocess_quoted_tokenLEAFappend)r|   r   outr   	tokenTrier   data_idxtoken_start_idxcur_data_idxquoted_tokenstcsymbols                rM   r   LogicParser.process   s   --/0	"T"#L%)%>%>x%N"L&2O%BAF'Ut9x'#f+5F34A ' yyB(7CH%JJu%E$,C!

6"CK'>W,,;C)

5) " *2(^+EAM T"N  /CHJJuIC #D	AC1|r[   c                    SnX!   nUnU R                    H  u  pgpXF:X  d  M  U	(       a  X4-  nUS-  nX%   U:w  au  X%   U:X  a9  U	(       a  X2U   -  nUS-  n[        U5      U:X  a  [        S SU-  5      eX2U   -  nOX2U   -  nUS-  n[        U5      U:X  a  [        S SU-  5      eX%   U:w  a  Mu  U	(       a  X2U   -  nUS-  nU(       d  [        S S5      e  X54$    X54$ )Nr   ri   z:End of input reached.  Escape character [%s] found at end.z%End of input reached.  Expected: [%s]zEmpty quoted token found)rx   r   r   )
r|   r   r   r   r   istartendescapeincl_quotess
             rM   r    LogicParser.process_quoted_token   s%   N/3/?/?+EzJEQgnw&(&!!W,EQt9>"< $!FHN!O# 
 a(a(FA4yA~8 "Ls"R ! gn& !W$EQ4T;UVVx= 0@< xr[   c                 "    [         R                  $ )z#This method exists to be overridden)r
   rI   r|   s    rM   r   LogicParser.get_all_symbols   s    ~~r[   c                 L    U R                   U-   [        U R                  5      :  $ )z6Return TRUE if the given location is within the buffer)ru   r   rv   )r|   locations     rM   r   LogicParser.inRange  s!    !!H,s4<</@@@r[   c                      Uc0  U R                   U R                     nU =R                  S-  sl        U$ U R                   U R                  U-      nU$ ! [         a  n[        U R                  S-   5      UeSnAff = f)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.Nri   )rv   ru   
IndexErrorExpectedMoreTokensException)r|   r   tokr   s       rM   r   LogicParser.token  s    	Mll4#5#56""a'" J ll4#5#5#@AJ 	M-d.@.@1.DE1L	Ms   2A A 
A;A66A;c                 (    U[         R                  ;  $ N)r
   rF   r|   r   s     rM   
isvariableLogicParser.isvariable  s    &--''r[   c                      U R                  5       nU R                  X!5      nU(       d  [	        U R                  USS9eU R                  XA5      $ ! [         a  n[        U R                  S-   SS9UeSnAff = f)zAParse the next complete expression from the stream and return it.ri   Expression expected.messageN)r   r   ru   handler   attempt_adjuncts)r|   contextr   r   accums        rM   r   #LogicParser.process_next_expression  s    	**,C C)*""C1G  $$U44 + 	-""Q&0F	s   A 
A7A22A7c                 z   U R                  U5      (       a  U R                  X5      $ U[        R                  ;   a  U R	                  X5      $ U[        R
                  ;   a  U R                  X5      $ U[        R                  ;   a  U R                  X5      $ U[        R                  :X  a  U R                  X5      $ g)z_This method is intended to be overridden for logics that
use different operators or expressionsN)r   handle_variabler
   r6   handle_negationr*   handle_lambdarD   handle_quantr2   handle_openr|   r   r   s      rM   r   LogicParser.handle+  s     ??3''55FOO#''55F&&&%%c33FMM!$$S22FKK##C11  r[   c                     S nX0R                   :w  aP  U R                   nU R                  X5      nU R                  X5      nU R                  X5      nX0R                   :w  a  MP  U$ r   )ru   attempt_EqualityExpressionattempt_ApplicationExpressionattempt_BooleanExpression)r|   
expressionr   cur_idxs       rM   r   LogicParser.attempt_adjuncts=  sd    +++((G88MJ;;JPJ77
LJ	 +++
 r[   c                 ^    U R                  U R                  [        R                  5      5      $ r   )make_NegatedExpressionr   r
   r5   r   s      rM   r   LogicParser.handle_negationF  s"    **4+G+G

+STTr[   c                     [        U5      $ r   NegatedExpression)r|   r   s     rM   r   "LogicParser.make_NegatedExpressionI  s     ,,r[   c                    U R                  U5      nU R                  S5      (       Gab  U R                  S5      [        R                  :X  Ga>  [        U[        5      (       d.  [        U[        5      (       d  [        U R                  SU-  5      eU R                  5         U R                  X0R                  [        5      5      nU R                  S5      (       a  U R                  S5      [        R                  :X  ao  U R                  5         U R                  X0R                  [        5      5      nU R                  S5      (       a%  U R                  S5      [        R                  :X  a  Mo  U R                  [        R                  5        U$ )Nr   zW'%s' is an illegal predicate name.  Individual variables may not be used as predicates.)make_VariableExpressionr   r   r
   r2   rs   FunctionVariableExpressionConstantExpressionr   ru   make_ApplicationExpressionr   r   r4   assertNextTokenr3   r|   r   r   r   s       rM   r   LogicParser.handle_variableL  s*    ,,S1<<??tzz!};e%?@@)J J 1&&"$'(  JJL 3333C8E ,,q//djjmv||&C

7777< ,,q//djjmv||&C
   .r[   c                     U R                  5       n[        U R	                  U5      [
        5      (       a  [        U R                  SU< SU< S35      e[        U5      $ ! [         a  n[        UR                  S5      UeS nAff = f)NzVariable expected.'z5' is an illegal variable name.  Constants may not be r   )	r   r   r   rs   r   r   r   ru   Variable)r|   descriptionr   r   s       rM   get_next_token_variable#LogicParser.get_next_token_variablej  s    	T**,C d22379KLL,"".1;@ 
 } + 	T-agg7KLRSS	Ts   A   
B*BBc                    U R                  S5      (       d  [        U R                  S-   SS9eU R                  S5      /n U R                  S5      (       a9  U R	                  S5      [
        R                  :X  a-  U R                  S5      (       d  [        U R                  S-   SS9eU R                  U R	                  S5      5      (       d  O"UR                  U R                  S5      5        M  U R                  S5      (       a3  U R	                  S5      [
        R                  :X  a  U R	                  5         U R                  U5      nU(       a)  U R                  UR                  5       U5      nU(       a  M)  U$ )Nr   rj   z;Variable and Expression expected following lambda operator.r   
abstractedri   r   )r   r   ru   r   r   r
   r1   r   r   r   make_LambdaExpressionpop)r|   r   r   varsr   s        rM   r   LogicParser.handle_lambdaw  s*   ||A-""Q&U  ,,\:;<<??

1+DLLOO1&&*4J  ??4::a=11KK44\BC  <<??tzz!}

:JJL,,S1..txxz5AE dr[   c                    U R                  U5      nU R                  S5      (       d  [        U R                  S-   SU-  S9eU R	                  S5      /n U R                  S5      (       a9  U R                  S5      [        R                  :X  a-  U R                  S5      (       d  [        U R                  S-   SS9eU R                  U R                  S5      5      (       d  O"UR                  U R	                  S5      5        M  U R                  S5      (       a3  U R                  S5      [        R                  :X  a  U R                  5         U R                  U5      nU(       a)  U R                  X4R                  5       U5      nU(       a  M)  U$ )Nr   rj   z;Variable and Expression expected following quantifier '%s'.r   
quantifiedri   r   ) get_QuantifiedExpression_factoryr   r   ru   r   r   r
   r1   r   r   r   make_QuanifiedExpressionr   )r|   r   r   factoryr   r   s         rM   r   LogicParser.handle_quant  sB   77<||A-""Q&U 
 ,,\:;<<??

1+DLLOO1&&*4J  ??4::a=11KK44\BC  <<??tzz!}

:JJL,,S111'88:uME dr[   c                     U[         R                  ;   a  [        $ U[         R                  ;   a  [        $ U[         R
                  ;   a  [        $ U R                  U[         R                  5        g)zTThis method serves as a hook for other logic parsers that
have different quantifiersN)	r
   r,   ExistsExpressionr.   AllExpressionr0   IotaExpressionassertTokenrD   r   s     rM   r   ,LogicParser.get_QuantifiedExpression_factory  sR     &$$$##FOO#  F$$$!!S&--0r[   c                     U" X#5      $ r   r$   )r|   r   variableterms       rM   r   $LogicParser.make_QuanifiedExpression  s    x&&r[   c                 f    U R                  S 5      nU R                  [        R                  5        U$ r   )r   r   r
   r3   r   s       rM   r   LogicParser.handle_open  s)    ,,T2V\\*r[   c                 t   U R                  S5      (       a  U R                  S5      nU[        R                  [        R                  -   ;   ak  U R                  X25      (       aU  U R                  5         U R                  XR                  U5      5      nU[        R                  ;   a  U R                  U5      nU$ )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   )	r   r   r
   r@   rB   has_prioritymake_EqualityExpressionr   r   )r|   r   r   r   s       rM   r   &LogicParser.attempt_EqualityExpression  s     <<??**Q-Cfnnv664;L;L< < 

!99 < <S A
 &//)!%!<!<Z!HJr[   c                     [        X5      $ )zdThis method serves as a hook for other logic parsers that
have different equality expression classes)EqualityExpressionr|   firstseconds      rM   r  #LogicParser.make_EqualityExpression  s     "%00r[   c                 J   U R                  S5      (       a  U R                  S5      nU R                  U5      nU(       aH  U R                  X25      (       a2  U R                  5         U R	                  XAU R                  U5      5      nO U$ U R                  S5      (       a  M  U$ )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   )r   r   get_BooleanExpression_factoryr  make_BooleanExpressionr   )r|   r   r   r   r   s        rM   r   %LogicParser.attempt_BooleanExpression  s     ll1oo**Q-C88=G4,,S::

!88)E)Ec)J
  ll1oo r[   c                     U[         R                  ;   a  [        $ U[         R                  ;   a  [        $ U[         R
                  ;   a  [        $ U[         R                  ;   a  [        $ g)zZThis method serves as a hook for other logic parsers that
have different boolean operatorsN)	r
   r8   AndExpressionr:   OrExpressionr<   ImpExpressionr>   IffExpressionr   s     rM   r  )LogicParser.get_BooleanExpression_factory  sO     &//!  FNN"FOO#  FOO#  r[   c                     U" X#5      $ r   r$   )r|   r   r  r  s       rM   r  "LogicParser.make_BooleanExpression  s    u%%r[   c                    U R                  [        U5      (       Ga  U R                  S5      (       Ga  U R                  S5      [        R
                  :X  Gam  [        U[        5      (       d[  [        U[        5      (       dF  [        U[        5      (       d1  [        U[        5      (       d  [        U R                  SU-  S-   5      eU R                  5         U R                  XR                  [        5      5      nU R                  S5      (       a  U R                  S5      [        R                  :X  ao  U R                  5         U R                  X0R                  [        5      5      nU R                  S5      (       a%  U R                  S5      [        R                  :X  a  Mo  U R!                  [        R"                  5        U$ U$ )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   r   r   r
   r2   rs   LambdaExpressionApplicationExpressionr   r   r   ru   r   r   r4   r   r3   )r|   r   r   r   s       rM   r   )LogicParser.attempt_ApplicationExpression  sJ   
 S'**||A4::a=FKK#?":/?@@&z3HII&z3MNN&z3EFF4**+j8..  

77 < <S A ll1oo$**Q-6<<*GJJL ;;;;C@E ll1oo$**Q-6<<*G
 $$V\\2r[   c                     [        X5      $ r   )r  r|   functionarguments      rM   r   &LogicParser.make_ApplicationExpression  s    $X88r[   c                 *    [        [        U5      5      $ r   )VariableExpressionr   r|   names     rM   r   #LogicParser.make_VariableExpression"  s    !(4.11r[   c                     [        X5      $ r   )r  r|   r   r   s      rM   r   !LogicParser.make_LambdaExpression%  s    //r[   c                     U R                   U   U R                   U   :  =(       d4    XR                  ;   =(       a    U R                   U   U R                   U   :H  $ r   )rz   r{   )r|   	operationr   s      rM   r  LogicParser.has_priority(  sb    ''	2T5M5M6
 
 
 999 Y((3t7O7OPW7XX		
r[   c                     U R                  5       n[        U[        5      (       a  X!;  a  [        U R                  X!5      eg X!:w  a  [        U R                  X!5      eg ! [         a  n[        UR                  SU-  S9UeS nAff = f)NExpected token '%s'.r   )r   r   r   rs   listr   ru   )r|   expectedr   r   s       rM   r   LogicParser.assertNextToken0  s    	**,C h%%".t/A/A3QQ # .t/A/A3QQ  + 	-!7(!B	s   A 
B)BBc                     [        U[        5      (       a  X;  a  [        U R                  X5      eg X:w  a  [        U R                  X5      eg r   )rs   r,  r   ru   )r|   r   r-  s      rM   r   LogicParser.assertToken?  sL    h%%".t/A/A3QQ # .t/A/A3QQ r[   c                     U R                  S5      (       a  SU R                  S5      -   nOSnSU R                  R                  -   S-   U-   S-   $ )Nr   zNext token: zNo more tokens<: >)r   r   	__class__r%   )r|   r   s     rM   __repr__LogicParser.__repr__G  sJ    <<?? 4::a=0C"CT^^,,,t3c9C??r[   )rv   ru   rz   rx   r{   rw   )Fr   )'r%   r&   r'   r(   __doc__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   r   r  r   r   r6  rJ   r$   r[   rM   rf   rf   c   s    .#1J64l"HAM(5$2$U-<6<
1'"1
 &!F920
RR@r[   rf   c                 v   Ub  U R                  U5      n Uc
  [        5       n/ n[        U R                  5       5       HT  u  pEUR	                  5       nUR                  S5      (       d  US:X  a  M3   UR                  UR                  U5      5        MV     U$ ! [         a  n[        SU SU 35      UeSnAff = f)av  
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#r   zUnable to parse line r3  )
decoderf   	enumerate
splitlinesstrip
startswithr   r   r   
ValueError)slogic_parserencoding
statementslinenumliner   s          rM   
read_logicrG  O  s     HHX"}J"1<<>2zz|??342:	Ol0067 3  * 	O4WIRvFGQN	Os   1 B
B8!B33B8c                   D    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
rg)r   im  c                 N    [        U[        5      (       d
   SU-  5       eXl        g)z'
:param name: the name of the variable
%s is not a stringN)rs   strr"  r!  s     rM   r}   Variable.__init__o  s'     $$$A&:T&AA$	r[   c                 b    [        U[        5      =(       a    U R                  UR                  :H  $ r   )rs   r   r"  r|   others     rM   __eq__Variable.__eq__v  s!    %*FtyyEJJ/FFr[   c                     X:X  + $ r   r$   rN  s     rM   __ne__Variable.__ne__y        r[   c                 j    [        U[        5      (       d  [        eU R                  UR                  :  $ r   )rs   r   	TypeErrorr"  rN  s     rM   __lt__Variable.__lt__|  s'    %**Oyy5::%%r[   c                 $    UR                  X 5      $ r   )getr|   bindingss     rM   substitute_bindingsVariable.substitute_bindings  s    ||D''r[   c                 ,    [        U R                  5      $ r   )hashr"  r   s    rM   __hash__Variable.__hash__  s    DIIr[   c                     U R                   $ r   r"  r   s    rM   __str__Variable.__str__  s    yyr[   c                      SU R                   -  $ )NzVariable('%s')re  r   s    rM   r6  Variable.__repr__  s    $))++r[   re  N)r%   r&   r'   r(   r}   rP  rS  rX  r^  rb  rf  r6  rJ   r$   r[   rM   r   r   m  s+    G!&
(,r[   r   c                 t   U b^  [        U R                  5      (       a  SnOC[        U R                  5      (       a  SnO&[        U R                  5      (       a  SnO	 S5       eSn[	        U [
        R                  5        35      nUb0  X1;   a+  [	        U [
        R                  5        35      nUb  X1;   a  M+  U$ )z
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
zFe0z!Cannot generate a unique constant)	is_indvarr"  
is_funcvaris_eventvarr   _counterr[  )patternignoreprefixvs       rM   unique_variablerv    s     W\\""F%%F&&F===5F8HLLN+,-A

x/01 
Hr[   c                     [        [        S[        R                  5       -  5      5      nU (       a#  [	        U 5       H  nU" [        U5      5      nM     U$ )zL
Return a skolem function over the variables in univ_scope
param univ_scope
zF%s)r   r   rq  r[  r,  )
univ_scopeskolemru  s      rM   skolem_functionrz    sE    
  )? @AFj!A.q12F "Mr[   c                   0    \ rS rSrS rS r\S 5       rSrg)Typei  c                     SU -  $ N%sr$   r   s    rM   r6  Type.__repr__  s    d{r[   c                     [        SU -  5      $ r~  )ra  r   s    rM   rb  Type.__hash__  s    D4K  r[   c                     [        U5      $ r   )	read_type)clsrA  s     rM   
fromstringType.fromstring  s    |r[   r$   N)	r%   r&   r'   r(   r6  rb  classmethodr  rJ   r$   r[   rM   r|  r|    s     !  r[   r|  c                   V    \ rS rSrS rS rS r\R                  rS r	S r
S rS rS	rg
)ComplexTypei  c                     [        U[        5      (       d
   SU-  5       e[        U[        5      (       d
   SU-  5       eXl        X l        g )Nz%s is not a Type)rs   r|  r  r  r  s      rM   r}   ComplexType.__init__  sH    %&&B(:U(BB&&$''D);f)DD'
r[   c                     [        U[        5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )rs   r  r  r  rN  s     rM   rP  ComplexType.__eq__  s;    uk* ,

ekk),u||+	
r[   c                     X:X  + $ r   r$   rN  s     rM   rS  ComplexType.__ne__  rU  r[   c                     [        U[        5      (       aQ  U R                  R                  UR                  5      =(       a%    U R                  R                  UR                  5      $ U [
        :H  $ r   )rs   r  r  matchesr  ANY_TYPErN  s     rM   r  ComplexType.matches  sK    e[))::%%ekk2Xt{{7J7J5<<7XX8##r[   c                 &   U[         :X  a  U $ [        U[        5      (       ad  U R                  R	                  UR                  5      nU R
                  R	                  UR
                  5      nU(       a  U(       a  [        X#5      $ g U [         :X  a  U$ g r   )r  rs   r  r  resolver  )r|   rO  frA  s       rM   r  ComplexType.resolve  so    HK{++

""5;;/A##ELL1AQ"1((XLr[   c                 b    U [         :X  a	  S[         -  $ SU R                   SU R                   S3$ )Nr  r2  r   r4  )r  r  r  r   s    rM   rf  ComplexType.__str__  s1    8(?"tzzl!DKK=22r[   c                     U [         :X  a  [         R                  5       $ SU R                  R                  5        SU R                  R                  5        S3$ )Nr   z -> r   )r  rK  r  r  r   s    rM   rK  ComplexType.str  sC    8<<>!tzz~~'(T[[__->,?qAAr[   r  r  N)r%   r&   r'   r(   r}   rP  rS  r|  rb  r  r  rf  rK  rJ   r$   r[   rM   r  r    s1    
! }}H$3Br[   r  c                   D    \ rS rSrS rS r\R                  rS rS r	Sr
g)	BasicTypei  c                 F    [        U[        5      =(       a    SU -  SU-  :H  $ r~  )rs   r  rN  s     rM   rP  BasicType.__eq__  s!    %+O$,0OOr[   c                     X:X  + $ r   r$   rN  s     rM   rS  BasicType.__ne__  rU  r[   c                 *    U[         :H  =(       d    X:H  $ r   )r  rN  s     rM   r  BasicType.matches   s     1DM1r[   c                 4    U R                  U5      (       a  U $ g r   )r  rN  s     rM   r  BasicType.resolve  s    <<Kr[   r$   N)r%   r&   r'   r(   rP  rS  r|  rb  r  r  rJ   r$   r[   rM   r  r    s"    P! }}H2r[   r  c                        \ rS rSrS rS rSrg)
EntityTypei
  c                     g)Nr   r$   r   s    rM   rf  EntityType.__str__      r[   c                     g)NINDr$   r   s    rM   rK  EntityType.str      r[   r$   Nr%   r&   r'   r(   rf  rK  rJ   r$   r[   rM   r  r  
  s    r[   r  c                        \ rS rSrS rS rSrg)TruthValueTypei  c                     g)Ntr$   r   s    rM   rf  TruthValueType.__str__  r  r[   c                     g)NBOOLr$   r   s    rM   rK  TruthValueType.str  s    r[   r$   Nr  r$   r[   rM   r  r    s    r[   r  c                        \ rS rSrS rS rSrg)	EventTypei  c                     g)Nru  r$   r   s    rM   rf  EventType.__str__  r  r[   c                     g)NEVENTr$   r   s    rM   rK  EventType.str  s    r[   r$   Nr  r$   r[   rM   r  r    s    r[   r  c                   v    \ rS rSrS r\S 5       r\S 5       rS rS r	\
R                  rS rS rS	 rS
 rSrg)AnyTypei"  c                     g r   r$   r   s    rM   r}   AnyType.__init__#  s    r[   c                     U $ r   r$   r   s    rM   r  AnyType.first&      r[   c                     U $ r   r$   r   s    rM   r  AnyType.second*  r  r[   c                 R    [        U[        5      =(       d    UR                  U 5      $ r   )rs   r  rP  rN  s     rM   rP  AnyType.__eq__.  s    %)?U\\$-??r[   c                     X:X  + $ r   r$   rN  s     rM   rS  AnyType.__ne__1  rU  r[   c                     g)NTr$   rN  s     rM   r  AnyType.matches6  s    r[   c                     U$ r   r$   rN  s     rM   r  AnyType.resolve9  s    r[   c                     g)N?r$   r   s    rM   rf  AnyType.__str__<  r  r[   c                     g)NANYr$   r   s    rM   rK  AnyType.str?  r  r[   r$   N)r%   r&   r'   r(   r}   propertyr  r  rP  rS  r|  rb  r  r  rf  rK  rJ   r$   r[   rM   r  r  "  sY        @! }}Hr[   r  c                     [        U [        5      (       d   eU R                  SS5      n U S   S:X  ay  U S   S:X  d   eSn[        U 5       H7  u  p#US:X  a  US-  nM  US:X  a  US-  nUS:  d   eM'  US:X  d  M/  US:X  d  M7    O   [	        [        U SW 5      [        XS-   S 5      5      $ U S   S	[        -  :X  a  [        $ U S   S	[        -  :X  a  [        $ U S   S	[        -  :X  a  [        $ [        S S
U S   -  5      e)Nr   r   r   r2  r4  ri   r   r  zUnexpected character: '%s'.)
rs   rK  replacer<  r  r  ENTITY_TYPE
TRUTH_TYPEr  r   )type_stringparen_countr   chars       rM   r  r  I  s*   k3''''%%c2.K1~2#%%% -GAs{q q "Q&!# . k!A&';1ur3J)K
 	
 
Q4+-	-	Q4*,	,	Q4(?	*(/+a.@
 	
r[   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )TypeExceptionih  c                 $   > [         TU ]  U5        g r   superr}   )r|   r   r5  s     rM   r}   TypeException.__init__i  s    r[   r$   r%   r&   r'   r(   r}   rJ   __classcell__r5  s   @rM   r  r  h  s     r[   r  c                   ,   ^  \ rS rSrSU 4S jjrSrU =r$ )"InconsistentTypeHierarchyExceptionim  c                 T   > U(       a  SU< SU< S3nOSU-  n[         TU ]  U5        g )NzThe variable 'z8' was found in multiple places with different types in ''.zDThe variable '%s' was found in multiple places with different types.r  )r|   r   r   r   r5  s       rM   r}   +InconsistentTypeHierarchyException.__init__n  s8     &.z; %'  	r[   r$   r   r  r  s   @rM   r  r  m  s     r[   r  c                   (   ^  \ rS rSrU 4S jrSrU =r$ )TypeResolutionExceptioni|  c           	      R   > [         TU ]  SU< SUR                  < SU< S35        g )NzThe type of 'z', 'z!', cannot be resolved with type 'r   )r  r}   type)r|   r   
other_typer5  s      rM   r}    TypeResolutionException.__init__}  s     :??J8	
r[   r$   r  r  s   @rM   r  r  |      
 
r[   r  c                   (   ^  \ rS rSrU 4S jrSrU =r$ )IllegalTypeExceptioni  c                 n   > [         TU ]  SUR                  R                  < SU< SU< SU< S3	5        g )NzCannot set type of z 'z' to 'z'; must match type 'r  )r  r}   r5  r%   )r|   r   r  allowed_typer5  s       rM   r}   IllegalTypeException.__init__  s+    ##,,j*lT	
r[   r$   r  r  s   @rM   r  r    r  r[   r  c                 t    U  H  nUR                  U5      nM     U SS  H  nUR                  U5        M     U$ )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  )r   )expressionsr   r   s      rM   r   r     sE     "
((3	 " "#2&
Y' 'r[   c                   $    \ rS rSrSrS rS rSrg)SubstituteBindingsIi  zH
An interface for classes that can perform substitutions for
variables.
c                     [        5       e)z
:return: The object that is obtained by replacing
    each variable bound by ``bindings`` with its values.
    Aliases are already resolved. (maybe?)
:rtype: (any)
NotImplementedErrorr\  s     rM   r^  'SubstituteBindingsI.substitute_bindings       "##r[   c                     [        5       e)z2
:return: A list of all variables in this object.
r   r   s    rM   	variablesSubstituteBindingsI.variables  s     "##r[   r$   N)r%   r&   r'   r(   r8  r^  r  rJ   r$   r[   rM   r  r    s    
$$r[   r  c                      \ rS rSrSr\" 5       r\" SS9r\S$S j5       r	S r
S rS	 rS
 rS rS rS rS rS rS rS%S jrS rS rS%S jrS r\S4S jrS&S jrS%S jrS rS rS rS r S r!S r"S r#S  r$S! r%S" r&S#r'g)'
Expressioni  z<This is the base abstract object for all logical expressionsT)rw   Nc                 |    U(       a  U R                   R                  X5      $ U R                  R                  X5      $ r   )_type_checking_logic_parserr   _logic_parser)r  rA  rw   r   s       rM   r  Expression.fromstring  s3    2288FF$$**188r[   c                 J    U R                  U5      nU H  nU" U5      nM     U$ r   )applyto)r|   rO  
additionalr   as        rM   __call__Expression.__call__  s(    U#A!HE r[   c                 V    [        U[        5      (       d
   SU-  5       e[        X5      $ N%s is not an Expression)rs   r  r  rN  s     rM   r  Expression.applyto  s+    %,,O.G%.OO,$T11r[   c                     [        U 5      $ r   r   r   s    rM   __neg__Expression.__neg__  s     &&r[   c                     U * $ )zOIf this is a negated expression, remove the negation.
Otherwise add a negation.r$   r   s    rM   negateExpression.negate  s     ur[   c                 ^    [        U[        5      (       d  [        SU-  5      e[        X5      $ r  )rs   r  rW  r  rN  s     rM   __and__Expression.__and__  +    %,,5=>>T))r[   c                 ^    [        U[        5      (       d  [        SU-  5      e[        X5      $ r  )rs   r  rW  r  rN  s     rM   __or__Expression.__or__  s+    %,,5=>>D((r[   c                 ^    [        U[        5      (       d  [        SU-  5      e[        X5      $ r  )rs   r  rW  r  rN  s     rM   __gt__Expression.__gt__  r   r[   c                 ^    [        U[        5      (       d  [        SU-  5      e[        X5      $ r  )rs   r  rW  r  rN  s     rM   rX  Expression.__lt__  r   r[   c                     [         $ r   )NotImplementedrN  s     rM   rP  Expression.__eq__  s    r[   c                     X:X  + $ r   r$   rN  s     rM   rS  Expression.__ne__  rU  r[   c                     [        U[        5      (       d
   SU-  5       eUc  SSKJn  U" 5       n[	        U R                  5       UR                  5       5      nUR                  U5      $ )a	  
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  r   )Prover9)rs   r  nltk.inferencer/  r  simplifyprove)r|   rO  proverr/  biconds        rM   equivExpression.equiv  sY     %,,O.G%.OO,>.YFt}}0@A||F##r[   c                 *    [        [        U 5      5      $ r   )ra  reprr   s    rM   rb  Expression.__hash__  s    DJr[   c                 D   U nUR                  5        H{  nX1;   d  M
  X   n[        U[        5      (       a  U R                  U5      nO$[        U[        5      (       d  [        SU< 35      eUR                  U5      nUR                  X45      nM}     UR                  5       $ )Nz>Can not substitute a non-expression value into an expression: )	r  rs   r   r   r  r@  r^  r  r1  )r|   r]  exprvarvals        rM   r^  Expression.substitute_bindings   s    >>#Cmc8,,66s;C#C44$:=@ 
 --h7||C- $ }}r[   c                 Z   [        [        5      nU(       a`  U HZ  nX   n[        [        U5      5      n[	        U[
        5      (       a  XEl        O[        U5      Ul        X#   R                  U5        M\     U R                  US9  U Vs0 s H  o3X#   S   R                  _M     sn$ s  snf )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
)r   r   )
r   r,  r   r   rs   r|  r  r  r   	_set_type)r|   r   sigkeyr=  varExs         rM   r   Expression.typecheck  s     $ n*8C=9c4((!$J!*3EJ& ! 	%145#SXa[%%%555s   
B(c                     [        5       e)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
r   r|   r   s     rM   findtypeExpression.findtype)  r  r[   c                     [        5       e)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))
r   r|   r  r   s      rM   r@  Expression._set_type2  s     "##r[   c                    ^^^^ [        T[        5      (       d
   ST-  5       e[        T[        5      (       d
   ST-  5       eU R                  UUUU4S jU R                  5      $ )aE  
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                 ,   > U R                  TTTT5      $ r   )r  )r   alpha_convertr   replace_boundr   s    rM   <lambda>$Expression.replace.<locals>.<lambda>J  s    aii*m]Sr[   )rs   r   r  visit_structuredr5  r|   r   r   rP  rO  s    ````rM   r  Expression.replace<  sd     (H--P/E/PP-*j11 	
%
2	
1 $$SNN
 	
r[   c                 t  ^ U4S jmU n[        [        T" U 5      S S95       H  u  p4[        U[        5      (       a!  UR	                  [        SUS-   -  5      5      nO8[        U[        5      (       a!  UR	                  [        SUS-   -  5      5      nOUnUR                  UR                  US5      nM     U$ )z&Rename auto-generated unique variablesc                    > [        U [        5      (       a  U 1$ [        U [        5      (       a
  [        5       $ U R	                  TS 5      $ )Nc                 H    [        [        R                  U [        5       5      $ r   r   operatoror_setpartss    rM   rQ  >Expression.normalize.<locals>.get_indiv_vars.<locals>.<lambda>X  s    &uce2Tr[   )rs   IndividualVariableExpressionAbstractVariableExpressionr\  visit)r   get_indiv_varss    rM   rc  ,Expression.normalize.<locals>.get_indiv_varsQ  sD    !9::s
A9::uww"$T r[   c                     U R                   $ r   r   r   s    rM   rQ  &Expression.normalize.<locals>.<lambda>\  s    r[   )rB  ze0%sri   zz%sT)	r<  sortedrs   EventVariableExpressionr5  r   r`  r  r   )r|   newvarsr   r   r   newVarrc  s         @rM   	normalizeExpression.normalizeN  s    	 f^D%9?STUDA!455XfA.>%?@A;<<Xeq1uo%>?^^AJJ=F V r[   c                     [        5       e)a  
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``
r   r|   r  
combinators      rM   rb  Expression.visitf  s     "##r[   c                 0   ^ U R                  UU4S j5      $ )a  
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                    > T" U 6 $ r   r$   )r^  rq  s    rM   rQ  -Expression.visit_structured.<locals>.<lambda>  s
    *e2Dr[   rb  rp  s     `rM   rS  Expression.visit_structuredw  s     zz($DEEr[   c                 <    SU R                   R                   SU  S3$ )Nr2  r   r4  )r5  r%   r   s    rM   r6  Expression.__repr__  s"    4>>**+1TF!44r[   c                 "    U R                  5       $ r   )rK  r   s    rM   rf  Expression.__str__  s    xxzr[   c                     U R                  5       U R                  5       U R                  5       -   Vs1 s H-  n[        R                  " SUR
                  5      (       d  M+  UiM/     sn-  $ s  snf )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
z^[?@])free
predicates	constantsrG   rH   r"  )r|   ps     rM   r  Expression.variables  s[     yy{(4>>+;;
;!rxxQRQWQW?XA;
 
 	
 
s   *A,!A,c                 *    U R                  S S 5      $ )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                 "    U R                  5       $ r   )r}  rg  s    rM   rQ  !Expression.free.<locals>.<lambda>  s
    affhr[   c                 H    [        [        R                  U [        5       5      $ r   rY  r]  s    rM   rQ  r    s    fX\\5#%.Pr[   rv  r   s    rM   r}  Expression.free  s     zz P
 	
r[   c                 *    U R                  S S 5      $ )z]
Return a set of individual constants (non-predicates).
:return: set of ``Variable`` objects
c                 "    U R                  5       $ r   )r  rg  s    rM   rQ  &Expression.constants.<locals>.<lambda>  s
    akkmr[   c                 H    [        [        R                  U [        5       5      $ r   rY  r]  s    rM   rQ  r    s    6(,,su3Ur[   rv  r   s    rM   r  Expression.constants  s    
 zz#%U
 	
r[   c                 *    U R                  S S 5      $ )z]
Return a set of predicates (constants, not variables).
:return: set of ``Variable`` objects
c                 "    U R                  5       $ r   )r~  rg  s    rM   rQ  'Expression.predicates.<locals>.<lambda>  s
    allnr[   c                 H    [        [        R                  U [        5       5      $ r   rY  r]  s    rM   rQ  r    s    F8<<PSPU4Vr[   rv  r   s    rM   r~  Expression.predicates  s    
 zz$&V
 	
r[   c                 <    U R                  S U R                  5      $ )z4
:return: beta-converted version of this expression
c                 "    U R                  5       $ r   )r1  rg  s    rM   rQ  %Expression.simplify.<locals>.<lambda>  s
    qzz|r[   )rS  r5  r   s    rM   r1  Expression.simplify  s     $$%;T^^LLr[   c                     [        U5      $ r   )r   rF  s     rM   r   "Expression.make_VariableExpression  s    !(++r[   r$   )FNr   FT)(r%   r&   r'   r(   r8  rf   r  r
  r  r  r  r  r  r  r  r"  r%  rX  rP  rS  r5  rb  r^  r   rG  r  r@  r  rm  rb  rS  r6  rf  r  r}  r  r~  r1  r   rJ   r$   r[   rM   r  r    s    FMM"-">9 92'
*
)
*
*
!$$ $6.$ $,t $
$0$"F5	



M,r[   r  c                       \ rS rSrSrS rS r\S 5       r\	S4S jr
S rS	 rS
 rS rS rS r\R$                  rS rS r\S 5       r\S 5       rS rSrg)r  i  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                     [        U[        5      (       d
   SU-  5       e[        U[        5      (       d
   SU-  5       eXl        X l        g)zp
:param function: ``Expression``, for the function expression
:param argument: ``Expression``, for the argument
r  N)rs   r  r  r  r  s      rM   r}   ApplicationExpression.__init__  sJ    
 (J//U1JX1UU/(J//U1JX1UU/  r[   c                    U R                   R                  5       nU R                  R                  5       n[        U[        5      (       a4  UR
                  R                  UR                  U5      R                  5       $ U R                  X5      $ r   )	r  r1  r  rs   r  r   r  r   r5  r  s      rM   r1  ApplicationExpression.simplify  si    ==))+==))+h 011==(():):HENNPP>>(55r[   c                     [        U R                  R                  [        5      (       a   U R                  R                  R                  $ [
        $ r   )rs   r  r  r  r  r  r   s    rM   r  ApplicationExpression.type  s4    dmm((+66==%%,,,Or[   Nc                 &   [        U[        5      (       d   eUc  [        [        5      nU R                  R                  [        U5         U R                  R                  [        U R                  R                  U5      U5        g! [         a  n[        SU R                  < SU R                  R                  < SU R                  < SU R                  R                  < SU R                  R                  R                  < S35      UeSnAff = f):see Expression._set_type()NzThe function 'z' is of type 'z' and cannot be applied to 'z' of type 'z"'.  Its argument must match type 'r  )rs   r|  r   r,  r  r@  r  r  r  r  r  r  r  )r|   r  r   r   s       rM   r@  ApplicationExpression._set_type  s    *d++++#D)I)4	MM##DMM..
;Y ' 	 MMMM&&MMMM&&MM&&,,
 
	s   :B 
DA;DDc                     [        U[        5      (       d
   SU-  5       eU R                  5       (       a  U R                  5       u  p#OU R                  nU R
                  /nU/U-    Vs/ s H  oDR                  U5      PM     nn/ nU HK  nU[        :w  d  M  U(       a$  U H  nUR                  U5      (       d  M    M6     M:  UR                  U5        MM     [        U5      S:X  a  [        U5      S   $ [        $ s  snf ):see Expression.findtype()rM  ri   r   )rs   r   is_atomuncurryr  r  rG  r  r  r   r   r,  )	r|   r   r  argsargfounduniquer  us	            rM   rG  ApplicationExpression.findtype  s    (H--P/E/PP-<<>>!\\^NHd }}HMM?D4<:3DE3DCh'3DEAH}#99Q<<! $ MM!$  v;!<?"O Fs   )C;c                     [        U R                  [        5      (       a  [        5       nOU R                  R	                  5       nXR
                  R	                  5       -  $ z:see: Expression.constants())rs   r  ra  r\  r  r  )r|   function_constantss     rM   r  ApplicationExpression.constants'  sE    dmm%?@@!$!%!8!8!:!MM$;$;$===r[   c                     [        U R                  [        5      (       a  U R                  R                  1nOU R                  R	                  5       nXR
                  R	                  5       -  $ z:see: Expression.predicates())rs   r  r   r   r~  r  )r|   function_predss     rM   r~   ApplicationExpression.predicates/  sN    dmm%788"mm445N!]]557N 8 8 :::r[   c                 V    U" U" U R                   5      U" U R                  5      /5      $ z:see: Expression.visit())r  r  rp  s      rM   rb  ApplicationExpression.visit7  s$    8DMM2HT]]4KLMMr[   c                     [        U[        5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )rs   r  r  r  rN  s     rM   rP  ApplicationExpression.__eq__;  s<    u34 0/0/	
r[   c                     X:X  + $ r   r$   rN  s     rM   rS  ApplicationExpression.__ne__B  rU  r[   c                    U R                  5       (       a+  U R                  5       u  pSR                  S U 5       5      nOU R                  nSU R                  -  nSU-  nSn[        U[        5      (       am  [        UR                  [        5      (       a,  [        UR                  R                  [        5      (       d  SnO9[        UR                  [        5      (       d  SnO[        U[        5      (       a  SnU(       a$  [        R                  U-   [        R                  -   nU[        R                  -   U-   [        R                  -   $ )Nr   c              3   ,   #    U  H
  nS U-  v   M     g7fr  Nr$   )rK   r  s     rM   	<genexpr>0ApplicationExpression.__str__.<locals>.<genexpr>K  s     :TctczT   r  FT)r  r  joinr  r  rs   r  r   r  ra  BooleanExpressionr
   r2   r3   )r|   r  r  arg_strfunction_strparenthesize_functions         rM   rf  ApplicationExpression.__str__G  s    <<>>!\\^NHhh:T::G }}HT]]*Gh %h 011(--)>??!(--"8"8:TUU,0)/@AA(,%"788$(! !;;5DLfkk)G3fllBBr[   c                     U R                   nU R                  /n[        U[        5      (       a?  UR	                  SUR                  5        UR                   n[        U[        5      (       a  M?  X4$ )zP
Uncurry this application expression

return: A tuple (base-function, arg-list)
r   )r  r  rs   r  insert)r|   r  r  s      rM   r  ApplicationExpression.uncurrya  s`     ==#899KK8,,-((H #899 r[   c                 (    U R                  5       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   r  r   s    rM   predApplicationExpression.predo  s     ||~a  r[   c                 (    U R                  5       S   $ )z
Return uncurried arg-list
ri   r  r   s    rM   r  ApplicationExpression.argsx  s    
 ||~a  r[   c                 6    [        U R                  [        5      $ )zS
Is this expression an atom (as opposed to a lambda expression applied
to a term)?
)rs   r  ra  r   s    rM   r  ApplicationExpression.is_atom  s    
 $))%?@@r[   )r  r  )r%   r&   r'   r(   r8  r}   r1  r  r  r  r@  rG  r  r~  rb  rP  rS  r  rb  rf  r  r  r  r  rJ   r$   r[   rM   r  r    s    8!6   $,t 26>;N
! ""HC4  ! ! ! !Ar[   r  c                   x    \ rS rSrSrS rS rSS jr\S4S jr	S r
S	 rS
 rS rS r\R                   rS rSrg)ra  i  zDThis class represents a variable to be used as a predicate or entityc                 N    [        U[        5      (       d
   SU-  5       eXl        g)z1
:param variable: ``Variable``, for the variable
rM  N)rs   r   r   rF  s     rM   r}   #AbstractVariableExpression.__init__  s'     (H--P/E/PP- r[   c                     U $ r   r$   r   s    rM   r1  #AbstractVariableExpression.simplify  s    r[   c                     [        U[        5      (       d
   SU-  5       e[        U[        5      (       d
   SU-  5       eU R                  U:X  a  U$ U $ ):see: Expression.replace()z%s is not an Variabler  )rs   r   r  r   rT  s        rM   r  "AbstractVariableExpression.replace  s[    (H--Q/F/QQ-*j11 	
%
2	
1 ==H$Kr[   Nc                    [        U[        5      (       d   eUc  [        [        5      nUnX R                  R
                      H0  nUR                  R                  U5      nU(       a  M'  [        U 5      e   X R                  R
                     R                  U 5        X R                  R
                      H	  nX4l        M     gr  N)
rs   r|  r   r,  r   r"  r  r  r  r   r|   r  r   
resolutionrC  s        rM   r@  $AbstractVariableExpression._set_type  s    *d++++#D)I
}}112E++J7J:8>> 3
 	--$$%,,T2}}112E#J 3r[   c                     [        U[        5      (       d
   SU-  5       eU R                  U:X  a  U R                  $ [        $ r  rM  )rs   r   r   r  r  rF  s     rM   rG  #AbstractVariableExpression.findtype  s;    (H--P/E/PP-==H$99Or[   c                     [        5       $ r  r\  r   s    rM   r~  %AbstractVariableExpression.predicates  	    ur[   c                 b    [        U[        5      =(       a    U R                  UR                  :H  $ )zLAllow equality between instances of ``AbstractVariableExpression``
subtypes.)rs   ra  r   rN  s     rM   rP  !AbstractVariableExpression.__eq__  s)     u89 0/	
r[   c                     X:X  + $ r   r$   rN  s     rM   rS  !AbstractVariableExpression.__ne__  rU  r[   c                 j    [        U[        5      (       d  [        eU R                  UR                  :  $ r   )rs   ra  rW  r   rN  s     rM   rX  !AbstractVariableExpression.__lt__  s(    %!;<<O}}u~~--r[   c                      SU R                   -  $ r~  rf  r   s    rM   rf  "AbstractVariableExpression.__str__  s    dmm##r[   rf  r  )r%   r&   r'   r(   r8  r}   r1  r  r  r@  rG  r~  rP  rS  rX  r  rb  rf  rJ   r$   r[   rM   ra  ra    sK    N!	 $,t $"
!.
 ""H$r[   ra  c                   J    \ rS rSrSr\S4S jrS r\" \\5      r	S r
S rSrg)	r`  i  zThis class represents variables that take the form of a single lowercase
character (other than 'e') followed by zero or more digits.Nc                     [        U[        5      (       d   eUc  [        [        5      nUR	                  [
        5      (       d  [        X[
        5      eX R                  R                     R                  U 5        gr  )
rs   r|  r   r,  r  r  r  r   r"  r   rJ  s      rM   r@  &IndividualVariableExpression._set_type  s^    *d++++#D)I!!+..&tEE--$$%,,T2r[   c                     [         $ r   )r  r   s    rM   	_get_type&IndividualVariableExpression._get_type  s    r[   c                     U R                   1$ z:see: Expression.free()rf  r   s    rM   r}  !IndividualVariableExpression.free      r[   c                     [        5       $ r  r  r   s    rM   r  &IndividualVariableExpression.constants  r  r[   r$   )r%   r&   r'   r(   r8  r  r@  r  r  r  r}  r  rJ   r$   r[   rM   r`  r`    s1    C $,t 
3 Iy)Dr[   r`  c                   (    \ rS rSrSr\rS rS rSr	g)r   i  zsThis class represents variables that take the form of a single uppercase
character followed by zero or more digits.c                     U R                   1$ r  rf  r   s    rM   r}  FunctionVariableExpression.free  r  r[   c                     [        5       $ r  r  r   s    rM   r  $FunctionVariableExpression.constants  r  r[   r$   N)
r%   r&   r'   r(   r8  r  r  r}  r  rJ   r$   r[   rM   r   r     s    2 Dr[   r   c                       \ rS rSrSr\rSrg)rj  i   zwThis class represents variables that take the form of a single lowercase
'e' character followed by zero or more digits.r$   N)r%   r&   r'   r(   r8  
EVENT_TYPEr  rJ   r$   r[   rM   rj  rj     s    6 Dr[   rj  c                   6    \ rS rSrSr\r\S4S jrS r	S r
Srg)r   i  zpThis class represents variables that do not take the form of a single
character followed by zero or more digits.Nc                 
   [        U[        5      (       d   eUc  [        [        5      nU[        :X  a  [
        nO1UnU R                  [
        :w  a  UR                  U R                  5      nX R                  R                      H0  nUR                  R                  U5      nU(       a  M'  [        U 5      e   X R                  R                     R                  U 5        X R                  R                      H	  nX4l        M     gr  )rs   r|  r   r,  r  r  r  r  r   r"  r  r   r  s        rM   r@  ConstantExpression._set_type  s    *d++++#D)I!$J#JyyK''//		:
}}112E++J7J:8>> 3
 	--$$%,,T2}}112E#J 3r[   c                     [        5       $ r  r  r   s    rM   r}  ConstantExpression.free%  r  r[   c                     U R                   1$ r  rf  r   s    rM   r  ConstantExpression.constants)  r  r[   r$   )r%   r&   r'   r(   r8  r  r  r  r@  r}  r  rJ   r$   r[   rM   r   r     s"    2 D#+t $0r[   r   c                 4   [        U [        5      (       d
   SU -  5       e[        U R                  5      (       a  [	        U 5      $ [        U R                  5      (       a  [        U 5      $ [        U R                  5      (       a  [        U 5      $ [        U 5      $ )z
This is a factory method that instantiates and returns a subtype of
``AbstractVariableExpression`` appropriate for the given variable.
rM  )
rs   r   rn  r"  r`  ro  r   rp  rj  r   rf  s    rM   r   r   .  s{    
 h))L+AH+LL)+H55	HMM	"	")(33	X]]	#	#&x00!(++r[   c                   j    \ rS rSrSrS rSS jrS rS rS r	S r
S	 rS
 rS r\R                  rSrg)VariableBinderExpressioni>  zThis an abstract class for any Expression that binds a variable in an
Expression.  This includes LambdaExpressions and Quantified Expressionsc                     [        U[        5      (       d
   SU-  5       e[        U[        5      (       d
   SU-  5       eXl        X l        g)z[
:param variable: ``Variable``, for the variable
:param term: ``Expression``, for the term
rM  r  N)rs   r   r  r   r   r%  s      rM   r}   !VariableBinderExpression.__init__B  sJ    
 (H--P/E/PP-$
++M-F-MM+ 	r[   c           	      V   [        U[        5      (       d
   SU-  5       e[        U[        5      (       d
   SU-  5       eU R                  U:X  a_  U(       aV  [        U[        5      (       d
   SU-  5       eU R                  UR                  U R                  R                  XSU5      5      $ U $ U(       a@  U R                  UR                  5       ;   a"  U R                  [        U R                  S95      n U R                  U R                  U R                  R                  XX45      5      $ )r  rM  r  z&%s is not a AbstractVariableExpressionT)rr  )rs   r   r  r   ra  r5  r   r  r}  rO  rv  rT  s        rM   r   VariableBinderExpression.replaceL  s   (H--P/E/PP-*j11 	
%
2	
1 ==H$!*.HII <zII ~~''II%%hD-P 
  *//2C!C))/$--*PQ >>		!!(U r[   c           	          [        U[        5      (       d
   SU-  5       eU R                  XR                  R	                  U R
                  [        U5      S5      5      $ )zRename all occurrences of the variable introduced by this variable
binder in the expression to ``newvar``.
:param newvar: ``Variable``, for the new variable
rM  T)rs   r   r5  r   r  r   r   )r|   newvars     rM   rO  &VariableBinderExpression.alpha_convertj  sT    
 &(++L-Cf-LL+~~II%%dmm5G5OQUV
 	
r[   c                 R    U R                   R                  5       U R                  1-
  $ r  )r   r}  r   r   s    rM   r}  VariableBinderExpression.freet  s    yy~~4==/11r[   c                     [        U[        5      (       d
   SU-  5       eXR                  :X  a  [        $ U R                  R                  U5      $ r  )rs   r   r   r  r   rG  rF  s     rM   rG  !VariableBinderExpression.findtypex  sD    (H--P/E/PP-}}$O99%%h//r[   c                 4    U" U" U R                   5      /5      $ r  r   rp  s      rM   rb  VariableBinderExpression.visit      8DII./00r[   c                 H    U" U R                   U" U R                  5      5      $ )z#:see: Expression.visit_structured())r   r   rp  s      rM   rS  )VariableBinderExpression.visit_structured  s    $--$)))<==r[   c                 b   [        XR                  5      (       d  [        XR                  5      (       a{  U R                  UR                  :X  a  U R                  UR                  :H  $ [	        U R                  5      nU R                  UR                  R                  UR                  U5      :H  $ g)zvDefines equality modulo alphabetic variance.  If we are comparing
\x.M  and \y.N, then check equality of M and N[x/y].F)rs   r5  r   r   r   r  )r|   rO  varexs      rM   rP  VariableBinderExpression.__eq__  s{     dOO,,
5..0Q0Q}}.yyEJJ.. +4==9yyEJJ$6$6u~~u$MMMr[   c                     X:X  + $ r   r$   rN  s     rM   rS  VariableBinderExpression.__ne__  rU  r[   )r   r   Nr  )r%   r&   r'   r(   r8  r}   r  rO  r}  rG  rb  rS  rP  rS  r  rb  rJ   r$   r[   rM   r
  r
  >  sB    O<
201>! ""Hr[   r
  c                   8    \ rS rSr\S 5       r\S4S jrS rSr	g)r  i  c                     [        U R                  R                  U R                  5      U R                  R                  5      $ r   )r  r   rG  r   r  r   s    rM   r  LambdaExpression.type  s*    499--dmm<diinnMMr[   Nc                     [        U[        5      (       d   eUc  [        [        5      nU R                  R                  UR                  U5        U R                  R                  U5      (       d  [        X5      egr  )
rs   r|  r   r,  r   r@  r  r  r  r  rJ  s      rM   r@  LambdaExpression._set_type  sd    *d++++#D)I		J--y9yy  ,,)$;; -r[   c                 n   U R                   /nU R                  nUR                  U R                  :X  aC  UR                  UR                   5        UR                  nUR                  U R                  :X  a  MC  [        R
                  SR                  S U 5       5      -   [        R                  -   SU-  -   $ )Nr   c              3   ,   #    U  H
  nS U-  v   M     g7fr  r$   rK   ru  s     rM   r  +LambdaExpression.__str__.<locals>.<genexpr>       3Ataxr  r  )r   r   r5  r   r
   r)   r  r1   r|   r  r   s      rM   rf  LambdaExpression.__str__  s    ]]O	yynn.T]]+99D nn. MMhh3334jj Tk	
r[   r$   
r%   r&   r'   r(   r  r  r  r@  rf  rJ   r$   r[   rM   r  r    s'    N N $,t 	<
r[   r  c                   8    \ rS rSr\S 5       r\S4S jrS rSr	g)QuantifiedExpressioni  c                     [         $ r   r  r   s    rM   r  QuantifiedExpression.type      r[   Nc                     [        U[        5      (       d   eUc  [        [        5      nUR	                  [
        5      (       d  [        X[
        5      eU R                  R                  [
        U5        gr  	rs   r|  r   r,  r  r  r  r   r@  rJ  s      rM   r@  QuantifiedExpression._set_type  W    *d++++#D)I!!*--&tDD		J	2r[   c                 t   U R                   /nU R                  nUR                  U R                  :X  aC  UR                  UR                   5        UR                  nUR                  U R                  :X  a  MC  U R	                  5       S-   SR                  S U 5       5      -   [        R                  -   SU-  -   $ )Nr   c              3   ,   #    U  H
  nS U-  v   M     g7fr  r$   r(  s     rM   r  /QuantifiedExpression.__str__.<locals>.<genexpr>  r*  r  r  )r   r   r5  r   getQuantifierr  r
   r1   r+  s      rM   rf  QuantifiedExpression.__str__  s    ]]O	yynn.T]]+99D nn.  hh3334 jj Tk		
r[   r$   r-  r$   r[   rM   r/  r/    s%      $,t 	3
r[   r/  c                       \ rS rSrS rSrg)r   i  c                 "    [         R                  $ r   )r
   r+   r   s    rM   r;  ExistsExpression.getQuantifier  s    }}r[   r$   Nr%   r&   r'   r(   r;  rJ   r$   r[   rM   r   r     s    r[   r   c                       \ rS rSrS rSrg)r   i  c                 "    [         R                  $ r   )r
   r-   r   s    rM   r;  AllExpression.getQuantifier      zzr[   r$   Nr@  r$   r[   rM   r   r     s    r[   r   c                       \ rS rSrS rSrg)r   i  c                 "    [         R                  $ r   )r
   r/   r   s    rM   r;  IotaExpression.getQuantifier  s    {{r[   r$   Nr@  r$   r[   rM   r   r     s    r[   r   c                   t    \ rS rSrS r\S 5       r\S4S jrS r	S r
S rS	 rS
 r\R                  rS rSrg)r   i  c                 N    [        U[        5      (       d
   SU-  5       eXl        g r  )rs   r  r   )r|   r   s     rM   r}   NegatedExpression.__init__  s%    $
++M-F-MM+	r[   c                     [         $ r   r1  r   s    rM   r  NegatedExpression.type  r3  r[   Nc                     [        U[        5      (       d   eUc  [        [        5      nUR	                  [
        5      (       d  [        X[
        5      eU R                  R                  [
        U5        gr  r5  rJ  s      rM   r@  NegatedExpression._set_type  r7  r[   c                 v    [        U[        5      (       d
   SU-  5       eU R                  R                  U5      $ )NrM  )rs   r   r   rG  rF  s     rM   rG  NegatedExpression.findtype  s5    (H--P/E/PP-yy!!(++r[   c                 4    U" U" U R                   5      /5      $ r  r  rp  s      rM   rb  NegatedExpression.visit  r  r[   c                     U R                   $ )z:see: Expression.negate()r  r   s    rM   r  NegatedExpression.negate  s    yyr[   c                 b    [        U[        5      =(       a    U R                  UR                  :H  $ r   )rs   r   r   rN  s     rM   rP  NegatedExpression.__eq__  s"    %!23O		UZZ8OOr[   c                     X:X  + $ r   r$   rN  s     rM   rS  NegatedExpression.__ne__
  rU  r[   c                 B    [         R                  SU R                  -  -   $ r~  )r
   r5   r   r   s    rM   rf  NegatedExpression.__str__  s    zzD499,,,r[   r  )r%   r&   r'   r(   r}   r  r  r  r@  rG  rb  r  rP  rS  r  rb  rf  rJ   r$   r[   rM   r   r     sS       $,t 	3,1P! ""H-r[   r   c                   f    \ rS rSrS r\S 5       rS rS rS r	S r
\R                  rS rS	 rS
rg)BinaryExpressioni  c                     [        U[        5      (       d
   SU-  5       e[        U[        5      (       d
   SU-  5       eXl        X l        g r  )rs   r  r  r  r  s      rM   r}   BinaryExpression.__init__  sH    %,,O.G%.OO,&*--Q/H6/QQ-
r[   c                     [         $ r   r1  r   s    rM   r  BinaryExpression.type  r3  r[   c                     [        U[        5      (       d
   SU-  5       eU R                  R                  U5      nU R                  R                  U5      nX#:X  d
  U[
        :X  a  U$ U[
        :X  a  U$ [
        $ r  )rs   r   r  rG  r  r  )r|   r   r  rA  s       rM   rG  BinaryExpression.findtype  si    (H--P/E/PP-JJ)KK  *6Q(]H(]HOr[   c                 V    U" U" U R                   5      U" U R                  5      /5      $ r  r  rp  s      rM   rb  BinaryExpression.visit*  s$    8DJJ/$++1FGHHr[   c                     [        XR                  5      =(       d    [        XR                  5      =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r   )rs   r5  r  r  rN  s     rM   rP  BinaryExpression.__eq__.  sK    oo.S*UNN2S ,

ekk),u||+	
r[   c                     X:X  + $ r   r$   rN  s     rM   rS  BinaryExpression.__ne__5  rU  r[   c                     U R                  U R                  5      nU R                  U R                  5      n[        R                  U-   S-   U R                  5       -   S-   U-   [        R                  -   $ )Nr   )
_str_subexr  r  r
   r2   getOpr3   r  s      rM   rf  BinaryExpression.__str__:  sX    

+-{{U"S(4::<7#=FUUr[   c                     SU-  $ r~  r$   )r|   subexs     rM   rj  BinaryExpression._str_subex?  s    e|r[   r  N)r%   r&   r'   r(   r}   r  r  rG  rb  rP  rS  r  rb  rf  rj  rJ   r$   r[   rM   r\  r\    sH      
I
! ""HV
r[   r\  c                   "    \ rS rSr\S4S jrSrg)r  iC  Nc                 *   [        U[        5      (       d   eUc  [        [        5      nUR	                  [
        5      (       d  [        X[
        5      eU R                  R                  [
        U5        U R                  R                  [
        U5        gr  )
rs   r|  r   r,  r  r  r  r  r@  r  rJ  s      rM   r@  BooleanExpression._set_typeD  sk    *d++++#D)I!!*--&tDD

Z3j)4r[   r$   )r%   r&   r'   r(   r  r@  rJ   r$   r[   rM   r  r  C  s    #+t 
5r[   r  c                   $    \ rS rSrSrS rS rSrg)r  iQ  z"This class represents conjunctionsc                 "    [         R                  $ r   )r
   r7   r   s    rM   rk  AndExpression.getOpT  rD  r[   c                 D    SU-  n[        U[        5      (       a  USS $ U$ Nr  ri   r  )rs   r  r|   rn  rA  s      rM   rj  AndExpression._str_subexW  s(    5Le]++Qr7Nr[   r$   Nr%   r&   r'   r(   r8  rk  rj  rJ   r$   r[   rM   r  r  Q  s    ,r[   r  c                   $    \ rS rSrSrS rS rSrg)r  i^  z"This class represents disjunctionsc                 "    [         R                  $ r   )r
   r9   r   s    rM   rk  OrExpression.getOpa      yyr[   c                 D    SU-  n[        U[        5      (       a  USS $ U$ rw  )rs   r  rx  s      rM   rj  OrExpression._str_subexd  s(    5Le\**Qr7Nr[   r$   Nrz  r$   r[   rM   r  r  ^  s    ,r[   r  c                       \ rS rSrSrS rSrg)r  ik  z"This class represents implicationsc                 "    [         R                  $ r   )r
   r;   r   s    rM   rk  ImpExpression.getOpn  rD  r[   r$   Nr%   r&   r'   r(   r8  rk  rJ   r$   r[   rM   r  r  k  s
    ,r[   r  c                       \ rS rSrSrS rSrg)r  ir  z$This class represents biconditionalsc                 "    [         R                  $ r   )r
   r=   r   s    rM   rk  IffExpression.getOpu  rD  r[   r$   Nr  r$   r[   rM   r  r  r  s
    .r[   r  c                   ,    \ rS rSrSr\S4S jrS rSrg)r  iy  z:This class represents equality expressions like "(x = y)".Nc                 *   [        U[        5      (       d   eUc  [        [        5      nUR	                  [
        5      (       d  [        X[
        5      eU R                  R                  [        U5        U R                  R                  [        U5        gr  )rs   r|  r   r,  r  r  r  r  r@  r  r  rJ  s      rM   r@  EqualityExpression._set_type|  sk    *d++++#D)I!!*--&tDD

[)4k95r[   c                 "    [         R                  $ r   )r
   r?   r   s    rM   rk  EqualityExpression.getOp  r~  r[   r$   )	r%   r&   r'   r(   r8  r  r@  rk  rJ   r$   r[   rM   r  r  y  s    D#+t 
6r[   r  c                       \ rS rSrS rSrg)r   i  c                 :    Xl         [        R                  X5        g r   )r   	Exceptionr}   r|   r   r   s      rM   r}   #LogicalExpressionException.__init__  s    
4)r[   )r   Nr%   r&   r'   r(   r}   rJ   r$   r[   rM   r   r     s    *r[   r   c                       \ rS rSrSS jrSrg)r   i  Nc                     U(       a  U(       a  SU< SU< S3nO!U(       a  SU-  nU(       a  USU-   -  nOSU-  n[         R                  XU5        g )NzUnexpected token: 'z'.  Expected token 'r  zUnexpected token: '%s'.z  r+  r   r}   )r|   r   
unexpectedr-  r   r   s         rM   r}   !UnexpectedTokenException.__init__  sO    (C +j8Ctg~%(83C"++D=r[   r$   )NNNr  r$   r[   rM   r   r     s    >r[   r   c                       \ rS rSrSS jrSrg)r   i  Nc                 H    U(       d  Sn[         R                  XSU-   5        g )NzMore tokens expected.zEnd of input found.  r  r  s      rM   r}   $ExpectedMoreTokensException.__init__  s#    -G"++07:	
r[   r$   r   r  r$   r[   rM   r   r     s    
r[   r   c                 r    [        U [        5      (       d
   SU -  5       e[        R                  " SU 5      SL$ )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
rJ  z^[a-df-z]\d*$Nrs   rK  rG   rH   r;  s    rM   rn  rn    s9     dC  ="6"== 88$d+477r[   c                 r    [        U [        5      (       d
   SU -  5       e[        R                  " SU 5      SL$ )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
rJ  z
^[A-Z]\d*$Nr  r  s    rM   ro  ro    s8     dC  ="6"== 88M4(44r[   c                 r    [        U [        5      (       d
   SU -  5       e[        R                  " SU 5      SL$ )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
rJ  z^e\d*$Nr  r  s    rM   rp  rp    s8     dC  ="6"== 88It$D00r[   c                     [         R                  n [        S5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S	5      5        [        U " S
5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        U " S5      5        [        S5        [        U " S5      R                  5       5        [        U " S5      R                  5       5        [        U " S5      R                  5       5        [        U " S5      R                  5       5        [        S5        U " S5      n[        U5        UR	                  [        S5      5      n[        U5        [        X:H  5        g )Nz3====================Test reader====================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)rk  )r  r  rV   r1  rO  r   )lexpre1e2s      rM   demor    s   !!E	
-.	%.	%	
	%

	%0
12	%9
:;	%
	%"
#$	%!
"#	%&
'(	%.
/0	%!
"#	%#
$%	%#
$%	%.
/0	%%
&'	
/0	%.
/
8
8
:;	%.
/
8
8
:;	%?
@
I
I
KL	%H
I
R
R
TU	
VW		B	"I			(3-	(B	"I	"(Or[   c                  8   [        S5        [        S5        [        S5        [        S5        [        S5        [        S5        [        S5        [        S5        [        S	5        [        S
5        [        S5        [        S5        [        S5        [        S5        g )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)rV   demoExceptionr$   r[   rM   demo_errorsr    st    	
45.!%&*%&'(+$)*+/"r[   c                      [         R                  U 5        g ! [         a.  n[        UR                  R
                   SU 35         S nAg S nAff = f)Nr3  )r  r  r   rV   r5  r%   )rA  r   s     rM   r  r    sF    .a % .%%&b,--.s    
A$AAc                 T    [        U R                  5        SU R                   35        g )Nz : )rV   rK  r  )exs    rM   	printtyper    s    	RVVXJc"''
#$r[   __main__)NNr   )Kr8  rZ  rG   collectionsr   	functoolsr   r   nltk.internalsr   	nltk.utilr   r   rq  r
   rZ   r_   rd   rf   rG  r   rv  rz  r|  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r   r  r  r  ra  r`  r   rj  r   r   r
  r  r/  r   r   r   r   r\  r  r  r  r  r  r  r   r   r   rn  ro  rp  r  r  r  r  r%   r$   r[   rM   <module>r     s  
  	 # , " 9*I *IZ"""i@ i@X< , , ,@8		 	2B$ 2Bj & Y 	 i B 
l[
9
>I 
 
m 

= 
 $ $,H,$ H,VGAJ GAT H$ H$ H$V#= <!; : $3 $N, Z#z Z#z
/ 
<
3 
>+ 
( 
) 
)-
 )-X-z -`5( 5
% 

$ 
% % ) ,* *>9 > 
"< 
	8	5	1B#".% zF r[   