a
    khX                     @   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+ej,e* d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
IGNORECASEDEFAULTING_TO_FLOATZWILL_NOT_DECLAREWILL_NOT_ASSERT r(   r(   N/var/www/auris/lib/python3.9/site-packages/sympy/printing/tests/test_smtlib.pyr      s   r   )expectedc              	   c   s   g }|j }|V  g }tt|| D ]v\}\}}|sN|d| d| dg7 }q$|sn|d| d|j dg7 }q$|j|s$|d| d| d|j dg7 }q$|rt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*   Z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sBJ W d    n1 sV0    Y  t g 8} ttddks~J ttddksJ W d    n1 s0    Y  d S )N   C   r6   Z67z-1)r;   r   r'   r   r   r9   r(   r(   r)   test_Integer-   s    6
rA   c                  C   s  t tjgd x} ttdd| ddks,J ttdd| ddksFJ ttdd	| dd
ks`J ttdd	| ddkszJ W d    n1 s0    Y  t tjtjgd N} tttdd d| ddksJ ttddt | ddksJ W d    n1 s0    Y  d S )N         r>   z(/ 3 7)   	   2iz(/ -3 7)r<   FZauto_declarer6   z(+ (/ 3 7) x)z$(declare-const x Real)
(* (/ 3 7) x))r;   r   r'   r   r   r&   xr@   r(   r(   r)   test_Rational6   s    8 rK   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sJJ ttttd| ddksfJ tt	ttd| ddksJ tt
ttd| ddksJ ttttd| dd	ksJ W d    n1 s0    Y  d S )
N   FrI   (assert (= x y))(assert (not (= x y)))(assert (<= x y))(assert (< x y))(assert (> x y))(assert (>= x y)))r;   r   r&   r   r   rJ   yr   r   r   r   r   r@   r(   r(   r)   test_RelationalC   s    rT   c                      s   t tjgd ĉ ttttd ddks0J ttttd ddksNJ tt	ttd ddkslJ tt
ttd ddksJ ttttd ddksJ ttttd dd	ksJ W d    n1 s0    Y  tt fd
d d S )NrL   FrI   rM   rN   rP   rO   rQ   rR   c                      s   t tt dS Nr>   )r   r   complexrJ   r(   r@   r(   r)   <lambda>V       z,test_AppliedBinaryRelation.<locals>.<lambda>)r;   r   r&   r   r   eqrJ   rS   neltlegtger   
ValueErrorr(   r(   r@   r)   test_AppliedBinaryRelationM   s    <r`   c                  C   s   t tjgd } tttd| ddks.J tttd| ddksJJ tttd| ddksfJ tt	td| ddksJ tt
td| ddksJ tttd| dd	ksJ W d    n1 s0    Y  d S )
N   FrI   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   ZpositiverJ   negativeZzerononpositiveZnonnegativeZnonzeror@   r(   r(   r)   test_AppliedPredicateY   s    rd   c                  C   s4  t tjtjg2} ttttt d| ddks4J W d    n1 sH0    Y  t tjg@} ttttt	t
tit	ditjdi| ddksJ W d    n1 s0    Y  td}t tjg8} t|t|ttgtf i| d	d
ksJ W d    n1 s0    Y  t g } t|t|ttgtf i| d	dks@J tt|ttt
|tt	tgtf i| d	dkstJ tt|ttt
|tt	tgtf i|dtdi| ddksJ W d    n1 s0    Y  t tjgd <} tt|ttt
|dtdi| ddksJ W d    n1 s&0    Y  d S )NFrI   z(pow (sin x) (cos x))ZINTEGER_TYPEZABSOLUTE_VALUE_OF)symbol_tableZknown_typesknown_functionsr6   z4(declare-const x INTEGER_TYPE)
(ABSOLUTE_VALUE_OF x)f1re   r6   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))ZMY_KNOWN_FUNz==)re   rf   r6   zf(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(assert (== (MY_KNOWN_FUN x z) y))rC   )rf   r6   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   rJ   r   absintrS   boolsympyZAbsr   r   floatr   z)r9   Zmy_fun1r(   r(   r)   test_Functionb   sh    @
&	(




*

ro   c                  C   s  t tjtjg*} ttd d| ddks,J W d    n1 s@0    Y  t tjtjtjg.} tttd  d| ddks~J W d    n1 s0    Y  t tjtjg} tttdd d| ddksJ tdd	d
}tdd	d}td}dd }d||d |||    |d |  }W d    n1 s40    Y  t g V} tt	|dk |t	||k||d	@ t	|dtdd g| ddksJ W d    n1 s0    Y  t tjtjgF} tt
d|tt
||ddddddd| ddksJ W d    n1 s0    Y  d S )NrC   FrI   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(   )rJ   r(   r(   r)   g   rX   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)))evaluater?   zG(declare-const b Real)
(declare-const c Real)
(* -2 c (pow (* b b) -1)))r;   r   r&   r'   r   rJ   rS   r   r   r   r   r   )r9   rp   rs   rv   rw   exprr(   r(   r)   test_Pow   s:    8< H
*"r}   c                  C   s   t tjtjtjg*} ttt d| ddks0J W d    n1 sD0    Y  t tjtjtjg*} ttt d| ddks~J W d    n1 s0    Y  t tjtjg(} tt d| ddksJ W d    n1 s0    Y  d S )NFrI   z(* x y)z(+ x y)z(* -1 x))r;   r   r&   r'   r   rJ   rS   r@   r(   r(   r)   test_basic_ops   s    88r~   c                     s  ddl m  ddlmmm G  fddd td} ttj	gN}t
tddft| t| t| ttgtf i|d	d
ksJ W d    n1 s0    Y  ttj	gd d}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sJ W d    n1 s*0    Y  tddd}tddd}td}tg T}t
|ddf|ddft||k t||k |B |ddksJ W d    n1 s0    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_exprr2   Z_known_typesre   ).0symstartend)r   printerr(   r)   
<listcomp>   s
   zFtest_quantifier_extensions.<locals>.ForAll._smtlib.<locals>.<listcomp>forall )limitsr   function)selfr   Z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>   rX   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(   r2   r   r   r(   r(   r)   r      rX   zJtest_quantifier_extensions.<locals>.ForAll.free_symbols.<locals>.<setcomp>c                    s   h | ]}|j  vr|qS r(   r   r   Z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sDJ t |dksTJ |d }t|rjg||j |jR  S  | }t||g |_||_||_|S )Nc                    s"   g | ]}t |t fr|qS r(   )
