def make_instance(typeclass, cls, add, mul, abs, signum, fromInteger, negate, sub=None): @sig(H[(Num, "a")] / "a" >> "a" >> "a") def default_sub(a, b): return a.__add__(b.__neg__()) sub = default_sub if sub is None else sub attrs = { "add": add, "mul": mul, "abs": abs, "signum": signum, "fromInteger": fromInteger, "negate": negate, "sub": sub } build_instance(Num, cls, attrs) return
def make_instance(typeclass, cls, quotRem, divMod, toInteger, quot=None, rem=None, div=None, mod=None): quot = lambda x: quotRem(x)[0] if quot is None else quot rem = lambda x: quotRem(x)[1] if rem is None else rem div = lambda x: divMod(x)[0] if div is None else div mod = lambda x: divMod(x)[1] if mod is None else mod attrs = { "quotRem": quotRem, "toInteger": toInteger, "quot": quot, "rem": rem, "div": div, "mod": mod, "divMod": divMod } build_instance(Integral, cls, attrs) return
def make_instance(typeclass, cls, pi, exp, sqrt, log, pow, logBase, sin, tan, cos, asin, atan, acos, sinh, tanh, cosh, asinh, atanh, acosh): attrs = { "pi": pi, "exp": exp, "sqrt": sqrt, "log": log, "pow": pow, "logBase": logBase, "sin": sin, "tan": tan, "cos": cos, "asin": asin, "atan": atan, "acos": acos, "sinh": sinh, "tanh": tanh, "cosh": cosh, "asinh": asinh, "atanh": atanh, "acosh": acosh } build_instance(Floating, cls, attrs) return
def make_instance(typeclass, cls, fmap): fmap = fmap ** \ (H[(Functor, "f")]/ (H/ "a" >> "b") >> t("f", "a") >> t("f", "b")) if not is_builtin(cls): cls.__rmul__ = lambda x, f: fmap(f, x) build_instance(Functor, cls, {"fmap": fmap}) return
def make_instance(typeclass, cls, foldr, foldr1=None, foldl=None, foldl_=None, foldl1=None, toList=None, null=None, length=None, elem=None, maximum=None, minimum=None, sum=None, product=None): # attributes that are not supplied are implemented in terms of toList if toList is None: if hasattr(cls, "__iter__"): toList = lambda x: L[iter(x)] else: toList = lambda t: foldr(lambda x, y: x ^ y, L[[]], t) foldr1 = (lambda x: DL.foldr1(toList(x))) if foldr1 is None else foldr1 foldl = (lambda x: DL.foldl(toList(x))) if foldl is None else foldl foldl_ = (lambda x: DL.foldl_(toList(x))) if foldl_ is None else foldl_ foldl1 = (lambda x: DL.foldl1(toList(x))) if foldl1 is None else foldl1 null = (lambda x: DL.null(toList(x))) if null is None else null length = (lambda x: DL.length(toList(x))) if length is None else length elem = (lambda x: DL.length(toList(x))) if length is None else length mi = (lambda x: DL.minimum(toList(x))) if minimum is None else minimum ma = (lambda x: DL.maximum(toList(x))) if maximum is None else maximum sum = (lambda x: DL.sum(toList(x))) if sum is None else sum p = (lambda x: DL.product(toList(x))) if product is None else product attrs = { "foldr": foldr, "foldr1": foldr1, "foldl": foldl, "foldl_": foldl_, "foldl1": foldl1, "toList": toList, "null": null, "length": length, "elem": elem, "maximum": ma, "minimum": mi, "sum": sum, "product": p } build_instance(Foldable, cls, attrs) if not hasattr(cls, "__len__") and not is_builtin(cls): cls.__len__ = length if not hasattr(cls, "__iter__") and not is_builtin(cls): cls.__iter__ = lambda x: iter(toList(x)) return
def make_instance(typeclass, cls, floatRange, isNan, isInfinite, isNegativeZero, atan2): attrs = { "floatRange": floatRange, "isNan": isNan, "isInfinite": isInfinite, "isNegativeZero": isNegativeZero, "atan2": atan2 } build_instance(RealFloat, cls, attrs) return
def make_instance(typeclass, cls, properFraction, truncate, round, ceiling, floor): attrs = { "properFraction": properFraction, "truncate": truncate, "round": round, "ceiling": ceiling, "floor": floor } build_instance(RealFrac, cls, attrs) return
def make_instance(self, cls, pure, ap): pure = pure**(H[(Applicative, "f")] / "a" >> t("f", "a")) ap = ap**(H[(Applicative, "f")] / t("f", H / "a" >> "b") >> t( "f", "a") >> t("f", "b")) build_instance(Applicative, cls, {"pure": pure, "ap": ap}) return
def make_instance(typeclass, cls, bind): bind = bind**(H[(Monad, "m")] / t("m", "a") >> (H / "a" >> t("m", "b")) >> t("m", "b")) build_instance(Monad, cls, {"bind": bind}) return
def make_instance(typeclass, cls, mempty, mappend, mconcat): attrs = {"mempty": mempty, "mappend": mappend, "mconcat": mconcat} build_instance(Monoid, cls, attrs) return
def make_instance(typeclass, cls, toRational): build_instance(Real, cls, {}) return
def make_instance(typeclass, cls, fromRational, div, recip=None): if recip is None: recip = lambda x: div(1, x) attrs = {"fromRational": fromRational, "div": div, "recip": recip} build_instance(Fractional, cls, attrs) return
def make_instance(typeclass, cls, traverse, sequenceA=None, mapM=None, sequence=None): attrs = {"traverse":traverse, "sequenceA":sequenceA, "mapM":mapM, "sequence":sequence} build_instance(Traversable, cls, attrs) return