o
    ohT                     @   s"  d dl Z d dlZd dlmZmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZ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mZ d dlmZmZmZ d d	l m!Z!m"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.m/Z/m0Z0m1Z1m2Z2 d dl-m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9 d dl:m;Z; d dl<m=Z=m>Z? d dl@mAZA d dlBmCZC d dlDmEZE d dlFmGZGmHZHmIZImJZJmKZK G dd de9ZL						d(ddZMde jNeef deLde jOePgdf fddZQdedeLde jOePgdf fd d!ZRdd"d#e
d$e jSeT d%eTfd&d'ZUdS ))    N)AddMul)SymbolExprFloatRationalIntegerBasic)UndefinedFunctionFunction)
Relational
UnequalityEqualityLessThanGreaterThanStrictLessThanStrictGreaterThan)Abs)explogPow)sinhcoshtanh)MinMax)	Piecewise)sincostanasinacosatanatan2)AndOrXorImpliesBoolean)BooleanTrueBooleanFalseBooleanFunctionNotITE)Printer)Interval)prec_to_dpsto_str)AppliedPredicate)AppliedBinaryRelation)Q)StrictGreaterThanPredicateStrictLessThanPredicateGreaterThanPredicateLessThanPredicateEqualityPredicatec                   @   sf  e Zd ZU dZdedededii i edede	de
d	ed
edede de d	e d
e de dedededededi edededededededede de!de"de#de$de%de&d e'd!e(d"d#Z)e*e+d$< e*e+d%< 		d]d&e,j-e* fd'd(Z.d)e/fd*d+Z0d,e/d-e,j1e2e3f d.e/fd/d0Z4d1d2 Z5d3e6fd4d5Z7d3e8fd6d7Z9d3e:fd8d9Z;d3e<fd:d;Z=d3e>fd<d=Z?d3e@fd>d?ZAd3eBfd@dAZCd3eBfdBdCZDdDeEfdEdFZFdDeGfdGdHZHdDeIfdIdJZJdDefdKdLZKdDeLfdMdNZMdDeNfdOdPZOdDefdQdRZPdDeQfdSdTZRdUdV ZSdWdX ZTdYdZ ZUd[d\ ZVdS )^SMTLibPrinter_smtlibNBoolIntReal+*=z<=z>=<>r   r   absr   r   r   arcsinarccosarctanarctan2r   r   r   minmaxpowandorxornotitez=>)	precisionknown_typesknown_constantsknown_functions_default_settingssymbol_tablesettingsc                 C   s   |pi }|pi | _ t| | | jd | _t| jd | _t| jd | _t| jd | _| j	 D ]	}| 
|s;J q2| j	 D ]	}| 
|sJJ qAd S )NrQ   rR   rS   rT   )rV   r.   __init__	_settings
_precisiondict_known_types_known_constants_known_functionsvalues_is_legal_name)selfrW   rV   _ rc   i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/smtlib.pyrX   T   s   
"zSMTLibPrinter.__init__sc                 C   s*   |sdS |d   rdS tdd |D S )NFr   c                 s   s     | ]}|  p|d kV  qdS )rb   N)isalnum.0rb   rc   rc   rd   	<genexpr>e   s    z/SMTLibPrinter._is_legal_name.<locals>.<genexpr>)	isnumericall)ra   re   rc   rc   rd   r`   b   s   zSMTLibPrinter._is_legal_nameopargsreturnc                    s*   d  fdd|D }d| d| dS )N c                 3   s(    | ]}t |tr|n |V  qd S N)
isinstancestr_print)rh   ara   rc   rd   ri   h   s    
z(SMTLibPrinter._s_expr.<locals>.<genexpr>())join)ra   rl   rm   args_strrc   ru   rd   _s_exprg   s   zSMTLibPrinter._s_exprc                 C   s   || j v r| j | }n8t|| j v r| j t| }n)tt|tkr&|j}nt|tr>|j| j v r>| j |j }| ||jS | j | }| ||j	S rp   )
r^   typer
   namerq   r3   functionrz   	argumentsrm   )ra   erl   rc   rc   rd   _print_Functiono   s   

zSMTLibPrinter._print_Functionr   c                 C   
   |  |S rp   r   ra   r   rc   rc   rd   _print_Relational~      
