
    QdhL                        d Z ddlZddlZddlmZmZ  G d de          Z G d de          Z G d	 d
e          Z G d de          Z	 G d de          Z
 G d de          Z G d dee	          Z G d dee
          Z G d de          Z G d dee	          Z G d dee
          Z G d dee          Z G d dee          Z G d d ej                  ZdS )!a  
Interfaces and base classes for theorem provers and model builders.

``Prover`` is a standard interface for a theorem prover which tries to prove a goal from a
list of assumptions.

``ModelBuilder`` is a standard interface for a model builder. Given just a set of assumptions.
the model builder tries to build a model for the assumptions. Given a set of assumptions and a
goal *G*, the model builder tries to find a counter-model, in the sense of a model that will satisfy
the assumptions plus the negation of *G*.
    N)ABCMetaabstractmethodc                   2    e Zd ZdZddZedd            ZdS )Proverz
    Interface for trying to prove a goal from assumptions.  Both the goal and
    the assumptions are constrained to be formulas of ``logic.Expression``.
    NFc                 <    |                      |||          d         S )zX
        :return: Whether the proof was successful or not.
        :rtype: bool
        r   )_proveselfgoalassumptionsverboses       2lib/python3.11/site-packages/nltk/inference/api.pyprovezProver.prove    s    
 {{4g66q99    c                     dS )z{
        :return: Whether the proof was successful or not, along with the proof
        :rtype: tuple: (bool, str)
        N r	   s       r   r   zProver._prove'         r   NNF)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      sR         
: : : :    ^  r   r   )	metaclassc                   2    e Zd ZdZddZedd            ZdS )ModelBuilderz
    Interface for trying to build a model of set of formulas.
    Open formulas are assumed to be universally quantified.
    Both the goal and the assumptions are constrained to be formulas
    of ``logic.Expression``.
    NFc                 <    |                      |||          d         S )zx
        Perform the actual model building.
        :return: Whether a model was generated
        :rtype: bool
        r   )_build_modelr	   s       r   build_modelzModelBuilder.build_model7   s!       {G<<Q??r   c                     dS )z
        Perform the actual model building.
        :return: Whether a model was generated, and the model itself
        :rtype: tuple(bool, sem.Valuation)
        Nr   r	   s       r   r   zModelBuilder._build_model?   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   /   sV         @ @ @ @    ^  r   r   c                       e Zd ZdZed             Zed	d            Zed             Zed             Zed             Z	dS )
TheoremToolCommandh
    This class holds a goal and a list of assumptions to be used in proving
    or model building.
    c                     dS z
        Add new assumptions to the assumption list.

        :param new_assumptions: new assumptions
        :type new_assumptions: list(sem.Expression)
        Nr   r
   new_assumptionss     r   add_assumptionsz"TheoremToolCommand.add_assumptionsN   r   r   Fc                     dS )-  
        Retract assumptions from the assumption list.

        :param debug: If True, give warning when ``retracted`` is not present on
            assumptions list.
        :type debug: bool
        :param retracted: assumptions to be retracted
        :type retracted: list(sem.Expression)
        Nr   r
   	retracteddebugs      r   retract_assumptionsz&TheoremToolCommand.retract_assumptionsW   r   r   c                     dS )X
        List the current assumptions.

        :return: list of ``Expression``
        Nr   r
   s    r   r   zTheoremToolCommand.assumptionsc   r   r   c                     dS )B
        Return the goal

        :return: ``Expression``
        Nr   r0   s    r   r   zTheoremToolCommand.goalk   r   r   c                     dS z<
        Print the list of the current assumptions.
        Nr   r0   s    r   print_assumptionsz$TheoremToolCommand.print_assumptionss   r   r   NF)
r   r   r   r   r   r'   r-   r   r   r5   r   r   r   r!   r!   H   s         
   ^ 	 	 	 ^	   ^   ^   ^  r   r!   c                   X    e Zd ZdZedd            Zed	d            Zed             ZdS )
