Skip to content

Commit 2daefc9

Browse files
committed
Trying a new approach for HKT traits and extensions for Uncertain type
Signed-off-by: Marvin Hansen <[email protected]>
1 parent 4a385f9 commit 2daefc9

File tree

15 files changed

+188
-54
lines changed

15 files changed

+188
-54
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deep_causality_uncertain/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ all-features = true
4646
path = "../deep_causality_haft"
4747
version = "0.2"
4848

49+
[dependencies.deep_causality_num]
50+
path = "../deep_causality_num"
51+
version = "0.1"
52+
4953
[dev-dependencies]
5054
rusty-fork = "0.3.1"
5155
criterion = { version = "0.7.0", features = ["html_reports"] }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use crate::Uncertain;
7+
use deep_causality_haft::HKT;
8+
9+
pub struct MaybeUncertainWitness {}
10+
11+
impl HKT for MaybeUncertainWitness {
12+
type Type<T> = Uncertain<T>;
13+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* SPDX-License-Identifier: MIT
3+
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
4+
*/
5+
6+
use crate::Uncertain;
7+
use crate::{ProbabilisticType, UncertainApplicative, UncertainFunctor, UncertainMonad};
8+
use deep_causality_haft::HKT;
9+
10+
pub struct UncertainWitness {}
11+
12+
impl HKT for UncertainWitness {
13+
type Type<T> = Uncertain<T>;
14+
}
15+
16+
impl UncertainApplicative<UncertainWitness> for UncertainWitness {
17+
fn pure<T>(value: T) -> <UncertainWitness as HKT>::Type<T>
18+
where
19+
T: ProbabilisticType,
20+
{
21+
unimplemented!()
22+
}
23+
24+
fn apply<A, B, Func>(
25+
f_ab: <UncertainWitness as HKT>::Type<Func>,
26+
f_a: <UncertainWitness as HKT>::Type<A>,
27+
) -> <UncertainWitness as HKT>::Type<B>
28+
where
29+
Func: FnMut(A) -> B,
30+
A: ProbabilisticType,
31+
B: ProbabilisticType,
32+
{
33+
unimplemented!()
34+
}
35+
}
36+
37+
impl UncertainFunctor<UncertainWitness> for UncertainWitness {
38+
fn fmap<A, B, Func>(
39+
m_a: <UncertainWitness as HKT>::Type<A>,
40+
f: Func,
41+
) -> <UncertainWitness as HKT>::Type<B>
42+
where
43+
Func: FnMut(A) -> B,
44+
A: ProbabilisticType,
45+
B: ProbabilisticType,
46+
{
47+
unimplemented!()
48+
}
49+
}
50+
51+
impl UncertainMonad<UncertainWitness> for UncertainWitness {
52+
fn bind<A, B, Func>(
53+
m_a: <UncertainWitness as HKT>::Type<A>,
54+
f: Func,
55+
) -> <UncertainWitness as HKT>::Type<B>
56+
where
57+
Func: FnMut(A) -> <UncertainWitness as HKT>::Type<B>,
58+
A: ProbabilisticType,
59+
B: ProbabilisticType,
60+
{
61+
unimplemented!()
62+
}
63+
}
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_maybe_uncertain;
6+
pub(crate) mod hkt_uncertain;
7+
pub(crate) mod probabilistic_type;

deep_causality_uncertain/src/traits/probabilistic_type/bool_probabilistic_type.rs renamed to deep_causality_uncertain/src/extensions/probabilistic_type/bool_probabilistic_type.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
44
*/
55

6-
use crate::ProbabilisticType;
7-
use crate::errors::UncertainError;
8-
use crate::types::cache::SampledValue;
6+
use crate::{FromSampledValue, IntoSampledValue, ProbabilisticType};
7+
use crate::{SampledValue, UncertainError};
98

10-
impl ProbabilisticType for bool {
11-
fn to_sampled_value(&self) -> SampledValue {
9+
impl IntoSampledValue for bool {
10+
fn into_sampled_value(&self) -> SampledValue {
1211
SampledValue::Bool(*self)
1312
}
13+
}
1414

15+
impl FromSampledValue for bool {
1516
fn from_sampled_value(value: SampledValue) -> Result<Self, UncertainError> {
1617
match value {
1718
SampledValue::Bool(b) => Ok(b),
@@ -20,7 +21,9 @@ impl ProbabilisticType for bool {
2021
)),
2122
}
2223
}
24+
}
2325

26+
impl ProbabilisticType for bool {
2427
fn default_value() -> Self {
2528
false
2629
}

deep_causality_uncertain/src/traits/probabilistic_type/f64_probabilistic_type.rs renamed to deep_causality_uncertain/src/extensions/probabilistic_type/f64_probabilistic_type.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
44
*/
55

6-
use crate::ProbabilisticType;
7-
use crate::errors::UncertainError;
8-
use crate::types::cache::SampledValue;
6+
use crate::{FromSampledValue, IntoSampledValue, ProbabilisticType};
7+
use crate::{SampledValue, UncertainError};
8+
use deep_causality_num::ToPrimitive;
99

10-
impl ProbabilisticType for f64 {
11-
fn to_sampled_value(&self) -> SampledValue {
12-
SampledValue::Float(*self)
10+
impl IntoSampledValue for f64 {
11+
fn into_sampled_value(&self) -> SampledValue {
12+
SampledValue::Float(self.to_f64().unwrap_or(f64::NAN))
1313
}
14+
}
1415

16+
impl FromSampledValue for f64 {
1517
fn from_sampled_value(value: SampledValue) -> Result<Self, UncertainError> {
1618
match value {
1719
SampledValue::Float(f) => Ok(f),
@@ -20,8 +22,10 @@ impl ProbabilisticType for f64 {
2022
)),
2123
}
2224
}
25+
}
2326

27+
impl ProbabilisticType for f64 {
2428
fn default_value() -> Self {
25-
0.0
29+
f64::default()
2630
}
2731
}
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+
6+
mod bool_probabilistic_type;
7+
mod f64_probabilistic_type;

deep_causality_uncertain/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
mod algos;
99
mod alias;
1010
mod errors;
11+
mod extensions;
1112
mod traits;
1213
mod types;
1314
pub mod utils_tests;
@@ -18,9 +19,14 @@ pub use crate::algos::hypothesis::sprt_eval;
1819
pub use crate::alias::{MaybeUncertainBool, MaybeUncertainF64, UncertainBool, UncertainF64};
1920
// Errors
2021
pub use crate::errors::UncertainError;
22+
// Extensions
23+
pub use crate::extensions::hkt_maybe_uncertain::MaybeUncertainWitness;
24+
pub use crate::extensions::hkt_uncertain::UncertainWitness;
2125
// Traits
22-
pub use crate::traits::hkt::{UncertainApplicative, UncertainFunctor, UncertainMonad};
23-
pub use crate::traits::probabilistic_type::ProbabilisticType;
26+
pub use crate::traits::hkt::uncertain_applicative::UncertainApplicative;
27+
pub use crate::traits::hkt::uncertain_functor::UncertainFunctor;
28+
pub use crate::traits::hkt::uncertain_monad::UncertainMonad;
29+
pub use crate::traits::probabilistic::{FromSampledValue, IntoSampledValue, ProbabilisticType};
2430
pub use crate::traits::sampler::Sampler;
2531
// Types
2632
pub use crate::types::cache::{GlobalSampleCache, SampledValue, with_global_cache};

deep_causality_uncertain/src/traits/hkt/mod.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,6 @@
33
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
44
*/
55

6-
use crate::ProbabilisticType;
7-
use deep_causality_haft::HKT;
8-
9-
pub trait UncertainFunctor<F: HKT> {
10-
fn fmap<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>
11-
where
12-
Func: FnMut(A) -> B,
13-
A: ProbabilisticType,
14-
B: ProbabilisticType;
15-
}
16-
17-
pub trait UncertainApplicative<F: HKT> {
18-
fn pure<T>(value: T) -> F::Type<T>
19-
where
20-
T: ProbabilisticType;
21-
22-
fn apply<A, B, Func>(f_ab: F::Type<Func>, f_a: F::Type<A>) -> F::Type<B>
23-
where
24-
Func: FnMut(A) -> B,
25-
A: ProbabilisticType,
26-
B: ProbabilisticType;
27-
}
28-
29-
pub trait UncertainMonad<F: HKT> {
30-
fn bind<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>
31-
where
32-
Func: FnMut(A) -> F::Type<B>,
33-
A: ProbabilisticType,
34-
B: ProbabilisticType;
35-
}
6+
pub(crate) mod uncertain_applicative;
7+
pub(crate) mod uncertain_functor;
8+
pub(crate) mod uncertain_monad;

0 commit comments

Comments
 (0)