Skip to content

Commit 591dcb8

Browse files
committed
feat(deep_causality_haft): Added HKT Trait for Arity 5 together with Monad and MonadEffect for HKT5. Refactored code based. Added HKT type extension for Vec.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent df80de2 commit 591dcb8

22 files changed

+806
-267
lines changed

deep_causality_haft/src/effect.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
// ----------------------------------------------------
7+
// Effect Traits (Arity 3)
8+
// ----------------------------------------------------
9+
10+
use crate::{HKT, HKT3, HKT4, HKT5};
11+
12+
/// Effect3: The Bridge Trait for Arity 3 Type Constructors.
13+
///
14+
/// This trait is implemented by a user-defined **System Witness** (e.g., `MyEffect`)
15+
/// to partially apply (fix) two of the three generic parameters of the HKT3 type.
16+
pub trait Effect3 {
17+
/// The fixed type for the first parameter (e.g., the Error type E).
18+
type Fixed1;
19+
20+
/// The fixed type for the second parameter (e.g., the Warning/Log type W).
21+
type Fixed2;
22+
23+
/// The concrete witness type that implements HKT3 with the two fixed types.
24+
/// It MUST implement HKT so we can pass it to Functor/Monad functions.
25+
type HktWitness: HKT3<Self::Fixed1, Self::Fixed2> + HKT;
26+
}
27+
28+
// ----------------------------------------------------
29+
// Effect Traits (Arity 4)
30+
// ----------------------------------------------------
31+
32+
/// Effect4: The Bridge Trait for Arity 4 Type Constructors.I
33+
pub trait Effect4 {
34+
/// The fixed type for the first parameter.
35+
type Fixed1;
36+
37+
/// The fixed type for the second parameter.
38+
type Fixed2;
39+
40+
/// The fixed type for the third parameter.
41+
type Fixed3;
42+
43+
/// The concrete witness type that implements HKT4 with the three fixed types.
44+
/// It MUST implement HKT so we can pass it to Functor/Monad functions.
45+
type HktWitness: HKT4<Self::Fixed1, Self::Fixed2, Self::Fixed3> + HKT;
46+
}
47+
48+
// ----------------------------------------------------
49+
// Effect Traits (Arity 5)
50+
// ----------------------------------------------------
51+
52+
/// Effect5: The Bridge Trait for Arity 5 Type Constructors.
53+
pub trait Effect5 {
54+
type Fixed1;
55+
type Fixed2;
56+
type Fixed3;
57+
type Fixed4;
58+
59+
/// The concrete witness type that implements HKT5 with the four fixed types.
60+
type HktWitness: HKT5<Self::Fixed1, Self::Fixed2, Self::Fixed3, Self::Fixed4> + HKT;
61+
}

deep_causality_haft/src/extensions.rs

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use crate::{Functor, HKT, Monad};
7+
8+
// Witness for Option
9+
pub struct OptionWitness;
10+
11+
impl HKT for OptionWitness {
12+
type Type<T> = Option<T>;
13+
}
14+
15+
// Implementation of Functor for OptionWitness
16+
impl Functor<OptionWitness> for OptionWitness {
17+
fn fmap<A, B, Func>(
18+
m_a: <OptionWitness as HKT>::Type<A>,
19+
f: Func,
20+
) -> <OptionWitness as HKT>::Type<B>
21+
where
22+
Func: FnOnce(A) -> B,
23+
{
24+
m_a.map(f)
25+
}
26+
}
27+
28+
// Implementation of Monad for OptionWitness
29+
impl Monad<OptionWitness> for OptionWitness {
30+
fn pure<T>(value: T) -> <OptionWitness as HKT>::Type<T> {
31+
Some(value)
32+
}
33+
34+
fn bind<A, B, Func>(
35+
m_a: <OptionWitness as HKT>::Type<A>,
36+
f: Func,
37+
) -> <OptionWitness as HKT>::Type<B>
38+
where
39+
Func: FnOnce(A) -> <OptionWitness as HKT>::Type<B>,
40+
{
41+
m_a.and_then(f)
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
use crate::{Functor, HKT, HKT2, Monad, Placeholder};
6+
7+
// Witness for Result<T, E> where E is fixed
8+
pub struct ResultWitness<E>(Placeholder, E);
9+
10+
impl<E> HKT2<E> for ResultWitness<E> {
11+
type Type<T> = Result<T, E>;
12+
}
13+
14+
impl<E> HKT for ResultWitness<E> {
15+
type Type<T> = Result<T, E>;
16+
}
17+
18+
// Implementation of Functor for ResultWitness
19+
impl<E> Functor<ResultWitness<E>> for ResultWitness<E>
20+
where
21+
E: 'static,
22+
{
23+
fn fmap<A, B, Func>(
24+
m_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
25+
f: Func,
26+
) -> <ResultWitness<E> as HKT2<E>>::Type<B>
27+
where
28+
Func: FnOnce(A) -> B,
29+
{
30+
m_a.map(f)
31+
}
32+
}
33+
34+
// Implementation of Monad for ResultWitness
35+
impl<E> Monad<ResultWitness<E>> for ResultWitness<E>
36+
where
37+
E: 'static,
38+
{
39+
fn pure<T>(value: T) -> <ResultWitness<E> as HKT2<E>>::Type<T> {
40+
Ok(value)
41+
}
42+
43+
fn bind<A, B, Func>(
44+
m_a: <ResultWitness<E> as HKT2<E>>::Type<A>,
45+
f: Func,
46+
) -> <ResultWitness<E> as HKT2<E>>::Type<B>
47+
where
48+
Func: FnOnce(A) -> <ResultWitness<E> as HKT2<E>>::Type<B>,
49+
{
50+
m_a.and_then(f)
51+
}
52+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use crate::{Functor, HKT, Monad};
7+
8+
// Witness for Vec<T>
9+
pub struct VecWitness;
10+
11+
impl HKT for VecWitness {
12+
type Type<T> = Vec<T>;
13+
}
14+
15+
// Implementation of Functor for VecWitness
16+
impl Functor<VecWitness> for VecWitness {
17+
fn fmap<A, B, Func>(m_a: <VecWitness as HKT>::Type<A>, f: Func) -> <VecWitness as HKT>::Type<B>
18+
where
19+
Func: FnMut(A) -> B,
20+
{
21+
m_a.into_iter().map(f).collect()
22+
}
23+
}
24+
25+
// Implementation of Monad for VecWitness
26+
impl Monad<VecWitness> for VecWitness {
27+
fn pure<T>(value: T) -> <VecWitness as HKT>::Type<T> {
28+
vec![value]
29+
}
30+
31+
fn bind<A, B, Func>(m_a: <VecWitness as HKT>::Type<A>, f: Func) -> <VecWitness as HKT>::Type<B>
32+
where
33+
Func: FnMut(A) -> <VecWitness as HKT>::Type<B>,
34+
{
35+
m_a.into_iter().flat_map(f).collect()
36+
}
37+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
pub(crate) mod hkt_option_ext;
6+
pub(crate) mod hkt_result_ext;
7+
pub(crate) mod hkt_vec_ext;

deep_causality_haft/src/fp.rs

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)