ProverCommand
    This class holds a ``Prover``, a goal, and a list of assumptions.  When
    prove() is called, the ``Prover`` is executed with the goal and assumptions.
    Fc                     dS )z+
        Perform the actual proof.
        Nr   r
   r   s     r   r   zProverCommand.prove   r   r   Tc                     dS )p
        Return the proof string
        :param simplify: bool simplify the proof?
        :return: str
        Nr   r
   simplifys     r   proofzProverCommand.proof   r   r   c                     dS )zF
        Return the prover object
        :return: ``Prover``
        Nr   r0   s    r   
get_proverzProverCommand.get_prover   r   r   Nr6   T)r   r   r   r   r   r   r@   rB   r   r   r   r8   r8   z   sy         
    ^
    ^   ^  r   r8   c                   X    e Zd ZdZedd            Zedd            Zed             ZdS )	ModelBuilderCommandz
    This class holds a ``ModelBuilder``, a goal, and a list of assumptions.
    When build_model() is called, the ``ModelBuilder`` is executed with the goal
    and assumptions.
    Fc                     dS )z
        Perform the actual model building.
        :return: A model if one is generated; None otherwise.
        :rtype: sem.Valuation
        Nr   r;   s     r   r   zModelBuilderCommand.build_model   r   r   Nc                     dS )
        Return a string representation of the model

        :param simplify: bool simplify the proof?
        :return: str
        Nr   r
   formats     r   modelzModelBuilderCommand.model   r   r   c                     dS )zS
        Return the model builder object
        :return: ``ModelBuilder``
        Nr   r0   s    r   get_model_builderz%ModelBuilderCommand.get_model_builder   r   r   r6   N)r   r   r   r   r   r   rK   rM   r   r   r   rE   rE      sy             ^    ^   ^  r   rE   c                   :    e Zd ZdZd