zSMTLibPrinter._print_Relationalc                 C   r   rp   r   r   rc   rc   rd   _print_BooleanFunction   r   z$SMTLibPrinter._print_BooleanFunctionc                 C   r   rp   r   r   rc   rc   rd   _print_Expr   r   zSMTLibPrinter._print_Exprc                 C   sD   t || jv r| |S | jt }| jt }| || ||jgS rp   )r{   r^   r   r   r,   rz   rm   )ra   r   eq_opnot_oprc   rc   rd   _print_Unequality   s
   


zSMTLibPrinter._print_Unequalityc                    s(   dt jttf f fdd  |jS )Nrm   c              	      s`   | d \}}t | dkr|du st|tsJ |S jt }||| | dd  gS )Nr      T)lenrq   r)   rs   r^   r-   rz   )rm   r   crP   _print_Piecewise_recursivera   rc   rd   r      s   

zBSMTLibPrinter._print_Piecewise.<locals>._print_Piecewise_recursive)typingUnionlisttuplerm   r   rc   r   rd   _print_Piecewise   s   
zSMTLibPrinter._print_Piecewisec                 C   sJ   |j jr
|jjr
dS |j j|jjkrtd| dd|j  d|j dS )N zOne-sided intervals (`z`) are not supported in SMT.[z, ])startis_infiniteend
ValueErrorr   rc   rc   rd   _print_Interval   s
   zSMTLibPrinter._print_Intervalc                 C   s   |j tjkrt|jd d}nX|j tjkr t|jd d}nH|j tjkr0t|jd d}n8|j tj	kr@t
|jd d}n(|j tjkrPt|jd d}n|j tjkr`t|jd d}ntd| d| |S )Nr   zPredicate (`z`) is not handled.)r}   r4   positivegtr~   negativeltzeroeqnonpositivelenonnegativegenonzeroner   _print_AppliedBinaryRelation)ra   r   relrc   rc   rd   _print_AppliedPredicate   s   
z%SMTLibPrinter._print_AppliedPredicatec                 C   s&   |j tjkr| t|j S | |S rp   )r}   r4   r   r   r   r~   r   r   rc   rc   rd   r      s   
z*SMTLibPrinter._print_AppliedBinaryRelationxc                 C      dS )Ntruerc   ra   r   rc   rc   rd   _print_BooleanTrue      z SMTLibPrinter._print_BooleanTruec                 C   r   )Nfalserc   r   rc   rc   rd   _print_BooleanFalse   r   z!SMTLibPrinter._print_BooleanFalsec                 C   s   t |j}t|j|dd d d}d|v r8|d\}}|d dkr&|dd  }| jt }| jt }d||||f S |dv r@td	|S )
NT)strip_zeros	min_fixed	max_fixedr   r   r?   r   z(%s %s (%s 10 %s)))z+infz-infz)Infinite values are not supported in SMT.)	r0   _precmlib_to_str_mpf_splitr^   r   r   r   )ra   r   dpsstr_realmantr   mulrK   rc   rc   rd   _print_Float   s   


zSMTLibPrinter._print_Floatc                 C   s   |  t|S rp   )rs   r   r   rc   rc   rd   _print_float   s   zSMTLibPrinter._print_floatc                 C   s   |  d|j|jgS )N/)rz   pqr   rc   rc   rd   _print_Rational   s   zSMTLibPrinter._print_Rationalc                 C   s   |j dksJ t|jS )Nr   )r   rr   r   r   rc   rc   rd   _print_Integer   s   
zSMTLibPrinter._print_Integerc                 C   s   t |S rp   )rr   r   rc   rc   rd   
_print_int   s   zSMTLibPrinter._print_intc                 C      |  |jsJ |jS rp   r`   r|   r   rc   rc   rd   _print_Symbol      zSMTLibPrinter._print_Symbolc                 C   s8   | j |}|r
|S | jr|| jn| }| |S rp   )r]   getrZ   evalfr   )ra   r   r|   frc   rc   rd   _print_NumberSymbol   s
   
z!SMTLibPrinter._print_NumberSymbolc                 C   r   rp   r   r   rc   rc   rd   _print_UndefinedFunction   r   z&SMTLibPrinter._print_UndefinedFunctionc                 C   s&   t | jv r| t dddS | |S )Nr   F)evaluate)r   r^   r   r   r   rc   rc   rd   _print_Exp1   s
   
zSMTLibPrinter._print_Exp1c                 C   s   t dt| dt| d)NzCannot convert `z` of type `z	` to SMT.)NotImplementedErrorreprr{   )ra   exprrc   rc   rd   emptyPrinter  s   zSMTLibPrinter.emptyPrinter)NN)W__name__
__module____qualname__printmethodboolintfloatr   r   r   r   r   r   r   r9   r8   r7   r6   r5   r   r   r   r   r   r   r    r!   r"   r#   r   r   r   r   r   r   r$   r%   r&   r,   r-   r'   rU   r[   __annotations__r   OptionalrX   rr   r`   r   r   r   rz   r   r   r   r+   r   r   r   r   r   r   r   r/   r   r2   r   r   r)   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rc   rc   rc   rd   r:      s   
 
