Skip to content

Commit

Permalink
monad
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jun 14, 2024
1 parent 680e3a9 commit 8bd77f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions library/core/src/monad.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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>(t: Self<T>) -> Self<U> where F: Fn<T, Self<U>>
}

impl Monad for Option {
fn bind<T, F, U>(t: Self<T>) -> Self<U> where F: Fn<T, Self<U>> {
match t {
Self::Some(t) => F::exec(t)
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() {
type OptionU8 = Option<u8>
let a = OptionU8::bind<bool, MyFn, u8>(Option::Some(true))
}
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 8bd77f2

Please sign in to comment.