
    Qd/                         d Z ddlZddlZddlmZmZ ddlmZmZ ddl	m
Z
mZ ddlmZ  G d dee          Z G d	 d
ee          ZddZd Zd Zd Zd Zd ZdddgfdddgfgZd Zedk    r e             dS dS )zA
A model builder that makes use of the external 'Mace4' package.
    N)BaseModelBuilderCommandModelBuilder)Prover9CommandParentProver9Parent)
Expression	Valuation)	is_indvarc                       e Zd ZdZdZddZed             Zd Ze	d             Z
e	d             Ze	d	             Zd
 Zd Zg dfdZdS )MaceCommandz
    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.
    N  c                     |t          |t                    sJ nt          |          }t          j        | |||           dS )a  
        :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        3lib/python3.11/site-packages/nltk/inference/mace.pyr   zMaceCommand.__init__   sM     $mT222222 ,,M(}dKPPPPP    c                 ,    |                      d          S )N	valuation)model)mbcs    r   r   zMaceCommand.valuation1   s    yy%%%r   c                    |                      |d          }g }|                    d          D ] }|                                }|                    d          rSt	          ||                    d          dz   |                    d                                                             }|                    d          r|                    d          d	k    r||                    d          dz   |                    d                                                   }t          |          r|                                }t	          ||                    d
          dz   |                    d                                                             }|	                    |t                              |          f           |                    d          rq||                    d          dz   d         }d|v r|d|                    d                                                   }d ||                    d
          dz   |                    d                                       d          D             }	|	                    |t                              ||	          f           |d|                    d                                                   }t	          ||                    d
          dz   |                    d                                                             }|	                    ||dk    f           "t          |          S )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_[]relationNc                 P    g | ]#}t          |                                          $S  )intstrip).0vs     r   
<listcomp>z,MaceCommand._convert2val.<locals>.<listcomp>S   s6        AGGII  r   )_transform_output
splitlinesr+   
startswithr*   indexfindr	   upperappendr   _make_model_varsplit_make_relation_setr   )
r   valuation_strvaluation_standard_formatvallinelnum_entitiesnamevaluevaluess
             r   _convert2valzMaceCommand._convert2val5   s    %)$:$:=*$U$U!-88?? 	3 	3D

A||,-- 3"1QWWS\\A%5%D#E#K#K#M#MNNj)) 3affSkkR.?.?)AGGCLL89??AAT?? (::<<DAaggcllQ.=>DDFFGG

D+"="=e"D"DEFFFFj)) 3aggcllQ&(()!88^qwws||^,2244D !"1773<<!#3aggcll#B!C!I!I#!N!N  F JJ{==lFSST   
 ^qwws||^,2244D!''#,,"2QWWS\\"A B H H J JKKEJJeqj1222~~r   c           
          t                      }d t          |          D             D ]>}|                    t          t                              |||                                ?|S )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
        c                 $    g | ]\  }}|d k    |S )r    r)   )r,   posr-   s      r   r.   z2MaceCommand._make_relation_set.<locals>.<listcomp>m   s!    III#q!q&&&&&r   )set	enumerateaddtupler   _make_relation_tuple)r>   rA   rpositions       r   r8   zMaceCommand._make_relation_setb   sq     EEIIYv->->III 	 	HEEk66xVVWW    r   c                    t          |          dk    rg S t          |          |z  }| |z  }t          | |z            }|||z  |dz   |z           }t                              |          gt                              |||          z   S Nr    )lenr*   r   r6   rJ   )rL   rA   r>   sublist_sizesublist_startsublist_positionsublists          r   rJ   z MaceCommand._make_relation_tuples   s    v;;!Iv;;,6L$4M"8l#:;;,0A\/QQG ++M::00 '<  r   c                 T    g d|          }| dz  }|dk    r|t          |          z   n|S )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qrK   stur-   wxyz   r   )str)r@   letternums      r   r6   zMaceCommand._make_model_var   sF    
 
 
6 78 rk$'!GGvC  7r   c                 l    |s|S |dk    r|                      |          S |                     ||          S )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   )rB   r/   r   r9   formats      r   _decorate_modelzMaceCommand._decorate_model   sH      	A  {""$$]333))-@@@r   c                 b    |dv r|                      ||g          d         S t          d          )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LookupErrorrq   s      r   r/   zMaceCommand._transform_output   sD      	
 	
 	
 **=6(CCAFFCDDDr   Fc                     | j          | j                            d|          | _         | j                            || j         ||          S )a  
        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``
        Ninterpformat)_interpformat_bin_modelbuilder_find_binary_call)r   	input_strargsverboses       r   r|   zMaceCommand._call_interpformat   sV     !)%)%7%D%D& &D" !''t-tW
 
 	