!"#$%&4 r:   Tc                    s   pdd  t | ts| g} dd | D } |si }t| d|i}i }|r(||d< ~|r/||d< ~|r6||d< ~|r=||d	< ~|sBg }|	sFg }	t||~| D ]A}|ttD ]8}|jrr|jvrr|j	vrr d
| d t
j	|< |jrt|jvrt|j	vr|jstd| dqVqNg }|rŇfdd| D }fdd| D } fdd| D  fdd| D  }dd |D }|rч fdd| D } dg fdd|D tdd |D fdd| D fdd|	D S )a  Converts ``expr`` to a string of smtlib code.

    Parameters
    ==========

    expr : Expr | List[Expr]
        A SymPy expression or system to be converted.
    auto_assert : bool, optional
        If false, do not modify expr and produce only the S-Expression equivalent of expr.
        If true, assume expr is a system and assert each boolean element.
    auto_declare : bool, optional
        If false, do not produce declarations for the symbols used in expr.
        If true, prepend all necessary declarations for variables used in expr based on symbol_table.
    precision : integer, optional
        The ``evalf(..)`` precision for numbers such as pi.
    symbol_table : dict, optional
        A dictionary where keys are ``Symbol`` or ``Function`` instances and values are their Python type i.e. ``bool``, ``int``, ``float``, or ``Callable[...]``.
        If incomplete, an attempt will be made to infer types from ``expr``.
    known_types: dict, optional
        A dictionary where keys are ``bool``, ``int``, ``float`` etc. and values are their corresponding SMT type names.
        If not given, a partial listing compatible with several solvers will be used.
    known_functions : dict, optional
        A dictionary where keys are ``Function``, ``Relational``, ``BooleanFunction``, or ``Expr`` instances and values are their SMT string representations.
        If not given, a partial listing optimized for dReal solver (but compatible with others) will be used.
    known_constants: dict, optional
        A dictionary where keys are ``NumberSymbol`` instances and values are their SMT variable names.
        When using this feature, extra caution must be taken to avoid naming collisions between user symbols and listed constants.
        If not given, constants will be expanded inline i.e. ``3.14159`` instead of ``MY_SMT_VARIABLE_FOR_PI``.
    prefix_expressions: list, optional
        A list of lists of ``str`` and/or expressions to convert into SMTLib and prefix to the output.
    suffix_expressions: list, optional
        A list of lists of ``str`` and/or expressions to convert into SMTLib and postfix to the output.
    log_warn: lambda function, optional
        A function to record all warnings during potentially risky operations.
        Soundness is a core value in SMT solving, so it is good to log all assumptions made.

    Examples
    ========
    >>> from sympy import smtlib_code, symbols, sin, Eq
    >>> x = symbols('x')
    >>> smtlib_code(sin(x).series(x).removeO(), log_warn=print)
    Could not infer type of `x`. Defaulting to float.
    Non-Boolean expression `x**5/120 - x**3/6 + x` will not be asserted. Converting to SMTLib verbatim.
    '(declare-const x Real)\n(+ x (* (/ -1 6) (pow x 3)) (* (/ 1 120) (pow x 5)))'

    >>> from sympy import Rational
    >>> x, y, tau = symbols("x, y, tau")
    >>> smtlib_code((2*tau)**Rational(7, 2), log_warn=print)
    Could not infer type of `tau`. Defaulting to float.
    Non-Boolean expression `8*sqrt(2)*tau**(7/2)` will not be asserted. Converting to SMTLib verbatim.
    '(declare-const tau Real)\n(* 8 (pow 2 (/ 1 2)) (pow tau (/ 7 2)))'

    ``Piecewise`` expressions are implemented with ``ite`` expressions by default.
    Note that if the ``Piecewise`` lacks a default term, represented by
    ``(expr, True)`` then an error will be thrown.  This is to prevent
    generating an expression that may not evaluate to anything.

    >>> from sympy import Piecewise
    >>> pw = Piecewise((x + 1, x > 0), (x, True))
    >>> smtlib_code(Eq(pw, 3), symbol_table={x: float}, log_warn=print)
    '(declare-const x Real)\n(assert (= (ite (> x 0) (+ 1 x) x) 3))'

    Custom printing can be defined for certain types by passing a dictionary of
    PythonType : "SMT Name" to the ``known_types``, ``known_constants``, and ``known_functions`` kwargs.

    >>> from typing import Callable
    >>> from sympy import Function, Add
    >>> f = Function('f')
    >>> g = Function('g')
    >>> smt_builtin_funcs = {  # functions our SMT solver will understand
    ...   f: "existing_smtlib_fcn",
    ...   Add: "sum",
    ... }
    >>> user_def_funcs = {  # functions defined by the user must have their types specified explicitly
    ...   g: Callable[[int], float],
    ... }
    >>> smtlib_code(f(x) + g(x), symbol_table=user_def_funcs, known_functions=smt_builtin_funcs, log_warn=print)
    Non-Boolean expression `f(x) + g(x)` will not be asserted. Converting to SMTLib verbatim.
    '(declare-const x Int)\n(declare-fun g (Int) Real)\n(sum (existing_smtlib_fcn x) (g x))'
    c                 S   s   d S rp   rc   )rb   rc   rc   rd   <lambda>d  s    zsmtlib_code.<locals>.<lambda>c                 S   s   g | ]}t j|d dddqS )TF)strictr   convert_xor)sympysympifyrg   rc   rc   rd   
