Skip to content

Commit

Permalink
monad
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jun 25, 2024
1 parent 680e3a9 commit 38df535
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 91 deletions.
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
174 changes: 174 additions & 0 deletions library/core/src/monad.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use self::option::Option
use super::num::Num
use super::num::arith::HAdd

trait Tuple<const N: u8> { }

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

pub enum Option<T> {
Some(T),
None
}

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
}
}
}

struct BindAdd<N> { n: N }

impl<M: * -> *, N> Fn<1, (N), M<N>> for BindAdd<N>
where M: Monad, N: HAdd<N, N>, (N): Tuple<1>
{
fn exec(self, args: (N)) -> M<N> {
M::pure(t: args.0.add(rhs: self.n))
}
}

impl<M: * -> *, N> HAdd<N, M<N>> for M<N>
where M: Monad, N: HAdd<N, N>, (N): Tuple<1>
{
fn add(self, rhs: N) -> M<N> {
self.bind<N, N, BindAdd<N>>(morph: BindAdd { n: rhs })
}
}

// impl HAdd<u8, Option<u8>> for Option<u8> {
// fn add(self, rhs: u8) -> Option<u8> {
// self.bind<u8, u8, BindAdd<u8>>(morph: BindAdd { n: rhs })
// }
// }

impl Tuple<1> for (u8) { }

fn test_monadd() {
let a: Option<u8> = Option::Some(42)
let b: u8 = 26
let c: Option<u8> = a.add(rhs: b)
}

// 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
}
28 changes: 15 additions & 13 deletions library/core/src/num.fe
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// use ingot::num::arith::{Add, Sub, Mul, Div, Neg, Mod, Exp}
use ingot::num::arith::{Add, Sub, Mul, Div, Neg, Mod, Pow}

// pub trait Num: Add + Sub + Mul + Div + Exp { }
pub trait Num: Add + Sub + Mul + Div + Pow { }

// pub trait Complex<N>: Num + Neg {
// fn re() -> N
// fn im() -> N
// }
pub trait Complex<N>: Num + Neg {
fn re() -> N
fn im() -> N
}

// pub trait Real: Num + Neg { }
pub trait Real: Num + Neg { }

// pub trait Frac<N>: Num + Neg {
// fn nu() -> N
// fn de() -> N
// }
pub trait Frac<N>: Num + Neg {
fn nu() -> N
fn de() -> N
}

// pub trait Int: Num + Mod + Neg { }
pub trait Int: Num + Mod + Neg { }

// pub trait Nat: Num + Mod { }
pub trait Nat: Num + Mod { }

impl Num for u8 { }
Loading

0 comments on commit 38df535

Please sign in to comment.