コード例 #1
0
ファイル: typeclasses.py プロジェクト: krackers/hask
    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
コード例 #2
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})
コード例 #3
0
ファイル: typeclasses.py プロジェクト: krackers/hask
    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
コード例 #4
0
ファイル: Monad.py プロジェクト: krackers/hask
 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})
コード例 #5
0
ファイル: Foldable.py プロジェクト: krackers/hask
    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))
コード例 #6
0
ファイル: typeclasses.py プロジェクト: krackers/hask
    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