
    gR.                         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        yy)zA
A model builder that makes use of the external 'Mace4' package.
    N)BaseModelBuilderCommandModelBuilder)Prover9CommandParentProver9Parent)
Expression	Valuation)	is_indvarc                   z    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y)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.
    Nc                 r    |t        |t              sJ t        |      }t        j                  | |||       y)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        H/var/www/openai/venv/lib/python3.12/site-packages/nltk/inference/mace.pyr   zMaceCommand.__init__   s8     $mT222 ,M((}dKP    c                 $    | j                  d      S )N	valuation)model)mbcs    r   r   zMaceCommand.valuation1   s    yy%%r   c                 x   | j                  |d      }g }|j                  d      D ]  }|j                         }|j                  d      r>t	        ||j                  d      dz   |j                  d       j                               }c|j                  d      r|j                  d      d	k(  r||j                  d      dz   |j                  d       j                         }t        |      r|j                         }t	        ||j                  d
      dz   |j                  d       j                               }|j                  |t        j                  |      f       <|j                  d      sO||j                  d      dz   d }d|v r|d|j                  d       j                         }||j                  d
      dz   |j                  d       j                  d      D 	cg c]  }	t	        |	j                                }
}	|j                  |t        j                  |
      f       |d|j                  d       j                         }t	        ||j                  d
      dz   |j                  d       j                               }|j                  ||dk(  f        t        |      S c c}	w )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_[]relationN)_transform_output
splitlinesstrip
startswithintindexfindr	   upperappendr   _make_model_varsplit_make_relation_setr   )r   valuation_strvaluation_standard_formatvallinelnum_entitiesnamevaluevvaluess              r   _convert2valzMaceCommand._convert2val5   sP    %)$:$:=*$U!-88?D

A||,-"1QWWS\A%5#E#K#K#MNj)affSkR.?)AGGCL9??AT?::<DAaggclQ.>DDFG

D+"="=e"DEFj)aggclQ&()!8^qwws|,224D "#1773<!#3aggcl!C!I!I#!N!NA AGGI!N   JJ{==lFST
 ^qwws|,224D!''#,"2QWWS\ B H H JKEJJeqj12? @B ~s   ) J7c           
          t               }t        |      D cg c]  \  }}|dk(  s| c}}D ]1  }|j                  t        t        j                  |||                    3 |S c c}}w )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
        r   )set	enumerateaddtupler   _make_relation_tuple)r8   r<   rposr;   positions         r   r2   zMaceCommand._make_relation_setb   sf     E-6v->I->#q!q&->IHEEk66xVW J 	 Js
   A%A%c                     t        |      dk(  rg S t        |      |z  }| |z  }t        | |z        }|||z  |dz   |z   }t        j                  |      gt        j	                  |||      z   S Nr   )lenr+   r   r0   rC   )rF   r<   r8   sublist_sizesublist_startsublist_positionsublists          r   rC   z MaceCommand._make_relation_tuples   s    v;!Iv;,6L$4M"8l#:;,0A\/QG ++M:00 '< r   c                 D    g d|    }| dz  }|dkD  r|t        |      z   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kr7   mnopqrD   stur;   wxyz   r   )str)r:   letternums      r   r0   zMaceCommand._make_model_var   s8    
6 78 rk$'!GvC 77r   c                 Z    |s|S |dk(  r| j                  |      S | j                  ||      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   r3   formats      r   _decorate_modelzMaceCommand._decorate_model   s9       {"$$]33))-@@r   c                 L    |dv r| j                  ||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LookupErrorrk   s      r   r'   zMaceCommand._transform_output   s9      	
 	
 **=6(CAFFCDDr   Fc                     | j                   !| j                  j                  d|      | _         | j                  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``
        interpformat)_interpformat_bin_modelbuilder_find_binary_call)r   	input_strargsverboses       r   rv   zMaceCommand._call_interpformat   sX     !!)%)%7%7%D%D&D" !!''t--tW
 	
