Ë
    çÍ:jR.  ã                   óâ   — 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/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/nltk/inference/mace.pyr   zMaceCommand.__init__   s8   € ð Ð$Ü˜m¬TÔ2Ð2Ð2ä  Ó,ˆMä×(Ñ(¨¨}¸dÀKÕPó    c                 ó$   — | j                  d«      S )NÚ	valuation)Úmodel)Úmbcs    r   r   zMaceCommand.valuation1   s   € à�y‰y˜Ó%Ð%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   sQ  € ð %)×$:Ñ$:¸=È*Ó$UÐ!àˆØ-×8Ñ8¸Ó?ó 	3ˆDØ—
‘
“ˆAà�|‰|Ð,Ô-ä" 1 Q§W¡W¨S£\°AÑ%5¸¿¹À»Ð#E×#KÑ#KÓ#MÓN‘à—‘˜jÔ)¨a¯f©f°S«k¸RÒ.?à˜Ÿ™ ›¨Ñ)¨A¯G©G°C«LÐ9×?Ñ?ÓA�Ü˜T”?ØŸ:™:›<�DÜ˜A˜aŸg™g c›l¨QÑ.°·±¸³Ð>×DÑDÓFÓG�Ø—
‘
˜D¤+×"=Ñ"=¸eÓ"DÐEÖFà—‘˜jÖ)Ø�a—g‘g˜c“l QÑ&Ð(Ð)�Ø˜!‘8à˜^˜qŸw™w s›|Ð,×2Ñ2Ó4�Dð "# 1§7¡7¨3£<°!Ñ#3°a·g±g¸c³lÐ!C×!IÑ!IÈ#Ó!Nöàô ˜AŸG™G›I�ð�Fð ð —J‘JØœ{×=Ñ=¸lÈFÓSÐTöð
 ˜^˜qŸw™w s›|Ð,×2Ñ2Ó4�DÜ  !§'¡'¨#£,°Ñ"2°Q·W±W¸S³\Ð B× HÑ HÓ JÓK�EØ—J‘J  e¨q¡jÐ1Ö2ð?	3ô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   sd   € ô ‹EˆÜ-6°vÓ->×I¡ # qÀ!ÀqÃ&šÓIò 	ˆHØ�E‰EÜ”k×6Ñ6°xÀÈÓVÓWõð	ð ˆùó	 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›;¨,Ñ6ˆLØ$¨Ñ4ˆMÜ" 8¨lÑ#:Ó;ÐàØ Ñ,°ÀÑ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 �r‰kˆØ$'¨!¢Gˆvœ˜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¸(ÓCÀAÑ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×-Ñ-¨t°Wó
ð 	
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ó ð$8òLAò"Eð* 24¸Uô 
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 [Ó1¸7ð .ó 
Ñˆ�
ð ˜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   € ð �?‰?Ð"Ø"×/Ñ/°¸ÓAˆDŒOàÐØ�>‰>˜AÒØÐ!<¸t¿~¹~Ñ!MÑMÐØ˜YÑ&Ðà�z‰zÐ+¨T¯_©_¸dÀGÓ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   € ð 'Ð/FÈfÑ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—   € ð 'ò 3ÑˆˆkÜ×!Ñ! $Ó'ˆØ&1Ö2 ”—‘˜!•Ð2ˆÐ2Ü˜ u¸Ô<ˆØ—‘“ˆØò 	ˆAÜ�'˜A‘+Õð	ä��A�3�bœ uÓ-Ð.¨bÐ1Õ2ñ3ùâ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Õ ð
€Eð 
ô 	�A 5Ô)€AØ‡M�M„OÜ
„HÜ	Ð
 Ô!Ü
„HØò ˆÜˆg˜‰kÕðä	ˆC�ˆs�"”] 1§=¡=£?Ó3Ð4°BÐ
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Æ   € ô 	×Ñ˜m¨AÑ.Ó/€AØ"/°Ñ"2Ö3˜QŒR�X‰X�a�[Ð3€EÐ3Ü�A 5Ô)€AØ‡M�M„OØò ˆÜˆg˜‰kÕðä	ˆC�ˆs�"�Q—]‘]“_Ð% RÐ
(Ô)Ø;ò &ˆÜŒÜÐ! FÑ*Ô+ÜŒÜˆa�g‰g˜VˆgÓ$Õ%ñ	&ùò 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„   € Ü	Ü×&Ñ&°AºiÐ&Ó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   