
    /hR.                         S r SSKrSSKrSSKJrJr  SSKJrJr  SSK	J
r
Jr  SSKJr   " S S\\5      r " S	 S
\\5      rSS jrS rS rS rS rS rSSS/4SSS/4/rS r\S:X  a  \" 5         gg)zA
A model builder that makes use of the external 'Mace4' package.
    N)BaseModelBuilderCommandModelBuilder)Prover9CommandParentProver9Parent)
Expression	Valuation)	is_indvarc                       \ rS rSrSrSrSS jr\S 5       rS r	\
S 5       r\
S 5       r\
S	 5       rS
 rS r/ S4S jrSrg)MaceCommand   z
A ``MaceCommand`` specific to the ``Mace`` model builder.  It contains
a print_assumptions() method that is used to print the list
of assumptions in multiple formats.
Nc                 ~    Ub  [        U[        5      (       d   eO[        U5      n[        R                  " XX5        g)aM  
:param goal: Input expression to prove
:type goal: sem.Expression
:param assumptions: Input expressions to use as assumptions in
    the proof.
:type assumptions: list(sem.Expression)
:param max_models: The maximum number of models that Mace will try before
    simply returning false. (Use 0 for no maximum.)
:type max_models: int
N)
isinstanceMacer   __init__)selfgoalassumptions
max_modelsmodel_builders        K/var/www/auris/envauris/lib/python3.13/site-packages/nltk/inference/mace.pyr   MaceCommand.__init__   s7     $mT2222 ,M((dP    c                 $    U R                  S5      $ )N	valuation)model)mbcs    r   r   MaceCommand.valuation1   s    yy%%r   c                    U R                  US5      n/ nUR                  S5       GH  nUR                  5       nUR                  S5      (       a>  [	        XUR                  S5      S-   UR                  S5       R                  5       5      nMh  UR                  S5      (       a  UR                  S5      S	:X  a  XUR                  S5      S-   UR                  S5       R                  5       n[        U5      (       a  UR                  5       n[	        XUR                  S
5      S-   UR                  S5       R                  5       5      nUR                  U[        R                  U5      45        GMK  UR                  S5      (       d  GMd  XUR                  S5      S-   S nSU;   a  USUR                  S5       R                  5       nXUR                  S
5      S-   UR                  S5       R                  S5       V	s/ s H  n	[	        U	R                  5       5      PM     n
n	UR                  U[        R                  WU
5      45        GM'  USUR                  S5       R                  5       n[	        XUR                  S
5      S-   UR                  S5       R                  5       5      nUR                  XxS:H  45        GM     [        U5      $ s  sn	f )z
Transform the output file into an NLTK-style Valuation.

:return: A model if one is generated; None otherwise.
:rtype: sem.Valuation
standardFinterpretation(   ,function_[]relationN)_transform_output
splitlinesstrip
startswithintindexfindr	   upperappendr   _make_model_varsplit_make_relation_setr   )r   valuation_strvaluation_standard_formatvallinelnum_entitiesnamevaluevvaluess              r   _convert2valMaceCommand._convert2val5   sM    %)$:$:=*$U!-88?D

A||,--"1WWS\A%5#E#K#K#MNj))affSkR.?)AGGCL9??AT??::<DAggclQ.>DDFG

D+"="=e"DEFj))ggclQ&()!8^qwws|,224D "#773<!#3aggcl!C!I!I#!N!NA AGGI!N   JJ{==lFST
 ^qwws|,224D''#,"2QWWS\ B H H JKEJJqj12? @B ~s   >#Kc           
          [        5       n[        U5       VVs/ s H  u  p4US:X  d  M  UPM     snn H1  nUR                  [        [        R                  XQU 5      5      5        M3     U$ s  snnf )a-  
Convert a Mace4-style relation table into a dictionary.

:param num_entities: the number of entities in the model; determines the row length in the table.
:type num_entities: int
:param values: a list of 1's and 0's that represent whether a relation holds in a Mace4 model.
:type values: list of int
r"   )set	enumerateaddtupler   _make_relation_tuple)r;   r?   rposr>   positions         r   r5   MaceCommand._make_relation_setb   sb     E-6v->I->#!q&->IHEEk66xVW J 	 Js
   A*A*c                     [        U5      S:X  a  / $ [        U5      U-  nX-  n[        X-  5      nUXC-  US-   U-   n[        R                  U5      /[        R	                  XVU5      -   $ Nr"   )lenr.   r   r3   rG   )rJ   r?   r;   sublist_sizesublist_startsublist_positionsublists          r   rG    MaceCommand._make_relation_tuples   s    v;!Iv;,6L$4M"8#:;,0A\/QG ++M:00 < r   c                 F    / SQU    nU S-  nUS:  a  U[        U5      -   $ U$ )z
Pick an alphabetic character as identifier for an entity in the model.

:param value: where to index into the list of characters
:type value: int
)abcdefghijkr:   mnopqrH   stur>   wxyz   r   )str)r=   letternums      r   r3   MaceCommand._make_model_var   s8    
6 78 rk$'!GvC 77r   c                 d    U(       d  U$ US:X  a  U R                  U5      $ U R                  X5      $ )a'  
Print out a Mace4 model using any Mace4 ``interpformat`` format.
See https://www.cs.unm.edu/~mccune/mace4/manual/ for details.

:param valuation_str: str with the model builder's output
:param format: str indicating the format for displaying
models. Defaults to 'standard' format.
:return: str
r   )r@   r*   r   r6   formats      r   _decorate_modelMaceCommand._decorate_model   s7       {"$$]33))-@@r   c                 N    US;   a  U R                  X/5      S   $ [        S5      e)z
Transform the output file into any Mace4 ``interpformat`` format.

:param format: Output format for displaying models.
:type format: str
)r   	standard2portabletabularrawcookedxmltexr   z#The specified format does not exist)_call_interpformatLookupErrorrr   s      r   r*   MaceCommand._transform_output   s7      	
 	
 **=(CAFFCDDr   Fc                     U R                   c!  U R                  R                  SU5      U l         U R                  R                  XR                   X#5      $ )z
Call the ``interpformat`` binary with the given input.

:param input_str: A string whose contents are used as stdin.
:param args: A list of command-line arguments.
:return: A tuple (stdout, returncode)
:see: ``config_prover9``
interpformat)_interpformat_bin_modelbuilder_find_binary_call)r   	input_strargsverboses       r   r~   MaceCommand._call_interpformat   sT     !!)%)%7%7%D%D&D" !!''--t
 	
r   )r   )NN  N)__name__
__module____qualname____firstlineno____doc__r   r   propertyr   r@   staticmethodr5   rG   r3   rt   r*   r~   __static_attributes__ r   r   r   r      s     Q$ & &+Z     " $8 $8LA"E* 24U 
r   r   c                   :    \ rS rSrSrSS jrS	S jr/ S4S jrSrg)
r      Nc                     Xl         g )N)	_end_size)r   end_sizes     r   r   Mace.__init__   s    !	?r   Fc                 d    U(       d  / nU R                  U R                  X5      US9u  pEUS:H  U4$ )z
Use Mace4 to build a first order model.

:return: ``True`` if a model was found (i.e. Mace returns value of 0),
else ``False``
)r   r   )_call_mace4prover9_input)r   r   r   r   stdout
returncodes         r   _build_modelMace._build_model   sE     K!--t17 . 
 a((r   c                     U R                   c  U R                  SU5      U l         SnU R                  S:  a  USU R                  -  -  nXA-  nU R                  X@R                   X#5      $ )z
Call the ``mace4`` binary with the given input.

:param input_str: A string whose contents are used as stdin.
:param args: A list of command-line arguments.
:return: A tuple (stdout, returncode)
:see: ``config_prover9``
mace4 r   zassign(end_size, %d).

)
_mace4_binr   r   r   )r   r   r   r   updated_input_strs        r   r   Mace._call_mace4   sh     ??""//ADO>>A!<t~~!MM&zz+__dLLr   )r   r   )r   )NNF)	r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    J?
) +-e Mr   r   c                      [        SU -  5        g )N-)print)ro   s    r   spacerr     s    	#)r   c                     SSSS.U    $ )za
Decode the result of model_found()