dZd ZddZd Zd Zd	 Z	dS )BaseTheoremToolCommandr"   Nc                 ^    || _         |sg | _        nt          |          | _        d| _        dS )z
        :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)
        N)_goal_assumptionslist_result)r
   r   r   s      r   __init__zBaseTheoremToolCommand.__init__   s;     
 	2 "D $[ 1 1DHHr   c                 H    | j                             |           d| _        dS r$   )rS   extendrU   r%   s     r   r'   z&BaseTheoremToolCommand.add_assumptions   s&     	  111r   Fc                    t                    t          t          fd| j                            }|r;|| j        k    r0t	          t          d                     |                                  || _        d| _        dS )r)   c                     | vS rN   r   )ar+   s    r   <lambda>z<BaseTheoremToolCommand.retract_assumptions.<locals>.<lambda>   s    AY,> r   z&Assumptions list has not been changed:N)setrT   filterrS   printWarningr5   rU   )r
   r+   r,   result_lists    `  r   r-   z*BaseTheoremToolCommand.retract_assumptions   s     	NN	6">">">">@QRRSS 	%[D$555'BCCDDD""$$$'r   c                     | j         S )r/   )rS   r0   s    r   r   z"BaseTheoremToolCommand.assumptions   s       r   c                     | j         S )r2   )rR   r0   s    r   r   zBaseTheoremToolCommand.goal   s     zr   c                 R    |                                  D ]}t          |           dS r4   )r   r_   )r
   r[   s     r   r5   z(BaseTheoremToolCommand.print_assumptions   s6     !!## 	 	A!HHHH	 	r   NNr6   )
r   r   r   r   rV   r'   r-   r   r   r5   r   r   r   rP   rP      s         
I I I I$     (! ! !      r   rP   c                   8    e Zd ZdZd
dZddZddZddZd	 ZdS )BaseProverCommandr9   Nc                 \    || _         	 t                              | ||           d| _        dS )z
        :param prover: The theorem tool to execute with the assumptions
        :type prover: Prover
        :see: ``BaseTheoremToolCommand``
        N)_proverrP   rV   _proof)r
   proverr   r   s       r   rV   zBaseProverCommand.__init__
  s1     >''dK@@@r   Fc                     | j         M| j                            |                                 |                                 |          \  | _         | _        | j         S )zh
        Perform the actual proof.  Store the result to prevent unnecessary
        re-proving.
        )rU   ri   r   r   r   rj   r;   s     r   r   zBaseProverCommand.prove  sQ    
 <(,(;(;		T--//) )%DL$+ |r   Tc                 d    | j         t          d          |                     | j        |          S r=   Nz.You have to call prove() first to get a proof!rU   LookupErrordecorate_proofrj   r>   s     r   r@   zBaseProverCommand.proof"  3     <NOOO&&t{H===r   c                     |S z
        Modify and return the proof string
        :param proof_string: str the proof to decorate
        :param simplify: bool simplify the proof?
        :return: str
        r   r
   proof_stringr?   s      r   rq   z BaseProverCommand.decorate_proof-  s
     r   c                     | j         S rN   )ri   r0   s    r   rB   zBaseProverCommand.get_prover6  s
    |r   re   r6   rC   	r   r   r   r   rV   r   r@   rq   rB   r   r   r   rg   rg     s}         
   	 	 	 		> 	> 	> 	>       r   rg   c                   8    e Zd ZdZd	dZd
dZddZddZd ZdS )BaseModelBuilderCommandz
    This class holds a ``ModelBuilder``, a goal, and a list of assumptions.  When
    build_model() is called, the ``ModelBuilder`` is executed with the goal and
    assumptions.
    Nc                 \    || _         	 t                              | ||           d| _        dS )z
        :param modelbuilder: The theorem tool to execute with the assumptions
        :type modelbuilder: ModelBuilder
        :see: ``BaseTheoremToolCommand``
        N)_modelbuilderrP   rV   _model)r
   modelbuilderr   r   s       r   rV   z BaseModelBuilderCommand.__init__A  s2     *>''dK@@@r   Fc                     | j         M| j                            |                                 |                                 |          \  | _         | _        | j         S zi
        Attempt to build a model.  Store the result to prevent unnecessary
        re-building.
        )rU   r|   r   r   r   r}   r;   s     r   r   z#BaseModelBuilderCommand.build_modelN  sR    
 <(,(:(G(G		T--//) )%DL$+ |r   c                 d    | j         t          d          |                     | j        |          S rH   Nz4You have to call build_model() first to get a model!rU   rp   _decorate_modelr}   rI   s     r   rK   zBaseModelBuilderCommand.modelY  3     <WXXX''V<<<r   c                     |S )z
        :param valuation_str: str with the model builder's output
        :param format: str indicating the format for displaying
        :return: str
        r   r
   valuation_strrJ   s      r   r   z'BaseModelBuilderCommand._decorate_modele  s
     r   c                     | j         S rN   )r|   r0   s    r   rM   z)BaseModelBuilderCommand.get_model_builderm  s    !!r   re   r6   rN   	r   r   r   r   rV   r   rK   r   rM   r   r   r   rz   rz   :  s}            	 	 	 	
= 
= 
= 
=   " " " " "r   rz   c                   8    e Zd ZdZd Zd Zd Zd Zd
dZd Z	d	S )TheoremToolCommandDecoratorz
    A base decorator for the ``ProverCommandDecorator`` and
    ``ModelBuilderCommandDecorator`` classes from which decorators can extend.
    c                 "    || _         d| _        dS )zD
        :param command: ``TheoremToolCommand`` to decorate
        N)_commandrU   )r
   commands     r   rV   z$TheoremToolCommandDecorator.__init__w  s       r   c                 4    | j                                         S rN   )r   r   r0   s    r   r   z'TheoremToolCommandDecorator.assumptions  s    }((***r   c                 4    | j                                         S rN   )r   r   r0   s    r   r   z TheoremToolCommandDecorator.goal  s    }!!###r   c                 H    | j                             |           d | _        d S rN   )r   r'   rU   r%   s     r   r'   z+TheoremToolCommandDecorator.add_assumptions  s#    %%o666r   Fc                 J    | j                             ||           d | _        d S rN   )r   r-   rU   r*   s      r   r-   z/TheoremToolCommandDecorator.retract_assumptions  s%    )))U;;;r   c                 8    | j                                          d S rN   )r   r5   r0   s    r   r5   z-TheoremToolCommandDecorator.print_assumptions  s    '')))))r   Nr6   )
r   r   r   r   rV   r   r   r'   r-   r5   r   r   r   r   r   q  s}         
  + + +$ $ $     * * * * *r   r   c                   6    e Zd ZdZd Zd
dZddZddZd Zd	S )ProverCommandDecoratorzu
    A base decorator for the ``ProverCommand`` class from which other
    prover command decorators can extend.
    c                 J    t                               | |           d| _        dS )zE
        :param proverCommand: ``ProverCommand`` to decorate
        N)r   rV   rj   )r
   proverCommands     r   rV   zProverCommandDecorator.__init__  s'     	$,,T=AAA r   Fc                     | j         \|                                 }|                    |                                 |                                 |          \  | _         | _        | j         S rN   )rU   rB   r   r   r   rj   )r
   r   rk   s      r   r   zProverCommandDecorator.prove  sZ    <__&&F(.		T--//) )%DL$+ |r   Tc                 d    | j         t          d          |                     | j        |          S rn   ro   r>   s     r   r@   zProverCommandDecorator.proof  rr   r   c                 8    | j                             ||          S rt   )r   rq   ru   s      r   rq   z%ProverCommandDecorator.decorate_proof  s     }++L(CCCr   c                 4    | j                                         S rN   r   rB   r0   s    r   rB   z!ProverCommandDecorator.get_prover      }'')))r   Nr6   rC   rx   r   r   r   r   r     s|         
     	> 	> 	> 	>D D D D* * * * *r   r   c                   6    e Zd ZdZd Zd	dZd
dZd
dZd ZdS )ModelBuilderCommandDecoratorz{
    A base decorator for the ``ModelBuilderCommand`` class from which other
    prover command decorators can extend.
    c                 J    t                               | |           d| _        dS )zQ
        :param modelBuilderCommand: ``ModelBuilderCommand`` to decorate
        N)r   rV   r}   )r
   modelBuilderCommands     r   rV   z%ModelBuilderCommandDecorator.__init__  s(     	$,,T3FGGG r   Fc                     | j         \|                                 }|                    |                                 |                                 |          \  | _         | _        | j         S r   )rU   rM   r   r   r   r}   )r
   r   r~   s      r   r   z(ModelBuilderCommandDecorator.build_model  s`    
 <1133L(4(A(A		T--//) )%DL$+ |r   Nc                 d    | j         t          d          |                     | j        |          S r   r   rI   s     r   rK   z"ModelBuilderCommandDecorator.model  r   r   c                 8    | j                             ||          S )z
        Modify and return the proof string
        :param valuation_str: str with the model builder's output
        :param format: str indicating the format for displaying
        :return: str
        )r   r   r   s      r   r   z,ModelBuilderCommandDecorator._decorate_model  s     },,]FCCCr   c                 4    | j                                         S rN   r   r0   s    r   rM   z.ModelBuilderCommandDecorator.get_model_builder  r   r   r6   rN   r   r   r   r   r   r     s|         
  
 
 
 

