Skip to content

Commit

Permalink
monad
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jun 18, 2024
1 parent 680e3a9 commit f4baa26
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions library/core/src/monad.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use self::option::Option

trait Applicative
where Self: * -> *
{
fn pure<T>(t: T) -> Self<T>
}

impl Applicative for Option {
fn pure<T>(t: T) -> Self<T> {
Option::Some(t)
}
}

trait Monad: Applicative
where Self: * -> *
{
fn bind<T, F, U>(self: Self<T>) -> Self<U> where F: Fn<T, Self<U>>
}

impl Monad for Option {
fn bind<T, F, U>(self: Self<T>) -> Self<U> where F: Fn<T, Self<U>> {
match self {
Self::Some(self) => F::exec(t: self)
Self::None => Self::None
}
}
}

trait Fn<T, U> {
fn exec(t: T) -> U
}

type MyFn = ()

impl Fn<bool, Option<u8>> for MyFn {
fn exec(t: bool) -> Option<u8> {
Option::Some(0)
}
}

#test
fn my_test() {
let a = Option::Some(true).bind<bool, MyFn, u8>()
}
Empty file added library/core/src/monad/map.fe
Empty file.
2 changes: 1 addition & 1 deletion library/core/src/monad/option.fe
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum Option<Inner> {
pub enum Option<Inner> {
Some(Inner),
None
}

0 comments on commit f4baa26

Please sign in to comment.