<listcomp>g  s    zsmtlib_code.<locals>.<listcomp>rV   rQ   rR   rT   rS   Could not infer type of `z`. Defaulting to float.z$Unknown type of undefined function `z^`. Must be mapped to ``str`` in known_functions or mapped to ``Callable[..]`` in symbol_table.c                    s*   i | ]}|j D ]}| jvr|j|qqS rc   )free_symbolsr]   r|   )rh   r   symr   rc   rd   
<dictcomp>  s    
zsmtlib_code.<locals>.<dictcomp>c                    s8   i | ]}| tD ]}t| jvr	|js	|j|q	qS rc   )atomsr   r{   r^   is_Piecewiser|   )rh   r   fncr   rc   rd   r     s    c                       g | ]}t | qS rc   _auto_declare_smtlib)rh   r   log_warnr   rc   rd   r         
c                    r   rc   r   )rh   r   r   rc   rd   r     r   c                 S   s   g | ]}|r|qS rc   rc   )rh   declrc   rc   rd   r     s    c                    r   rc   )_auto_assert_smtlibrh   r   r   rc   rd   r     s    
c                    $   g | ]}t |tr|n |qS rc   rq   rr   doprintr   r   rc   rd   r         c                 s   s    | ]}|V  qd S rp   rc   r   rc   rc   rd   ri     s    zsmtlib_code.<locals>.<genexpr>c                    r   rc   r   r   r   rc   rd   r     r   c                    r   rc   r   r   r   rc   rd   r     r   )rq   r   _auto_infer_smtlib_typesr:   r   r   r   	is_Symbolr]   rV   r   is_Functionr{   r^   r   	TypeErrorr_   rx   sorted)r   auto_assertauto_declarerQ   rV   rR   rS   rT   prefix_expressionssuffix_expressionsr   rW   r   r   declarations	constants	functionsrc   r   rd   smtlib_code  s   Y







