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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#![feature(tuple_trait)]
#![feature(unboxed_closures)]
#![feature(fn_traits)]
#![feature(generic_const_exprs)]
#![feature(const_trait_impl)]

//! https://en.wikipedia.org/wiki/Function_composition
//! 
//! A crate providing a trait for performing currying (and non-currying) function-composition in rust.
//! 
//! Non-currying composition:
//! h(x) = g ∘ f = g(f(x))
//! 
//! Currying composition:
//! h(..., x) = g ∘ f = g(f(x), ...)
//! 
//! When currying, arguments of the function being curried with (f) is moved to the end of the argument-list
//! 
//! Both operands must implement FnOnce. If both implement FnMut or Fn, the resulting composition will also implement these traits.
//! 
//! g must also have one or more argument, where the first argument type equals the return type of f.
//! 
//! ```rust
//! #![feature(generic_const_exprs)]
//! 
//! use currycompose::*;
//! 
//! // g ∘ f
//! // where
//! // g :: f32 -> f32
//! // f :: u8 -> f32
//! // g ∘ f :: u8 -> f32
//! let g = |x: f32| x*x;
//! let f = |x: u8| x as f32;
//! 
//! let gf = g.compose(f);
//! 
//! let x = 1;
//! 
//! assert_eq!(gf(x), g(f(x)));
//! 
//! // g ∘ f
//! // where
//! // g :: f32 -> f32 -> f32
//! // f :: u8 -> f32
//! // g ∘ f :: f32 -> u8 -> f32
//! let g = |x: f32, y: f32| x + y;
//! let f = gf;
//! 
//! let gf = g.compose(f);
//! 
//! let x = 1;
//! let y = 1.0;
//! 
//! // note here the argument x has been shifted to the end of the args in gf
//! assert_eq!(gf(y, x), g(f(x), y));
//! 
//! // g ∘ f ∘ f
//! // where
//! // g :: f32 -> f32 -> f32
//! // f :: u8 -> f32
//! // g ∘ f ∘ f :: u8 -> u8 -> f32
//! let gff = gf.compose(f);
//! 
//! let x = 1;
//! let y = 1;
//! 
//! assert_eq!(gff(x, y), g(f(x), f(y)));
//! ```

use std::marker::{Tuple, PhantomData};

use tupleops::{ConcatTuples, TupleConcat, concat_tuples, TupleLength, TupleUnprepend, Head, Tail};

use tuple_split::{TupleSplit, SplitInto};

/// https://en.wikipedia.org/wiki/Function_composition
/// 
/// Trait for composing two functions (currying and non-curring composition)
/// 
/// Non-currying composition:
/// h(x) = g ∘ f = g(f(x))
/// 
/// Currying composition:
/// h(..., x) = g ∘ f = g(f(x), ...)
/// 
/// When currying, arguments of the function being curried with (f) is moved to the end of the argument-list
/// 
/// Both operands must implement FnOnce. If both implement FnMut or Fn, the resulting composition will also implement these traits.
/// 
/// g must also have one or more argument, where the first argument type equals the return type of f.
/// 
/// ```rust
/// #![feature(generic_const_exprs)]
/// 
/// use currycompose::*;
/// 
/// // g ∘ f
/// // where
/// // g :: f32 -> f32
/// // f :: u8 -> f32
/// // g ∘ f :: u8 -> f32
/// let g = |x: f32| x*x;
/// let f = |x: u8| x as f32;
/// 
/// let gf = g.compose(f);
/// 
/// let x = 1;
/// 
/// assert_eq!(gf(x), g(f(x)));
/// 
/// // g ∘ f
/// // where
/// // g :: f32 -> f32 -> f32
/// // f :: u8 -> f32
/// // g ∘ f :: f32 -> u8 -> f32
/// let g = |x: f32, y: f32| x + y;
/// let f = gf;
/// 
/// let gf = g.compose(f);
/// 
/// let x = 1;
/// let y = 1.0;
/// 
/// // note here the argument x has been shifted to the end of the args in gf
/// assert_eq!(gf(y, x), g(f(x), y));
/// 
/// // g ∘ f ∘ f
/// // where
/// // g :: f32 -> f32 -> f32
/// // f :: u8 -> f32
/// // g ∘ f ∘ f :: u8 -> u8 -> f32
/// let gff = gf.compose(f);
/// 
/// let x = 1;
/// let y = 1;
/// 
/// assert_eq!(gff(x, y), g(f(x), f(y)));
/// ```
#[const_trait]
pub trait Compose<F, XG, XF>: Sized
{
    /// Composing two functions
    /// 
    /// h(x) = g ∘ f = g(f(x))
    fn compose(self, with: F) -> Composition<Self, F, XG, XF>;
}