= 
= 
= 
=D D D D* * * * *r   r   c                   .    e Zd ZdZd ZddZddZd ZdS )	ParallelProverBuildera  
    This class stores both a prover and a model builder and when either
    prove() or build_model() is called, then both theorem tools are run in
    parallel.  Whichever finishes first, the prover or the model builder, is the
    result that will be used.
    c                 "    || _         || _        d S rN   )ri   r|   )r
   rk   r~   s      r   rV   zParallelProverBuilder.__init__   s    )r   NFc                 4    |                      |||          dfS N _runr	   s       r   r   zParallelProverBuilder._prove  s    yy{G44b88r   c                 6    |                      |||           dfS r   r   r	   s       r   r   z"ParallelProverBuilder._build_model  s     99T;888"<<r   c                     t           fdd          }t           fdd          }|                                 |                                 |                                r=|                                r)	 |                                r|                                )|j        |j        S |j        |j         S d S )Nc                  <    j                                        S rN   )ri   r   r   r   r
   r   s   r   r\   z,ParallelProverBuilder._run.<locals>.<lambda>  s    DL&&t['BB r   TPc                  <    j                                        S rN   )r|   r   r   s   r   r\   z,ParallelProverBuilder._run.<locals>.<lambda>  s    D&224gNN r   MB)TheoremToolThreadstartis_aliveresult)r
   r   r   r   	tp_thread	mb_threads   ````  r   r   zParallelProverBuilder._run
  s    %BBBBBBBGT
 
	 &NNNNNNN
 
	 	  "" 	y'9'9';'; 	   "" 	y'9'9';'; 	 '##) '''4r   r   )r   r   r   r   rV   r   r   r   r   r   r   r   r     sd         * * *9 9 9 9= = = =    r   r   c                   0    e Zd ZdZddZd	dZd	dZd ZdS )
