Ejemplo n.º 1
0
from hask3.lang.typeclasses import Ord

from hask3.lang.syntax import H
from hask3.lang.syntax import sig

from hask3.lang.syntax import data
from hask3.lang.syntax import d
from hask3.lang.syntax import deriving

from hask3.Data.Eq import Eq


# data Ordering = LT | EQ | GT deriving(Show, Eq, Ord, Bounded)
Ordering, LT, EQ, GT = (
    data.Ordering == (d.LT | d.EQ | d.GT
                      & deriving(Read, Show, Eq, Ord, Bounded))
)


# TODO: Down?


@sig(H[(Ord, "a")]/ "a" >> "a" >> "a")
def max(x, y):
    """``max :: a -> a -> a``

    Maximum function.

    """
    return x if x >= y else y
Ejemplo n.º 2
0
from hask3.lang.syntax import t

from hask3.lang.syntax import data
from hask3.lang.syntax import d
from hask3.lang.syntax import deriving

from hask3.lang.syntax import instance
from hask3.Data.Eq import Eq
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 Either a b = Left b | Right a deriving(Read, Show, Eq, Ord)
Either, Left, Right = (data.Either("a", "b") == d.Left("a")
                       | 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)
Ejemplo n.º 3
0
from hask3.lang.syntax import t

from hask3.lang.syntax import data
from hask3.lang.syntax import d
from hask3.lang.syntax import deriving
from hask3.lang.syntax import instance

from hask3.Data.Eq import Eq
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)
Ejemplo n.º 4
0
    Attributes:

    - ``children :: a -> f b``

    '''
    @classmethod
    def make_instance(cls, instance, children):
        children = children ** (H/ 'a' >> t('f', 'b'))
        build_instance(cls, instance,
                       {'children': lambda self: children(self)})


Tree, Leaf, Node = data.Tree('a') == (
    d.Leaf | d.Node('a', [t(data.Tree, 'a')]) &
    deriving(Show, Eq)
)


def tree_children(t):
    return ~(caseof(t)
             | m(Leaf) >> Nothing
             | m(Node(m.p, m.xs)) >> Just(p.xs))


class TestTypeclassX(unittest.TestCase):
    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
        )
Ejemplo n.º 5
0
Archivo: Num.py Proyecto: krackers/hask
    Minimal complete definition:

    - ``fromRational``
    - ``div``

    """
    @classmethod
    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)


Ratio, R = (data.Ratio("a") == d.R("a", "a") & deriving(Eq))

Rational = t(Ratio, int)

instance(Fractional,
         float).where(fromRational=lambda rat: float(rat[0]) / float(rat[1]),
                      div=lambda a, b: float(a) / float(b),
                      recip=lambda x: 1.0 / x)


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

    Reciprocal fraction.