:param found: The output of model_found()
:type found: bool
zCountermodel foundzNo countermodel foundNone)TFNr   )founds    r   decode_resultr     s     '/FfU r   c           	      2   U  H  u  p[         R                  " U5      nU Vs/ s H  n[        R                  U5      PM     nn[	        X5SS9nUR                  5       nU H  n[        SU-  5        M     [        SU S[        U5       S35        M     gs  snf )z*
Try some proofs and exhibit the results.
2   )r   r      %s|- : 
N)r   
fromstringlpparser   build_modelr   r   )	argumentsr   r   r[   rU   alistr`   r   s           r   test_model_foundr      s     '!!$'&12k!k2<A'A+ A3bu-.b12 '2s   Bc           	         [         R                  " S5      nS Vs/ s H  n[         R                  " U5      PM     nn[        XS9nUR                  5         [	        5         [        S5        [	        5         U H  n[        SU-  5        M     [        SU S[        UR                  5       5       S35        [	        5         [        S	5        [	        5         [        UR                  S5        g
s  snf )z(
Try to build a ``nltk.sem.Valuation``.
zall x.man(x))z	man(John)man(Socrates)z	man(Bill)z,some x.(-(x = John) & man(x) & sees(John,x))zsome x.(-(x = Bill) & man(x))z,all x.some y.(man(x) -> gives(Socrates,x,y))r   zAssumptions and Goalr   r   r   r   r   N)r   r   r   r   r   r   r   r   )r   r[   rU   r   r`   s        r   test_build_modelr   .  s     	n-A


A 	a 
 
 
 	A)AMMO
