
    cwiH                        S r SSKJrJrJrJrJrJrJr  SSK	J
r
JrJrJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  / S	Qr " S
 S5      r " S S5      r " S S\5      r " S S\\   5      r " S S5      r " S S\S   5      rg)a  
Functions and classes related to solving.

Examples
--------

The following example shows how to intercept models with a callback:

    >>> from clingo import Control
    >>>
    >>> ctl = Control(["0"])
    >>> ctl.add("base", [], "1 { a; b } 1.")
    >>> ctl.ground([("base", [])])
    >>> print(ctl.solve(on_model=print))
    Answer: a
    Answer: b
    SAT

The following example shows how to yield models:

    >>> from clingo import Control
    >>>
    >>> ctl = Control(["0"])
    >>> ctl.add("base", [], "1 { a; b } 1.")
    >>> ctl.ground([("base", [])])
    >>> with ctl.solve(yield_=True) as hnd:
    ...     for m in hnd:
    ...         print(m)
    ...     print(hnd.get())
    ...
    Answer: a
    Answer: b
    SAT

The following example shows how to solve asynchronously:

    >>> from clingo import Control
    >>>
    >>> ctl = Control(["0"])
    >>> ctl.add("base", [], "1 { a; b } 1.")
    >>> ctl.ground([("base", [])])
    >>> with ctl.solve(on_model=print, async_=True) as hnd:
    ...     # some computation here
    ...     hnd.wait()
    ...     print(hnd.get())
    ...
    Answer: a
    Answer: b
    SAT

This example shows how to solve both iteratively and asynchronously:

    >>> from clingo import Control
    >>>
    >>> ctl = Control(["0"])
    >>> ctl.add("base", [], "1 { a; b } 1.")
    >>> ctl.ground([("base", [])])
    >>> with ctl.solve(yield_=True, async_=True) as hnd:
    ...     while True:
    ...         hnd.resume()
    ...         # some computation here
    ...         _ = hnd.wait()
    ...         m = hnd.model()
    ...         if m is None:
    ...             print(hnd.get())
    ...             break
    ...         print(m)
    b
    a
    a b
    None
    )ContextManagerIteratorListOptionalSequenceTupleUnion   )_c_call_c_call2_ffi_handle_error_lib)OrderedEnum)Symbol)SymbolicAtoms)SliceSlicedSequence)Model	ModelTypeSolveControlSolveHandleSolveResultc                       \ rS rSrSrS r\S\4S j5       r\S\4S j5       r	\S\
\   4S j5       r\S\4S j5       r\S\
\   4S	 j5       rS
 rS rSrg)r   U   z&
Captures the result of a solve call.
c                     Xl         g N_repselfreps     H/home/james-whalen/.local/lib/python3.13/site-packages/clingo/solving.py__init__SolveResult.__init__Z       	    returnc                 B    [         R                  U R                  -  S:g  $ )z.
Determine if the search space was exhausted.
r   )r   clingo_solve_result_exhaustedr   r!   s    r#   	exhaustedSolveResult.exhausted]   s    
 22TYY>1DDr'   c                 B    [         R                  U R                  -  S:g  $ )z*
Determine if the search was interrupted.
r   )r   clingo_solve_result_interruptedr   r+   s    r#   interruptedSolveResult.interruptedd   s    
 44tyy@QFFr'   c                     [         R                  U R                  -  S:w  a  g[         R                  U R                  -  S:w  a  gg)z}
`True` if the problem is satisfiable, `False` if the problem is
unsatisfiable, or `None` if the satisfiablity is not known.
r   TFN)r   clingo_solve_result_satisfiabler   !clingo_solve_result_unsatisfiabler+   s    r#   satisfiableSolveResult.satisfiablek   s;     00499<B22TYY>1Dr'   c                     U R                   SL $ )zZ
Determine if the satisfiablity is not known.

