a
     ˜ëjR.  ã                   @   sØ   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„ deeƒZG d	d
„ d
eeƒZddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdddgfdddgfgZdd„ ZedkrÔeƒ  dS ) zA
A model builder that makes use of the external 'Mace4' package.
é    N)ÚBaseModelBuilderCommandÚModelBuilder)ÚProver9CommandParentÚProver9Parent)Ú
ExpressionÚ	Valuation)Ú	is_indvarc                   @   st   e Zd ZdZdZddd„Zedd„ ƒZdd	„ Ze	d
d„ ƒZ
e	dd„ ƒZe	dd„ ƒZdd„ Zdd„ Zg dfd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                 C   s4   |durt |tƒs J ‚nt|ƒ}t | |||¡ 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_modelsZmodel_builder© r   úR/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/inference/mace.pyr      s    zMaceCommand.__init__c                 C   s
   |   d¡S )NÚ	valuation)Úmodel)Úmbcr   r   r   r   1   s    zMaceCommand.valuationc           
      C   s´  |   |d¡}g }| d¡D �]Ž}| ¡ }| d¡rXt|| d¡d | d¡…  ¡ ƒ}q| d¡rÚ| d¡d	krÚ|| d¡d | d¡…  ¡ }t|ƒr | ¡ }t|| d
¡d | d¡…  ¡ ƒ}| 	|t
 |¡f¡ q| d¡r|| d¡d d… }d|v �r^|d| d¡…  ¡ }dd„ || d
¡d | d¡…  d¡D ƒ}	| 	|t
 ||	¡f¡ q|d| d¡…  ¡ }t|| d
¡d | d¡…  ¡ ƒ}| 	||dkf¡ qt|ƒS )z¦
        Transform the output file into an NLTK-style Valuation.

        :return: A model if one is generated; None otherwise.
        :rtype: sem.Valuation
        ÚstandardFZinterpretationú(é   ú,ÚfunctionÚ_éÿÿÿÿú[ú]ZrelationNc                 S   s   g | ]}t | ¡ ƒ‘qS r   )ÚintÚstrip)Ú.0Úvr   r   r   Ú
<listcomp>S   s   ÿz,MaceCommand._convert2val.<locals>.<listcomp>)Ú_transform_outputÚ
splitlinesr!   Ú
startswithr    ÚindexÚfindr   ÚupperÚappendr	   Ú_make_model_varÚsplitÚ_make_relation_setr   )
r   Úvaluation_strZvaluation_standard_formatÚvalÚlineÚlÚnum_entitiesÚnameÚvalueÚvaluesr   r   r   Ú_convert2val5   s4    
& $

 þÿ$zMaceCommand._convert2valc              	   C   s:   t ƒ }dd„ t|ƒD ƒD ]}| tt ||| ¡ƒ¡ q|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                 S   s   g | ]\}}|d kr|‘qS )r   r   )r"   Úposr#   r   r   r   r$   m   ó    z2MaceCommand._make_relation_set.<locals>.<listcomp>)ÚsetÚ	enumerateÚaddÚtupler	   Ú_make_relation_tuple)r3   r6   ÚrÚpositionr   r   r   r.   b   s    
ÿzMaceCommand._make_relation_setc                 C   sf   t |ƒdkrg S t |ƒ| }| | }t| | ƒ}||| |d | … }t |¡gt |||¡ S d S ©Nr   )Úlenr    r	   r,   r>   )r@   r6   r3   Zsublist_sizeZsublist_startZsublist_positionZsublistr   r   r   r>   s   s    ÿÿÿþz MaceCommand._make_relation_tuplec                 C   s,   g d¢|  }| d }|dkr(|t |ƒ S |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Úkr2   ÚmÚnÚoÚpÚqr?   ÚsÚtÚur#   ÚwÚxÚyÚzé   r   )Ústr)r5   ÚletterÚnumr   r   r   r,   …   s
    åzMaceCommand._make_model_varc                 C   s*   |s|S |dkr|   |¡S |  ||¡S d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   N)r7   r%   ©r   r/   Úformatr   r   r   Ú_decorate_model¬   s
    

zMaceCommand._decorate_modelc                 C   s&   |dv r|   ||g¡d S tdƒ‚dS )zª
        Transform the output file into any Mace4 ``interpformat`` format.

        :param format: Output format for displaying models.
        :type format: str
        )r   Z	standard2ÚportableZtabularÚrawÚcookedÚxmlZtexr   z#The specified format does not existN)Ú_call_interpformatÚLookupErrorr^   r   r   r   r%   ½   s    
