B
    P?ð[”.  ã               @   sä   d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	 ddl
mZmZ ddlmZ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.
é    )Úprint_functionN)Ú	is_indvar)Ú	ValuationÚ
Expression)ÚModelBuilderÚBaseModelBuilderCommand)ÚProver9CommandParentÚProver9Parentc               @   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k	rt |tƒs t‚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ÚMaceÚAssertionErrorr   Ú__init__)ÚselfÚgoalÚassumptionsÚ
max_modelsZmodel_builder© r   ú2lib/python3.7/site-packages/nltk/inference/mace.pyr   !   s    zMaceCommand.__init__c             C   s
   |   d¡S )NÚ	valuation)Úmodel)Zmbcr   r   r   r   3   s    zMaceCommand.valuationc       
      C   sº  |   |d¡}g }xž| d¡D ]Ž}| ¡ }| d¡r\t|| 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|krb|d| d¡…  ¡ }dd„ || d
¡d | d¡…  d¡D ƒ}	| 	|t
 ||	¡f¡ q|d| d¡…  ¡ }t|| d
¡d | d¡…  ¡ ƒ}| 	||dkf¡ qW 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ú(é   ú,ZfunctionÚ_éÿÿÿÿú[ú]ZrelationNc             S   s   g | ]}t | ¡ ƒ‘qS r   )ÚintÚstrip)Ú.0Úvr   r   r   ú
<listcomp>V   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   Ú_convert2val7   s0    
& $

&$zMaceCommand._convert2valc          	   C   s>   t ƒ }x2dd„ t|ƒD ƒD ]}| tt ||| ¡ƒ¡ qW |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$   o   s    z2MaceCommand._make_relation_set.<locals>.<listcomp>)ÚsetÚ	enumerateÚaddÚtupler
   Ú_make_relation_tuple)r3   r6   ÚrÚpositionr   r   r   r.   d   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=   u   s    
z MaceCommand._make_relation_tuplec             C   s\   ddddddddd	d
ddddddddddddddddg|  }| d }|dkrX|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   Z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 http://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k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ÚLookupError)r   r/   r[   r   r   r   r%   ¿   s    
zMaceCommand._transform_outputFc             C   s.   | j dk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   ra   Ô   s
    	

zMaceCommand._call_interpformat)NNr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rc   r   Úpropertyr   r7   Ústaticmethodr.   r=   r,   r\   r%   ra   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   Néô  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``
        )rh   r   )Ú_call_mace4Zprover9_input)r   r   r   rh   ÚstdoutÚ
returncoder   r   r   Ú_build_modelï   s
    zMace._build_modelc             C   sN   | j dk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_binrd   rp   re   )r   rf   rg   rh   Zupdated_input_strr   r   r   rq   þ   s    	

zMace._call_mace4)ro   )NNF)ri   rj   rk   rv   r   rt   rq   r   r   r   r   r   ç   s   

r   é   c             C   s   t d|  ƒ d S )Nú-)Úprint)rZ   r   r   r   Úspacer  s    rz   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   sr   xl| D ]d\}}t  |¡}dd„ |D ƒ}t||dd}| ¡ }x|D ]}td| ƒ qBW td|t|ƒf ƒ qW dS )z2
    Try some proofs and exhibit the results.
    c             S   s   g | ]}t  |¡‘qS r   )ÚlpÚparse)r"   rA   r   r   r   r$   (  s    z$test_model_found.<locals>.<listcomp>é2   )r   r   z   %sz
|- %s: %s
N)r   Ú
fromstringr
   Úbuild_modelry   r}   )Ú	argumentsr   r   rG   ÚalistrL   r|   rA   r   r   r   Útest_model_found"  s    

r…   c             C   s–   t  d¡}dd„ dD ƒ}t||d}| ¡  tƒ  tdƒ tƒ  x|D ]}td| ƒ qFW td|t| ¡ ƒf ƒ 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"   rA   r   r   r   r$   6  s   z$test_build_model.<locals>.<listcomp>)z	man(John)z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 Goalz   %sz
|- %s: %s
r   Ú
N)r   r   r
   r‚   rz   ry   r}   r   )rƒ   rG   r„   rL   rA   r   r   r   Útest_build_model0  s    


r‡   c             C   sœ   t  | d ¡}dd„ | d D ƒ}t||d}| ¡  x|D ]}td| ƒ q:W td|| ¡ f ƒ x4dD ],}tƒ  td	| ƒ tƒ  t|j|d
ƒ qhW dS )zJ
    Transform the model into various Mace4 ``interpformat`` formats.
    r   c             S   s   g | ]}t  |¡‘qS r   )r~   r   )r"   rA   r   r   r   r$   V  s    z)test_transform_output.<locals>.<listcomp>r   )r   z   %sz
|- %s: %s
)r   r]   r`   r_   zUsing '%s' format)r[   N)r   r   r
   r‚   ry   rz   r   )Zargument_pairrG   r„   rL   rA   r[   r   r   r   Útest_transform_outputQ  s    

rˆ   c               C   s„   t tjddddgdtddgƒkƒ t tjddddddddddg	dtdgƒkƒ t tjdddddddddgdtd	d
gƒkƒ d S )Né   r   r   )r3   r6   )rC   )rA   )rC   rA   é   )rA   rB   rA   )rB   rB   rA   )ry   r
   r.   r9   r   r   r   r   Útest_make_relation_setc  s    r‹   zmortal(Socrates)zall x.(man(x) -> mortal(x))zman(Socrates)z(not mortal(Socrates))c               C   s    t tƒ ttƒ ttd ƒ d S )Nr   )r…   rƒ   r‡   rˆ   r   r   r   r   Údemoz  s    rŒ   Ú__main__)rw   )rl   Z
__future__r   ÚosZtempfileZnltk.sem.logicr   Znltk.semr   r   Znltk.inference.apir   r   Znltk.inference.prover9r   r	   r
   r   rz   r}   r…   r‡   rˆ   r‹   rƒ   rŒ   ri   r   r   r   r   Ú<module>   s*    P+
!