This is equivalent to satisfiable is None.
N)r5   r+   s    r#   unknownSolveResult.unknownw   s     4''r'   c                     [         R                  U R                  -  S:w  a  g[         R                  U R                  -  S:w  a  gg)z}
`True` if the problem is unsatisfiable, `False` if the problem is
satisfiable, or `None` if the satisfiablity is not known.
r   TFN)r   r4   r   r3   r+   s    r#   unsatisfiableSolveResult.unsatisfiable   s;     22TYY>1D00499<Br'   c                 L    U R                   (       a  gU R                  (       a  gg)NSATUNSATUNKNOWN)r5   r;   r+   s    r#   __str__SolveResult.__str__   s    r'   c                 "    SU R                    S3$ )NzSolveResult()r   r+   s    r#   __repr__SolveResult.__repr__   s    dii[**r'   r   N)__name__
__module____qualname____firstlineno____doc__r$   propertyboolr,   r0   r   r5   r8   r;   rA   rE   __static_attributes__ r'   r#   r   r   U   s     E4 E E GT G G 	Xd^ 	 	 ( ( ( 	x~ 	 	+r'   r   c                       \ rS rSrSrS rS\\\\	\
4   \4      SS4S jrS\\\	\
4   \4   S\\\	\
4   \4   4S	 jrS\\\\	\
4   \4      SS4S
 jr\S\4S j5       rSrg)r      z6
Object that allows for controlling a running search.
c                     Xl         g r   r   r    s     r#   r$   SolveControl.__init__   r&   r'   literalsr(   Nc           	      v   U R                   n[        R                  " S[        U5      5      n[	        U5       HJ  u  pE[        U[        5      (       a  XSU'   M   X%S      nUb  UR                  nOSnUS   (       a  UOU* X4'   ML     [        [        R                  " U R                  U[        U5      5      5        g)aG  
Add a clause that applies to the current solving step during the search.

Parameters
----------
literals
    List of literals either represented as pairs of symbolic atoms and
    Booleans or as program literals.

Notes
-----
This function can only be called in a model callback or while iterating
when using a `SolveHandle`.
zclingo_literal_t[]r   Nr
   )symbolic_atomsr   newlen	enumerate
isinstanceintliteralr   r   clingo_solve_control_add_clauser   )r!   rT   atomsp_litsilitatomslits           r#   
add_clauseSolveControl.add_clause   s     ##.H>)FA#s##q	V}#<<DD$'FD	 * 	00FCMR	
r'   rb   c                 P    [        U[        5      (       a  U* $ US   US   (       + 4$ )Nr   r
   )r[   r\   )r!   rb   s     r#   _invertSolveControl._invert   s-     c34K1v3q6z!!r'   c                 n    U R                  U Vs/ s H  o R                  U5      PM     sn5        gs  snf )zE
