1#[fp_macros::document_module]
6mod inner {
7 use {
8 crate::{
9 Apply,
10 brands::optics::*,
11 classes::{
12 profunctor::{
13 Closed,
14 Profunctor,
15 },
16 *,
17 },
18 impl_kind,
19 kinds::*,
20 },
21 fp_macros::*,
22 };
23
24 #[document_type_parameters(
26 "The lifetime of the functions.",
27 "The cloneable function brand.",
28 "The type of the value produced by the inner function.",
29 "The type of the value consumed by the inner function.",
30 "The source type of the structure.",
31 "The target type of the structure."
32 )]
33 pub struct Grating<'a, FunctionBrand: LiftFn, A: 'a, B: 'a, S: 'a, T: 'a> {
34 pub run: <FunctionBrand as CloneFn>::Of<
36 'a,
37 <FunctionBrand as CloneFn>::Of<'a, <FunctionBrand as CloneFn>::Of<'a, S, A>, B>,
38 T,
39 >,
40 }
41
42 #[document_type_parameters(
43 "The lifetime of the functions.",
44 "The cloneable function brand.",
45 "The type of the value produced by the inner function.",
46 "The type of the value consumed by the inner function.",
47 "The source type of the structure.",
48 "The target type of the structure."
49 )]
50 impl<'a, FunctionBrand: LiftFn, A: 'a, B: 'a, S: 'a, T: 'a> Grating<'a, FunctionBrand, A, B, S, T> {
51 #[document_signature]
53 #[document_parameters("The grating function.")]
55 #[document_returns("A new instance of the type.")]
57 #[document_examples]
59 pub fn new(
81 run: <FunctionBrand as CloneFn>::Of<
82 'a,
83 <FunctionBrand as CloneFn>::Of<'a, <FunctionBrand as CloneFn>::Of<'a, S, A>, B>,
84 T,
85 >
86 ) -> Self {
87 Grating {
88 run,
89 }
90 }
91 }
92
93 impl_kind! {
94 impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> for GratingBrand<FunctionBrand, A, B> {
95 #[document_default]
96 type Of<'a, S: 'a, T: 'a>: 'a = Grating<'a, FunctionBrand, A, B, S, T>;
97 }
98 }
99
100 #[document_type_parameters(
101 "The cloneable function brand.",
102 "The type of the value produced by the inner function.",
103 "The type of the value consumed by the inner function."
104 )]
105 impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Profunctor
106 for GratingBrand<FunctionBrand, A, B>
107 {
108 #[document_signature]
110 #[document_type_parameters(
112 "The lifetime of the functions.",
113 "The source type of the new structure.",
114 "The target type of the new structure.",
115 "The source type of the original structure.",
116 "The target type of the original structure."
117 )]
118 #[document_parameters(
120 "The function to apply to the input.",
121 "The function to apply to the output.",
122 "The grating instance to transform."
123 )]
124 #[document_returns("A transformed `Grating` instance.")]
126 #[document_examples]
128 fn dimap<'a, S: 'a, T: 'a, U: 'a, V: 'a>(
163 st: impl Fn(S) -> T + 'a,
164 uv: impl Fn(U) -> V + 'a,
165 puv: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, T, U>),
166 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, V>) {
167 let run = puv.run;
168 let st = <FunctionBrand as LiftFn>::new(st);
169 let uv = <FunctionBrand as LiftFn>::new(uv);
170 Grating::<FunctionBrand, A, B, S, V>::new(<FunctionBrand as LiftFn>::new(
171 move |f: <FunctionBrand as CloneFn>::Of<
172 'a,
173 <FunctionBrand as CloneFn>::Of<'a, S, A>,
174 B,
175 >| {
176 let st = st.clone();
177 let uv = uv.clone();
178 (*uv)((*run)(<FunctionBrand as LiftFn>::new(
179 move |g: <FunctionBrand as CloneFn>::Of<'a, T, A>| {
180 let st = st.clone();
181 f(<FunctionBrand as LiftFn>::new(move |s| g((*st)(s))))
182 },
183 )))
184 },
185 ))
186 }
187 }
188
189 #[document_type_parameters(
190 "The cloneable function brand.",
191 "The type of the value produced by the inner function.",
192 "The type of the value consumed by the inner function."
193 )]
194 impl<FunctionBrand: LiftFn + 'static, A: 'static, B: 'static> Closed<FunctionBrand>
195 for GratingBrand<FunctionBrand, A, B>
196 {
197 #[document_signature]
199 #[document_type_parameters(
201 "The lifetime of the functions.",
202 "The source type of the structure.",
203 "The target type of the structure.",
204 "The type of the function input."
205 )]
206 #[document_parameters("The grating instance to transform.")]
208 #[document_returns("A transformed `Grating` instance that operates on functions.")]
210 #[document_examples]
212 fn closed<'a, S: 'a, T: 'a, X: 'a + Clone>(
254 pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
255 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, <FunctionBrand as CloneFn>::Of<'a, X, S>, <FunctionBrand as CloneFn>::Of<'a, X, T>>)
256 {
257 let run = pab.run;
258 Grating::<
259 FunctionBrand,
260 A,
261 B,
262 <FunctionBrand as CloneFn>::Of<'a, X, S>,
263 <FunctionBrand as CloneFn>::Of<'a, X, T>,
264 >::new(<FunctionBrand as LiftFn>::new(
265 move |g: <FunctionBrand as CloneFn>::Of<
266 'a,
267 <FunctionBrand as CloneFn>::Of<'a, <FunctionBrand as CloneFn>::Of<'a, X, S>, A>,
268 B,
269 >| {
270 let run = run.clone();
271 <FunctionBrand as LiftFn>::new(move |x: X| {
272 let g = g.clone();
273 let x = x.clone();
274 (*run)(<FunctionBrand as LiftFn>::new(
275 move |h: <FunctionBrand as CloneFn>::Of<'a, S, A>| {
276 let x = x.clone();
277 g(<FunctionBrand as LiftFn>::new(
278 move |k: <FunctionBrand as CloneFn>::Of<'a, X, S>| {
279 h(k(x.clone()))
280 },
281 ))
282 },
283 ))
284 })
285 },
286 ))
287 }
288 }
289}
290pub use inner::*;