ParallelProverBuilderCommanda  
    This command stores both a prover and a model builder and when either
    prove() or build_model() is called, then both theorem tools are run in
    parallel.  Whichever finishes first, the prover or the model builder, is the
    result that will be used.

    Because the theorem prover result is the opposite of the model builder
    result, we will treat self._result as meaning "proof found/no model found".
    Nc                 z    t                               | |||           t                              | |||           d S rN   )rg   rV   rz   )r
   rk   r~   r   r   s        r   rV   z%ParallelProverBuilderCommand.__init__/  s<    ""4{CCC((|T;OOOOOr   Fc                 ,    |                      |          S rN   r   r;   s     r   r   z"ParallelProverBuilderCommand.prove3  s    yy!!!r   c                 .    |                      |           S rN   r   r;   s     r   r   z(ParallelProverBuilderCommand.build_model6  s    99W%%%%r   c                     t           fdd          }t           fdd          }|                                 |                                 |                                r=|                                r)	 |                                r|                                )|j        |j         _        n|j        |j          _         j        S )Nc                  :    t                                          S rN   )rg   r   r;   s   r   r\   z3ParallelProverBuilderCommand._run.<locals>.<lambda><  s    %++D':: r   r   c                  :    t                                          S rN   )rz   r   r;   s   r   r\   z3ParallelProverBuilderCommand._run.<locals>.<lambda>?  s    +77gFF r   r   )r   r   r   r   rU   )r
   r   r   r   s   ``  r   r   z!ParallelProverBuilderCommand._run9  s    %:::::GT
 
	 &FFFFFQU
 
	 	  "" 	y'9'9';'; 	   "" 	y'9'9';'; 	 '$+DLL)(//DL|r   re   r6   )r   r   r   r   rV   r   r   r   r   r   r   r   r   $  sm         P P P P" " " "& & & &    r   r   c                   2    e Zd ZddZd Zed             ZdS )r   Nc                 |    t           j                            |            || _        d | _        || _        || _        d S rN   )	threadingThreadrV   r   rU   _verbose_name)r
   r   r   names       r   rV   zTheoremToolThread.__init__Q  s9    !!$'''


r   c                 R   	 |                                  | _        | j        rGt          d| j        d| j        dt          j        t          j                                         d S d S # t          $ r1}t          |           t          d| j        z             Y d }~d S d }~ww xY w)NzThread z finished with result z at zThread %s completed abnormally)r   rU   r   r_   r   time	localtime	Exception)r
   es     r   runzTheoremToolThread.runX  s    		C==??DL} zzz4<<<	1L1L1LN     
  	C 	C 	C!HHH2djABBBBBBBBB	Cs   A%A+ +
B&5&B!!B&c                     | j         S rN   )rU   r0   s    r   r   zTheoremToolThread.resultd  s
    |r   rN   )r   r   r   rV   r   propertyr   r   r   r   r   r   P  sU           
C 
C 
C   X  r   r   )r   r   r   abcr   r   r   r   r!   r8   rE   rP   rg   rz   r   r   r   r   r   r   r   r   r   r   <module>r      s  
 
      ' ' ' ' ' ' ' '    w    *    W    2/ / / / /7 / / / /d    &   8    ,   @K K K K K/ K K K\3 3 3 3 3. 3 3 3l4" 4" 4" 4" 4"46I 4" 4" 4"n* * * * *"4 * * *D-* -* -* -* -*8- -* -* -*`2* 2* 2* 2* 2*#>@S 2* 2* 2*j) ) ) ) )FL ) ) )X) ) ) ) )#46M ) ) )X    	(     r   