From 8bd77f287c89196c490cd894dc4052af992104a4 Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Thu, 13 Jun 2024 19:50:03 -0600 Subject: [PATCH] monad --- library/core/src/monad.fe | 46 ++++++++++++++++++++++++++++++++ library/core/src/monad/map.fe | 0 library/core/src/monad/option.fe | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 library/core/src/monad/map.fe diff --git a/library/core/src/monad.fe b/library/core/src/monad.fe index e69de29bb2..3881b96060 100644 --- a/library/core/src/monad.fe +++ b/library/core/src/monad.fe @@ -0,0 +1,46 @@ +use self::option::Option + +trait Applicative +where Self: * -> * +{ + fn pure(t: T) -> Self +} + +impl Applicative for Option { + fn pure(t: T) -> Self { + Option::Some(t) + } +} + +trait Monad: Applicative +where Self: * -> * +{ + fn bind(t: Self) -> Self where F: Fn> +} + +impl Monad for Option { + fn bind(t: Self) -> Self where F: Fn> { + match t { + Self::Some(t) => F::exec(t) + Self::None => Self::None + } + } +} + +trait Fn { + fn exec(t: T) -> U +} + +type MyFn = () + +impl Fn> for MyFn { + fn exec(t: bool) -> Option { + Option::Some(0) + } +} + +#test +fn my_test() { + type OptionU8 = Option + let a = OptionU8::bind(Option::Some(true)) +} \ No newline at end of file diff --git a/library/core/src/monad/map.fe b/library/core/src/monad/map.fe new file mode 100644 index 0000000000..e69de29bb2 diff --git a/library/core/src/monad/option.fe b/library/core/src/monad/option.fe index 418b6de817..6e8bfa4798 100644 --- a/library/core/src/monad/option.fe +++ b/library/core/src/monad/option.fe @@ -1,4 +1,4 @@ -enum Option { +pub enum Option { Some(Inner), None } \ No newline at end of file