isinstancetupler   rp   )r   r   r(   r)   r      rX   zFtest_quantifier_extensions.<locals>.ForAll.__new__.<locals>.<listcomp>c                    s   g | ]}t | r|qS r(   )r   r   )r   r   r(   r)   r      rX   rx   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   rh   z,(assert (forall ( (x Real [-42, 21])) true))r<   irC   zs(declare-fun f (Real) Real)
(assert (forall ( (x Real [-42, 21]) (y Real [-100, 3])) (=> (= x y) (= (f x) (f y)))))rp   Trq   rs   rt   rv   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))))))Zsympy.logic.boolalgr   rl   r   r   r   r   r;   r   r&   r   rJ   r   r   rm   rS   r   r   r   )r   r9   rp   rs   rv   r(   r   r)   test_quantifier_extensions   sL    +&*

r   c                  C   s  t tjg.} tdt tdi| ddks,J W d    n1 s@0    Y  t tjg<} tttddddt gtdi| ddksJ W d    n1 s0    Y  t tjgh} 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sJ W d    n1 s0    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 s0    Y  t tjgP} 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 s0    Y  t tjgX} 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sfJ W d    n1 s|0    Y  d S )Nrx   ZMY_PI)known_constantsr6   z(pow MY_PI -1)gQ	@Frz   z&(assert (= MY_PI 3.14))
(pow MY_PI -1)prw   r:   plusr   rC   )r   rf   	precisionr6   z!(plus 0 1 -1 (/ 1 2) (exp 1) p g)z$(plus 0 1 -1 (/ 1 2) (exp 1) p 1.62))rf   r   r6   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	   ZZeroZOneZNegativeOneZHalfZExp1PiZGoldenRatior   r@   r(   r(   r)   test_mix_number_mult_symbols7  s    &&
***r   c                  C   s  t g r} ttt@ | ddks"J tttB | ddks:J tt | ddksPJ ttt@ t@ | ddkslJ W d    n1 s0    Y  t tjg2} ttt @ tdkB | ddksJ W d    n1 s0    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sJJ W d    n1 s`0    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 s0    Y  t g } 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    n1 sr0    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))rC   zj(declare-const x Bool)
(declare-const y Bool)
(declare-const z Real)
(assert (or (> z 3) (and x (not y))))r   rw   hrh   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   rJ   rS   rn   r   r&   r   r   r   r   rk   rj   r   rm   )r9   r   rw   r   r(   r(   r)   test_boolean}  s\    
:@*
*

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 sR0    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sJ W d    n1 s0    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    n1 s@0    Y  d S )Nrx   r<   TFrI   z(ite (< x 1) x (pow x 2))rC   rB      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 rU   r   r(   r|   r9   r(   r)   rW     rX   z'test_smtlib_piecewise.<locals>.<lambda>)
r;   r   r&   r'   r   r   rJ   r   r   AssertionErrorr(   r(   r   r)   test_smtlib_piecewise  s2    
&
&,r   c                  C   sJ  t ttdk ftd df} ttjtjg(}td|  |ddksDJ W d    n1 sX0    Y  ttjtjg(}t| t |ddksJ W d    n1 s0    Y  ttjtjtjg,}t| tt  |ddksJ W d    n1 s0    Y  ttjtjg*}t| d |dd	ks&J W d    n1 s<0    Y  d S )
Nrx   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)))rC   z<(declare-const x Real)
(* (/ 1 3) (ite (< x 1) x (pow x 2))))r   rJ   r;   r   r&   r'   r   rS   )pwr9   r(   r(   r)   !test_smtlib_piecewise_times_const  s    66:r   c                  C   s   t g } tdd| ddks J td| ddks4J ttj| ddksJJ ttj| ddks`J td| ddkstJ tdd| ddksJ W d    n1 s0    Y  d S )	NTF)Zauto_assertr6   truer>   z(assert true)z(assert false)false)r;   r   r	   r   r   r@   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 sB0    Y  ttjg"ttfdd W d    n1 s0    Y  d S )Nr   c                      s&   t  tt ttgtf idS )Nrh   )r   rJ   diffr   rm   r(   r   r9   r(   r)   rW   !  rX   z$test_not_supported.<locals>.<lambda>c                      s   t tj dS rU   )r   r	   ZComplexInfinityr(   r@   r(   r)   rW   #  rX   )r   r;   r   r&   r'   r   KeyErrorr(   r(   r   r)   test_not_supported  s
    2r   c                   C   s4   t ddksJ t ddks J t ddks0J d S )Ng        z0.0geK<z(* 3.0 (pow 10 -18))g333333@z5.3r   r(   r(   r(   r)   
test_Float&  s    r   )>
contextlibr0   r#   typingenumr   r   rl   r   r   r   Z
sympy.corer   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   Zsympy.functionsr   r   r   r   Zsympy.assumptions.askr   Zsympy.printing.smtlibr   Zsympy.testing.pytestr   r   rJ   rS   rn   r   contextmanagerIterabler;   rA   rK   rT   r`   rd   ro   r}   r~   r   r   r   r   r   r   r   r   r(   r(   r(   r)   <module>   sB   <	
	@.XFX#
