o
    §ƒðjä#  ã                   @   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|v rdS tt| ƒtd�}tt	dt
|ƒd ƒƒ}t||ƒ}t||i ƒ}|s7|S i }|D ]}| ||d  || i¡ q;|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   ÚsymbolsÚsymbols_int_reprÚclauses_int_reprÚresultÚoutputr   © r   ú�/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/logic/algorithms/dpll.pyÚdpll_satisfiable   s   

r   c           
      C   sH  t | |ƒ\}}|r(| ||i¡ | |¡ |s| }t| |ƒ} t | |ƒ\}}|s	t|| ƒ\}}|rP| ||i¡ | |¡ |sB| }t| |ƒ} t|| ƒ\}}|s1g }| D ]}t||ƒ}|du rb dS |durk| |¡ qT|sp|S | st|S | ¡ }| ¡ }| |di¡ | |di¡ |dd… }	t	t||ƒ||ƒp£t	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ÚvalÚ
model_copyÚsymbols_copyr   r   r   r'   1   sL   

ú

ú

€ÿr'   c           
      C   s:  t | |ƒ\}}|r(| ||i¡ | |¡ |s| }t| |ƒ} t | |ƒ\}}|s	t|| ƒ\}}|rP| ||i¡ | |¡ |sB| }t| |ƒ} t|| ƒ\}}|s1g }| D ]}t||ƒ}|du rb dS |durk| |¡ qT|sp|S | ¡ }| ¡ }| |di¡ | |di¡ | ¡ }	t	t||ƒ||ƒpœt	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   sH   

ú

ú

€ÿr   c                 C   sZ   d}| D ]&}|dk r|  | ¡}|dur| }n|  |¡}|du r$ dS |du 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   ÚlitÚpr   r   r   r4   ’   s   €
€r4   c                    sv   g }| D ]4}|j tkr| |¡ q|jD ]}|ˆ  kr,| t‡ fdd„|jD ƒŽ ¡  n|ˆ kr2 n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   r<   r   r-   Úargr   r;   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 | ]
}ˆ|vr|ˆ  ‘qS r   r   )r9   r6   ©ÚnegatedÚsr   r   r=   Ù   s    z+unit_propagate_int_repr.<locals>.<listcomp>r   )r   rC   r   rA   r   r2   Î   s   
r2   c                 C   s`   | D ]+}d\}}|D ]}|s|t |ƒv rd}|s"t|ƒt |ƒv 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 ]}| |vr$|df  S q|D ]}| |vr5| df  S q'd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   )r9   rC   r   r   r   r=      ó    z-find_pure_symbol_int_repr.<locals>.<listcomp>TFrD   )r   ÚunionÚintersection)r   r,   Úall_symbolsrF   rG   r8   r   r   r   r3   ó   s   

ÿ
ÿr3   c                 C   s^   | D ]*}d}t |ƒD ]}t|ƒ}||v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   rD   )r   r   r   r   )r   r)   r6   Únum_not_in_modelÚliteralrE   r*   r+   r   r   r   r    
  s   
€ÿr    c                 C   sb   t |ƒdd„ |D ƒB }| D ]!}|| }t|ƒdkr.| ¡ }|dk r(| 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   )r9   rE   r   r   r   Ú	<setcomp>+  rH   z,find_unit_clause_int_repr.<locals>.<setcomp>r   r   FTrD   )r   r   r%   )r   r)   Úboundr6   Úunboundr8   r   r   r   r1      s   ûr1   N)Ú__doc__Úsympy.core.sortingr   Úsympy.logic.boolalgr   r   r   r   r   r   r	   Úsympy.assumptions.cnfr
   Úsympy.logic.inferencer   r   r   r'   r   r4   r"   r2   r#   r3   r    r1   r   r   r   r   Ú<module>   s    
$10!