U
    9%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)ej*e( d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'   _/var/www/html/Darija-Ai-API/env/lib/python3.8/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 6} ttd| ddks*tttd| ddksBtW 5 Q R X t g .} ttddksjtttddks~tW 5 Q R X d S )N   C   r5   Z67z-1)r:   r   r&   r   r   AssertionErrorr8   r'   r'   r(   test_Integer,   s    "
rA   c               	   C   s   t tjgd n} ttdd| ddks,tttdd| ddksFtttdd	| dd
ks`tttdd	| ddksztW 5 Q R X t tjtjgd D} tttdd d| ddkstttddt | ddkstW 5 Q R X 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?   r%   xr@   r'   r'   r(   test_Rational5   s    $ rK   c               	   C   s   t tjgd } ttttd| ddks.tttttd| ddksJttt	ttd| ddksfttt
ttd| ddkstttttd| ddkstttttd| dd	kstW 5 Q R X d S )
N   FrI   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   rJ   yr?   r   r   r   r   r   r@   r'   r'   r(   test_RelationalB   s    rN   c               	   C   s  t tjtjg(} ttttt d| ddks4tW 5 Q R X t tjg6} tt	ttt
ttit
ditjdi| ddks|tW 5 Q R X td}t tjg.} t|t|ttgtf i| d	d
kstW 5 Q R X t g } t|t|ttgtf i| d	dksttt|ttt|tt
tgtf i| d	dks6ttt|ttt|tt
tgtf i|dtdi| ddksttW 5 Q R X t tjgd 2} tt|ttt|dtdi| ddkstW 5 Q R X d S )NFrI   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rO   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==)rO   rP   r5   zf(declare-const x Int)
(declare-const y Bool)
(declare-const z Bool)
(assert (== (MY_KNOWN_FUN x z) y))rC   )rP   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   rJ   r   r?   absintrM   boolsympyZAbsr   r   floatr   z)r8   Zmy_fun1r'   r'   r(   test_FunctionL   sh    ,
	






rY   c                  C   s  t tjtjg } ttd d| ddks,tW 5 Q R X t tjtjtjg$} tttd  d| ddksjtW 5 Q R X t tjtjgv} tttdd d| ddkstt	dd	d
}t	dd	d}t	d}dd }d||d |||    |d |  }W 5 Q R X t g L} tt
|dk |t
||k||d	@ t
|dtdd g| ddksPtW 5 Q R X t tjtjg<} ttd|tt||ddddddd| ddkstW 5 Q R X 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       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   rJ   r?   rM   r   r   r   r   r   )r8   rZ   r]   r`   ra   exprr'   r'   r(   test_Pow   s:    $( 2
"rh   c               	   C   s   t tjtjtjg } ttt d| ddks0tW 5 Q R X t tjtjtjg } ttt d| ddksjtW 5 Q R X t tjtjg} tt d| ddkstW 5 Q R X d S )NFrI   z(* x y)z(+ x y)z(* -1 x))r:   r   r%   r&   r   rJ   rM   r?   r@   r'   r'   r(   test_basic_ops   s    $$ri   c                     s  ddl m  ddlmmm G  fddd td} ttj	gD}t
tddft| t| t| ttgtf i|d	d
kstW 5 Q R X ttj	gd X}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stW 5 Q R X tddd}tddd}td}tg J}t
|ddf|ddft||k t||k |B |ddksvtW 5 Q R X 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_typesrO   ).0symstartend)rk   printerr'   r(   
<listcomp>   s
   zFtest_quantifier_extensions.<locals>.ForAll._smtlib.<locals>.<listcomp>forall )limitsrn   function)selfrs   Zbound_symbol_declarations)rk   )rs   r(   _smtlib   s    
z2test_quantifier_extensions.<locals>.ForAll._smtlibc                 S   s   dd | j D S )Nc                 S   s   h | ]\}}}|qS r'   r'   )ro   s_r'   r'   r(   	<setcomp>   s     zKtest_quantifier_extensions.<locals>.ForAll.bound_symbols.<locals>.<setcomp>)rw   ry   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   ro   r{   r'   r'   r(   r}      s     zJtest_quantifier_extensions.<locals>.ForAll.free_symbols.<locals>.<setcomp>c                    s   h | ]}|j  kr|qS r'   r   r   Zbound_symbol_namesr'   r(   r}      s   
)r   rx   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sDtt |dksTt|d }t|rjf||j |jf S  | }t||g |_||_||_|S )Nc                    s(   g | ] }t |tst | r|qS r'   )
isinstancetuplero   rZ   )rl   rm   r'   r(   rt      s     
 
 zFtest_quantifier_extensions.<locals>.ForAll.__new__.<locals>.<listcomp>c                    s   g | ]}t | r|qS r'   )r   r   )rj   rm   r'   r(   rt      s     
 rc   r   )lenr?   r   __new__rw   rx   r   _args)clsargsrw   rx   inst)rj   ForAllrl   rm   r'   r(   r      s"    
  
