Skip to content

Commit

Permalink
monad
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jun 21, 2024
1 parent 680e3a9 commit 4aec4d4
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 1 deletion.
173 changes: 173 additions & 0 deletions library/core/src/monad copy.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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
// }
// }
// }

// fn foo<F>(f: F)
// where F: Fn(u8, u8, u8) -> u8 {
// where F: Fn<{u8, u8, u8}, u8>

// }

// fn f(u8, u8, u8) -> u8 {} // (u8, u8, u8) -> u8
// fn g((u8, u8, u8)) -> u8 {} //((u8,u8,u8)) -> u8

// fn main() {
// f(1, 2, 3) // f.exec((1, 2, 3))

// let x = (1, 2, 3)
// f(x)
// g(x)

// let args = {1, 2, 3}
// args.push(1)

// let args = (1, 2, 3)
// f (1, 2, 3)
// f args

// let x = ((u8, u8, u8))
// let x = ((u8, u8, u8),)

// f(args..) // f(x, y) f (
// }
// (u8, u8, u8) -> u8
// u8 -> u8 -> u8 -> u8



// impl Fn<((u8, u8, u8)), u8> for G

// impl Fn<(u8, u8, u8), u8> for Foo {

// }

// trait Fn<T, U> where T: FnArgs {
// fn exect(&self, t: T) -> U
// }

// trait Fn<Arg, Ret, const Arity: usize>
// where Arg: Tuple<Arity>
// {
// fn call(self, arg : Arg) -> Ret
// }

// trait FnMut<T, U> where T: FnArgs {
// fn exect(&mut self, t: T) -> U
// }

// trait Tuple<const N: usize>;

// fn map<T, U,F>(t: T, f: F) -> U
// where F: Fn(T) -> U {...}

// type MyFn = ()

// impl<T, U> Fn<T, U> for Foo {
// fn exec(&self, t: T) -> U {
// t.element(0)
// t.len()
// }
// }

// impl Fn<(u8, u8>, Option<u8>> for MyFn {
// fn exec(t: (bool, bool)) -> Option<u8> {
// let (a, b) = t
// f(a, b)
// }
// }

// #test
// fn my_test() {
// let f: MyFn = ()
// let a = Option::Some(true).bind<_, _, _>(f)
// let a: Option<u8> = Option::Some(true).bind(f)
// }


// sub x y = x - y
// f = sub 1 // f 2 == 1 - 2
// g = `sub` 1 // g 2 == 2 - 1


// enum Result<T, E> {
// Ok(T)
// Err(E)
// }
// impl<E> Functor for Result<*, E> { // or Result<_, E> maybe?
// fn map<A, B, F: Fn<A, B>>(self: Self<A>, _ f: F) -> Self<B> {
// match self {
// Result::Ok(a) => {
// return Result::Ok(f(a))
// }
// Result::Err(e) => {
// return Result::Err(e)
// }
// }
// }
// }


// enum Res<E, T> { // Result<T, E>
// Err(E)
// Ok(T)
// }
// impl Functor for Res<MyError> {
// fn map<A, B, F: Fn<A, B>>(self: Self<A>, _ f: F) -> Self<B> {
// match self {
// Result::Ok(a) => {
// return Result::Ok(f(a))
// }
// Result::Err(e) => {
// return Result::Err(e)
// }
// }
// }
// }



// fn main() {
// let r = foo()
// r.map(|t| ...)
// }


// enum Result1<E, T>

// Result<_, E>

// Flip (Result)



// ((+) `foldl` 0) [1..5]
// ((`foldl` 0) [1..5]) (+)

// flip :: (a -> b -> c) -> b -> a -> c
// f :: a -> b -> c -> d -> e
// flip f :: b -> a -> c -> d -> e
135 changes: 135 additions & 0 deletions library/core/src/monad.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
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<Source, Target, Morph>(self: Self<Source>, morph: Morph) -> Self<Target>
where Morph: Fn<1, (Source), Self<Target>>, (Source): Tuple<1>
}

impl Monad for Option {
fn bind<Source, Target, Morph>(self: Self<Source>, morph: Morph) -> Self<Target>
where Morph: Fn<1, (Source), Self<Target>>, (Source): Tuple<1>
{
match self {
Self::Some(self) => morph.exec(args: (self, ))
Self::None => Self::None
}
}
}

trait Fn<const Arity: u8, Args, Ret> where Args: Tuple<Arity> {
fn exec(self, args: Args) -> Ret
}

trait Tuple<const N: u8> { }

// type MyFn = ()

// impl<T> Fn<1, (bool), Option<u8>> for MyFn {
// fn exec(self, args: (bool)) -> Option<u8> {
// Option::Some(0)
// }
// }

// impl Tuple<1> for (bool) { }

// #test
// fn my_test() {
// let f: MyFn = ()
// let a = Option::Some(true).bind<bool, u8, MyFn>(f)
// }

// fn map<T, U,F>(t: T, f: F) -> U
// where F: Fn(T) -> U {...}

// type MyFn = ()

// impl<T, U> Fn<T, U> for Foo {
// fn exec(&self, t: T) -> U {
// t.element(0)
// t.len()
// }
// }

// impl Fn<(u8, u8>, Option<u8>> for MyFn {
// fn exec(t: (bool, bool)) -> Option<u8> {
// let (a, b) = t
// f(a, b)
// }
// }

// sub x y = x - y
// f = sub 1 // f 2 == 1 - 2
// g = `sub` 1 // g 2 == 2 - 1


// enum Result<T, E> {
// Ok(T)
// Err(E)
// }
// impl<E> Functor for Result<*, E> { // or Result<_, E> maybe?
// fn map<A, B, F: Fn<A, B>>(self: Self<A>, _ f: F) -> Self<B> {
// match self {
// Result::Ok(a) => {
// return Result::Ok(f(a))
// }
// Result::Err(e) => {
// return Result::Err(e)
// }
// }
// }
// }


// enum Res<E, T> { // Result<T, E>
// Err(E)
// Ok(T)
// }
// impl Functor for Res<MyError> {
// fn map<A, B, F: Fn<A, B>>(self: Self<A>, _ f: F) -> Self<B> {
// match self {
// Result::Ok(a) => {
// return Result::Ok(f(a))
// }
// Result::Err(e) => {
// return Result::Err(e)
// }
// }
// }
// }



// fn main() {
// let r = foo()
// r.map(|t| ...)
// }


// enum Result1<E, T>

// Result<_, E>

// Flip (Result)



// ((+) `foldl` 0) [1..5]
// ((`foldl` 0) [1..5]) (+)

// flip :: (a -> b -> c) -> b -> a -> c
// f :: a -> b -> c -> d -> e
// flip f :: b -> a -> c -> d -> e
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 4aec4d4

Please sign in to comment.