Ë
    7^(hä#  ã                   ó”   — 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„ Zd„ Zd„ Zi fd	„Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zy)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                 ól  — t        | t        «      st        t        | «      «      }n| j                  }d|v ryt        t        | «      t        ¬«      }t        t        dt        |«      dz   «      «      }t        ||«      }t        ||i «      }|s|S i }|D ]  }|j                  ||dz
     ||   i«       Œ  |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   s           úY/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sympy/logic/algorithms/dpll.pyÚdpll_satisfiabler       sº   € ô �dœCÔ ÜœF 4›LÓ)‰à—,‘,ˆØ�ÑØÜÔ% dÓ+Ô1AÔB€GÜœ5 ¤C¨£L°1Ñ$4Ó5Ó6ÐÜ" 7¨GÓ4ÐÜÐ+Ð-=¸rÓB€FÙØˆØ€FØò 7ˆØ�‰�w˜s Q™wÑ'¨°©Ð5Õ6ð7à€Mó    c                 óÜ  — t        | |«      \  }}|rG|j                  ||i«       |j                  |«       |s| }t        | |«      } t        | |«      \  }}|rŒGt	        || «      \  }}|rG|j                  ||i«       |j                  |«       |s| }t        | |«      } t	        || «      \  }}|rŒGg }| D ]*  }t        ||«      }|du r y|dusŒ|j                  |«       Œ, |s|S | s|S |j                  «       }|j                  «       }|j                  |di«       |j                  |di«       |dd }	t        t        ||«      ||«      xs  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_copys
             r   r*   r*   1   s„  € ô   ¨Ó/�H€A€uÙ
Ø�‰�a˜�ZÔ Ø�‰�qÔÙØ�ˆAÜ  ¨!Ó,ˆÜ# G¨UÓ3‰ˆˆ5ò ô   ¨Ó1�H€A€uÙ
Ø�‰�a˜�ZÔ Ø�‰�qÔÙØ�ˆAÜ  ¨!Ó,ˆÜ# G¨WÓ5‰ˆˆ5ò ð €OØò &ˆÜ�a˜ÓˆØ�%‰<ÙØ�dŠ?Ø×"Ñ" 1Õ%ð&ñ ØˆÙØˆØ�‰‹€AØ—‘“€JØ	‡L�L�!�T�ÔØ×Ñ�q˜%�jÔ!Ø™1�:€LÜ” °Ó3°W¸eÓDò TÜ” ´°Q³Ó8¸,È
ÓSðUr!   c                 óÚ  — t        | |«      \  }}|rG|j                  ||i«       |j                  |«       |s| }t        | |«      } t        | |«      \  }}|rŒGt	        || «      \  }}|rG|j                  ||i«       |j                  |«       |s| }t        | |«      } t	        || «      \  }}|rŒGg }| D ]*  }t        ||«      }|du r y|dusŒ|j                  |«       Œ, |s|S |j                  «       }|j                  «       }|j                  |di«       |j                  |di«       |j                  «       }	t        t        ||«      ||«      xs 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+   s
             r   r   r   b   s€  € ô )¨°%Ó8�H€A€uÙ
