
    /hH                     N   S r SSKJr  SSKJr  SSKJrJr  SSKJ	r	J
r
  SSKJrJrJrJrJrJrJrJrJrJrJrJrJrJr   " S S\5      rS	 r " S
 S\5      r " S S\5      r " S S\5      r  " S S\5      r! " S S5      r"S r#S r$S r%S r&S r'S r(S r)\*S:X  a  \)" 5         gg)z
A module to perform nonmonotonic reasoning.  The ideas and demonstrations in
this module are based on "Logical Foundations of Artificial Intelligence" by
Michael R. Genesereth and Nils J. Nilsson.
    )defaultdict)reduce)ProverProverCommandDecorator)Prover9Prover9Command)AbstractVariableExpressionAllExpressionAndExpressionApplicationExpressionBooleanExpressionEqualityExpressionExistsExpression
ExpressionImpExpressionNegatedExpressionVariableVariableExpressionoperatorunique_variablec                       \ rS rSrSrg)ProverParseError&    N)__name__
__module____qualname____firstlineno____static_attributes__r       S/var/www/auris/envauris/lib/python3.13/site-packages/nltk/inference/nonmonotonic.pyr   r   &   s    r    r   c                 n    U c  UnOX* /-   n[        [        R                  S U 5       [        5       5      $ )Nc              3   @   #    U  H  oR                  5       v   M     g 7fN)	constants).0as     r!   	<genexpr>get_domain.<locals>.<genexpr>/   s      H1s   )r   r   or_set)goalassumptionsall_expressionss      r!   
get_domainr/   *   s3    |%%/(,, H H#%PPr    c                   *    \ rS rSrSrS rS rS rSrg)ClosedDomainProver2   zQ
This is a prover decorator that adds domain closure assumptions before
proving.
c                     U R                   R                  5        Vs/ s H  oPM     nnU R                   R                  5       n[        X25      nU Vs/ s H  oPR	                  XT5      PM     sn$ s  snf s  snf r$   )_commandr-   r,   r/   replace_quants)selfr'   r-   r,   domainexs         r!   r-   ClosedDomainProver.assumptions8   sj    "&--";";"=>"=Qq"=>}}!!#D.:EF+B##B/+FF ? Gs   A0A5c                     U R                   R                  5       n[        XR                   R                  5       5      nU R	                  X5      $ r$   )r4   r,   r/   r-   r5   )r6   r,   r7   s      r!   r,   ClosedDomainProver.goal>   s<    }}!!#D--";";"=>""400r    c           	      H   [        U[        5      (       ak  U Vs/ s H1  o1R                  R                  UR                  [        U5      5      PM3     nnU Vs/ s H  oPR                  XR5      PM     nn[        S U5      $ [        U[        5      (       aF  UR                  U R                  UR                  U5      U R                  UR                  U5      5      $ [        U[        5      (       a  U R                  UR                  U5      * $ [        U[        5      (       ak  U Vs/ s H1  o1R                  R                  UR                  [        U5      5      PM3     nnU Vs/ s H  o0R                  X25      PM     nn[        S U5      $ U$ s  snf s  snf s  snf s  snf )a  
Apply the closed domain assumption to the expression

- Domain = union([e.free()|e.constants() for e in all_expressions])
- translate "exists x.P" to "(z=d1 | z=d2 | ... ) & P.replace(x,z)" OR
            "P.replace(x, d1) | P.replace(x, d2) | ..."
- translate "all x.P" to "P.replace(x, d1) & P.replace(x, d2) & ..."

:param ex: ``Expression``
:param domain: set of {Variable}s
:return: ``Expression``
c                 
    X-  $ r$   r   xys     r!   <lambda>3ClosedDomainProver.replace_quants.<locals>.<lambda>U       qur    c                 
    X-  $ r$   r   r>   s     r!   rA   rB   b   rC   r    )
isinstancer
   termreplacevariabler   r5   r   r   	__class__firstsecondr   r   )r6   r8   r7   d	conjunctsc	disjunctss          r!   r5   !ClosedDomainProver.replace_quantsC   so    b-((MSMS-?-BCV   BKKA,,Q7IK,i88-..<<##BHHf5##BIIv6  -..''888,--MSMS-?-BCV   BKKA,,Q7IK,i88I' L Ls   8FF'8F%Fr   N)	r   r   r   r   __doc__r-   r,   r5   r   r   r    r!   r1   r1   2   s    
G1
!r    r1   c                       \ rS rSrSrS rSrg)UniqueNamesProverg   zO
This is a prover decorator that adds unique names assumptions before
proving.
c                    U R                   R                  5       n[        [        U R                   R	                  5       U5      5      n[        5       nU HY  n[        U[        5      (       d  M  UR                  R                  nUR                  R                  nX5   R                  U5        M[     / n[        U5       H  u  pX(S-   S  Hp  n	XU   ;  d  M  [        [        U5      [        U	5      5      n
[        5       R                  X5      (       a  X4   R                  U	5        M^  UR!                  U
* 5        Mr     M     X-   $ )z
- Domain = union([e.free()|e.constants() for e in all_expressions])
- if "d1 = d2" cannot be proven from the premises, then add "d1 != d2"
   N)r4   r-   listr/   r,   	SetHolderrE   r   rJ   rH   rK   add	enumerater   r   proveappend)r6   r-   r7   eq_setsr'   avbvnew_assumptionsibnewEqExs              r!   r-   UniqueNamesProver.assumptionsm   s   
 mm//1j!3!3!5{CD +A!/00WW%%XX&&#  f%DAEG_AJ&0*1-/A!/DG yw<<  
q) (..x8 % & ,,r    r   N)r   r   r   r   rQ   r-   r   r   r    r!   rS   rS   g   s    
"-r    rS   c                       \ rS rSrSrS rSrg)rX      z
A list of sets of Variables.
c                     [        U[        5      (       d   eU  H  nX;   d  M
  Us  $    U1nU R                  U5        U$ )z>
:param item: ``Variable``
:return: the set containing 'item'
)rE   r   r\   )r6   itemsnews       r!   __getitem__SetHolder.__getitem__   sE    
 $))))Ay  fC
r    r   N)r   r   r   r   rQ   rk   r   r   r    r!   rX   rX      s    r    rX   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
ClosedWorldProver   a=  
This is a prover decorator that completes predicates before proving.

If the assumptions contain "P(A)", then "all x.(P(x) -> (x=A))" is the completion of "P".
If the assumptions contain "all x.(ostrich(x) -> bird(x))", then "all x.(bird(x) -> ostrich(x))" is the completion of "bird".
If the assumptions don't contain anything that are "P", then "all x.-P(x)" is the completion of "P".

walk(Socrates)
Socrates != Bill
+ all x.(walk(x) -> (x=Socrates))
----------------
-walk(Bill)

see(Socrates, John)
see(John, Mary)
Socrates != John
John != Mary
+ all x.all y.(see(x,y) -> ((x=Socrates & y=John) | (x=John & y=Mary)))
----------------
-see(Socrates, Mary)

all x.(ostrich(x) -> bird(x))
bird(Tweety)
-ostrich(Sam)
Sam != Tweety
+ all x.(bird(x) -> (ostrich(x) | x=Tweety))
+ all x.-ostrich(x)
-------------------
-bird(Sam)
c           	      <   U R                   R                  5       nU R                  U5      n/ nU GH`  nX$   nU R                  U5      nU Vs/ s H  n[	        U5      PM     nn/ n	UR
                   HO  n
/ n[        X5       H  u  pUR                  [        X5      5        M!     U	R                  [        S U5      5        MQ     UR                   HC  n0 n[        XS   5       H	  u  pXU'   M     U	R                  US   R                  U5      5        ME     U	(       a+  U R                  XF5      n[        S U	5      n[        UU5      nO[        U R                  XF5      5      nUS S S2    H  n[        UU5      nM     UR                  U5        GMc     X-   $ s  snf )Nc                 
    X-  $ r$   r   r>   s     r!   rA   /ClosedWorldProver.assumptions.<locals>.<lambda>   s    QUr    r   rV   c                 
    X-  $ r$   r   r>   s     r!   rA   rr      s    r    )r4   r-   _make_predicate_dict_make_unique_signaturer   
signatureszipr\   r   r   
propertiessubstitute_bindings_make_antecedentr   r   r
   )r6   r-   
predicatesr`   p
predHoldernew_sigvnew_sig_exsrO   sigequality_exsv1v2propbindings
antecedent
consequentaccumnew_sig_vars                       r!   r-   ClosedWorldProver.assumptions   s   mm//1..{;
A#J11*=G:AB'Q-a0'KBI ",,!!+3FB ''(:2(BC 4  (:L!IJ	 - #--!+Aw7FB#%RL 8  a!<!<X!FG . !221>
#$6	B
%j*= *$*?*?*KL  'tt}%k59  -""5)G J ,,E Cs   Fc                 L    [        S [        UR                  5       5       5      $ )z
This method figures out how many arguments the predicate takes and
returns a tuple containing that number of unique variables.
c              3   6   #    U  H  n[        5       v   M     g 7fr$   )r   )r&   ra   s     r!   r(   ;ClosedWorldProver._make_unique_signature.<locals>.<genexpr>   s     P0O1_&&0Os   )tuplerangesignature_len)r6   r~   s     r!   rv   (ClosedWorldProver._make_unique_signature   s     
 Pj6N6N0OPPPr    c                 >    UnU H  nU" [        U5      5      nM     U$ )zn
Return an application expression with 'predicate' as the predicate
and 'signature' as the list of arguments.
)r   )r6   	predicate	signaturer   r   s        r!   r{   "ClosedWorldProver._make_antecedent   s)    
 
A#$6q$9:J r    c                 X    [        [        5      nU H  nU R                  X25        M     U$ )z
Create a dictionary of predicates from the assumptions.

:param assumptions: a list of ``Expression``s
:return: dict mapping ``AbstractVariableExpression`` to ``PredHolder``
)r   
PredHolder_map_predicates)r6   r-   r|   r'   s       r!   ru   &ClosedWorldProver._make_predicate_dict  s,     !,
A  / r    c                    [        U[        5      (       aE  UR                  5       u  p4[        U[        5      (       a  X#   R	                  [        U5      5        g g [        U[        5      (       a9  U R                  UR                  U5        U R                  UR                  U5        g [        U[        5      (       Ga  UR                  /nUR                  n[        U[        5      (       a>  UR                  UR                  5        UR                  n[        U[        5      (       a  M>  [        U[        5      (       Ga!  [        UR                  [        5      (       Ga   [        UR                  [        5      (       a  UR                  R                  5       u  pxUR                  R                  5       u  p[        U[        5      (       a  [        U	[        5      (       a|  XX Vs/ s H  oR                  PM     sn:X  a\  XZ Vs/ s H  oR                  PM     sn:X  a<  X)   R                  [        U5      UR                  45        X'   R!                  U5        g g g g g g g g g s  snf s  snf r$   )rE   r   uncurryr	   
append_sigr   r   r   rJ   rK   r
   rH   rF   r\   r   append_propvalidate_sig_len)r6   
expressionpredDictfuncargsr   rF   func1args1func2args2r   s               r!   r   !ClosedWorldProver._map_predicates  s   j"788#++-JD$ :;;))%+6 <
M22  !1!18<  !2!2H=
M22&&'C??DT=11

4==)yy T=11 $..djj*?@@ZKK!6F F $(::#5#5#7LE#';;#6#6#8LE"5*DEE&u.HII#>1JJ#>>#>1JJ#>> 33U3Z4LM 88= ? ? J FF@ / 3  $?#>s   &I$I)r   N)r   r   r   r   rQ   r-   rv   r{   ru   r   r   r   r    r!   rn   rn      s"    >+-ZQ
>r    rn   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)r   i/  as  
This class will be used by a dictionary that will store information
about predicates to be used by the ``ClosedWorldProver``.

The 'signatures' property is a list of tuples defining signatures for
which the predicate is true.  For instance, 'see(john, mary)' would be
result in the signature '(john,mary)' for 'see'.

The second element of the pair is a list of pairs such that the first
element of the pair is a tuple of variables and the second element is an
expression of those variables that makes the predicate true.  For instance,
'all x.all y.(see(x,y) -> know(x,y))' would result in "((x,y),('see(x,y)'))"
for 'know'.
c                 .    / U l         / U l        S U l        g r$   rw   ry   r   r6   s    r!   __init__PredHolder.__init__?  s    !r    c                 \    U R                  U5        U R                  R                  U5        g r$   )r   rw   r\   r6   r   s     r!   r   PredHolder.append_sigD  s"    g&w'r    c                 b    U R                  US   5        U R                  R                  U5        g )Nr   )r   ry   r\   )r6   new_props     r!   r   PredHolder.append_propH  s&    hqk*x(r    c                     U R                   c  [        U5      U l         g U R                   [        U5      :w  a  [        S5      eg )NzSignature lengths do not match)r   len	Exceptionr   s     r!   r   PredHolder.validate_sig_lenL  s=    %!$WD3w</<== 0r    c                 V    SU R                    SU R                   SU R                   S3$ )N(,)r   r   s    r!   __str__PredHolder.__str__R  s.    4??#1T__$5Qt7I7I6J!LLr    c                     SU -  $ )Nz%sr   r   s    r!   __repr__PredHolder.__repr__U  s    d{r    )ry   r   rw   N)r   r   r   r   rQ   r   r   r   r   r   r   r   r   r    r!   r   r   /  s&    "
()>Mr    r   c                     [         R                  n U " S5      nU " S5      nU " S5      n[        X1U/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S5      nU " S5      nU " S5      nU " S5      n[        X1X'/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S5      nU " S5      nU " S5      nU " S5      n[        X1X'/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S5      nU " S5      nU " S	5      n[        X1U/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S
5      nU " S5      nU " S5      nU " S5      nU " S5      n	U " S5      n[        X1X'X/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        g )Nzexists x.walk(x)man(Socrates)walk(Socrates)assumptions:   goal:-walk(Bill)z
walk(Bill)zall x.walk(x)z
girl(mary)z
dog(rover)zall x.(girl(x) -> -dog(x))zall x.(dog(x) -> -girl(x))zchase(mary, rover)z1exists y.(dog(y) & all x.(girl(x) -> chase(x,y))))r   
fromstringr   printr[   r1   r-   r,   )
lexprp1p2rN   provercdpr'   p3p4p5s
             r!   closed_domain_demor   Y  s   !!E	"	#B		 B AABx(F	&,,.
V
$C	.__eQ 	'388:	#))+	"	#B		 B	~	B AAB|,F	&,,.
V
$C	.__eQ 	'388:	#))+	"	#B		 B	~	B AAB|,F	&,,.
V
$C	.__eQ 	'388:	#))+	 	!B	}	BAABx(F	&,,.
V
$C	.__eQ 	'388:	#))+	}	B	}	B	,	-B	,	-B	$	%BBCAABB34F	&,,.
V
$C	.__eQ 	'388:	#))+r    c                     [         R                  n U " S5      nU " S5      nU " S5      n[        X1U/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S5      nU " S5      nU " S	5      nU " S
5      n[        X1X'/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        g )Nr   z	man(Bill)zexists x.exists y.(x != y)r   r   r   z!all x.(walk(x) -> (x = Socrates))zBill = WilliamzBill = Billyz-walk(William))r   r   r   r   r[   rS   r-   r,   )r   r   r   rN   r   unpr'   r   s           r!   unique_names_demor     s$   !!E		 B	|	B+,AABx(F	&,,.
F
#C	.__eQ 	'388:	#))+	3	4B	 	!B		B AAB|,F	&,,.
F
#C	.__eQ 	'388:	#))+r    c                  d   [         R                  n U " S5      nU " S5      nU " S5      n[        X1U/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S5      nU " S5      nU " S	5      nU " S
5      nU " S5      n[        X1X'U/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        U " S5      nU " S5      nU " S5      nU " S5      nU " S5      n[        X1X'U/5      n[        UR	                  5       5        [        U5      n[        S5        UR                  5        H  n[        SU5        M     [        SUR                  5       5        [        UR	                  5       5        g )Nr   z(Socrates != Bill)r   r   r   r   see(Socrates, John)see(John, Mary)z(Socrates != John)z(John != Mary)-see(Socrates, Mary)zall x.(ostrich(x) -> bird(x))zbird(Tweety)z-ostrich(Sam)zSam != Tweetyz
-bird(Sam))r   r   r   r   r[   rn   r-   r,   )	r   r   r   rN   r   cwpr'   r   r   s	            r!   closed_world_demor     s   !!E	 	!B	$	%BnAABx(F	&,,.
F
#C	.__eQ 	'388:	#))+	%	&B	!	"B	$	%B	 	!B%&AABB/0F	&,,.
F
#C	.__eQ 	'388:	#))+	/	0B		B		 B		 BmAABB/0F	&,,.
F
#C	.__eQ 	'388:	#))+r    c                  P   [         R                  n U " S5      nU " S5      nU " S5      n[        X1U/5      n[        UR	                  5       5        [        [        [        U5      5      5      nUR                  5        H  n[        U5        M     [        UR	                  5       5        g )Nr   r   r   )	r   r   r   r   r[   r1   rS   rn   r-   )r   r   r   rN   r   commandr'   s          r!   combination_prover_demor     s    !!E	%	&B	!	"B%&AABx(F	&,,. !23DV3L!MNG  "a #	'--/r    c                  x   [         R                  n / nUR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S	5      5        UR                  U " S
5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        UR                  U " S5      5        [        S U5      n[	        [        U5      5      nUR                  5        H  n[        U5        M     [        SU5        [        SU5        [        SU5        g )Nz'all x.(elephant(x)        -> animal(x))z'all x.(bird(x)            -> animal(x))z%all x.(dove(x)            -> bird(x))z%all x.(ostrich(x)         -> bird(x))z(all x.(flying_ostrich(x)  -> ostrich(x))z)all x.((animal(x)  & -Ab1(x)) -> -fly(x))z(all x.((bird(x)    & -Ab2(x)) -> fly(x))z)all x.((ostrich(x) & -Ab3(x)) -> -fly(x))z#all x.(bird(x)           -> Ab1(x))z#all x.(ostrich(x)        -> Ab2(x))z#all x.(flying_ostrich(x) -> Ab3(x))zelephant(E)zdove(D)z
ostrich(O)z-fly(E)zfly(D)z-fly(O))	r   r   r\   r   rS   rn   r-   r   print_proof)r   premisesr   r   r'   s        r!   default_reasoning_demor     sx   !!EH OOEDEFOOEDEFOOEBCDOOEBCDOOEEFG OO:; OO9: OO:;
 OOE@ABOOE@ABOOE@AB OOE.)*OOE*%&OOE-() D(+F 1& 9:G  "a # 	8$(#	8$r    c                     [         R                  n[        U" U 5      U5      n[        [	        U5      5      n[        XR                  5       UR                  5       5        g r$   )r   r   r   rS   rn   r   r[   )r,   r   r   r   r   s        r!   r   r   !  sC    !!EE$K2F 1& 9:G	$0r    c                  h    [        5         [        5         [        5         [        5         [	        5         g r$   )r   r   r   r   r   r   r    r!   demor   (  s     r    __main__N)+rQ   collectionsr   	functoolsr   nltk.inference.apir   r   nltk.inference.prover9r   r   nltk.sem.logicr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r/   r1   rS   rW   rX   rn   r   r   r   r   r   r   r   r   r   r   r    r!   <module>r      s    $  = :   $	y 	Q2/ 2j(-. (-V (F>. F>R' 'TBJ:)X)%X1 zF r    