ó
¡¼™\c           @  sÊ   d  Z  d d l m Z m Z d d l m Z m Z m Z m Z d d l	 m
 Z
 d d l m Z d „  Z d e d „ Z d	 „  Z i  e d
 „ Z i  d „ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s    Inference in propositional logiciÿÿÿÿ(   t   print_functiont   division(   t   Andt   Nott	   conjunctst   to_cnf(   t   ordered(   t   sympifyc         C  sw   |  t  k s |  t k r |  Sy1 |  j r, |  S|  j rF t |  j d ƒ St ‚ Wn# t t f k
 rr t d ƒ ‚ n Xd S(   só   
    The symbol in this literal (without the negation).

    Examples
    ========

    >>> from sympy.abc import A
    >>> from sympy.logic.inference import literal_symbol
    >>> literal_symbol(A)
    A
    >>> literal_symbol(~A)
    A

    i    s#   Argument must be a boolean literal.N(   t   Truet   Falset	   is_Symbolt   is_Nott   literal_symbolt   argst
   ValueErrort   AttributeError(   t   literal(    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR   	   s    		
t   dpll2c         C  se   t  |  ƒ }  | d k r2 d d l m } | |  ƒ S| d k r[ d d l m } | |  | ƒ St ‚ d S(   sÚ  
    Check satisfiability of a propositional sentence.
    Returns a model when it succeeds.
    Returns {true: true} for trivially true expressions.

    On setting all_models to True, if given expr is satisfiable then
    returns a generator of models. However, if expr is unsatisfiable
    then returns a generator containing the single element False.

    Examples
    ========

    >>> from sympy.abc import A, B
    >>> from sympy.logic.inference import satisfiable
    >>> satisfiable(A & ~B)
    {A: True, B: False}
    >>> satisfiable(A & ~A)
    False
    >>> satisfiable(True)
    {True: True}
    >>> next(satisfiable(A & ~A, all_models=True))
    False
    >>> models = satisfiable((A >> B) & B, all_models=True)
    >>> next(models)
    {A: False, B: True}
    >>> next(models)
    {A: True, B: True}
    >>> def use_models(models):
    ...     for model in models:
    ...         if model:
    ...             # Do something with the model.
    ...             print(model)
    ...         else:
    ...             # Given expr is unsatisfiable.
    ...             print("UNSAT")
    >>> use_models(satisfiable(A >> ~A, all_models=True))
    {A: False}
    >>> use_models(satisfiable(A ^ A, all_models=True))
    UNSAT

    t   dplliÿÿÿÿ(   t   dpll_satisfiableR   N(   R   t   sympy.logic.algorithms.dpllR   t   sympy.logic.algorithms.dpll2t   NotImplementedError(   t   exprt	   algorithmt
   all_modelsR   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   satisfiable&   s    *
c         C  s   t  t |  ƒ ƒ S(   sx  
    Check validity of a propositional sentence.
    A valid propositional sentence is True under every assignment.

    Examples
    ========

    >>> from sympy.abc import A, B
    >>> from sympy.logic.inference import valid
    >>> valid(A | ~A)
    True
    >>> valid(A | B)
    False

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Validity

    (   R   R   (   R   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   validZ   s    c           s!  d d l  m ‰ d d l m ‰  t t f ‰ ‡  ‡ ‡ ‡ f d †  ‰ |  ˆ k rT |  St |  ƒ }  ˆ |  ƒ s t d |  ƒ ‚ n  t ‡ f d †  | j	 ƒ  Dƒ ƒ } |  j
 | ƒ } | ˆ k rÆ t | ƒ S| rt d „  | j ƒ  Dƒ ƒ } t | | ƒ r
t | ƒ rt Sqt | ƒ st Sn  d S(	   s-  
    Returns whether the given assignment is a model or not.

    If the assignment does not specify the value for every proposition,
    this may return None to indicate 'not obvious'.

    Parameters
    ==========

    model : dict, optional, default: {}
        Mapping of symbols to boolean values to indicate assignment.
    deep: boolean, optional, default: False
        Gives the value of the expression under partial assignments
        correctly. May still return None to indicate 'not obvious'.


    Examples
    ========

    >>> from sympy.abc import A, B, C
    >>> from sympy.logic.inference import pl_true
    >>> pl_true( A & B, {A: True, B: True})
    True
    >>> pl_true(A & B, {A: False})
    False
    >>> pl_true(A & B, {A: True})
    >>> pl_true(A & B, {A: True}, deep=True)
    >>> pl_true(A >> (B >> A))
    >>> pl_true(A >> (B >> A), deep=True)
    True
    >>> pl_true(A & ~A)
    >>> pl_true(A & ~A, deep=True)
    False
    >>> pl_true(A & B & (~A | ~B), {A: True})
    >>> pl_true(A & B & (~A | ~B), {A: True}, deep=True)
    False
    iÿÿÿÿ(   t   Symbol(   t   BooleanFunctionc           sO   t  |  ˆ ƒ s |  ˆ k r t St  |  ˆ  ƒ s2 t St ‡ f d †  |  j Dƒ ƒ S(   Nc         3  s   |  ] } ˆ  | ƒ Vq d  S(   N(    (   t   .0t   arg(   t	   _validate(    s4   lib/python2.7/site-packages/sympy/logic/inference.pys	   <genexpr>¢   s    (   t
   isinstanceR   R	   t   allR   (   R   (   R   R   R    t   boolean(    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR       s
    s$   %s is not a valid boolean expressionc         3  s-   |  ]# \ } } | ˆ  k r | | f Vq d  S(   N(    (   R   t   kt   v(   R#   (    s4   lib/python2.7/site-packages/sympy/logic/inference.pys	   <genexpr>©   s    c         s  s   |  ] } | t  f Vq d  S(   N(   R   (   R   R$   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pys	   <genexpr>®   s    N(   t   sympy.core.symbolR   t   sympy.logic.boolalgR   R   R	   R   R   t   dictt   itemst   subst   boolt   atomst   pl_trueR   R   t   None(   R   t   modelt   deept   result(    (   R   R   R    R#   s4   lib/python2.7/site-packages/sympy/logic/inference.pyR-   r   s*    '"
c         C  s0   t  | ƒ } | j t |  ƒ ƒ t t | Œ  ƒ S(   sø  
    Check whether the given expr_set entail an expr.
    If formula_set is empty then it returns the validity of expr.

    Examples
    ========

    >>> from sympy.abc import A, B, C
    >>> from sympy.logic.inference import entails
    >>> entails(A, [A >> B, B >> C])
    False
    >>> entails(C, [A >> B, B >> C, A])
    True
    >>> entails(A >> B)
    False
    >>> entails(A >> (B >> A))
    True

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Logical_consequence

    (   t   listt   appendR   R   R   (   R   t   formula_set(    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   entails¸   s    t   KBc           B  sD   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z e d „  ƒ Z	 RS(   s"   Base class for all knowledge basesc         C  s&   t  ƒ  |  _ | r" |  j | ƒ n  d  S(   N(   t   sett   clauses_t   tell(   t   selft   sentence(    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   __init__Ø   s    c         C  s
   t  ‚ d  S(   N(   R   (   R:   R;   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR9   Ý   s    c         C  s
   t  ‚ d  S(   N(   R   (   R:   t   query(    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   askà   s    c         C  s
   t  ‚ d  S(   N(   R   (   R:   R;   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   retractã   s    c         C  s   t  t |  j ƒ ƒ S(   N(   R2   R   R8   (   R:   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   clausesæ   s    N(
   t   __name__t
   __module__t   __doc__R.   R<   R9   R>   R?   t   propertyR@   (    (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR6   Ö   s   			t   PropKBc           B  s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s=   A KB for Propositional Logic.  Inefficient, with no indexing.c         C  s1   x* t  t | ƒ ƒ D] } |  j j | ƒ q Wd S(   sh  Add the sentence's clauses to the KB

        Examples
        ========

        >>> from sympy.logic.inference import PropKB
        >>> from sympy.abc import x, y
        >>> l = PropKB()
        >>> l.clauses
        []

        >>> l.tell(x | y)
        >>> l.clauses
        [x | y]

        >>> l.tell(y)
        >>> l.clauses
        [y, x | y]
        N(   R   R   R8   t   add(   R:   R;   t   c(    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR9   î   s    c         C  s   t  | |  j ƒ S(   s7  Checks if the query is true given the set of clauses.

        Examples
        ========

        >>> from sympy.logic.inference import PropKB
        >>> from sympy.abc import x, y
        >>> l = PropKB()
        >>> l.tell(x & ~y)
        >>> l.ask(x)
        True
        >>> l.ask(y)
        False
        (   R5   R8   (   R:   R=   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR>     s    c         C  s1   x* t  t | ƒ ƒ D] } |  j j | ƒ q Wd S(   sl  Remove the sentence's clauses from the KB

        Examples
        ========

        >>> from sympy.logic.inference import PropKB
        >>> from sympy.abc import x, y
        >>> l = PropKB()
        >>> l.clauses
        []

        >>> l.tell(x | y)
        >>> l.clauses
        [x | y]

        >>> l.retract(x | y)
        >>> l.clauses
        []
        N(   R   R   R8   t   discard(   R:   R;   RG   (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyR?     s    (   RA   RB   RC   R9   R>   R?   (    (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyRE   ë   s   		N(   RC   t
   __future__R    R   R'   R   R   R   R   t   sympy.core.compatibilityR   t   sympy.core.sympifyR   R   R	   R   R   R-   R5   t   objectR6   RE   (    (    (    s4   lib/python2.7/site-packages/sympy/logic/inference.pyt   <module>   s   "	4	F