Equivalent to `SolveControl.add_clause` with the literals inverted.
N)re   rh   )r!   rT   rb   s      r#   
add_nogoodSolveControl.add_nogood   s)     	h?hsc*h?@?s   2c                 b    [        S[        R                  U R                  5      n[	        U5      $ )zM
`clingo.symbolic_atoms.SymbolicAtoms` object to inspect the symbolic atoms.
zclingo_symbolic_atoms_t*)r   r   #clingo_solve_control_symbolic_atomsr   r   )r!   r_   s     r#   rW   SolveControl.symbolic_atoms   s.    
 &44II

 U##r'   r   )rG   rH   rI   rJ   rK   r$   r   r	   r   r   rM   r\   re   rh   rk   rL   r   rW   rN   rO   r'   r#   r   r      s    
8E%2Es2J,K#L 
QU 
@"vt|,c12"	uVT\"C'	("A8E%2Es2J,K#L AQU A 	$ 	$ 	$r'   r   c                   d    \ rS rSrSr\R                  r \R                  r	 \R                  rSrg)r      z/
Enumeration of the different types of models.
rO   N)rG   rH   rI   rJ   rK   r   $clingo_model_type_brave_consequencesBraveConsequences'clingo_model_type_cautious_consequencesCautiousConsequencesclingo_model_type_stable_modelStableModelrN   rO   r'   r#   r   r      s?     AA  GG 55Kr'   r   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)_SymbolSequence   z9
Helper class to efficiently store sequences of symbols.
c                     Xl         g r   
_p_symbols)r!   	p_symbolss     r#   r$   _SymbolSequence.__init__   s    #r'   c                 ,    [        U R                  5      $ r   )rY   r}   r+   s    r#   __len___SymbolSequence.__len__   s    4??##r'   c                     [        U[        5      (       a  [        U [        U5      5      $ US:  a  U[	        U 5      -  nUS:  d  U[	        U 5      :  a  [        S5      e[        U R                  U   5      $ )Nr   zinvalid index)r[   slicer   r   rY   
IndexErrorr   r}   )r!   slcs     r#   __getitem___SymbolSequence.__getitem__   sf    c5!!!$c
3373t9C7cSY&_--dooc*++r'   c              #   v   #    [        [        U 5      5       H  n[        U R                  U   5      v   M     g 7fr   )rangerY   r   r}   )r!   ra   s     r#   __iter___SymbolSequence.__iter__   s,     s4y!A+,, "s   79c                 :    SSR                  S U  5       5       S3$ )N[, c              3   8   #    U  H  n[        U5      v   M     g 7fr   )str.0syms     r#   	<genexpr>*_SymbolSequence.__str__.<locals>.<genexpr>  s     6#SXX   ]joinr+   s    r#   rA   _SymbolSequence.__str__  s!    4996667q99r'   c                 :    SSR                  S U  5       5       S3$ )Nr   r   c              3   8   #    U  H  n[        U5      v   M     g 7fr   )reprr   s     r#   r   +_SymbolSequence.__repr__.<locals>.<genexpr>  s     7$3T#YY$r   r   r   r+   s    r#   rE   _SymbolSequence.__repr__  s!    4997$778::r'   r|   N)rG   rH   rI   rJ   rK   r$   r   r   r   rA   rE   rN   rO   r'   r#   ry   ry      s%    $$,-:;r'   ry   c                   h   \ rS rSrSrS rS\S\4S jrS\	\   SS4S	 jr
S
\S\4S jrS
\S\\   4S jr     SS\S\S\S\S\S\	\   4S jjrS rS r\S\4S j5       r\S\\   4S j5       r\S\\   4S j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       r\S\4S j5       rSrg)r   i  a(  
Provides access to a model during a solve call and provides a
`SolveContext` object to influence the running search.

Notes
-----
The string representation of a model object is similar to the output of
models by clingo using the default output.

`Model` objects cannot be constructed from Python. Instead they are obained
during solving (see `Control.solve`). Furthermore, the lifetime of a model
object is limited to the scope of the callback it was passed to or until
the search for the next model is started. They must not be stored for later
use.
c                     Xl         g r   r   r    s     r#   r$   Model.__init__  r&   r'   rc   r(   c                 b    [        S[        R                  U R                  UR                  5      $ )z
Efficiently check if an atom is contained in the model.

Parameters
----------
atom
    The atom to lookup.

Returns
-------
Whether the given atom is contained in the model.

Notes
-----
The atom must be represented using a function symbol.
rM   )r   r   clingo_model_containsr   )r!   rc   s     r#   containsModel.contains  s#    $ vt99499diiPPr'   symbolsNc           	          [         R                  " S[        U5      5      n[        U5       H  u  p4UR                  X#'   M     [        [        R                  " U R                  U[        U5      5      5        g)z
Extend a model with the given symbols.

Parameters
----------
symbols
    The symbols to add to the model.

Notes
-----
This only has an effect if there is an underlying clingo application,
which will print the added symbols.
clingo_symbol_t[]N)r   rX   rY   rZ   r   r   r   clingo_model_extend)r!   r   	c_symbolsra   r   s        r#   extendModel.extend3  sT     HH0#g,?	(FA88IL )d..tyy)S\RSr'   r]   c                 N    [        S[        R                  U R                  U5      $ )z
Check if the given program literal is true.

Parameters
----------
literal
    The given program literal.

Returns
-------
Whether the given program literal is true.
rM   )r   r   clingo_model_is_truer   )r!   r]   s     r#   is_trueModel.is_trueG  s     vt88$))WMMr'   c                     [        S[        R                  U R                  U5      nU[        R                  :X  a  gU[        R
                  :X  a  gg)a  
Check if the given program literal is a consequence.

The function returns `True`, `False`, or `None` if the literal is a
consequence, not a consequence, or it is not yet known whether it is a
consequence, respectively.

While enumerating cautious or brave consequences, there is partial
information about which literals are consequences. The current state of
a literal can be requested using this function. If this function is
used during normal model enumeration, the function just returns whether
a literal is true of false in the current model.

Parameters
----------
literal
    The given program literal.

Returns
-------
Whether the given program literal is a consequence.
clingo_consequence_tTFN)r   r   clingo_model_is_consequencer   clingo_consequence_trueclingo_consequence_false)r!   r]   ress      r#   is_consequenceModel.is_consequenceV  sI    . "D$D$DdiiQX
 $...$///r'   r_   termsshowntheory
