1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use crate::functional::{ApplicativeFunctor, ApplyFunctor, FMapFunctor, Functor};
use std::marker::PhantomData;
pub trait Maybe {}
pub struct Just<T> {
_phantom: PhantomData<T>,
}
impl<T> Maybe for Just<T> {}
pub struct Nothing;
impl Maybe for Nothing {}
pub struct UnwrapFunctor {}
pub type Unwrap<T> = ApplyFunctor<UnwrapFunctor, T>;
impl<T> Functor<Just<T>> for UnwrapFunctor {
type Output = T;
}
pub struct UnwrapOrFunctor<DefaultValue> {
_phantom: PhantomData<DefaultValue>,
}
pub type UnwrapOr<T, DefaultValue> = ApplyFunctor<UnwrapOrFunctor<DefaultValue>, T>;
impl<T, DefaultValue> Functor<Just<T>> for UnwrapOrFunctor<DefaultValue> {
type Output = T;
}
impl<DefaultValue> Functor<Nothing> for UnwrapOrFunctor<DefaultValue> {
type Output = DefaultValue;
}
pub struct MaybeMapFunctor<Func> {
_phantom: PhantomData<Func>,
}
pub type MaybeMap<Input, Func> = ApplyFunctor<MaybeMapFunctor<Func>, Input>;
impl<Func> Functor<Nothing> for MaybeMapFunctor<Func> {
type Output = Nothing;
}
impl<Func, T> Functor<Just<T>> for MaybeMapFunctor<Func>
where
Func: Functor<T>,
{
type Output = Just<ApplyFunctor<Func, T>>;
}
pub struct MaybeFilterFunctor<Func> {
_phantom: PhantomData<Func>,
}
pub type MaybeFilter<Input, Func> = ApplyFunctor<MaybeFilterFunctor<Func>, Input>;
impl<Func> Functor<Nothing> for MaybeFilterFunctor<Func> {
type Output = Nothing;
}
impl<Func, T> Functor<Just<T>> for MaybeFilterFunctor<Func>
where
Func: Functor<T>,
Func::Output: Maybe,
{
type Output = ApplyFunctor<Func, T>;
}
impl<Func> Functor<Nothing> for FMapFunctor<Func> {
type Output = Nothing;
}
impl<T, Func> Functor<Just<T>> for FMapFunctor<Func>
where
MaybeMapFunctor<Func>: Functor<Just<T>>,
{
type Output = MaybeMap<Just<T>, Func>;
}
impl Functor<Nothing> for ApplicativeFunctor<Nothing> {
type Output = Nothing;
}
impl<Func> Functor<Just<Func>> for ApplicativeFunctor<Nothing> {
type Output = Nothing;
}
impl<Value> Functor<Nothing> for ApplicativeFunctor<Just<Value>> {
type Output = Nothing;
}
impl<Func, Value> Functor<Just<Func>> for ApplicativeFunctor<Just<Value>>
where
Func: Functor<Value>,
{
type Output = Just<ApplyFunctor<Func, Value>>;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
boolean::Boolean,
control::{IfElsePredicate, IfElsePredicateOutput, IfSameOutput},
functional::{Applicative, FMap},
numeric::AddOneFunctor,
};
use typenum::{consts::*, GrEq, IsGreaterOrEqual, Unsigned};
type Opt1 = Just<U3>;
type Opt2 = Nothing;
type AssertSame<Lhs, Rhs> = IfSameOutput<(), Lhs, Rhs>;
type Assert1 = AssertSame<Unwrap<Opt1>, U3>;
type Assert2 = AssertSame<UnwrapOr<Opt1, U0>, U3>;
type Assert3 = AssertSame<UnwrapOr<Opt2, U0>, U0>;
struct BoxFunc;
impl<Input> Functor<Input> for BoxFunc {
type Output = Box<Input>;
}
type Assert4 = IfSameOutput<(), MaybeMap<Just<i8>, BoxFunc>, Just<Box<i8>>>;
type Assert5 = IfSameOutput<(), MaybeMap<Nothing, BoxFunc>, Nothing>;
struct ThresholdFunc;
impl<Input> Functor<Input> for ThresholdFunc
where
Input: Unsigned + IsGreaterOrEqual<U4>,
GrEq<Input, U4>: Boolean,
Just<Input>: IfElsePredicate<GrEq<Input, U4>, Nothing>,
{
type Output = IfElsePredicateOutput<Just<Input>, GrEq<Input, U4>, Nothing>;
}
type Assert6 = IfSameOutput<(), MaybeFilter<Just<U8>, ThresholdFunc>, Just<U8>>;
type Assert7 = IfSameOutput<(), MaybeFilter<Just<U2>, ThresholdFunc>, Nothing>;
type Assert8 = IfSameOutput<(), MaybeFilter<Nothing, ThresholdFunc>, Nothing>;
type Assert9 = IfSameOutput<(), FMap<Nothing, AddOneFunctor>, Nothing>;
type Assert10 = IfSameOutput<(), FMap<Just<U8>, AddOneFunctor>, Just<U9>>;
type Assert11 = IfSameOutput<(), Applicative<Nothing, Nothing>, Nothing>;
type Assert12 = IfSameOutput<(), Applicative<Just<AddOneFunctor>, Nothing>, Nothing>;
type Assert13 = IfSameOutput<(), Applicative<Nothing, Just<U7>>, Nothing>;
type Assert14 = IfSameOutput<(), Applicative<Just<AddOneFunctor>, Just<U7>>, Just<U8>>;
#[test]
fn maybe_test() {
let _: Assert1 = ();
let _: Assert2 = ();
let _: Assert3 = ();
let _: Assert4 = ();
let _: Assert5 = ();
let _: Assert6 = ();
let _: Assert7 = ();
let _: Assert8 = ();
let _: Assert9 = ();
let _: Assert10 = ();
let _: Assert11 = ();
let _: Assert12 = ();
let _: Assert13 = ();
let _: Assert14 = ();
}
}