Ø�‰�a˜�ZÔ Ø�‰�qÔÙØ�ˆAÜ)¨'°1Ó5ˆÜ,¨W°eÓ<‰ˆˆ5ò ô )¨°'Ó:�H€A€uÙ
Ø�‰�a˜�ZÔ Ø�‰�qÔÙØ�ˆAÜ)¨'°1Ó5ˆÜ,¨W°gÓ>‰ˆˆ5ò ð €OØò &ˆÜ˜q %Ó(ˆØ�%‰<ÙØ�dŠ?Ø×"Ñ" 1Õ%ð&ñ ØˆØ�‰‹€AØ—‘“€JØ	‡L�L�!�T�ÔØ×Ñ�q˜%�jÔ!Ø—<‘<“>€LÜÔ1°/À1ÓEÀwÐPUÓVò bÜÔ1°/ÀAÀ2ÓFÈÐV`Óaðcr!   c                 óŠ   — d}| D ];  }|dk  r|j                  | «      }|�| }n|j                  |«      }|du r y|�Œ:d}Œ= |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Úps        r   r8   r8   ’   sb   € ð €FØò 
ˆØ�Š7Ø—	‘	˜3˜$“ˆAØˆ}Ø�E‘à—	‘	˜#“ˆAØ�‰9ÙØ‰YØ‰Fð
ð €Mr!   c                 ó:  — g }| D ]Ž  }|j                   t        k7  r|j                  |«       Œ(|j                  D ]G  }|| k(  r8|j                  t        |j                  D �cg c]  }|| k7  sŒ
|‘Œ c}Ž «        Œu||k(  sŒG Œ} |j                  |«       Œ� |S c c}w )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]

    )Úfuncr   r'   Úargs)r   Úsymbolr   r0   ÚargÚxs         r   r%   r%   ­   sž   € ð" €FØò ˆØ�6‰6”RŠ<Ø�M‰M˜!ÔØØ—6‘6ò 	ˆCØ�v�gŠ~Ø—‘œb¨a¯f©fÖ"E¨¸¸f¸W»¢1Ò"EÐFÔGÙØ�f‹}Ùð	ð �M‰M˜!Õðð €Mùò #Fs   Á BÁ,Bc                 óD   — | h}| D �cg c]  }||vsŒ||z
  ‘Œ c}S c c}w )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}]

    © )r   ÚsÚnegatedr;   s       r   r6   r6   Î   s,   € ð ˆrˆd€GØ+2ÖF °a¸v²oˆF�WÓÒFÐFùÒFs   ‰	“c                 óš   — | D ]F  }d\  }}|D ]/  }|s|t        |«      v rd}|rŒt        |«      t        |«      v sŒ.d}Œ1 ||k7  sŒB||fc S  y)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_negr0   s         r   r&   r&   Ü   so   € ð ò "ˆØ+Ñˆ	�9Ø ò 	!ˆAÙ ¬	°!«Ñ!4Ø �	Ú¤ S£¬Y°q«\Ò!9Ø ‘	ð		!ð
 ˜	Ó!Ø˜	�>Ò!ð"ð r!   c                 óè   —  t        «       j                  |Ž }|j                  | «      }|j                  | D �cg c]  }| ‘Œ c}«      }|D ]  }| |vsŒ	|dfc S  |D ]  }| |vsŒ	| dfc S  yc c}w )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)

    TFrI   )r   ÚunionÚintersection)r   r/   Úall_symbolsrK   rF   rL   r=   s          r   r7   r7   ó   s–   € ð ”#“%—+‘+˜Ð/€KØ×(Ñ(¨Ó1€IØ×(Ñ(°gÖ)>°¨1ª"Ò)>Ó?€IØò ˆØˆ2�YÒØ�d�7ŠNðð ò ˆØˆ2�YÒØ�2�u�9Òðð ùò *?s   ¸
A/c                 óœ   — | D ]G  }d}t        |«      D ]*  }t        |«      }||vsŒ|dz  }|t        |t        «       }}Œ, |dk(  sŒCfc S  y)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   rI   )r   r   r   r   )r   r,   r;   Únum_not_in_modelÚliteralrJ   r-   r.   s           r   r#   r#   
  su   € ð ò ˆØÐÜ  Ó(ò 	=ˆGÜ  Ó)ˆCØ˜%ÒØ  AÑ%Ð Ø¤J¨w¼Ó$<Ð <�5‘ð		=ð
 ˜qÓ Ø�e�8ŠOðð r!   c                 óÀ   — t        |«      |D �ch c]  }| ’Œ c}z  }| D ]7  }||z
  }t        |«      dk(  sŒ|j                  «       }|dk  r| dfc S |dfc S  yc c}w )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)

    r   r   FTrI   )r   r   r(   )r   r,   rJ   Úboundr;   Úunboundr=   s          r   r5   r5      st   € ô �‹J¨%Ö0 3˜3š$Ò0Ñ0€EØò ˆØ˜5‘.ˆÜˆw‹<˜1ÓØ—‘“ˆAØ�1ŠuØ�r˜5�yÒ à˜$�w’ðð ùò 1s   �
AN)Ú__doc__Úsympy.core.sortingr   Úsympy.logic.boolalgr   r   r   r   r   r	   r
   Úsympy.assumptions.cnfr   Úsympy.logic.inferencer   r   r    r*   r   r8   r%   r6   r&   r7   r#   r5   rE   r!   r   ú<module>r\      sa   ðñõ 0÷"÷ "ñ "å %ß 9òò>.Uòb+cð` $&ó ò6òBGòò.ò.ó,r!   