r   )NNr   N)__name__
__module____qualname____doc__r   r   propertyr   rB   staticmethodr8   rJ   r6   rs   r/   r|   r)   r   r   r   r      s          Q Q Q Q$ & & X&+ + +Z   \    \" $8 $8 \$8LA A A"E E E* 24U 
 
 
 
 
 
r   r   c                   .    e Zd ZdZddZddZg dfdZdS )	r   Nr   c                     || _         d S )N)	_end_size)r   end_sizes     r   r   zMace.__init__   s    !	? 	?r   Fc                 v    |sg }|                      |                     ||          |          \  }}|dk    |fS )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_modelzMace._build_model   sV      	K!--t[117 . 
 

 a((r   c                     | j         |                     d|          | _         d}| j        dk    r|d| j        z  z  }||z  }|                     || j         ||          S )a  
        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``
        Nmace4 r   zassign(end_size, %d).

)
_mace4_binr   r   r   )r   r   r   r   updated_input_strs        r   r   zMace._call_mace4   so     ?""//AADO>A!<t~!MMY&zz+T_dGLLLr   )r   )NNF)r   r   r   r   r   r   r   r)   r   r   r   r      sb        J? ? ? ?
) ) ) ) +-e M M M M M Mr   r      c                 *    t          d| z             d S )N-)print)ro   s    r   spacerr     s    	#)r   c                     dddd|          S )zq
    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U r   c           	         | D ]\  }}t          j        |          }d |D             }t          ||d          }|                                }|D ]}t	          d|z             t	          d| dt          |           d           dS )	z2
    Try some proofs and exhibit the results.
    c                 B    g | ]}t                               |          S r)   lpparser,   rU   s     r   r.   z$test_model_found.<locals>.<listcomp>&  s"    222!222r   2   )r   r      %s|- : 
N)r   
fromstringr   build_modelr   r   )	argumentsr   r   r[   alistr`   r   rU   s           r   test_model_foundr      s      ) 3 3{!$''22k222u<<< 	 	A'A+1A11u--11122223 3r   c           	         t          j        d          }d dD             }t          ||          }|                                 t	                       t          d           t	                       |D ]}t          d|z             t          d| dt          |                                           d	           t	                       t          d
           t	                       t          |j        d	           dS )z0
    Try to build a ``nltk.sem.Valuation``.
    zall x.man(x)c                 6    g | ]}t          j        |          S r)   )r   r   r   s     r   r.   z$test_build_model.<locals>.<listcomp>3  s3     
 
 
 	a  
 
 
r   )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[   r   r`   rU   s        r   test_build_modelr   .  s    	n--A
 


 
 
E 	A5)))AMMOOO
HHH	
 !!!
HHH  gk	
7
7
7]1==??33
7
7
7888
HHH 
+
HHH	!+tr   c                    t          j        | d                   }d | d         D             }t          ||          }|                                 |D ]}t	          d|z             t	          d| d|                                 d           d	D ]S}t                       t	          d
|z             t                       t	          |                    |                     TdS )zJ
    Transform the model into various Mace4 ``interpformat`` formats.
    r   c                 B    g | ]}t                               |          S r)   r   r   s     r   r.   z)test_transform_output.<locals>.<listcomp>T  s"    333QRXXa[[333r   r    r   r   r   r   r   )r   rv   rz   ry   zUsing '%s' format)rr   N)r   r   r   r   r   r   r   )argument_pairr[   r   r`   rU   rr   s         r   test_transform_outputr   O  s     	mA.//A33-"2333EA5)))AMMOOO  gk	
(
(
(Q]]__
(
(
())); & &!F*+++aggVg$$%%%%	& &r   c                  *   t          t                              dg d          ddhk               t          t                              dg d          dhk               t          t                              dg d	          d
dhk               d S )N   )r    r   r    )r>   rA   )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   r8   r)   r   r   test_make_relation_setr   a  s    	&&Aiii&HHF	   
&&#>#>#> 	' 	
 	
 <	   
&&A>V>V>V&WW_-	.    r   zmortal(Socrates)zall x.(man(x) -> mortal(x))r   z(not mortal(Socrates))c                      t          t                     t          t                     t          t          d                    d S rN   )r   r   r   r   r)   r   r   demor   x  s6    YY)A,'''''r   __main__)r   )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 D D D D D D D F F F F F F F F * * * * * * * * $ $ $ $ $ $L
 L
 L
 L
 L
&(? L
 L
 L
^(M (M (M (M (M=, (M (M (MV   	 	 	3 3 3  B& & &$  $ 7IJ =OP	( ( ( zDFFFFF r   