Exemple #1
0
    def make_instance(typeclass, cls, lt, le=None, gt=None, ge=None):
        from hask3.hack import is_builtin
        from hask3.lang.type_system import build_instance
        if le is None:
            le = lambda s, o: s.__lt__(o) or s.__eq__(o)
        if gt is None:
            gt = lambda s, o: not s.__lt__(o) and not s.__eq__(o)
        if ge is None:
            ge = lambda s, o: not s.__lt__(o) or s.__eq__(o)

        __lt__ = lt ** (H/ "a" >> "a" >> bool)
        __le__ = le ** (H/ "a" >> "a" >> bool)
        __gt__ = gt ** (H/ "a" >> "a" >> bool)
        __ge__ = ge ** (H/ "a" >> "a" >> bool)

        lt = lambda self, other: __lt__(self, other)
        le = lambda self, other: __le__(self, other)
        gt = lambda self, other: __gt__(self, other)
        ge = lambda self, other: __ge__(self, other)

        attrs = {"lt": lt, "le": le, "gt": gt, "ge": ge}
        build_instance(Ord, cls, attrs)
        if not is_builtin(cls):
            cls.__lt__ = lt
            cls.__le__ = le
            cls.__gt__ = gt
            cls.__ge__ = ge
Exemple #2
0
    def make_instance(typeclass,
                      cls,
                      quotRem,
                      divMod,
                      toInteger,
                      quot=None,
                      rem=None,
                      div=None,
                      mod=None):
        from hask3.lang.type_system import build_instance

        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)
Exemple #3
0
    def make_instance(typeclass,
                      cls,
                      add,
                      mul,
                      abs,
                      signum,
                      fromInteger,
                      negate,
                      sub=None):
        from hask3.lang.syntax import H, sig
        from hask3.lang.type_system import build_instance

        @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)
Exemple #4
0
 def make_instance(typeclass, cls, pi, exp, sqrt, log, pow, logBase, sin,
                   tan, cos, asin, atan, acos, sinh, tanh, cosh, asinh,
                   atanh, acosh):
     from hask3.lang.type_system import build_instance
     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)
Exemple #5
0
 def make_instance(typeclass, cls, fmap):
     from hask3.hack import is_builtin
     from hask3.lang.type_system import build_instance
     from hask3.lang import H, t
     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})
Exemple #6
0
    def make_instance(typeclass, cls, show):
        from hask3.hack import is_builtin
        from hask3.lang.type_system import build_instance
        __show__ = show ** (H/ "a" >> str)
        show = lambda self: __show__(self)

        build_instance(Show, cls, {"show": show})
        if not is_builtin(cls):
            cls.__repr__ = show
            cls.__str__ = show
Exemple #7
0
 def make_instance(typeclass, cls, bind):
     from hask3.hack import is_builtin
     from hask3.lang.type_system import build_instance
     from hask3.lang.syntax import H, t
     bind = bind ** (H[Monad, "m"]/
                     t("m", "a") >> (H/ "a" >> t("m", "b")) >> t("m", "b"))
     if not is_builtin(cls):
         def bind_wrap(s, o):
             return Monad[s].bind(s, o)
         cls.__rshift__ = bind_wrap
     build_instance(Monad, cls, {"bind": bind})
Exemple #8
0
 def make_instance(typeclass, cls, floatRange, isNan, isInfinite,
                   isNegativeZero, atan2):
     from hask3.lang.type_system import build_instance
     attrs = {
         "floatRange": floatRange,
         "isNan": isNan,
         "isInfinite": isInfinite,
         "isNegativeZero": isNegativeZero,
         "atan2": atan2
     }
     build_instance(RealFloat, cls, attrs)
Exemple #9
0
 def make_instance(typeclass, cls, properFraction, truncate, round, ceiling,
                   floor):
     from hask3.lang.type_system import build_instance
     attrs = {
         "properFraction": properFraction,
         "truncate": truncate,
         "round": round,
         "ceiling": ceiling,
         "floor": floor
     }
     build_instance(RealFrac, cls, attrs)