r  r   r   r   c                    s   | j r j|  }t|tsJ  j| } d| |gS | jrZ jt|  }t|s,J  fdd|jD }t	|dks>J dd
|d d  d}|d } d	t| ||gS |d
|  d d S )Nzdeclare-constc                    s   g | ]} j | qS rc   )r\   rg   r   rc   rd   r     s    z(_auto_declare_smtlib.<locals>.<listcomp>r   rv   ro   rw   zdeclare-funzNon-Symbol/Function `z` will not be declared.)r   rV   rq   r{   r\   rz   r   callable__args__r   rx   )r   r   r   type_signatureparams_signaturereturn_signaturerc   r   rd   r     s   

r   r   c                 C   sp   t | ts'| |jv r|j|  tks'| jr.t| |jv r.|jt|  jd tkr.|d| gS |d|  d | S )Nr  assertzNon-Boolean expression `z6` will not be asserted. Converting to SMTLib verbatim.)rq   r(   rV   r   r   r{   r
  rz   )r   r   r   rc   rc   rd   r     s   
r   )rV   exprsrV   rn   c                    sZ  | rt | ni  dtf fdd}|dd |D t |dd |D t | fdd|D t | fdd|D t |d	d |D t |d
d |D t dd |D }dd |D dd |D  }|D ]B\}}| v rt | n-| v r| | n%|jrt| v r t| jd n|jrtn|j	s|j
rtn|jrtnd }|r||h| qh S )Nsymsc              	      sH   | D ]}|j s	J  || }|kr!td| d| d| dqd S )Nr   z`. Apparently both `z` and `z`?)r   
setdefaultr   )r  infre   old_type_symbolsrc   rd   safe_update  s   
z-_auto_infer_smtlib_types.<locals>.safe_updatec                 S   s   h | ]}|j r|qS rc   )r   r   rc   rc   rd   	<setcomp>  s    z+_auto_infer_smtlib_types.<locals>.<setcomp>c                 S   s2   h | ]}| tD ]}|jD ]}|jr|qq	qS rc   )r   r+   rm   r   )rh   r   boolfuncsymbolrc   rc   rd   r  	  s    
c                    Z   h | ])}| tD ]!}t| v r*t|j t| jD ]\}}|jr)|tkr|qq	qS rc   )r   r   r{   ziprm   r
  r   r   )rh   r   r  r  paramr  rc   rd   r        
c                    r  rc   )r   r   r{   r  rm   r
  r   r   )rh   r   intfuncr  r  r  rc   rd   r    r  c                 S   s&   h | ]}| tD ]}|jr	|q	qS rc   )r   r   
is_integerrh   r   r  rc   rc   rd   r  #  s    c                 S   s,   h | ]}| tD ]
}|jr	|js	|q	qS rc   )r   r   is_realr  r   rc   rc   rd   r  *  s    c                 S   s    g | ]}| tD ]}|q	qS rc   )r   r   )rh   r   r   rc   rc   rd   r   2  s     z,_auto_infer_smtlib_types.<locals>.<listcomp>c                 S       g | ]}|j jr|j |jfqS rc   )lhsr   rhsrh   r   rc   rc   rd   r   3  
    
c                 S   r"  rc   )r$  r   r#  r%  rc   rc   rd   r   5  r&  r  )r[   setr   r   r   r   r{   r
  
is_Booleanr  
is_Integerr!  )rV   r  r  relsinferreltd	inferencerc   r  rd   r     sn   		

r   )
TTNNNNNNNN)Vr   r   
sympy.corer   r   r   r   r   r   r   r	   sympy.core.functionr
   r   sympy.core.relationalr   r   r   r   r   r   r   $sympy.functions.elementary.complexesr   &sympy.functions.elementary.exponentialr   r   r   %sympy.functions.elementary.hyperbolicr   r   r   (sympy.functions.elementary.miscellaneousr   r   $sympy.functions.elementary.piecewiser   (sympy.functions.elementary.trigonometricr   r   r   r    r!   r"   r#   sympy.logic.boolalgr$   r%   r&   r'   r(   r)   r*   r+   r,   r-   sympy.printing.printerr.   
sympy.setsr/   mpmath.libmp.libmpfr0   r1   r   sympy.assumptions.assumer2   !sympy.assumptions.relation.binrelr3   sympy.assumptions.askr4   #sympy.assumptions.relation.equalityr5   r6   r7   r8   r9   r:   r  r   Callablerr   r   r   r   r[   r   rc   rc   rc   rd   <module>   sR     $$ u
 ,<"