U
    9%e#                     @   s   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZ dd Zdd	 Zd
d Zi fddZdd Zdd Zdd Zdd Zdd Zdd ZdS )a&  Implementation of DPLL algorithm

Further improvements: eliminate calls to pl_true, implement branching rules,
efficient unit propagation.

References:
  - https://en.wikipedia.org/wiki/DPLL_algorithm
  - https://www.researchgate.net/publication/242384772_Implementations_of_the_DPLL_Algorithm
    )default_sort_key)OrNot	conjuncts	disjunctsto_cnfto_int_repr_find_predicates)CNF)pl_trueliteral_symbolc                 C   s   t | tstt| }n| j}d|kr*dS tt| td}tt	dt
|d }t||}t||i }|sn|S i }|D ]}|||d  || i qv|S )a>  
    Check satisfiability of a propositional sentence.
    It returns a model rather than True when it succeeds

    >>> from sympy.abc import A, B
    >>> from sympy.logic.algorithms.dpll import dpll_satisfiable
    >>> dpll_satisfiable(A & ~B)
    {A: True, B: False}
    >>> dpll_satisfiable(A & ~A)
    False

    F)key   )
isinstancer
   r   r   clausessortedr	   r   setrangelenr   dpll_int_reprupdate)exprr   symbolsZsymbols_int_reprZclauses_int_reprresultoutputr    r   Z/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/logic/algorithms/dpll.pydpll_satisfiable   s    

r   c           
      C   sF  t | |\}}|rN|||i || |s4| }t| |} t | |\}}qt|| \}}|r|||i || |s| }t| |} t|| \}}q\g }| D ].}t||}|dkr dS |dk	r|| q|s|S | s|S | }| }||di ||di |dd }	t	t||||pDt	t|t
||	|S )z
    Compute satisfiability in a partial model.
    Clauses is an array of conjuncts.

    >>> from sympy.abc import A, B, D
    >>> from sympy.logic.algorithms.dpll import dpll
    >>> dpll([A, B, D], [A, B], {D: False})
    False

    FTN)find_unit_clauser   removeunit_propagatefind_pure_symbolr   appendpopcopydpllr   
r   r   modelPvalueunknown_clausescvalZ
model_copyZsymbols_copyr   r   r   r%   1   sF    




r%   c           
      C   s8  t | |\}}|rN|||i || |s4| }t| |} t | |\}}qt|| \}}|r|||i || |s| }t| |} t|| \}}q\g }| D ].}t||}|dkr dS |dk	r|| q|s|S | }| }||di ||di | }	t	t||||p6t	t|| |	|S )z
    Compute satisfiability in a partial model.
    Arguments are expected to be in integer representation

    >>> from sympy.logic.algorithms.dpll import dpll_int_repr
    >>> dpll_int_repr([{1}, {2}, {3}], {1, 2}, {3: False})
    False

    FT)
find_unit_clause_int_reprr   r   unit_propagate_int_reprfind_pure_symbol_int_reprpl_true_int_reprr"   r#   r$   r   r&   r   r   r   r   b   sB    




r   c                 C   sZ   d}| D ]L}|dk r0| | }|dk	r:| }n
| |}|dkrH dS |dkrd}q|S )af  
    Lightweight version of pl_true.
    Argument clause represents the set of args of an Or clause. This is used
    inside dpll_int_repr, it is not meant to be used directly.

    >>> from sympy.logic.algorithms.dpll import pl_true_int_repr
    >>> pl_true_int_repr({1, 2}, {1: False})
    >>> pl_true_int_repr({1, 2}, {1: False, 2: False})
    False

    Fr   NT)get)clauser'   r   Zlitpr   r   r   r0      s    
r0   c                    sv   g }| D ]h}|j tkr"|| q|jD ]<}|  krX|t fdd|jD    q| kr( qq(|| q|S )a  
    Returns an equivalent set of clauses
    If a set of clauses contains the unit clause l, the other clauses are
    simplified by the application of the two following rules:

      1. every clause containing l is removed
      2. in every clause that contains ~l this literal is deleted

    Arguments are expected to be in CNF.

    >>> from sympy.abc import A, B, D
    >>> from sympy.logic.algorithms.dpll import unit_propagate
    >>> unit_propagate([A | B, D | ~B, B], B)
    [D, B]

    c                    s   g | ]}|  kr|qS r   r   ).0xsymbolr   r   
