o
    ohW                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlZd dl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mZmZmZmZmZmZ d dlmZmZmZmZ d dl m!Z! d d	l"m#Z# d d
l$m%Z%m&Z& ed\Z'Z(Z)G dd deZ*e j+dej,e* fddZ-dd Z.dd Z/dd Z0dd Z1dd Z2dd Z3dd Z4dd  Z5d!d" Z6d#d$ Z7d%d& Z8d'd( Z9d)d* Z:d+d, Z;d-d. Z<d/d0 Z=dS )1    N)Enum)Callable)AddImpliessqrt)MulPow)SpisymbolsFunctionRationalIntegerSymbolEqNeLeLtGtGe)	Piecewiseexpsincos)Qsmtlib_code)raisesFailedzx,y,zc                   @   s6   e Zd ZedejZedejZedejZdS )_Wz2Could not infer type of `.+`. Defaulting to float.z.Non-Symbol/Function `.+` will not be declared.zPNon-Boolean expression `.+` will not be asserted. Converting to SMTLib verbatim.N)	__name__
__module____qualname__recompileIDEFAULTING_TO_FLOATWILL_NOT_DECLAREWILL_NOT_ASSERT r)   r)   t/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/printing/tests/test_smtlib.pyr      s    r   expectedc              	   c   s    g }|j }|V  g }tt|| D ];\}\}}|s(|d| d| dg7 }q|s8|d| d|j dg7 }q|j|sN|d| d| d|j dg7 }q|rXtd|d S )	N[z] Received unexpected warning `z`.z$] Did not receive expected warning `z] Warning `z` does not match expected .
)	append	enumerate	itertoolszip_longestnamevaluematchr   join)r+   warnslog_warnerrorsiwer)   r)   r*   _check_warns   s    r=   c                  C   s   t tjgd  } ttd| ddksJ ttd| ddks!J W d    n1 s+w   Y  t g } ttddks?J ttddksIJ W d    d S 1 sTw   Y  d S )N   C   r8   67z-1)r=   r   r(   r   r   r;   r)   r)   r*   test_Integer-   s   
"rD   c                  C   s  t tjgd <} ttdd| ddksJ ttdd| ddks#J ttdd	| dd
ks0J ttdd	| ddks=J W d    n1 sGw   Y  t tjtjgd (} tttdd d| ddksgJ ttddt | ddksvJ W d    d S 1 sw   Y  d S )N         r@   z(/ 3 7)   	   2iz(/ -3 7)r>   Fauto_declarer8   z(+ (/ 3 7) x)z$(declare-const x Real)
(* (/ 3 7) x))r=   r   r(   r   r   r&   xrC   r)   r)   r*   test_Rational6   s     "rO   c                  C   s   t tjgd ]} ttttd| ddksJ ttttd| ddks%J ttttd| ddks3J tt	ttd| ddksAJ tt
ttd| ddksOJ ttttd| dd	ks]J W d    d S 1 shw   Y  d S )
N   FrL   (assert (= x y))(assert (not (= x y)))(assert (<= x y))(assert (< x y))(assert (> x y))(assert (>= x y)))r=   r   r&   r   r   rN   yr   r   r   r   r   rC   r)   r)   r*   test_RelationalC      "rX   c                      s   t tjgd b ttttd ddksJ ttttd ddks'J tt	ttd ddks6J tt
ttd ddksEJ ttttd ddksTJ ttttd dd	kscJ W d    n1 smw   Y  tt fd
d d S )NrP   FrL   rQ   rR   rT   rS   rU   rV   c                      s   t tt dS Nr@   )r   r   complexrN   r)   rC   r)   r*   <lambda>V       z,test_AppliedBinaryRelation.<locals>.<lambda>)r=   r   r&   r   r   eqrN   rW   neltlegtger   
ValueErrorr)   r)   rC   r*   test_AppliedBinaryRelationM   s    re   c                  C   s   t tjgd ]} tttd| ddksJ tttd| ddks%J tttd| ddks3J tt	td| ddksAJ tt
td| ddksOJ tttd| dd	ks]J W d    d S 1 shw   Y  d S )
N   FrL   z(assert (> x 0))z(assert (< x 0))z(assert (= x 0))z(assert (<= x 0))z(assert (>= x 0))z(assert (not (= x 0))))r=   r   r&   r   r   positiverN   negativezerononpositivenonnegativenonzerorC   r)   r)   r*   test_AppliedPredicateY   rY   rm   c                  C   s,  t tjtjg} ttttt d| ddksJ W d    n1 s$w   Y  t tjg } ttttt	t
tit	ditjdi| ddksHJ W d    n1 sRw   Y  td}t tjg} t|t|ttgtf i| d	d
ksvJ W d    n1 sw   Y  t g S} t|t|ttgtf i| d	dksJ tt|ttt
|tt	tgtf i| d	dksJ tt|ttt
|tt	tgtf i|dtdi| ddksJ W d    n1 sw   Y  t tjgd } tt|ttt
|dtdi| ddksJ W d    d S 1 sw   Y  d S )NFrL   z(pow (sin x) (cos x))INTEGER_TYPEABSOLUTE_VALUE_OF)symbol_tableknown_typesknown_functionsr8   z4(declare-const x INTEGER_TYPE)
(ABSOLUTE_VALUE_OF x)f1rp   r8   z:(declare-const x Bool)
(declare-fun f1 (Bool) Real)
(f1 x)zC(declare-const x Bool)
(declare-fun f1 (Bool) Bool)
(assert (f1 x))z|(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(declare-fun f1 (Int Bool) Bool)
(assert (= (f1 x z) y))MY_KNOWN_FUNz==)rp   rr   r8   zf(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(assert (== (MY_KNOWN_FUN x z) y))rF   )rr   r8   zg(declare-const x Real)
(declare-const y Real)
(declare-const z Real)
(assert (== (MY_KNOWN_FUN x z) y)))r=   r   r&   r(   r   r   rN   r   absintrW   boolsympyAbsr   r   floatr   z)r;   my_fun1r)   r)   r*   test_Functionb   sr   $




	



$r~   c                  C   s  t tjtjg} ttd d| ddksJ W d    n1 s w   Y  t tjtjtjg} tttd  d| ddks?J W d    n1 sIw   Y  t tjtjg@} tttdd d| ddksgJ tdd	d
}tdd	d}td}dd }d||d |||    |d |  }W d    n1 sw   Y  t g *} tt	|dk |t	||k||d	@ t	|dtdd g| ddksJ W d    n1 sw   Y  t tjtjg#} tt
d|tt
||ddddddd| ddksJ W d    d S 1 sw   Y  d S )NrF   FrL   z	(pow x 3)z(pow x (pow y 3))r>   z(pow x (/ 2 3))aTintegerbrealcc                 S   s   d|  S )Nr>   r)   )rN   r)   r)   r*   g   s    ztest_Pow.<locals>.g   g      @r@   z(declare-const a Int)
(declare-const b Real)
(declare-const c Bool)
(assert (= (< a 2) c))
(assert (= (> b a) c))
(assert c)
(assert (= (* (pow (* 7.0 a) (+ (pow b a) (* -1 a))) (pow (+ b (pow a 2)) -1)) (/ 7 3)))evaluaterB   zG(declare-const b Real)
(declare-const c Real)
(* -2 c (pow (* b b) -1)))r=   r   r&   r(   r   rN   rW   r   r   r   r   r   )r;   r   r   r   r   exprr)   r)   r*   test_Pow   sD     *

"
$r   c                  C   s   t tjtjtjg} ttt d| ddksJ W d    n1 s"w   Y  t tjtjtjg} ttt d| ddks?J W d    n1 sIw   Y  t tjtjg} tt d| ddkscJ W d    d S 1 snw   Y  d S )NFrL   z(* x y)z(+ x y)z(* -1 x))r=   r   r&   r(   r   rN   rW   rC   r)   r)   r*   test_basic_ops   s   "r   c                     s  ddl m  ddlmmm G  fddd td} ttj	g'}t
tddft| t| t| ttgtf i|d	d
ksHJ W d    n1 sRw   Y  ttj	gd 1}t
tddftddfttttt| t| t| ttgtf i|d	dksJ W d    n1 sw   Y  tddd}tddd}td}tg *}t
|ddf|ddft||k t||k |B |ddksJ W d    d S 1 sw   Y  d S )Nr   )Boolean)IntervalTuplesympifyc                       sB   e Zd ZfddZedd Zedd Z fddZd	S )
z*test_quantifier_extensions.<locals>.ForAllc                    s0    fdd| j D } d d|| jgS )Nc              	      s6   g | ]\}}} |jjj|   ||gqS r)   )_s_exprr3   _known_typesrp   ).0symstartend)r   printerr)   r*   
<listcomp>   s    zFtest_quantifier_extensions.<locals>.ForAll._smtlib.<locals>.<listcomp>forall )limitsr   function)selfr   bound_symbol_declarations)r   )r   r*   _smtlib   s   
z2test_quantifier_extensions.<locals>.ForAll._smtlibc                 S   s   dd | j D S )Nc                 S   s   h | ]\}}}|qS r)   r)   )r   s_r)   r)   r*   	<setcomp>   s    zKtest_quantifier_extensions.<locals>.ForAll.bound_symbols.<locals>.<setcomp>)r   r   r)   r)   r*   bound_symbols   s   z8test_quantifier_extensions.<locals>.ForAll.bound_symbolsc                    s&   dd | j D   fdd| jjD S )Nc                 S   s   h | ]}|j qS r)   r3   r   r   r)   r)   r*   r      r]   zJtest_quantifier_extensions.<locals>.ForAll.free_symbols.<locals>.<setcomp>c                    s   h | ]	}|j  vr|qS r)   r   r   bound_symbol_namesr)   r*   r      s
    
)r   r   free_symbolsr   r)   r   r*   r      s   
z7test_quantifier_extensions.<locals>.ForAll.free_symbolsc                    s   fdd|D } fdd|D }t |t | t |ks"J t |dks*J |d }t|rBjg||j |jR  S  | }t||g |_||_||_|S )Nc                    s"   g | ]}t |t fr|qS r)   )
isinstancetupler   r   )r   r   r)   r*   r      s   " zFtest_quantifier_extensions.<locals>.ForAll.__new__.<locals>.<listcomp>c                    s   g | ]}t | r|qS r)   )r   r   )r   r   r)   r*   r      s    r   r   )lenr   __new__r   r   r   _args)clsargsr   r   inst)r   ForAllr   r   r)   r*   r      s"   
z2test_quantifier_extensions.<locals>.ForAll.__new__N)r    r!   r"   r   propertyr   r   r   r)   r   r   r   r   r   r)   r*   r      s    

r   fi   rt   z,(assert (forall ( (x Real [-42, 21])) true))r>   irF   zs(declare-fun f (Real) Real)
(assert (forall ( (x Real [-42, 21]) (y Real [-100, 3])) (=> (= x y) (= (f x) (f y)))))r   Tr   r   r   r   d   r@   zx(declare-const c Bool)
(assert (forall ( (a Int [2, 100]) (b Real [2, 100])) (or c (=> (< a b) (< (pow a (/ 1 2)) b))))))sympy.logic.boolalgr   ry   r   r   r   r   r=   r   r&   r   rN   r   r   r{   rW   r   r   r   )r   r;   r   r   r   r)   r   r*   test_quantifier_extensions   sR   +




"r   c                  C   s  t tjg} tdt tdi| ddksJ W d    n1 s w   Y  t tjg} tttddddt gtdi| ddksBJ W d    n1 sLw   Y  t tjg3} tttjtj	tj
tjtjtjtjddtjd	tjd
tjditdtdid| ddksJ W d    n1 sw   Y  t tjg-} tttjtj	tj
tjtjtjtjddtjd	itdtdid| ddksJ W d    n1 sw   Y  t tjg'} tttjtj	tj
tjtjtjtjddtdid| ddksJ W d    n1 sw   Y  t tjg-} tttjtj	tj
tjtjtjtjddtjditdid| ddks-J W d    d S 1 s9w   Y  d S )Nr   MY_PI)known_constantsr8   z(pow MY_PI -1)gQ	@Fr   z&(assert (= MY_PI 3.14))
(pow MY_PI -1)pr   r<   plusr   rF   )r   rr   	precisionr8   z!(plus 0 1 -1 (/ 1 2) (exp 1) p g)z$(plus 0 1 -1 (/ 1 2) (exp 1) p 1.62))rr   r   r8   z$(plus 0 1 -1 (/ 1 2) 2.72 3.14 1.62)z!(plus 0 1 -1 (/ 1 2) e 3.14 1.62))r=   r   r(   r   r
   r   r   r	   ZeroOneNegativeOneHalfExp1PiGoldenRatior   rC   r)   r)   r*   test_mix_number_mult_symbols7  s   




	$r   c                  C   sz  t g 9} ttt@ | ddksJ tttB | ddksJ tt | ddks(J ttt@ t@ | ddks6J W d    n1 s@w   Y  t tjg} ttt @ tdkB | ddks]J W d    n1 sgw   Y  td}td	}td
}t tjg-} tt|ttt	t|tg|t
tgtf |t
tgtf i| ddksJ W d    n1 sw   Y  t g -} tt|ttt	t|tg|t
tgtf |t
tgtf i| ddksJ W d    n1 sw   Y  t g B} tt|ttt||ttt|||ttg|t
tgtf |t
tgtf |t
tgtf i| ddks*J W d    d S 1 s6w   Y  d S )Nr@   z@(declare-const x Bool)
(declare-const y Bool)
(assert (and x y))z?(declare-const x Bool)
(declare-const y Bool)
(assert (or x y))z'(declare-const x Bool)
(assert (not x))zY(declare-const x Bool)
(declare-const y Bool)
(declare-const z Bool)
(assert (and x y z))rF   zj(declare-const x Bool)
(declare-const y Bool)
(declare-const z Real)
(assert (or (> z 3) (and x (not y))))r   r   hrt   z(declare-const x Bool)
(declare-const y Real)
(declare-const z Bool)
(declare-fun f (Bool) Int)
(declare-fun g (Bool) Int)
(assert (> (f x) y))
(assert (< y (g z)))z(declare-const x Bool)
(declare-const y Int)
(declare-const z Bool)
(declare-fun f (Bool) Int)
(declare-fun g (Bool) Int)
(assert (= (f x) y))
(assert (< y (g z)))z(declare-const x Real)
(declare-const y Int)
(declare-const z Bool)
(declare-fun f (Real) Int)
(declare-fun g (Int) Bool)
(declare-fun h (Bool) Real)
(assert (= (f x) y))
(assert (= (g (f x)) z))
(assert (= (h (g (f x))) x)))r=   r   rN   rW   r|   r   r&   r   r   r   r   rx   rw   r   r{   )r;   r   r   r   r)   r)   r*   test_boolean}  sf   
$




$r   c                      sN  t tjtjgttttdk ftd dfdddksJ W d    n1 s)w   Y  t tjtjg.tttd tdk ftd tdk ftd tdk ftd	 dfddd
ks]J W d    n1 sgw   Y  tttdk ftd tdkftttdkf t tjtjgtt	 fdd W d    d S 1 sw   Y  d S )Nr   r>   TFrL   z(ite (< x 1) x (pow x 2))rF   rE      zQ(ite (< x 1) (pow x 2) (ite (< x 2) (pow x 3) (ite (< x 3) (pow x 4) (pow x 5))))r   c                      s   t  dS rZ   r   r)   r   r;   r)   r*   r\     s    z'test_smtlib_piecewise.<locals>.<lambda>)
r=   r   r&   r(   r   r   rN   r   r   AssertionErrorr)   r)   r   r*   test_smtlib_piecewise  s8   



,"r   c                  C   sH  t ttdk ftd df} ttjtjg}td|  |ddks"J W d    n1 s,w   Y  ttjtjg}t| t |ddksFJ W d    n1 sPw   Y  ttjtjtjg}t| tt  |ddksnJ W d    n1 sxw   Y  ttjtjg}t| d |dd	ksJ W d    d S 1 sw   Y  d S )
Nr   r>   Tr@   z6(declare-const x Real)
(* 2 (ite (< x 1) x (pow x 2)))z?(declare-const x Real)
(* (pow x -1) (ite (< x 1) x (pow x 2)))za(declare-const x Real)
(declare-const y Real)
(* (pow x -1) (pow y -1) (ite (< x 1) x (pow x 2)))rF   z<(declare-const x Real)
(* (/ 1 3) (ite (< x 1) x (pow x 2))))r   rN   r=   r   r&   r(   r   rW   )pwr;   r)   r)   r*   !test_smtlib_piecewise_times_const  s   "r   c                  C   s   t g I} tdd| ddksJ td| ddksJ ttj| ddks%J ttj| ddks0J td| ddks:J tdd| ddksEJ W d    d S 1 sPw   Y  d S )	NTF)auto_assertr8   truer@   z(assert true)z(assert false)false)r=   r   r	   r   r   rC   r)   r)   r*   test_smtlib_boolean  s   
"r   c                      s   t d ttjtjgtt fdd W d    n1 s!w   Y  ttjgttfdd W d    d S 1 sAw   Y  d S )Nr   c                      s&   t  tt ttgtf idS )Nrt   )r   rN   diffr   r{   r)   r   r;   r)   r*   r\   !  s   & z$test_not_supported.<locals>.<lambda>c                      s   t tj dS rZ   )r   r	   ComplexInfinityr)   rC   r)   r*   r\   #  s    )r   r=   r   r&   r(   r   KeyErrorr)   r)   r   r*   test_not_supported  s   "r   c                   C   s4   t ddksJ t ddksJ t ddksJ d S )Ng        z0.0geK<z(* 3.0 (pow 10 -18))g333333@z5.3r   r)   r)   r)   r*   
test_Float&  s   r   )>
contextlibr1   r#   typingenumr   r   ry   r   r   r   
sympy.corer   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   sympy.functionsr   r   r   r   sympy.assumptions.askr   sympy.printing.smtlibr   sympy.testing.pytestr   r   rN   rW   r|   r   contextmanagerIterabler=   rD   rO   rX   re   rm   r~   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   sD    <	
	@.XFX#
