Skip to content

Free monad

Free

Bases: Generic[F, A]

summary

Parameters:

Name Type Description Default
Generic _type_

description

required
Source code in funclift/free_monad.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Free(Generic[F, A]):
    """_summary_

    Args:
        Generic (_type_): _description_
    """

    def fmap(self, f: Callable[[A], B]) -> Free[F, B]:
        return self.flatmap(lambda a: Free.pure(f(a)))

    # def flatMap(fa: Free[M, A], f: A -> Free[M, B]) -> Free[M, B]:
    # @staticmethod
    def flatmap(self, f: Callable[[A], Free[F, B]]) -> Free[F, B]:
        return FlatMap(self, f)
    # def flatmap(self, f: Callable[[A], Monad[B]]) -> Free[F, B]:
    #     return FlatMap(self, cast(Callable[[A], Free[F, B]], f))

    def ap(self, a: Free[F, D]) -> Free[F, E]:
        return a.fmap(cast(Callable[[D], E], self))

    # def pure(a: A) -> F[A]:
    @staticmethod
    def pure(a: E) -> Free[F, E]:
        return Pure(a)

    @staticmethod
    def mempty(a: E) -> Free[F, E]:
        return Pure(a)

    @staticmethod
    # def liftm(ma: F[A]) -> Free[F, A]:
    # def liftm(fa: Any) -> Free[F, A]:
    # def liftm(fa: Functor[K, E]) -> Lift[K, E]:
    def liftm(fa: Functor[F, A]) -> Lift[F, A]:
        return Lift(fa)

    # def foldmap(self, nt: Callable[[F], G]) -> G:
    @abstractmethod
    def foldmap(self, nt: FunctionK[F, G]) -> Monad[G, A]: ...