class Monad(Applicative): b = None def __init__(self, b, p): Applicative.__init__( self, lambda f: lambda a: b(lambda ff: b(lambda aa: p(lambda: ff(aa))) (a))(f), p) self.b = b def bind(self, f, a): return self.b(f)(a) def join(self, k): return self.bind(identity, k) def __sequenceBind(f, a): r = [] for i in a: r.extend(f(i)) return r sequenceMonad = Monad(curry(__sequenceBind), lambda k: [k()]) lambdaMonad = Monad(lambda f: lambda a: lambda t: f(a(t))(t), lambda k: lambda _: k())
from functor import Functor, sequenceFunctor, lambdaFunctor from function import curry class Pointed(Functor): p = None def __init__(self, f, p): Functor.__init__(self, f) self.p = p def pure(self, a): return self.p(a) sequencePointed = Pointed(curry(sequenceFunctor.fmap), lambda k: [k()]) lambdaPointed = Pointed(curry(lambdaFunctor.fmap), lambda k: lambda _: k())
from function import curry, identity class Monad(Applicative): b = None def __init__(self, b, p): Applicative.__init__(self, lambda f: lambda a: b( lambda ff: b(lambda aa: p( lambda: ff(aa)))(a))(f), p) self.b = b def bind(self, f, a): return self.b(f)(a) def join(self, k): return self.bind(identity, k) def __sequenceBind(f, a): r = [] for i in a: r.extend(f(i)) return r sequenceMonad = Monad(curry(__sequenceBind), lambda k: [k()]) lambdaMonad = Monad(lambda f: lambda a: lambda t: f(a(t))(t), lambda k: lambda _: k())
import function from libs import subfunc if __name__ == '__main__': function.curry() subfunc.spice()
from pointed import Pointed, sequencePointed, lambdaPointed from function import curry class Applicative(Pointed): ap = None def __init__(self, ap, p): Pointed.__init__(self, lambda k: ap(p(lambda: k)), p) self.ap = ap def apply(self, f, a): return self.ap(f)(a) def __sequenceApply(k, f): r = [] for i in k: for j in f: r.append(i(j)) return r sequenceApplicative = Applicative(curry(__sequenceApply), sequencePointed.pure) lambdaApplicative = Applicative(lambda f: lambda a: lambda t: f(t)(a(t)), lambdaPointed.pure)