zMaceCommand._transform_outputFc                 C   s.   | j du r| 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``
        NZinterpformat)Ú_interpformat_binZ_modelbuilderÚ_find_binaryÚ_call)r   Ú	input_strÚargsÚverboser   r   r   re   Ò   s    	
ÿ
ÿzMaceCommand._call_interpformat)NNr
   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rg   r   Úpropertyr   r7   Ústaticmethodr.   r>   r,   r`   r%   re   r   r   r   r   r	      s   

-


&r	   c                   @   s2   e Zd ZdZd
dd„Zddd„Zg dfdd	„ZdS )r   Nr
   c                 C   s
   || _ d S )N)Ú	_end_size)r   Zend_sizer   r   r   r   è   s    zMace.__init__Fc                 C   s.   |sg }| j |  ||¡|d�\}}|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``
        )rl   r   )Ú_call_mace4Zprover9_input)r   r   r   rl   ÚstdoutÚ
returncoder   r   r   Ú_build_modelí   s    ÿ
zMace._build_modelc                 C   sN   | j du r|  d|¡| _ d}| jdkr4|d| j 7 }||7 }|  || 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``
        NZmace4Ú r   zassign(end_size, %d).

)Ú
_mace4_binrh   rs   ri   )r   rj   rk   rl   Zupdated_input_strr   r   r   rt   ü   s    	

zMace._call_mace4)r
   )NNF)rm   rn   ro   ry   r   rw   rt   r   r   r   r   r   å   s   

r   é   c                 C   s   t d|  ƒ d S )Nú-)Úprint)r]   r   r   r   Úspacer  s    r}   c                 C   s   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   )Úfoundr   r   r   Údecode_result  s    
ÿr€   c                 C   sp   | D ]f\}}t  |¡}dd„ |D ƒ}t||dd�}| ¡ }|D ]}td| ƒ q>td|› dt|ƒ› d�ƒ qd	S )
z2
    Try some proofs and exhibit the results.
    c                 S   s   g | ]}t  |¡‘qS r   ©ÚlpÚparse©r"   rC   r   r   r   r$   &  r9   z$test_model_found.<locals>.<listcomp>é2   )r   r   ú   %sú|- ú: Ú
N)r   Ú
fromstringr	   Úbuild_modelr|   r€   )Ú	argumentsr   r   rI   ÚalistrN   r   rC   r   r   r   Útest_model_found   s    
rŽ   c                 C   s˜   t  d¡}dd„ dD ƒ}t||d�}| ¡  tƒ  tdƒ tƒ  |D ]}td| ƒ qDt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                 S   s   g | ]}t  |¡‘qS r   )r   rŠ   r„   r   r   r   r$   3  s   ÿz$test_build_model.<locals>.<listcomp>)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Œ   rI   r�   rN   rC   r   r   r   Útest_build_model.  s     
þr‘   c                 C   sš   t  | d ¡}dd„ | d D ƒ}t||d�}| ¡  |D ]}td| ƒ q8td|› d| ¡ › d	�ƒ d
D ],}tƒ  td| ƒ tƒ  t|j|d�ƒ qhdS )zJ
    Transform the model into various Mace4 ``interpformat`` formats.
    r   c                 S   s   g | ]}t  |¡‘qS r   r�   r„   r   r   r   r$   T  r9   z)test_transform_output.<locals>.<listcomp>r   r�   r†   r‡   rˆ   r‰   )r   ra   rd   rc   zUsing '%s' format)r_   N)r   rŠ   r	   r‹   r|   r}   r   )Zargument_pairrI   r�   rN   rC   r_   r   r   r   Útest_transform_outputO  s    r’   c                   C   s\   t tjdg d¢d�ddhkƒ t tjdg d¢d�dhkƒ t tjdg d	¢d�d
dhkƒ d S )Né   )r   r   r   )r3   r6   )rE   )rC   )	r   r   r   r   r   r   r   r   r   )rE   rC   é   )r   r   r   r   r   r   r   r   )rC   rD   rC   )rD   rD   rC   )r|   r	   r.   r   r   r   r   Útest_make_relation_seta  s"    ÿÿÿýÿÿÿr•   zmortal(Socrates)zall x.(man(x) -> mortal(x))r�   z(not mortal(Socrates))c                   C   s    t tƒ ttƒ ttd ƒ d S rA   )rŽ   rŒ   r‘   r’   r   r   r   r   Údemox  s    r–   Ú__main__)rz   )rp   ÚosÚtempfileZnltk.inference.apir   r   Znltk.inference.prover9r   r   Znltk.semr   r   Znltk.sem.logicr   r	   r   r}   r€   rŽ   r‘   r’   r•   rŒ   r–   rm   r   r   r   r   Ú<module>	   s*    P+
!

þ