o
    eQ                     @   sx  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#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 Z.dd Z/dd Z0dd Z1dd Z2dd Z3d d! Z4d"d# Z5d$d% Z6d&d' Z7d(d) Z8dS )*    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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_FLOATZWILL_NOT_DECLAREWILL_NOT_ASSERT r'   r'   PD:\Projects\ConvertPro\env\Lib\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)   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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   r5   Z67z-1)r:   r   r&   r   r   r8   r'   r'   r(   test_Integer,   s   
"r@   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Zauto_declarer5   z(+ (/ 3 7) x)z$(declare-const x Real)
(* (/ 3 7) x))r:   r   r&   r   r   r%   xr?   r'   r'   r(   test_Rational5   s     "rJ   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   FrH   z(assert (= x y))z(assert (not (= x y)))z(assert (<= x y))z(assert (< x y))z(assert (> x y))z(assert (>= x y)))r:   r   r%   r   r   rI   yr   r   r   r   r   r?   r'   r'   r(   test_RelationalB   s   "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 )NFrH   z(pow (sin x) (cos x))ZINTEGER_TYPEZABSOLUTE_VALUE_OF)symbol_tableZknown_typesknown_functionsr5   z4(declare-const x INTEGER_TYPE)
(ABSOLUTE_VALUE_OF x)f1rN   r5   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==)rN   rO   r5   zf(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(assert (== (MY_KNOWN_FUN x z) y))rB   )rO   r5   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   rI   r   absintrL   boolsympyZAbsr   r   floatr   z)r8   Zmy_fun1r'   r'   r(   test_FunctionL   sr   $




	



$rX   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 )NrB   FrH   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'   )rI   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. 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   rI   rL   r   r   r   r   r   )r8   rY   r\   r_   r`   exprr'   r'   r(   test_Pow   sD     *

"
$rf   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 )NFrH   z(* x y)z(+ x y)z(* -1 x))r:   r   r%   r&   r   rI   rL   r?   r'   r'   r(   test_basic_ops   s   "rg   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_exprr1   Z_known_typesrN   ).0symstartend)ri   printerr'   r(   
<listcomp>   s    zFtest_quantifier_extensions.<locals>.ForAll._smtlib.<locals>.<listcomp>forall )limitsrl   function)selfrq   Zbound_symbol_declarations)ri   )rq   r(   _smtlib   s   
z2test_quantifier_extensions.<locals>.ForAll._smtlibc                 S   s   dd | j D S )Nc                 S   s   h | ]\}}}|qS r'   r'   )rm   s_r'   r'   r(   	<setcomp>   s    zKtest_quantifier_extensions.<locals>.ForAll.bound_symbols.<locals>.<setcomp>)ru   rw   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'   r1   rm   ry   r'   r'   r(   r{      s    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}   rv   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st | r|qS r'   )
isinstancetuplerm   rY   )rj   rk   r'   r(   rr      s   ( zFtest_quantifier_extensions.<locals>.ForAll.__new__.<locals>.<listcomp>c                    s   g | ]}t | r|qS r'   )r   r   )rh   rk   r'   r(   rr      s    ra   r   )lenr   __new__ru   rv   r   _args)clsargsru   rv   inst)rh   ForAllrj   rk   r'   r(   r      s"   
z2test_quantifier_extensions.<locals>.ForAll.__new__N)r   r    r!   rx   propertyr}   r   r   r'   rh   r   ri   rj   rk   r'   r(   r      s    

r   fi   rQ   z,(assert (forall ( (x Real [-42, 21])) true))r;   irB   zs(declare-fun f (Real) Real)
(assert (forall ( (x Real [-42, 21]) (y Real [-100, 3])) (=> (= x y) (= (f x) (f y)))))rY   TrZ   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))))))Zsympy.logic.boolalgrh   rU   ri   rj   rk   r   r:   r   r%   r   rI   r   r   rV   rL   r   r   r   )r   r8   rY   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 )Nra   ZMY_PI)known_constantsr5   z(pow MY_PI -1)gQ	@Frc   z&(assert (= MY_PI 3.14))
(pow MY_PI -1)pr`   r9   plusr   rB   )r   rO   	precisionr5   z!(plus 0 1 -1 (/ 1 2) (exp 1) p g)z$(plus 0 1 -1 (/ 1 2) (exp 1) p 1.62))rO   r   r5   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_symbols!  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))rB   zj(declare-const x Bool)
(declare-const y Bool)
(declare-const z Real)
(assert (or (> z 3) (and x (not y))))r   r`   hrQ   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   rI   rL   rW   r   r%   r   r   r   r   rT   rS   r   rV   )r8   r   r`   r   r'   r'   r(   test_booleang  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 )Nra   r;   TFrH   z(ite (< x 1) x (pow x 2))rB   rA      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 Nr=   r   r'   re   r8   r'   r(   <lambda>  s    z'test_smtlib_piecewise.<locals>.<lambda>)
r:   r   r%   r&   r   r   rI   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 )
Nra   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)))rB   z<(declare-const x Real)
(* (/ 1 3) (ite (< x 1) x (pow x 2))))r   rI   r:   r   r%   r&   r   rL   )pwr8   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)Zauto_assertr5   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 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 )NrQ   )r   rI   diffr   rV   r'   r   r8   r'   r(   r     s   & z$test_not_supported.<locals>.<lambda>c                      s   t tj dS r   )r   r	   ZComplexInfinityr'   r?   r'   r(   r     s    )r   r:   r   r%   r&   r   KeyErrorr'   r'   r   r(   test_not_supported  s   "r   )9
contextlibr/   r"   typingenumr   r   rU   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.printing.smtlibr   Zsympy.testing.pytestr   r   rI   rL   rW   r   contextmanagerIterabler:   r@   rJ   rM   rX   rf   rg   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s<    <	
@.XFX#
