<s> import math <EOL> import fractions <EOL> import sys <EOL> from . . lang import data <EOL> from . . lang import d <EOL> from . . lang import deriving <EOL> from . . lang import H <EOL> from . . lang import sig <EOL> from . . lang import t <EOL> from . . lang import instance <EOL> from . . lang import build_instance <EOL> from . . lang import Enum <EOL> from . . lang import Show <EOL> from Eq import Eq <EOL> from Ord import Ord <EOL> class Num ( Show , Eq ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , add , mul , abs , signum , fromInteger , <EOL> negate , sub = None ) : <EOL> @ sig ( H [ ( Num , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def default_sub ( a , b ) : <EOL> return a . __add__ ( b . __neg__ ( ) ) <EOL> sub = default_sub if sub is None else sub <EOL> attrs = { "<STR_LIT>" : add , "<STR_LIT>" : mul , "<STR_LIT>" : abs , "<STR_LIT>" : signum , <EOL> "<STR_LIT>" : fromInteger , "<STR_LIT>" : negate , "<STR_LIT>" : sub } <EOL> build_instance ( Num , cls , attrs ) <EOL> return <EOL> @ sig ( H [ ( Num , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def negate ( a ) : <EOL> """<STR_LIT>""" <EOL> return Num [ a ] . negate ( a ) <EOL> @ sig ( H [ ( Num , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def signum ( a ) : <EOL> """<STR_LIT>""" <EOL> return Num [ a ] . signum ( a ) <EOL> @ sig ( H [ ( Num , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def abs ( a ) : <EOL> """<STR_LIT>""" <EOL> return Num [ a ] . abs ( a ) <EOL> instance ( Num , int ) . where ( <EOL> add = int . __add__ , <EOL> mul = int . __mul__ , <EOL> abs = int . __abs__ , <EOL> signum = lambda x : - <NUM_LIT:1> if x < <NUM_LIT:0> else ( <NUM_LIT:1> if x > <NUM_LIT:0> else <NUM_LIT:0> ) , <EOL> fromInteger = int , <EOL> negate = int . __neg__ , <EOL> sub = int . __sub__ <EOL> ) <EOL> instance ( Num , long ) . where ( <EOL> add = long . __add__ , <EOL> mul = long . __mul__ , <EOL> abs = long . __abs__ , <EOL> signum = lambda x : - <NUM_LIT:1> L if x < <NUM_LIT:0> L else ( <NUM_LIT:1> L if x > <NUM_LIT:0> L else <NUM_LIT:0> L ) , <EOL> fromInteger = long , <EOL> negate = long . __neg__ , <EOL> sub = long . __sub__ <EOL> ) <EOL> instance ( Num , float ) . where ( <EOL> add = float . __add__ , <EOL> mul = float . __mul__ , <EOL> abs = float . __abs__ , <EOL> signum = lambda x : - <NUM_LIT:1.0> if x < <NUM_LIT:0.0> else ( <NUM_LIT:1.0> if x > <NUM_LIT:0.0> else <NUM_LIT:0.0> ) , <EOL> fromInteger = float , <EOL> negate = float . __neg__ , <EOL> sub = float . __sub__ <EOL> ) <EOL> instance ( Num , complex ) . where ( <EOL> add = complex . __add__ , <EOL> mul = complex . __mul__ , <EOL> abs = complex . __abs__ , <EOL> signum = lambda x : complex ( - <NUM_LIT:1> ) if x < <NUM_LIT:0> else complex ( <NUM_LIT:1> if x > <NUM_LIT:0> else <NUM_LIT:0> ) , <EOL> fromInteger = complex , <EOL> negate = complex . __neg__ , <EOL> sub = complex . __sub__ <EOL> ) <EOL> class Fractional ( Num ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , fromRational , div , recip = None ) : <EOL> if recip is None : <EOL> recip = lambda x : div ( <NUM_LIT:1> , x ) <EOL> attrs = { "<STR_LIT>" : fromRational , "<STR_LIT>" : div , "<STR_LIT>" : recip } <EOL> build_instance ( Fractional , cls , attrs ) <EOL> return <EOL> Ratio , R = data . Ratio ( "<STR_LIT:a>" ) == d . R ( "<STR_LIT:a>" , "<STR_LIT:a>" ) & deriving ( Eq ) <EOL> Rational = t ( Ratio , int ) <EOL> instance ( Fractional , float ) . where ( <EOL> fromRational = lambda rat : float ( rat [ <NUM_LIT:0> ] ) / float ( rat [ <NUM_LIT:1> ] ) , <EOL> div = float . __div__ , <EOL> recip = lambda x : <NUM_LIT:1.0> / x <EOL> ) <EOL> @ sig ( H [ ( Fractional , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def recip ( a ) : <EOL> """<STR_LIT>""" <EOL> return Fractional [ a ] . recip ( a ) <EOL> class Floating ( Fractional ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , pi , exp , sqrt , log , pow , logBase , sin , <EOL> tan , cos , asin , atan , acos , sinh , tanh , cosh , asinh , atanh , acosh ) : <EOL> attrs = { "<STR_LIT>" : pi , "<STR_LIT>" : exp , "<STR_LIT>" : sqrt , "<STR_LIT>" : log , "<STR_LIT>" : pow , <EOL> "<STR_LIT>" : logBase , "<STR_LIT>" : sin , "<STR_LIT>" : tan , "<STR_LIT>" : cos , <EOL> "<STR_LIT>" : asin , "<STR_LIT>" : atan , "<STR_LIT>" : acos , "<STR_LIT>" : sinh , <EOL> "<STR_LIT>" : tanh , "<STR_LIT>" : cosh , "<STR_LIT>" : asinh , "<STR_LIT>" : atanh , <EOL> "<STR_LIT>" : acosh } <EOL> build_instance ( Floating , cls , attrs ) <EOL> return <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def exp ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . exp ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def sqrt ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . sqrt ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def log ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . log ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def pow ( x , y ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . pow ( x , y ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def logBase ( x , b ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . logBase ( x , b ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def sin ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . sin ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def cos ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . cos ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def tan ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . tan ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def asin ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . asin ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def atan ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . atan ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def acos ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . acos ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def sinh ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . sinh ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def tanh ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . tanh ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def cosh ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . cosh ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def asinh ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . asinh ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def atanh ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . atanh ( x ) <EOL> @ sig ( H [ ( Floating , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def acosh ( x ) : <EOL> """<STR_LIT>""" <EOL> return Floating [ x ] . acosh ( x ) <EOL> instance ( Floating , float ) . where ( <EOL> pi = math . pi , <EOL> exp = math . exp , <EOL> sqrt = math . sqrt , <EOL> log = math . log , <EOL> pow = pow , <EOL> logBase = math . log , <EOL> sin = math . sin , <EOL> tan = math . tan , <EOL> cos = math . cos , <EOL> asin = math . asin , <EOL> atan = math . atan , <EOL> acos = math . acos , <EOL> sinh = math . sinh , <EOL> tanh = math . tanh , <EOL> cosh = math . cosh , <EOL> asinh = math . asinh , <EOL> atanh = math . atanh , <EOL> acosh = math . acosh <EOL> ) <EOL> class Real ( Num , Ord ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , toRational ) : <EOL> build_instance ( Real , cls , { } ) <EOL> return <EOL> @ sig ( H [ ( Real , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> Rational ) <EOL> def toRational ( x ) : <EOL> """<STR_LIT>""" <EOL> return Real [ x ] . toRational ( x ) <EOL> class Integral ( Real , Enum ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , quotRem , divMod , toInteger , quot = None , <EOL> rem = None , div = None , mod = None ) : <EOL> quot = lambda x : quotRem ( x ) [ <NUM_LIT:0> ] if quot is None else quot <EOL> rem = lambda x : quotRem ( x ) [ <NUM_LIT:1> ] if rem is None else rem <EOL> div = lambda x : divMod ( x ) [ <NUM_LIT:0> ] if div is None else div <EOL> mod = lambda x : divMod ( x ) [ <NUM_LIT:1> ] if mod is None else mod <EOL> attrs = { "<STR_LIT>" : quotRem , "<STR_LIT>" : toInteger , "<STR_LIT>" : quot , <EOL> "<STR_LIT>" : rem , "<STR_LIT>" : div , "<STR_LIT>" : mod , "<STR_LIT>" : divMod } <EOL> build_instance ( Integral , cls , attrs ) <EOL> return <EOL> @ sig ( H [ ( Integral , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" >> t ( Ratio , "<STR_LIT:a>" ) ) <EOL> def toRatio ( num , denom ) : <EOL> """<STR_LIT>""" <EOL> frac = fractions . Fraction ( num , denom ) <EOL> return R ( frac . numerator , frac . denominator ) <EOL> instance ( Real , int ) . where ( <EOL> toRational = lambda x : toRatio ( x , <NUM_LIT:1> ) <EOL> ) <EOL> instance ( Real , long ) . where ( <EOL> toRational = lambda x : toRatio ( x , <NUM_LIT:1> ) <EOL> ) <EOL> instance ( Real , float ) . where ( <EOL> toRational = lambda x : toRatio ( round ( x ) , <NUM_LIT:1> ) <EOL> ) <EOL> instance ( Integral , int ) . where ( <EOL> quotRem = lambda x , y : ( x / y , x % y ) , <EOL> toInteger = int , <EOL> quot = int . __div__ , <EOL> rem = int . __mod__ , <EOL> div = int . __div__ , <EOL> mod = int . __mod__ , <EOL> divMod = divmod <EOL> ) <EOL> instance ( Integral , long ) . where ( <EOL> quotRem = lambda x , y : ( x / y , x % y ) , <EOL> toInteger = int , <EOL> quot = long . __div__ , <EOL> rem = long . __mod__ , <EOL> div = long . __div__ , <EOL> mod = long . __mod__ , <EOL> divMod = divmod <EOL> ) <EOL> class RealFrac ( Real , Fractional ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , properFraction , truncate , round , ceiling , <EOL> floor ) : <EOL> attrs = { "<STR_LIT>" : properFraction , "<STR_LIT>" : truncate , <EOL> "<STR_LIT>" : round , "<STR_LIT>" : ceiling , "<STR_LIT>" : floor } <EOL> build_instance ( RealFrac , cls , attrs ) <EOL> return <EOL> @ sig ( H [ ( RealFrac , "<STR_LIT:a>" ) , ( Integral , "<STR_LIT:b>" ) ] / "<STR_LIT:a>" >> ( "<STR_LIT:b>" , "<STR_LIT:a>" ) ) <EOL> def properFraction ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFrac [ x ] . properFraction ( x ) <EOL> @ sig ( H [ ( RealFrac , "<STR_LIT:a>" ) , ( Integral , "<STR_LIT:b>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:b>" ) <EOL> def truncate ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFrac [ x ] . truncate ( x ) <EOL> @ sig ( H [ ( RealFrac , "<STR_LIT:a>" ) , ( Integral , "<STR_LIT:b>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:b>" ) <EOL> def round ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFrac [ x ] . round ( x ) <EOL> @ sig ( H [ ( RealFrac , "<STR_LIT:a>" ) , ( Integral , "<STR_LIT:b>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:b>" ) <EOL> def ceiling ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFrac [ x ] . ceiling ( x ) <EOL> @ sig ( H [ ( RealFrac , "<STR_LIT:a>" ) , ( Integral , "<STR_LIT:b>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:b>" ) <EOL> def floor ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFrac [ x ] . floor ( x ) <EOL> instance ( RealFrac , float ) . where ( <EOL> properFraction = lambda x : ( int ( math . floor ( x ) ) , x - math . floor ( x ) ) , <EOL> truncate = lambda x : int ( math . floor ( x ) if x > <NUM_LIT:0> else math . floor ( x + <NUM_LIT:1> ) ) , <EOL> round = lambda x : int ( round ( x , <NUM_LIT:0> ) ) , <EOL> ceiling = math . ceil , <EOL> floor = math . floor <EOL> ) <EOL> class RealFloat ( Floating , RealFrac ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def make_instance ( typeclass , cls , floatRange , isNan , isInfinite , <EOL> isNegativeZero , atan2 ) : <EOL> attrs = { "<STR_LIT>" : floatRange , "<STR_LIT>" : isNan , <EOL> "<STR_LIT>" : isInfinite , "<STR_LIT>" : isNegativeZero , <EOL> "<STR_LIT>" : atan2 } <EOL> build_instance ( RealFloat , cls , attrs ) <EOL> return <EOL> @ sig ( H [ ( RealFloat , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> bool ) <EOL> def isNaN ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFloat [ x ] . isNan ( x ) <EOL> @ sig ( H [ ( RealFloat , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> bool ) <EOL> def isInfinite ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFloat [ x ] . isInfinite ( x ) <EOL> @ sig ( H [ ( RealFloat , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> bool ) <EOL> def isNegativeZero ( x ) : <EOL> """<STR_LIT>""" <EOL> return RealFloat [ x ] . isNegativeZero ( x ) <EOL> @ sig ( H [ ( RealFloat , "<STR_LIT:a>" ) ] / "<STR_LIT:a>" >> "<STR_LIT:a>" >> "<STR_LIT:a>" ) <EOL> def atan2 ( y , x ) : <EOL> """<STR_LIT>""" <EOL> return RealFloat [ x ] . atan2 ( y , x ) <EOL> instance ( RealFloat , float ) . where ( <EOL> floatRange = ( sys . float_info . min , sys . float_info . max ) , <EOL> isNan = math . isnan , <EOL> isInfinite = lambda x : x == float ( '<STR_LIT>' ) or x == - float ( '<STR_LIT>' ) , <EOL> isNegativeZero = lambda x : math . copysign ( <NUM_LIT:1> , x ) == - <NUM_LIT:1.0> , <EOL> atan2 = math . atan2 <EOL> ) </s>