r   )NN  N)__name__
__module____qualname____doc__rz   r   propertyr   r=   staticmethodr2   rC   r0   rm   r'   rv    r   r   r   r      s     Q$ & &+Z     " $8 $8LA"E* 24U 
r   r   c                   ,    e Zd ZdZddZddZg dfdZy)r   Nc                     || _         y )N)	_end_size)r   end_sizes     r   r   zMace.__init__   s    !	?r   Fc                 d    |sg }| j                  | j                  ||      |      \  }}|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   sI     K!--t[17 . 

 a((r   c                     | j                   | j                  d|      | _         d}| j                  dkD  r|d| j                  z  z  }||z  }| j                  || 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``
        mace4 r   zassign(end_size, %d).

)
_mace4_binr|   r   r}   )r   r~   r   r   updated_input_strs        r   r   zMace._call_mace4   sn     ??""//ADO>>A!<t~~!MMY&zz+T__dGLLr   )r   )NNF)r   r   r   r   r   r   r   r   r   r   r   r      s    J?
) +-e Mr   r   c                      t        d| z         y )N-)print)ri   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 r   c           	      ,   | D ]  \  }}t        j                  |      }|D cg c]  }t        j                  |       }}t	        ||d      }|j                         }|D ]  }t        d|z          t        d| dt        |       d        yc c}w )z2
    Try some proofs and exhibit the results.
    2   )r   r      %s|- : 
N)r   
fromstringlpparser   build_modelr   r   )	argumentsr   r   rU   rO   alistrZ   r   s           r   test_model_foundr      s     'k!!$'&12k!k2u<A'A+ A3bu-.b12 '2s   Bc           	         t        j                  d      }dD cg c]  }t        j                  |       }}t        ||      }|j                          t	                t        d       t	                |D ]  }t        d|z          t        d| dt        |j                                d       t	                t        d	       t	                t        |j                  d       y
c c}w )z0
    Try to build a ``nltk.sem.Valuation``.
    zall x.man(x))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   rU   rO   r   rZ   s        r   test_build_modelr   .  s     	n-A


A 	a 
 
 
 	A5)AMMO
H	
 !
Hgk 	Cs"]1==?34B
78
H 
+
H	!++t3
s   C(c                    t        j                  | d         }| d   D cg c]  }t        j                  |       }}t	        ||      }|j                          |D ]  }t        d|z          t        d| d|j                          d       dD ]?  }t                t        d	|z         t                t        |j                  |
             A yc c}w )zJ
    Transform the model into various Mace4 ``interpformat`` formats.
    r   r   r   r   r   r   r   )r   rp   rt   rs   zUsing '%s' format)rl   N)	r   r   r   r   r   r   r   r   r   )argument_pairrU   rO   r   rZ   rl   s         r   test_transform_outputr   O  s     	mA./A"/"23"2QRXXa["2E3A5)AMMOgk 	Cs"Q]]_%R
();!F*+aggVg$%	 < 4s   Cc                      t        t        j                  dg d      ddhk(         t        t        j                  dg d      dhk(         t        t        j                  dg d	      d
dhk(         y )N   )r   r   r   )r8   r<   )rQ   )rO   )	r   r   r   r   r   r   r   r   r   )rQ   rO      )r   r   r   r   r   r   r   r   )rO   rP   rO   )rP   rP   rO   )r   r   r2   r   r   r   test_make_relation_setr   a  s    	&&Ai&HF	 
&&#> 	' 	
 <	 
&&A>V&W_-	.r   zmortal(Socrates)zall x.(man(x) -> mortal(x))r   z(not mortal(Socrates))c                  d    t        t               t        t               t        t        d          y rH   )r   r   r   r   r   r   r   demor   x  s    YY)A,'r   __main__)   )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 F * $L
&(? L
^(M=, (MV	3B&$$ 7IJ =OP	( zF r   