H	
 !
Hgk 	Cs"]1==?34B
78
H 
+
H	!++t3
s    C+c                    [         R                  " U S   5      nU S    Vs/ s H  n[        R                  U5      PM     nn[	        XS9nUR                  5         U H  n[        SU-  5        M     [        SU SUR                  5        S35        S H=  n[        5         [        S	U-  5        [        5         [        UR                  US
95        M?     gs  snf )zB
Transform the model into various Mace4 ``interpformat`` formats.
r   r"   r   r   r   r   r   )r   rx   r|   r{   zUsing '%s' format)rs   N)	r   r   r   r   r   r   r   r   r   )argument_pairr[   rU   r   r`   rs   s         r   test_transform_outputr   O  s     	mA./A"/"23"2QRXXa["2E3A)AMMOgk 	Cs"Q]]_%R
();!F*+aggVg$%	 < 4s   Cc                      [        [        R                  S/ SQS9SS1:H  5        [        [        R                  S/ SQS9S1:H  5        [        [        R                  S/ S	QS9S
S1:H  5        g )N   )r"   r   r"   )r;   r?   )rW   )rU   )	r   r   r   r   r   r   r"   r   r   )rW   rU      )r   r   r"   r   r   r   r"   r   )rU   rV   rU   )rV   rV   rU   )r   r   r5   r   r   r   test_make_relation_setr   a  s    	&&Ai&HF	 
&&#> 	' 	
 <	 
&&A>V&W_-	.r   zmortal(Socrates)zall x.(man(x) -> mortal(x))r   z(not mortal(Socrates))c                  d    [        [        5        [        [        5        [        [        S   5        g rM   )r   r   r   r   r   r   r   demor   x  s    YY)A,'r   __main__)   )r   ostempfilenltk.inference.apir   r   nltk.inference.prover9r   r   nltk.semr   r   nltk.sem.logicr	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r      s    
  D F * $L
&(? L
^(M=, (MV	3B&$$ 7IJ =OP	( zF r   