Exemple #10
0
    def make_instance(typeclass, cls, toEnum, fromEnum):
        from hask3.lang.type_system import build_instance

        def succ(a):
            return toEnum(fromEnum(a) + 1)

        def pred(a):
            return toEnum(fromEnum(a) - 1)

        def enumFromThen(start, second):
            pointer = fromEnum(start)
            step = fromEnum(second) - pointer
            while True:
                yield toEnum(pointer)
                pointer += step

        def enumFrom(start):
            return enumFromThen(start, succ(start))

        def enumFromThenTo(start, second, end):
            if start == end:
                yield start
                return

            elif (second >= start > end) or (second <= start < end):
                return

            pointer, stop = fromEnum(start), fromEnum(end)
            step = fromEnum(second) - pointer
            while (start < end and pointer <= stop) or \
                  (start > end and pointer >= stop):
                yield toEnum(pointer)
                pointer += step

        def enumFromTo(start, end):
            second = succ(start) if start < end else pred(start)
            return enumFromThenTo(start, second, end)

        attrs = {
            "toEnum": toEnum,
            "fromEnum": fromEnum,
            "succ": succ,
            "pred": pred,
            "enumFromThen": enumFromThen,
            "enumFrom": enumFrom,
            "enumFromThenTo": enumFromThenTo,
            "enumFromTo": enumFromTo
        }
        build_instance(Enum, cls, attrs)
Exemple #11
0
    def make_instance(typeclass, cls, eq, ne=None):
        from hask3.hack import is_builtin
        from hask3.lang.type_system import build_instance

        def default_ne(self, other):
            return not eq(self, other)

        __eq__ = eq ** (H/ "a" >> "a" >> bool)
        __ne__ = (default_ne if ne is None else ne) ** (H/ "a" >> "a" >> bool)
        eq = lambda self, other: __eq__(self, other)
        ne = lambda self, other: __ne__(self, other)

        build_instance(Eq, cls, {"eq": eq, "ne": ne})
        if not is_builtin(cls):
            cls.__eq__ = eq
            cls.__ne__ = ne
Exemple #12
0
    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):
        from hask3.hack import is_builtin
        from hask3.lang.type_system import build_instance
        from hask3.lang.lazylist import L
        from hask3.Data import List as DL

        # 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)

        # TODO: Automate this
        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))
Exemple #13
0
 def make_instance(typeclass, cls, traverse, sequenceA=None, mapM=None,
                   sequence=None):
     from hask3.lang.type_system import build_instance
     attrs = {"traverse": traverse, "sequenceA": sequenceA, "mapM": mapM,
              "sequence": sequence}
     build_instance(Traversable, cls, attrs)
Exemple #14
0
 def make_instance(typeclass, cls, toRational):
     from hask3.lang.type_system import build_instance
     build_instance(Real, cls, {})
Exemple #15
0
 def make_instance(cls, instance, children):
     children = children ** (H/ 'a' >> t('f', 'b'))
     build_instance(cls, instance,
                    {'children': lambda self: children(self)})
Exemple #16
0
 def make_instance(typeclass, cls, mempty, mappend, mconcat):
     from hask3.lang.type_system import build_instance
     attrs = {"mempty": mempty, "mappend": mappend, "mconcat": mconcat}
     build_instance(Monoid, cls, attrs)
Exemple #17
0
 def make_instance(typeclass, cls, read):
     from hask3.lang.type_system import build_instance
     build_instance(Read, cls, {"read": read})
Exemple #18
0
 def make_instance(typeclass, cls, minBound, maxBound):
     from hask3.lang.type_system import build_instance
     attrs = {"minBound": minBound, "maxBound": maxBound}
     build_instance(Bounded, cls, attrs)
Exemple #19
0
 def make_instance(typeclass, cls, fromRational, div, recip=None):
     from hask3.lang.type_system import build_instance
     if recip is None:
         recip = lambda x: div(1, x)
     attrs = {"fromRational": fromRational, "div": div, "recip": recip}
     build_instance(Fractional, cls, attrs)