o
    iR.                     @   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_modelsmodel_builder r   N/var/www/edux/Edux_v2/venv/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
        standardFinterpretation(   ,function_[]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_str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)r7   r:   rpositionr   r   r   r1   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/   rA   )rC   r:   r7   sublist_sizesublist_startsublist_positionsublistr   r   r   rA   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kr6   mnopqrB   stur&   wxyz   r   )str)r9   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   )r;   r(   r   r2   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   	standard2portabletabularrawcookedxmltexr   z#The specified format does not exist)_call_interpformatLookupErrorre   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``
        Ninterpformat)_interpformat_bin_modelbuilder_find_binary_call)r   	input_strargsverboser   r   r   ro      s   
	
zMaceCommand._call_interpformat)NNr
   N)__name__
__module____qualname____doc__rr   r   propertyr   r;   staticmethodr1   rA   r/   rg   r(   ro   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   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``
        )rx   r   )_call_mace4prover9_input)r   r   r   rx   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``
        Nmace4 r   zassign(end_size, %d).

)
_mace4_binrt   r   ru   )r   rv   rw   rx   updated_input_strr   r   r   r      s   
	
zMace._call_mace4)r
   )NNF)ry   rz   r{   r   r   r   r   r   r   r   r   r      s
    

r      c                 C   s   t d|   d S )N-)print)rd   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   lpparser%   rJ   r   r   r   r'   &      z$test_model_found.<locals>.<listcomp>2   )r   r      %s|- : 
N)r   
fromstringr	   build_modelr   r   )	argumentsr   r   rP   alistrU   r   rJ   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   rP   r   rU   rJ   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   ri   rm   rl   zUsing '%s' format)rf   N)r   r   r	   r   r   r   r   )argument_pairrP   r   rU   rJ   rf   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   )r7   r:   )rL   )rJ   )	r   r   r   r   r   r   r   r   r   )rL   rJ      )r   r   r   r   r   r   r   r   )rJ   rK   rJ   )rK   rK   rJ   )r   r	   r1   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 rD   )r   r   r   r   r   r   r   r   demox  s   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   ry   r   r   r   r   <module>   s.    P
+!


