ó <¿CVc@s2dZddlmZddlZddlZddlmZddlmZm Z ddl m Z m Z ddl mZmZdee fd „ƒYZd ee fd „ƒYZd d „Zd„Zd„Zd„Zd„Zd„ZdddgfdddgfgZd„Zedkr.eƒndS(sA A model builder that makes use of the external 'Mace4' package. iÿÿÿÿ(tprint_functionN(t is_indvar(t Valuationt Expression(t ModelBuildertBaseModelBuilderCommand(tProver9CommandParentt Prover9Parentt MaceCommandcBseZdZd Zd d dd d„Zed„ƒZd„Ze d„ƒZ e d„ƒZ e d„ƒZ d„Z d „Zged „ZRS( s¸ 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. iôcCsJ|dk r$t|tƒs0t‚n t|ƒ}tj||||ƒdS(s• :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(tNonet isinstancetMacetAssertionErrorRt__init__(tselftgoalt assumptionst max_modelst model_builder((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR s  cCs |jdƒS(Nt valuation(tmodel(tmbc((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR2sc Cs[|j|dƒ}g}x6|jtƒD]%}|jƒ}|jdƒr{t||jdƒd|jdƒ!jƒƒ}q(|jdƒr1|jdƒdkr1||jdƒd|jdƒ!jƒ}t|ƒrã|j ƒ}nt||jd ƒd|jd ƒ!jƒƒ}|j |t j |ƒfƒq(|jd ƒr(||jdƒd}d|kré||jdƒ jƒ}g||jd ƒd|jd ƒ!j dƒD]} t| jƒƒ^q©} |j |t j|| ƒfƒqM||jdƒ jƒ}t||jd ƒd|jd ƒ!jƒƒ}|j ||dkfƒq(q(Wt|ƒS( s¦ Transform the output file into an NLTK-style Valuation. :return: A model if one is generated; None otherwise. :rtype: sem.Valuation tstandardtinterpretationt(it,tfunctiont_iÿÿÿÿt[t]trelation(t_transform_outputt splitlinestFalsetstript startswithtinttindextfindRtuppertappendRt_make_model_vartsplitt_make_relation_setR( Rt valuation_strtvaluation_standard_formattvaltlinetlt num_entitiestnametvaluetvtvalues((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyt _convert2val5s, 2$) / K"/ cCsktƒ}x[gt|ƒD]\}}|dkr|^qD](}|jttj|||ƒƒƒq;W|S(s] 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 i(tsett enumeratetaddttupleRt_make_relation_tuple(R1R5trtposR4tposition((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR+]s 8&cCs€t|ƒdkrgSt|ƒ|}||}t||ƒ}||||d|!}tj|ƒgtj|||ƒSdS(Ni(tlenR$RR)R;(R>R5R1t sublist_sizet sublist_starttsublist_positiontsublist((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR;ls  cCs€ddddddddd d d d d dddddddddddddg|}|d}|dkr||t|ƒS|S(s³ 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 tatbtctdtetftgthtitjtkR0tmtntotptqR<tstttuR4twtxtytzii(tstr(R3tlettertnum((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR){s*. cCs7|s |S|dkr#|j|ƒS|j||ƒSdS(s^ Print out a Mace4 model using any Mace4 ``interpformat`` format. See http://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 RN(R6R(RR,tformat((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyt_decorate_modelˆs   c Cs3|d kr#|j||gƒd Std ƒ‚d S( sª Transform the output file into any Mace4 ``interpformat`` format. :param format: Output format for displaying models. :type format: str Rt standard2tportablettabulartrawtcookedtxmlttexis#The specified format does not existN(sstandardR`RaRbsrawRdsxmlRf(t_call_interpformatt LookupError(RR,R^((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR™s cCsF|jdkr*|jjd|ƒ|_n|jj||j||ƒS(s 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`` t interpformatN(t_interpformat_binR t _modelbuildert _find_binaryt_call(Rt input_strtargstverbose((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyRg¦s  N(t__name__t __module__t__doc__R RjR tpropertyRR6t staticmethodR+R;R)R_RR!Rg(((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyRs (  R cBs;eZdZdd„Zdded„Zged„ZRS(iôcCs ||_dS(N(t _end_size(Rtend_size((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR ºs cCsF|sg}n|j|j||ƒd|ƒ\}}|dk|fS(s  Use Mace4 to build a first order model. :return: ``True`` if a model was found (i.e. Mace returns value of 0), else ``False`` Rpi(t _call_mace4t prover9_input(RRRRptstdoutt returncode((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyt _build_model¿s  cCss|jdkr'|jd|ƒ|_nd}|jdkrP|d|j7}n||7}|j||j||ƒS(s  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`` tmace4tisassign(end_size, %d). N(t _mace4_binR RlRvRm(RRnRoRptupdated_input_str((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyRxÍs  N(RqRrR RR R!R|Rx(((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyR ·s icCstd|ƒdS(Nt-(tprint(R]((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pytspaceráscCsidt6dt6dd6|S(sq Decode the result of model_found() :param found: The output of model_found() :type found: bool sCountermodel foundsNo countermodel foundR N(tTrueR!R (tfound((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyt decode_resultäscCs©x¢|D]š\}}tj|ƒ}g|D]}tj|ƒ^q)}t|d|ddƒ}|jƒ}x|D]}td|ƒqoWtd|t|ƒfƒqWdS(s2 Try some proofs and exhibit the results. RRi2s %ss |- %s: %s N(Rt fromstringtlptparseRt build_modelR‚R†(t argumentsRRRJRDtalistROR…((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyttest_model_foundís"  cCsâtjdƒ}gddddddgD]}tj|ƒ^q(}t|d|ƒ}|jƒtƒtd ƒtƒx|D]}td |ƒq~Wtd |t|jƒƒfƒtƒtd ƒtƒt|jd ƒdS(s0 Try to build a ``nltk.sem.Valuation``. s all x.man(x)s man(John)s man(Socrates)s man(Bill)s,some x.(-(x = John) & man(x) & sees(John,x))ssome x.(-(x = Bill) & man(x))s,all x.some y.(man(x) -> gives(Socrates,x,y))RsAssumptions and Goals %ss |- %s: %s Rs N(RR‡RRŠRƒR‚R†R(R‹RJRDRŒRO((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyttest_build_modelûs&"     cCsátj|dƒ}g|dD]}tj|ƒ^q}t|d|ƒ}|jƒx|D]}td|ƒq\Wtd||jƒfƒxLdddd gD]8}tƒtd |ƒtƒt|jd |ƒƒq¡Wd S( sJ Transform the model into various Mace4 ``interpformat`` formats. iiRs %ss |- %s: %s RRaReRdsUsing '%s' formatR^N( RR‡RˆR‰RRŠR‚RƒR(t argument_pairRJRDRŒROR^((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyttest_transform_outputs&  cCsÇttjddddddgƒtd d gƒkƒttjddddddddddddg ƒtd gƒkƒttjdddddddddddgƒtd dgƒkƒdS(NR1iR5iiRFRDiRE(RF(RD(RFRD(RDRERD(RERERD(R‚RR+R7(((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyttest_make_relation_set's7Fsmortal(Socrates)sall x.(man(x) -> mortal(x))s man(Socrates)s(not mortal(Socrates))cCs&ttƒttƒttdƒdS(Ni(RR‹RŽR(((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pytdemo1s  t__main__(Rst __future__Rtosttempfiletnltk.sem.logicRtnltk.semRRtnltk.inference.apiRRtnltk.inference.prover9RRRR RƒR†RRŽRR‘R‹R’Rq(((se/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/mace.pyt s(  Ÿ*