コード例 #1
0
    def test_typeclasses_x(self):
        # Instantiate using defined higher-kinded type
        instance(Composite, t(t(Tree, 'a'), Maybe, [t(Tree, 'a')])).where(
            children=tree_children
        )

        # String representation for higher-kinded type.
        self.assertEqual(str(t(t(Tree, 'a'), Maybe, [t(Tree, 'a')])),
                         '((Tree a) Maybe [(Tree a)])')
コード例 #2
0
ファイル: lazylist.py プロジェクト: krackers/hask
    """
    return L[Enum[start].enumFromThenTo(start, second, end)]


@sig(H / "a" >> "a" >> ["a"])
def enumFromTo(start, end):
    """``enumFromTo :: a -> a -> [a]``

    Used in translation of L[n, ..., m]

    """
    return L[Enum[start].enumFromTo(start, end)]


instance(Enum, int).where(fromEnum=int, toEnum=int)
instance(Enum, bool).where(fromEnum=int, toEnum=bool)
instance(Enum, str).where(fromEnum=ord, toEnum=chr)


class List(Sequence, Hask):
    """Statically typed lazy sequence datatype.

    See `L`:obj: for more information.

    """
    def __init__(self, head=None, tail=None):
        from itertools import chain
        from hask3.lang.type_system import typeof
        from hask3.lang.hindley_milner import unify
        if head is not None:
コード例 #3
0
ファイル: Monad.py プロジェクト: krackers/hask
    remove one level of monadic structure, projecting its bound argument into
    the outer level.

    """
    from hask3.Prelude import id
    return bind(m, id)


@sig(H[Monad, "m"]/ (H/ "a" >> "r") >> t("m", "a") >> t("m", "r"))
def liftM(fn, m):
    """``liftM :: Monad m => (a1 -> r) -> m a1 -> m r``

    Promote a function to a monad.

    """
    return fmap(fn, m)


def _list_bind(x, fn):
    from itertools import chain
    from hask3.lang import L
    return L[chain.from_iterable(fmap(fn, x))]


instance(Monad, List).where(
    bind = _list_bind
)


del _list_bind, Applicative, List, instance, t, H, sig
コード例 #4
0
ファイル: Foldable.py プロジェクト: krackers/hask
    there is no such element.

    """
    from hask3.Data import List as DL
    return DL.find(f, toList(t))


from hask3.Data import List as DL    # noqa

instance(Foldable, List).where(
    foldr = DL.foldr,
    foldr1 = DL.foldr1,
    foldl = DL.foldl,
    foldl_ = DL.foldl_,
    foldl1 = DL.foldl1,
    null = DL.null,
    length = DL.length,
    elem = DL.elem,
    minimum = DL.minimum,
    maximum = DL.maximum,
    sum = DL.sum,
    product = DL.product
)

del DL

del Applicative, Monad, Eq, Num, Ordering, Ord, Maybe
del instance
del List
del Typeclass
del t, H, sig
コード例 #5
0
                       | d.Right("b") & deriving(Read, Show, Eq, Ord))


def _fmap(f, v):
    from hask3.lang.syntax import caseof, m, p
    return ~(caseof(v)
             | m(Left(m.e)) >> Left(p.e)
             | m(Right(m.ra)) >> Right(f(p.ra)))


def _bind(v, f):
    from hask3.lang.syntax import caseof, m, p
    return ~(caseof(v) | m(Left(m.e)) >> Left(p.e) | m(Right(m.a)) >> f(p.a))


instance(Functor, Either).where(fmap=_fmap)

instance(Applicative, Either).where(pure=Right)

instance(Monad, Either).where(bind=_bind)


def in_either(fn):
    """Decorator for monadic error handling.

    If the decorated function raises an exception, return the exception inside
    `Left`.  Otherwise, take the result and wrap it in `Right`.

    """
    from hask3.lang.syntax import typify, t
コード例 #6
0
ファイル: typeclasses.py プロジェクト: krackers/hask
    Minimal complete definition:

    - ``read``

    """
    @classmethod
    def make_instance(typeclass, cls, read):
        from hask3.lang.type_system import build_instance
        build_instance(Read, cls, {"read": read})

    @classmethod
    def derive_instance(typeclass, cls):
        Read.make_instance(cls, read=eval)


instance(Show, str).where(show=str.__repr__)
instance(Show, int).where(show=int.__str__)
instance(Show, float).where(show=tuple.__str__)
instance(Show, complex).where(show=complex.__str__)
instance(Show, bool).where(show=bool.__str__)
instance(Show, list).where(show=list.__str__)
instance(Show, tuple).where(show=tuple.__str__)
instance(Show, set).where(show=set.__str__)
instance(Show, dict).where(show=dict.__str__)
instance(Show, frozenset).where(show=frozenset.__str__)
instance(Show, slice).where(show=slice.__str__)
instance(Show, type(None)).where(show=lambda self: str(self))
instance(Show, Exception).where(show=lambda self: repr(self))

instance(Eq, str).where(eq=str.__eq__, ne=str.__ne__)
instance(Eq, int).where(eq=int.__eq__, ne=int.__ne__)
コード例 #7
0
from hask3.Data.Ord import Ord
from hask3.Data.Functor import Functor
from hask3.Control.Applicative import Applicative
from hask3.Control.Monad import Monad

# data Maybe a = Nothing | Just a deriving(Show, Eq, Ord)
Maybe, Nothing, Just = (data.Maybe("a") == d.Nothing
                        | d.Just("a") & deriving(Read, Show, Eq, Ord))


def _fmap(f, x):
    from hask3.lang.syntax import caseof, m, p
    return ~(caseof(x) | m(Just(m.a)) >> Just(f(p.a)) | m(Nothing) >> Nothing)


instance(Functor, Maybe).where(fmap=_fmap)

instance(Applicative, Maybe).where(pure=Just)


def _bind(x, f):
    from hask3.lang.syntax import caseof, m, p
    return ~(caseof(x) | m(Just(m.a)) >> f(p.a) | m(Nothing) >> Nothing)


instance(Monad, Maybe).where(bind=_bind)


def in_maybe(fn):
    """Decorator for monadic error handling.
コード例 #8
0
ファイル: Num.py プロジェクト: krackers/hask

@sig(H[(Num, "a")] / "a" >> "a")
def abs(a):
    """``abs :: Num a => a -> a``

    Absolute value.

    """
    return Num[a].abs(a)


instance(Num, int).where(add=int.__add__,
                         mul=int.__mul__,
                         abs=int.__abs__,
                         signum=lambda x: -1 if x < 0 else (1 if x > 0 else 0),
                         fromInteger=int,
                         negate=int.__neg__,
                         sub=int.__sub__)

instance(Num, float).where(add=float.__add__,
                           mul=float.__mul__,
                           abs=float.__abs__,
                           signum=lambda x: -1.0
                           if x < 0.0 else (1.0 if x > 0.0 else 0.0),
                           fromInteger=float,
                           negate=float.__neg__,
                           sub=float.__sub__)

instance(Num, complex).where(add=complex.__add__,
                             mul=complex.__mul__,