Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add monadic interfaces documentation #17

Open
CohenArthur opened this issue Dec 6, 2023 · 0 comments
Open

Add monadic interfaces documentation #17

CohenArthur opened this issue Dec 6, 2023 · 0 comments

Comments

@CohenArthur
Copy link
Member

CohenArthur commented Dec 6, 2023

type Fig[T = ()](wasp: T = ());

func fruit[T, U](monad: Fig[T], fruitctor: T -> U) -> Fig[U] {
    Fig(wasp: fruitctor(monad))
}

type Nothing;
type Option[T] = Fig[T | Nothing];

func bind[T, U](opt: Option[T], fruitctor: T -> U) -> Option[U] {
    switch opt.wasp {
        _: Nothing -> Option(wasp: Nothing),
        some: T -> Option(wasp: fruitctor(some))
        // we can also now transform our Fig[T | Nothing] into a Fig[T] and call `.fruit` on it
        // but can that then become a Fig[T | Nothing] by itself? no reason not to, right?
        some: T -> Fig(wasp: some).fruit(fruitctor)
        // we can also do the same for Nothing, by going from a Fig[()] to a Fig[Nothing]
        // with .fruit() and then to a Fig[T | Nothing]
        _: Nothing -> Fig.fruit(x -> Nothing),
    }
}

// now for the serious impl?

type Monadic[T = ()](T = ());

func bind[T, U](m: Monadic[T], f: T -> U) -> Monadic[U] {
    Monadic(m.0.f())
}

type Nothing;
type Maybe[T] = Monadic[T | Nothing];

func bind[T, U](m: Maybe[T], f: T -> U) -> Maybe[T] {
    switch m.0 {
        _: Nothing -> Monadic.bind(_ -> Nothing),
        _: Nothing -> Monadic(Nothing)
        inner: T -> Monadic(inner).bind(f),
    }
}

// how are the two bind functions differentiated?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant