o
    rZhR.                     @   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rle  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                   @   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   B/var/www/auris/lib/python3.10/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r+t||dd |d  }q|drl|dd	krl||dd |d  }t|rO| }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   s6   
& $
 $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   s    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   sb   t |dkrg S t || }| | }t| | }||| |d |  }t|gt||| 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 )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   )r7   r%   r   r/   formatr   r   r   _decorate_model   s
   

zMaceCommand._decorate_modelc                 C   s"   |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   Z	standard2portableZtabularrawcookedxmltexr   z#The specified format does not exist)_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r|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 ]3\}}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      g | ]}t |qS r   Zlpparser"   rB   r   r   r   r$   &      z$test_model_found.<locals>.<listcomp>2   )r   r      %s|- : 
N)r   
fromstringr	   build_modelr|   r   )	argumentsr   r   rH   alistrM   r   rB   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|  q"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                 S   r   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   rH   r   rM   rB   r   r   r   test_build_model.  s    
r   c                 C   s   t | d }dd | d D }t||d}|  |D ]}td|  qtd| d|  d	 d
D ]}t  td|  t  t|j|d q4dS )zJ
    Transform the model into various Mace4 ``interpformat`` formats.
    r   c                 S   r   r   r   r   r   r   r   r$   T  r   z)test_transform_output.<locals>.<listcomp>r   r   r   r   r   r   )r   r`   rc   rb   zUsing '%s' format)r^   N)r   r   r	   r   r|   r}   r   )Zargument_pairrH   r   rM   rB   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   )rD   )rB   )	r   r   r   r   r   r   r   r   r   )rD   rB      )r   r   r   r   r   r   r   r   )rB   rC   rB   )rC   rC   rB   )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 r@   )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
+!