<listcomp>   s     
 z"unit_propagate.<locals>.<listcomp>)funcr   r"   args)r   r7   r   r+   argr   r6   r   r       s    



r    c                    s    h  fdd| D S )z
    Same as unit_propagate, but arguments are expected to be in integer
    representation

    >>> from sympy.logic.algorithms.dpll import unit_propagate_int_repr
    >>> unit_propagate_int_repr([{1, 2}, {3, -2}, {2}], 2)
    [{3}]

    c                    s   g | ]}|kr|  qS r   r   )r4   r2   Znegatedsr   r   r8      s      z+unit_propagate_int_repr.<locals>.<listcomp>r   )r   r=   r   r<   r   r.      s    
r.   c                 C   s`   | D ]V}d\}}|D ]0}|s,|t |kr,d}|st|t |krd}q||kr||f  S qdS )a#  
    Find a symbol and its value if it appears only as a positive literal
    (or only as a negative) in clauses.

    >>> from sympy.abc import A, B, D
    >>> from sympy.logic.algorithms.dpll import find_pure_symbol
    >>> find_pure_symbol([A, B, D], [A|~B,~B|~D,D|A])
    (A, True)

    )FFTNN)r   r   )r   r*   sym	found_pos	found_negr+   r   r   r   r!      s    r!   c                 C   sp   t  j| }|| }|dd | D }|D ]}| |kr.|df  S q.|D ]}| |krN| df  S qNdS )a  
    Same as find_pure_symbol, but arguments are expected
    to be in integer representation

    >>> from sympy.logic.algorithms.dpll import find_pure_symbol_int_repr
    >>> find_pure_symbol_int_repr({1,2,3},
    ...     [{1, -2}, {-2, -3}, {3, 1}])
    (1, True)

    c                 S   s   g | ]
}| qS r   r   )r4   r=   r   r   r   r8      s     z-find_pure_symbol_int_repr.<locals>.<listcomp>TFr>   )r   unionintersection)r   r*   Zall_symbolsr@   rA   r3   r   r   r   r/      s    


r/   c                 C   s^   | D ]T}d}t |D ].}t|}||kr|d7 }|t|t  }}q|dkr||f  S qdS )a  
    A unit clause has only 1 variable that is not bound in the model.

    >>> from sympy.abc import A, B, D
    >>> from sympy.logic.algorithms.dpll import find_unit_clause
    >>> find_unit_clause([A | B | D, B | ~D, A | ~B], {A:True})
    (B, False)

    r   r   r>   )r   r   r   r   )r   r'   r2   Znum_not_in_modelliteralr?   r(   r)   r   r   r   r   
  s    
r   c                 C   sb   t |dd |D B }| D ]B}|| }t|dkr| }|dk rP| df  S |df  S qdS )a  
    Same as find_unit_clause, but arguments are expected to be in
    integer representation.

    >>> from sympy.logic.algorithms.dpll import find_unit_clause_int_repr
    >>> find_unit_clause_int_repr([{1, 2, 3},
    ...     {2, -3}, {1, -2}], {1: True})
    (2, False)

    c                 S   s   h | ]
}| qS r   r   )r4   r?   r   r   r   	<setcomp>+  s     z,find_unit_clause_int_repr.<locals>.<setcomp>r   r   FTr>   )r   r   r#   )r   r'   boundr2   unboundr3   r   r   r   r-      s    r-   N)__doc__Zsympy.core.sortingr   Zsympy.logic.boolalgr   r   r   r   r   r   r	   Zsympy.assumptions.cnfr
   Zsympy.logic.inferencer   r   r   r%   r   r0   r    r.   r!   r/   r   r-   r   r   r   r   <module>   s   
$10!