complementc                    SnU(       a  U[         R                  -  nU(       a  U[         R                  -  nU(       a  U[         R                  -  nU(       a  U[         R                  -  nU(       a  U[         R
                  -  n[        S[         R                  U R                  U5      n[        R                  " SU5      n[        [         R                  " U R                  XhU5      5        [        U5      $ )a  
Return the list of atoms, terms, or CSP assignments in the model.

Parameters
----------
atoms
    Select all atoms in the model (independent of `#show` statements).
terms
    Select all terms displayed with `#show` statements in the model.
shown
    Select all atoms and terms as outputted by clingo.
theory
    Select atoms added with `Model.extend`.
complement
    Return the complement of the answer set w.r.t. to the atoms known
    to the grounder.

Returns
-------
The selected symbols.

Notes
-----
Atoms are represented using functions (`Symbol` objects), and CSP
assignments are represented using functions with name `"$"` where the
first argument is the name of the CSP variable and the second its
value.
r   size_tr   )r   clingo_show_type_atomsclingo_show_type_termsclingo_show_type_shownclingo_show_type_theoryclingo_show_type_complementr   clingo_model_symbols_sizer   r   rX   r   clingo_model_symbolsry   )	r!   r_   r   r   r   r   showsizer~   s	            r#   r   Model.symbolsv  s    H D///DD///DD///DD000DD444Dx!?!?DQHH0$7	d//		4DQRy))r'   c           	      Z    SR                  [        [        U R                  SS95      5      $ )N T)r   )r   mapr   r   r+   s    r#   rA   Model.__str__  s#    xxCD!9:;;r'   c                 $    SU R                   < S3$ )NzModel(rD   r   r+   s    r#   rE   Model.__repr__  s    		}A&&r'   c                 b    [        S[        R                  U R                  5      n[	        U5      $ )z8
Object that allows for controlling the running search.
zclingo_solve_control_t*)r   r   clingo_model_contextr   r   )r!   ctls     r#   contextModel.context  s(    
 /1J1JDIIVC  r'   c                     [        S[        R                  U R                  5      n[        R
                  " SU5      n[        [        R                  " U R                  X!5      5        [        U5      $ )zl
Return the list of integer cost values of the model.

The return values correspond to clasp's cost output.
r   z	int64_t[])	r   r   clingo_model_cost_sizer   r   rX   r   clingo_model_costlist)r!   r   p_costss      r#   cost
Model.cost  sO     x!<!<diiH((;-d,,TYYFGG}r'   c                     [        S[        R                  U R                  5      n[        R
                  " SU5      n[        [        R                  " U R                  X!5      5        [        U5      $ )z3
Return the priorities of the model's cost values.
r   zclingo_weight_t[])	r   r   r   r   r   rX   r   clingo_model_priorityr   )r!   r   p_prioritiess      r#   priorityModel.priority  sQ    
 x!<!<diiHxx 3T:d00LOPL!!r'   c                 L    [        S[        R                  U R                  5      $ )z"
The running number of the model.
uint64_t)r   r   clingo_model_numberr   r+   s    r#   numberModel.number  s    
 z4#;#;TYYGGr'   c                 L    [        S[        R                  U R                  5      $ )z6
Whether the optimality of the model has been proven.
rM   )r   r   clingo_model_optimality_provenr   r+   s    r#   optimality_provenModel.optimality_proven  s    
 vtBBDIINNr'   c                 L    [        S[        R                  U R                  5      $ )z-
The id of the thread which found the model.
clingo_id_t)r   r   clingo_model_thread_idr   r+   s    r#   	thread_idModel.thread_id  s    
 }d&A&A499MMr'   c                 ^    [        [        S[        R                  U R                  5      5      $ )z
The type of the model.
clingo_model_type_t)r   r   r   clingo_model_typer   r+   s    r#   type
Model.type  s(    
 )4+A+A499M
 	
r'   r   )FFFFF)rG   rH   rI   rJ   rK   r$   r   rM   r   r   r   r\   r   r   r   r   rA   rE   rL   r   r   r   r   r   r   r   r   r   r   rN   rO   r'   r#   r   r     s    QV Q Q(Thv. T4 T(Ns Nt Nc htn D  5*5* 5* 	5*
 5* 5* 
&	5*n<' ! ! ! d3i   	"$s) 	" 	" H H H O4 O O N3 N N 
i 
 
r'   r   c                       \ rS rSrSrS rS\\   4S jrS r	S r
SS	 jrS\\   4S
 jrS\\   4S jrS\4S jrS\\   4S jrSS jrSS\\   S\4S jjrSrg)r   i  aH  
Handle for solve calls.

They can be used to control solving, like, retrieving models or cancelling
a search.

See Also
--------
Control.solve

Notes
-----
A `SolveHandle` is a context manager and must be used with Python's `with`
statement.