z2test_quantifier_extensions.<locals>.ForAll.__new__N)r   r    r!   rz   propertyr   r   r   r'   rj   r   rk   rl   rm   r'   r(   r      s   

r   fi   rR   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)))))rZ   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))))))Zsympy.logic.boolalgrj   rV   rk   rl   rm   r   r:   r   r%   r   rJ   r   r   rW   r?   rM   r   r   r   )r   r8   rZ   r]   r`   r'   r   r(   test_quantifier_extensions   sP    + 
 r   c                  C   s  t tjg$} tdt tdi| ddks,tW 5 Q R X t tjg2} tttddddt gtdi| ddksptW 5 Q R X 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	tjd
tjditdtdid| ddkstW 5 Q R X t tjgR} 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sBtW 5 Q R X t tjgF} 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stW 5 Q R X t tjgN} 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stW 5 Q R X d S )Nrc   ZMY_PI)known_constantsr5   z(pow MY_PI -1)gQ	@Fre   z&(assert (= MY_PI 3.14))
(pow MY_PI -1)pra   r9   plusr   rC   )r   rP   	precisionr5   z!(plus 0 1 -1 (/ 1 2) (exp 1) p g)z$(plus 0 1 -1 (/ 1 2) (exp 1) p 1.62))rP   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   r	   ZZeroZOneZNegativeOneZHalfZExp1PiZGoldenRatior   r@   r'   r'   r(   test_mix_number_mult_symbols!  s    
                     r   c                  C   s  t g h} ttt@ | ddks"ttttB | ddks:ttt | ddksPtttt@ t@ | ddksltW 5 Q R X t tjg(} ttt @ tdkB | ddkstW 5 Q R X td}td	}td
}t tjgR} tt	|ttt
t|tg|ttgtf |ttgtf i| ddks"tW 5 Q R X t g R} tt|ttt
t|tg|ttgtf |ttgtf i| ddkstW 5 Q R X t g x} 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tW 5 Q R X 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   ra   hrR   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   rM   r?   rX   r   r%   r   r   r   r   rU   rT   r   rW   )r8   r   ra   r   r'   r'   r(   test_booleang  sn    
&,   
   
   
r   c                	      s  t tjtjg2ttttdk ftd dfdddks>tW 5 Q R X t tjtjgR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stW 5 Q R X 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 5 Q R X d S )Nrc   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 Nr=   r   r'   rg   r8   r'   r(   <lambda>  rb   z'test_smtlib_piecewise.<locals>.<lambda>)
r:   r   r%   r&   r   r   rJ   r?   r   r   r'   r'   r   r(   test_smtlib_piecewise  s2    

,r   c               	   C   s   t ttdk ftd df} ttjtjg}td|  |ddksDtW 5 Q R X ttjtjg}t| t |ddksxtW 5 Q R X ttjtjtjg"}t| tt  |ddkstW 5 Q R X ttjtjg}t| d |dd	kstW 5 Q R X d S )
Nrc   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   r?   rM   )pwr8   r'   r'   r(   !test_smtlib_piecewise_times_const  s    ""&r   c               	   C   s   t g } tdd| ddks ttd| ddks4tttj| ddksJtttj| ddks`ttd| ddkstttdd| ddkstW 5 Q R X d S )	NTF)Zauto_assertr5   truer=   z(assert true)z(assert false)false)r:   r   r?   r	   r   r   r@   r'   r'   r(   test_smtlib_boolean  s    
r   c                	      sf   t d ttjtjgtt fdd W 5 Q R X ttjgttfdd W 5 Q R X d S )Nr   c                      s&   t  tt ttgtf idS )NrR   )r   rJ   diffr   rW   r'   r   r8   r'   r(   r     rb   z$test_not_supported.<locals>.<lambda>c                      s   t tj dS r   )r   r	   ZComplexInfinityr'   r@   r'   r(   r     rb   )r   r:   r   r%   r&   r   KeyErrorr'   r'   r   r(   test_not_supported  s
    r   )9
contextlibr/   r"   typingenumr   r   rV   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   rJ   rM   rX   r   contextmanagerIterabler:   rA   rK   rN   rY   rh   ri   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s:   <	
@.XFX#