impl<G, F, XG, XF> const Compose<F, XG, XF> for G
where
    XG: Tuple + TupleUnprepend<XG>,
    XF: Tuple,
    Self: FnOnce<XG>,
    F: FnOnce<XF, Output = Head<XG>>,
    (Tail<XG>, XF): TupleConcat<Tail<XG>, XF>,
    ConcatTuples<Tail<XG>, XF>: Tuple,
    Composition<Self, F, XG, XF>: FnOnce<ConcatTuples<Tail<XG>, XF>>
{
    fn compose(self, with: F) -> Composition<Self, F, XG, XF>
    {
        Composition {
            g: self,
            f: with,
            phantom: PhantomData
        }
    }
}

/// A struct representing a function composed with another.
/// 
/// When calling the composition as a function, the leftover arguments of the composition function come first (if curried), then the arguments of the function being composed with.
/// 
/// ```rust
/// #![feature(generic_const_exprs)]
/// 
/// use currycompose::*;
/// 
/// // g ∘ f
/// // where
/// // g :: f32 -> f32
/// // f :: u8 -> f32
/// // g ∘ f :: u8 -> f32
/// let g = |x: f32| x*x;
/// let f = |x: u8| x as f32;
/// 
/// let gf = g.compose(f);
/// 
/// let x = 1;
/// 
/// assert_eq!(gf(x), g(f(x)));
/// 
/// // g ∘ f
/// // where
/// // g :: f32 -> f32 -> f32
/// // f :: u8 -> f32
/// // g ∘ f :: f32 -> u8 -> f32
/// let g = |x: f32, y: f32| x + y;
/// let f = gf;
/// 
/// let gf = g.compose(f);
/// 
/// let x = 1;
/// let y = 1.0;
/// 
/// // note here the argument x has been shifted to the end of the args in gf
/// assert_eq!(gf(y, x), g(f(x), y));
/// 
/// // g ∘ f ∘ f
/// // where
/// // g :: f32 -> f32 -> f32
/// // f :: u8 -> f32
/// // g ∘ f ∘ f :: u8 -> u8 -> f32
/// let gff = gf.compose(f);
/// 
/// let x = 1;
/// let y = 1;
/// 
/// assert_eq!(gff(x, y), g(f(x), f(y)));
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Composition<G, F, XG, XF>
{
    g: G,
    f: F,
    phantom: PhantomData<(XG, XF)>,
}

impl<G, F, XG, XF> FnOnce<ConcatTuples<Tail<XG>, XF>> for Composition<G, F, XG, XF>
where
    XG: Tuple + TupleUnprepend<XG>,
    XF: Tuple,
    G: FnOnce<XG>,
    F: FnOnce<XF, Output = Head<XG>>,
    (Tail<XG>, XF): TupleConcat<Tail<XG>, XF>,
    ConcatTuples<Tail<XG>, XF>: Tuple + SplitInto<Tail<XG>, XF>,
    [(); <Tail<XG> as TupleLength>::LENGTH]:,
    ((F::Output,), Tail<XG>): TupleConcat<(F::Output,), Tail<XG>, Type = XG>
{
    type Output = <G as FnOnce<XG>>::Output;

    extern "rust-call" fn call_once(self, args: ConcatTuples<Tail<XG>, XF>) -> Self::Output
    {
        let (left, right): (Tail<XG>, XF) = args.split_tuple();
        self.g.call_once(concat_tuples((self.f.call_once(right),), left))
    }
}

impl<G, F, XG, XF> FnMut<ConcatTuples<Tail<XG>, XF>> for Composition<G, F, XG, XF>
where
    XG: Tuple + TupleUnprepend<XG>,
    XF: Tuple,
    G: FnMut<XG>,
    F: FnMut<XF, Output = Head<XG>>,
    (Tail<XG>, XF): TupleConcat<Tail<XG>, XF>,
    ConcatTuples<Tail<XG>, XF>: Tuple + SplitInto<Tail<XG>, XF>,
    [(); <Tail<XG> as TupleLength>::LENGTH]:,
    ((F::Output,), Tail<XG>): TupleConcat<(F::Output,), Tail<XG>, Type = XG>
{
    extern "rust-call" fn call_mut(&mut self, args: ConcatTuples<Tail<XG>, XF>) -> Self::Output
    {
        let (left, right) = args.split_tuple();
        self.g.call_mut(concat_tuples((self.f.call_mut(right),), left))
    }
}

impl<G, F, XG, XF> Fn<ConcatTuples<Tail<XG>, XF>> for Composition<G, F, XG, XF>
where
    XG: Tuple + TupleUnprepend<XG>,
    XF: Tuple,
    G: Fn<XG>,
    F: Fn<XF, Output = Head<XG>>,
    (Tail<XG>, XF): TupleConcat<Tail<XG>, XF>,
    ConcatTuples<Tail<XG>, XF>: Tuple + SplitInto<Tail<XG>, XF>,
    [(); <Tail<XG> as TupleLength>::LENGTH]:,
    ((F::Output,), Tail<XG>): TupleConcat<(F::Output,), Tail<XG>, Type = XG>
{
    extern "rust-call" fn call(&self, args: ConcatTuples<Tail<XG>, XF>) -> Self::Output
    {
        let (left, right) = args.split_tuple();
        self.g.call(concat_tuples((self.f.call(right),), left))
    }
}