Blocking functions in this object release the GIL. They are not thread-safe
though.
c                     Xl         X l        g r   )r   _handler)r!   r"   handlers      r#   r$   SolveHandle.__init__  s    	r'   r(   c              #   `   #     U R                  5         U R                  5       nUc  g Uv   M*  7fr   )resumemodel)r!   ms     r#   r   SolveHandle.__iter__  s.     KKM

AyG s   ,.c                     U $ r   rO   r+   s    r#   	__enter__SolveHandle.__enter__  s    r'   c                 l    [        [        R                  " U R                  5      U R                  5        g)NF)r   r   clingo_solve_handle_closer   r   )r!   exc_typeexc_valexc_tbs       r#   __exit__SolveHandle.__exit__  s"    d44TYY?Or'   Nc                 l    [        [        R                  " U R                  5      U R                  5        g)zP
Cancel the running search.

See Also
--------
clingo.control.Control.interrupt
N)r   r   clingo_solve_handle_cancelr   r   r+   s    r#   cancelSolveHandle.cancel  s!     	d55dii@$--Pr'   c                     [        SS[        R                  U R                  U R                  S9u  p[        U5       Vs/ s H  o1U   PM	     sn$ s  snf )z@
The subset of assumptions that made the problem unsatisfiable.
zclingo_literal_t*r   r   )r   r   clingo_solve_handle_corer   r   r   )r!   corer   ra   s       r#   r  SolveHandle.core%  sM     ))IIMM

 "'t-AQ---s   Ac                     [         R                  " S5      n[        [        R                  " U R
                  U5      U R                  5        US   [         R                  :X  a  g[        US   5      $ )z
The last computed model if there is any.

Notes
-----
If the search is not completed yet or the problem is unsatisfiable, the function returns None.
clingo_model_t**r   N)	r   rX   r   r   clingo_solve_handle_lastr   r   NULLr   r!   p_models     r#   lastSolveHandle.last2  sT     ((-.d33DIIwGW1:"WQZ  r'   c                 t    [        S[        R                  U R                  U R                  S9n[        U5      $ )zu
Get the result of a solve call.

If the search is not completed yet, the function blocks until the
result is ready.
clingo_solve_result_bitset_tr  )r   r   clingo_solve_handle_getr   r   r   )r!   r   s     r#   getSolveHandle.get@  s5     *((IIMM	
 3r'   c                     [         R                  " S5      n[        [        R                  " U R
                  U5      U R                  5        US   [         R                  :X  a  g[        US   5      $ )z(
Get the current model if there is any.
r  r   N)	r   rX   r   r   clingo_solve_handle_modelr   r   r  r   r  s     r#   r   SolveHandle.modelO  sT     ((-.d44TYYH$--X1:"WQZ  r'   c                 l    [        [        R                  " U R                  5      U R                  5        g)z
Discards the last model and starts searching for the next one.

Notes
-----
If the search has been started asynchronously, this function starts the
search in the background.
N)r   r   clingo_solve_handle_resumer   r   r+   s    r#   r   SolveHandle.resumeY  s!     	d55dii@$--Pr'   timeoutc                     [         R                  " S5      n[        R                  " U R                  Uc  SOUU5        US   $ )a  
Wait for solve call to finish or the next result with an optional timeout.

If a timeout is given, the behavior of the function changes depending
on the sign of the timeout. If a postive timeout is given, the function
blocks for the given amount time or until a result is ready. If the
timeout is negative, the function will block until a result is ready,
which also corresponds to the behavior of the function if no timeout is
given. A timeout of zero can be used to poll if a result is ready.

Parameters
----------
timeout
    If a timeout is given, the function blocks for at most timeout seconds.

Returns
-------
Indicates whether the solve call has finished or the next result is ready.
zbool*rV   r   )r   rX   r   clingo_solve_handle_waitr   )r!   r%  p_ress      r#   waitSolveHandle.waitd  s<    ( !%%IIW_r'5	
 Qxr'   )r   r   )r(   Nr   )rG   rH   rI   rJ   rK   r$   r   r   r   r   r  r  r   r\   r  r   r  r   r  r   r   floatrM   r)  rN   rO   r'   r#   r   r     s    & (5/ Q.d3i .!huo ! [  !x !	QHUO t  r'   r   N)rK   typingr   r   r   r   r   r   r	   	_internalr   r   r   r   r   r  r   symbolr   rW   r   utilr   r   __all__r   r   r   ry   r   r   rO   r'   r#   <module>r1     s   GR T S S C C   ) '
N?+ ?+D?$ ?$D &;hv& ;>f
 f
RH./ Hr'   