ó
    ´OÂfR.  ã                   óæ   • S r SSKrSSKrSSKJrJr  SSKJrJr  SSK	J
r
Jr  SSKJr   " S S\\5      r " S	 S
\\5      rSS jrS rS rS rS rS rSSS/4SSS/4/rS r\S:X  a  \" 5         gg)zA
A model builder that makes use of the external 'Mace4' package.
é    N)ÚBaseModelBuilderCommandÚModelBuilder)ÚProver9CommandParentÚProver9Parent)Ú
ExpressionÚ	Valuation)Ú	is_indvarc                   ó†   • \ rS rSrSrSrSS jr\S 5       rS r	\
S 5       r\
S 5       r\
S	 5       rS
 rS r/ S4S jrSrg)ÚMaceCommandé   z¨
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                 ó~   • Ub  [        U[        5      (       d   eO[        U5      n[        R                  " XX5        g)aM  
: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        Ú5/usr/lib/python3/dist-packages/nltk/inference/mace.pyr   ÚMaceCommand.__init__   s7   € ð Ñ$Ü˜m¬T×2Ñ2Ð2Ñ2ä  Ó,ˆMä×(Ò(¨¸dÕPó    c                 ó$   • U R                  S5      $ )NÚ	valuation)Úmodel)Úmbcs    r   r   ÚMaceCommand.valuation1   s   € à�y‰y˜Ó%Ð%r   c                 óª  • U R                  US5      n/ nUR                  S5       GHš  nUR                  5       nUR                  S5      (       a>  [	        XUR                  S5      S-   UR                  S5       R                  5       5      nMh  UR                  S5      (       aÍ  UR                  S5      S	:X  a¸  XUR                  S5      S-   UR                  S5       R                  5       n[        U5      (       a  UR                  5       n[	        XUR                  S
5      S-   UR                  S5       R                  5       5      nUR                  U[        R                  U5      45        GMK  UR                  S5      (       d  GMd  XUR                  S5      S-   S nSU;   a§  USUR                  S5       R                  5       nXUR                  S
5      S-   UR                  S5       R                  S5       V	s/ s H  n	[	        U	R                  5       5      PM     n
n	UR                  U[        R                  WU
5      45        GM'  USUR                  S5       R                  5       n[	        XUR                  S
5      S-   UR                  S5       R                  5       5      nUR                  XxS:H  45        GM�     [        U5      $ s  sn	f )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   Ú_convert2valÚMaceCommand._convert2val5   sM  € ð %)×$:Ñ$:¸=È*Ó$UÐ!àˆØ-×8Ñ8¸×?ˆDØ—
‘
“ˆAà�|‰|Ð,×-Ñ-ä" 1§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Ÿg™g c›l¨QÑ.°·±¸³Ð>×DÑDÓFÓG�Ø—
‘
˜D¤+×"=Ñ"=¸eÓ"DÐE×Fà—‘˜j×)Ô)Ø—g‘g˜c“l QÑ&Ð(Ð)�Ø˜!“8à˜^˜qŸw™w s›|Ð,×2Ñ2Ó4�Dð "#§7¡7¨3£<°!Ñ#3°a·g±g¸c³lÐ!C×!IÑ!IÈ#Ô!Nóâ!N˜Aô ˜AŸG™G›IžÙ!Nð ð ð —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 ¨q¡jÐ1×2ñ? @ôB ˜‹~Ðùòs   Ç>#Kc           
      óà   • [        5       n[        U5       VVs/ s H  u  p4US:X  d  M  UPM     snn H1  nUR                  [        [        R                  XQU 5      5      5        M3     U$ s  snnf )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)r;   r?   ÚrÚposr>   Úpositions         r   r5   ÚMaceCommand._make_relation_setb   sb   € ô ‹EˆÜ-6°vÔ->ÔIÒ->¡ #À!ÀqÁ&ŸÑ->ÕIˆHØ�E‰EÜ”k×6Ñ6°xÈÓVÓWöñ Jð ˆùó	 Js
   ™A*©A*c                 óÖ   • [        U5      S:X  a  / $ [        U5      U-  nX-  n[        X-  5      nUXC-  US-   U-   n[        R                  U5      /[        R	                  XVU5      -   $ ©Nr"   )Úlenr.   r   r3   rG   )rJ   r?   r;   Úsublist_sizeÚsublist_startÚsublist_positionÚsublists          r   rG   Ú MaceCommand._make_relation_tuples   s…   € äˆv‹;˜!ÓØˆIä˜v›;¨,Ñ6ˆLØ$Ñ4ˆMÜ" 8Ñ#:Ó;ÐàØÑ,°ÀÑ0AÀ\Ñ/QðˆGô ×+Ñ+¨MÓ:ðä×0Ñ0Ø ¨<óñð r   c                 óF   • / SQU    nU S-  nUS:”  a  U[        U5      -   $ U$ )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Úkr:   ÚmÚnÚoÚpÚqrH   ÚsÚtÚur>   ÚwÚxÚyÚzé   r   )Ústr)r=   ÚletterÚnums      r   r3   ÚMaceCommand._make_model_var…   s8   € ò
ð6 ñ7ˆð8 �r‰kˆØ$'¨!£Gˆvœ˜C›Ñ Ð7°Ð7r   c                 ód   • U(       d  U$ US:X  a  U R                  U5      $ U R                  X5      $ )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   r6   Úformats      r   Ú_decorate_modelÚMaceCommand._decorate_model¬   s7   € ö Ø Ð Ø�{Ó"Ø×$Ñ$ ]Ó3Ð3à×)Ñ)¨-Ó@Ð@r   c                 óN   • US;   a  U R                  X/5      S   $ [        S5      e)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ÚLookupErrorrr   s      r   r*   ÚMaceCommand._transform_output½   s7   € ð ð 	
ó 	
ð ×*Ñ*¨=¸(ÓCÀAÑFÐFäÐCÓDÐDr   Fc                 óª   • U R                   c!  U R                  R                  SU5      U l         U R                  R                  XR                   X#5      $ )zä
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   r~   ÚMaceCommand._call_interpformatÒ   sT   € ð ×!Ñ!Ñ)Ø%)×%7Ñ%7×%DÑ%DØ ó&ˆDÔ"ð ×!Ñ!×'Ñ'Ø×-Ñ-¨tó
ð 	
r   )rƒ   )NNéô  N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rƒ   r   Úpropertyr   r@   Ústaticmethodr5   rG   r3   rt   r*   r~   Ú__static_attributes__© r   r   r   r      s„   † ñð ÐôQð$ ñ&ó ð&ò+ðZ ñó ðð  ñó ðð" ñ$8ó ð$8òLAò"Eð* 24¸U÷ 
r   r   c                   ó:   • \ rS rSrSrSS jrS	S jr/ S4S jrSrg)
r   éå   Nc                 ó   • Xl         g )N)Ú	_end_size)r   Úend_sizes     r   r   ÚMace.__init__è   s   € Ø!Œð	?r   Fc                 ód   • U(       d  / nU R                  U R                  X5      US9u  pEUS:H  U4$ )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_modelÚMace._build_modelí   sE   € ö ØˆKà!×-Ñ-Ø×Ñ˜tÓ1¸7ð .ð 
Ñˆð ˜a‘ Ð(Ð(r   c                 óÒ   • U R                   c  U R                  SU5      U l         SnU R                  S:”  a  USU R                  -  -  nXA-  nU R                  X@R                   X#5      $ )zÝ
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œ   ÚMace._call_mace4ü   sh   € ð �?‰?Ñ"Ø"×/Ñ/°¸ÓAˆDŒOàÐØ�>‰>˜AÓØÐ!<¸t¿~¹~Ñ!MÑMÐØÑ&Ðà�z‰zÐ+¯_©_¸dÓLÐLr   )r˜   r¥   )r‹   )NNF)	rŒ   r�   rŽ   r�   r¥   r   r    rœ   r“   r”   r   r   r   r   å   s   † Ø€Jô?ô
)ð +-°e÷ Mr   r   c                 ó    • [        SU -  5        g )NÚ-)Úprint)ro   s    r   Úspacerr«     s   € Ü	ˆ#�‰)Õr   c                 ó   • SSSS.U    $ )za
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           	      ó2  • U  HŒ  u  p[         R                  " U5      nU Vs/ s H  n[        R                  U5      PM     nn[	        X5SS9nUR                  5       nU H  n[        SU-  5        M     [        SU S[        U5       S35        MŽ     gs  snf )z*
Try some proofs and exhibit the results.
é2   )r   r   ú   %sú|- ú: Ú
N)r   Ú
fromstringÚlpÚparser   Úbuild_modelrª   r¯   )Ú	argumentsr   r   r[   rU   Úalistr`   r®   s           r   Útest_model_foundr¼      sŠ   € ó 'ÑˆÜ×!Ò! $Ó'ˆÙ&1Ó2¢k ”—‘˜!–¡kˆÐ2Ü˜¸Ñ<ˆØ—‘“ˆÛˆAÜ�'˜A‘+Öñ ä��A�3�bœ uÓ-Ð.¨bÐ1Ö2ò 'ùâ2s   ¢Bc           	      óà  • [         R                  " S5      nS Vs/ s H  n[         R                  " U5      PM     nn[        XS9nUR                  5         [	        5         [        S5        [	        5         U H  n[        SU-  5        M     [        SU S[        UR                  5       5       S35        [	        5         [        S	5        [	        5         [        UR                  S5        g
s  snf )z(
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º   r[   rU   r»   r`   s        r   Útest_build_modelrÀ   .  sÍ   € ô 	×Ò˜nÓ-€Añ
ó
ò
ˆAô 	×Ò˜aÖ ñ
ð 
ð 
ô 	�AÑ)€AØ‡M�M„OÜ
„HÜ	Ð
 Ô!Ü
„HÛˆÜˆg˜‰kÖñ ä	ˆC�ˆs�"”] 1§=¡=£?Ó3Ð4°BÐ
7Ô8Ü
„Hô 
ˆ+ÔÜ
„HÜ	ˆ!�+‰+�tÕùò3
s   › C+c                 ó¶  • [         R                  " U S   5      nU S    Vs/ s H  n[        R                  U5      PM     nn[	        XS9nUR                  5         U H  n[        SU-  5        M     [        SU SUR                  5        S35        S H=  n[        5         [        S	U-  5        [        5         [        UR                  US
95        M?     gs  snf )zB
Transform the model into various Mace4 ``interpformat`` formats.
r   r"   r¿   r²   r³   r´   rµ   )r   rx   r|   r{   zUsing '%s' format)rs   N)	r   r¶   r·   r¸   r   r¹   rª   r«   r   )Úargument_pairr[   rU   r»   r`   rs   s         r   Útest_transform_outputrÃ   O  s½   € ô 	×Ò˜m¨AÑ.Ó/€AØ"/°Ò"2Ó3Ò"2˜QŒR�X‰X�aŽ[Ñ"2€EÐ3Ü�AÑ)€AØ‡M�M„OÛˆÜˆg˜‰kÖñ ä	ˆC�ˆs�"�Q—]‘]“_Ð% RÐ
(Ô)Û;ˆÜŒÜÐ! FÑ*Ô+ÜŒÜˆa�g‰g˜VˆgÐ$Ö%ò	 <ùò 4s   ¡Cc                  óÚ   • [        [        R                  S/ SQS9SS1:H  5        [        [        R                  S/ SQS9S1:H  5        [        [        R                  S/ S	QS9S
S1:H  5        g )Né   )r"   r   r"   )r;   r?   )rW   )rU   )	r   r   r   r   r   r   r"   r   r   )rW   rU   é   )r   r   r"   r   r   r   r"   r   )rU   rV   rU   )rV   rV   rU   )rª   r   r5   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   • [        [        5        [        [        5        [        [        S   5        g rM   )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   