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 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. """ def closure_in_either(*args, **kwargs): try: return Right(fn(*args, **kwargs)) except Exception as e: return Left(e) return typify(fn, hkt=lambda x: t(Either, "aa", x))(closure_in_either)
def in_maybe(fn): """ Decorator for monadic error handling. If the decorated function raises an exception, return Nothing. Otherwise, take the result and wrap it in a Just. """ def closure_in_maybe(*args, **kwargs): try: return Just(fn(*args, **kwargs)) except: return Nothing return typify(fn, hkt=lambda x: t(Maybe, x))(closure_in_maybe)
def lookupEnv(var): """ ``lookupEnv :: String -> IO (Maybe String)`` Return the value of the environment variable ``var``, or ``Nothing`` if there is no such value. """ def unsafeToMaybe(x): if x is None: return Nothing else: return Just(x) return LazyPure((lambda n: unsafeToMaybe(os.environ.get(var, None)))**( H / Unit >> t(Maybe, String)))
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
from hask.Data.Unit import Unit from hask.System.IO import IO, LazyPure import sys, os getArgs = LazyPure( (lambda n: L[[]] if len(sys.argv) < 1 else L[sys.argv[1:]])**(H / Unit >> [String])) getArgs.__doc__ = """ ``getArgs :: IO [String]`` Computation ``getArgs`` returns a list of the program’s command line arguments (not including the program name). """ @sig(H / String >> t(IO, t(Maybe, String))) def lookupEnv(var): """ ``lookupEnv :: String -> IO (Maybe String)`` Return the value of the environment variable ``var``, or ``Nothing`` if there is no such value. """ def unsafeToMaybe(x): if x is None: return Nothing else: return Just(x) return LazyPure((lambda n: unsafeToMaybe(os.environ.get(var, None)))**( H / Unit >> t(Maybe, String)))
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
build_instance(Monad, cls, {"bind": bind}) return @Infix def bind(m, fn): """ (bind) :: Monad m => m a -> (a -> m b) -> m b This is infix and non-curried version of `mbind`. Monadic bind. """ return Monad[m].bind(m, fn) @sig(H[(Monad, "m")] / t("m", "a") >> (H / "a" >> t("m", "b")) >> t("m", "b")) def mbind(m, fn): """ mbind :: Monad m => m a -> (a -> m b) -> m b Monadic bind. """ return Monad[m].bind(m, fn) @Infix def chain(m, k): """ (chain) :: Monad m => m a -> m b -> m b This is infix and non-curried version of `bindIgnore`.
def __new__(cls, *args): return t(cls, *args)
return Num[a].mul(a, b) @ADT("a", deriving=[Eq]) class Ratio: """ `data Ratio a = R a a` Rational numbers, with numerator and denominator of some `Integral` type. """ R: ["a", "a"] R = Ratio.R Rational = t(Ratio, int) instance(Fractional, float).where(fromRational=lambda rat: float(rat[0]) / float(rat[1]), div=float.__truediv__, recip=lambda x: 1.0 / x) @sig(H[(Fractional, "a")] / "a" >> "a") def recip(a): """ recip :: Fractional a => a -> a Reciprocal fraction. """ return Fractional[a].recip(a)
traverse, sequenceA, mapM, sequence Minimal complete definition: traverse """ @classmethod 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 @sig(H[(Applicative, "f"), (Traversable, "t")]/ (H/ "a" >> t("f", "b")) >> t("t", "a") >> t("f", t("t", "b"))) def traverse(f, t): """ ``traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)`` Map each element of a structure to an action, evaluate these these actions from left to right, and collect the results. actions from left to right, and collect the results. For a version that ignores the results see traverse_. """ return Traversable[t].traverse(f, t) @sig(H[(Applicative, "f"), (Traversable, "t")]/ t("t", t("f", "a")) >> t("f", t("t", "a"))) def sequenceA(t):
@sig(H/ ["a"] >> ["a"]) def init(xs): """ init :: [a] -> [a] Return all the elements of a list except the last one. The list must be non-empty. """ if null(xs): raise IndexError("empty list") return xs[:-1] @sig(H/ ["a"] >> t(Maybe, ("a", ["a"]))) def uncons(xs): """ uncons :: [a] -> Maybe (a, [a]) Decompose a list into its head and tail. If the list is empty, returns Nothing. If the list is non-empty, returns Just((x, xs)), where x is the head of the list and xs its tail. """ return Just((head(xs), tail(xs))) if not null(xs) else Nothing @sig(H/ ["a"] >> bool) def null(xs): """ null :: [a] -> bool