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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! Injective type-level functions


use crate::TypeFn;

use core::marker::PhantomData;

/// An  [injective]  type-level function
/// 
/// This trait is implemented automatically when both
/// [`TypeFn`] and [`RevTypeFn`] are implemented, and the function is [injective].
/// `InjTypeFn` cannot be manually implemented.
/// 
/// # Properties
/// 
/// These are properties about `InjTypeFn` that users can rely on.
/// 
/// For any given `F: InjTypeFn<A> + InjTypeFn<B>` these hold:
/// 
/// 1. If `A == B`, then `CallInjFn<F, A> == CallInjFn<F, B>`.
/// 2. If `CallInjFn<F, A> == CallInjFn<F, B>`, then `A == B`.
/// 3. If `A != B`, then `CallInjFn<F, A> != CallInjFn<F, B>`.
/// 4. If `CallInjFn<F, A> != CallInjFn<F, B>`, then `A != B`.
/// 
/// 
/// # Examples
/// 
/// ### Macro-based Implementation
/// 
/// ```rust
/// use typewit::{CallInjFn, UncallFn, inj_type_fn};
/// 
/// let _: CallInjFn<BoxFn, u32> = Box::new(3u32);
/// let _: UncallFn<BoxFn, Box<u32>> = 3u32;
/// 
/// inj_type_fn!{
///     struct BoxFn;
/// 
///     impl<T: ?Sized> T => Box<T>
/// }
/// ```
/// 
/// ### Non-macro Implementation
/// 
/// ```rust
/// use typewit::{CallInjFn, RevTypeFn, TypeFn, UncallFn};
/// 
/// let _: CallInjFn<BoxFn, u32> = Box::new(3u32);
/// let _: UncallFn<BoxFn, Box<u32>> = 3u32;
/// 
/// 
/// struct BoxFn;
///
/// impl<T: ?Sized> TypeFn<T> for BoxFn {
///     type Output = Box<T>;
/// 
///     // Asserts that this impl of `TypeFn` for `BoxFn` is injective.
///     const TYPE_FN_ASSERTS: () = { let _: CallInjFn<Self, T>; };
/// }
/// 
/// impl<T: ?Sized> RevTypeFn<Box<T>> for BoxFn {
///     type Arg = T;
/// }
/// 
/// ```
/// 
/// [injective]: mod@crate::type_fn#injective
pub trait InjTypeFn<A: ?Sized>: TypeFn<A, Output = Self::Ret> + RevTypeFn<Self::Ret, Arg = A> {
    /// Return value of the function
    type Ret: ?Sized;
}

impl<F, A: ?Sized, R: ?Sized> InjTypeFn<A> for F
where
    F: TypeFn<A, Output = R>,
    F: RevTypeFn<R, Arg = A>,
{
    type Ret = R;
}

/// The inverse of [`TypeFn`], 
/// for getting the argument of a [`TypeFn`](crate::type_fn::TypeFn)
/// from its return value.
/// 
/// # Properties
/// 
/// These are properties about `RevTypeFn` that users can rely on.
/// 
/// For any given `F: RevTypeFn<R> + RevTypeFn<O>` these hold:
/// 
/// 1. If `R == O`, then `UncallFn<F, R> == UncallFn<F, O>`
/// 
/// 2. If `R != O`, then `UncallFn<F, R> != UncallFn<F, O>`
/// 
/// Disclaimer: this trait **does not** by itself ensure that a function is 
/// [injective],
/// since `RevTypeFn<Ret>` can't know if `Self::Arg` is the only argument 
/// that could produce `Ret`.
/// 
/// # Examples
/// 
/// ### Macro-based impl
/// 
/// ```rust
/// use std::ops::Range;
///
/// use typewit::{RevTypeFn, UncallFn};
///
/// let array = [3usize, 5];
///
/// // Getting the argument of `ArrayFn` from its return value
/// let value: UncallFn<ArrayFn<2>, [usize; 2]> = array[0];
///
/// assert_eq!(value, 3usize);
///
/// typewit::inj_type_fn!{
///     struct ArrayFn<const N: usize>;
///     impl<T> T => [T; N]
/// }
/// ```
/// 
/// ### Manual impl
/// 
/// ```rust
/// use std::ops::Range;
///
/// use typewit::{CallInjFn, RevTypeFn, TypeFn, UncallFn};
///
/// let array = [3usize, 5];
///
/// // Getting the argument of `ArrayFn` from its return value
/// let value: UncallFn<ArrayFn<2>, [usize; 2]> = array[0];
///
/// assert_eq!(value, 3usize);
///
/// struct ArrayFn<const N: usize>;
///
/// impl<T, const N: usize> TypeFn<T> for ArrayFn<N> {
///     type Output = [T; N];
///
///     // Ensures that this impl of `TypeFn` for `ArrayFn` is injective.
///     const TYPE_FN_ASSERTS: () = { let _: CallInjFn<Self, T>; };
/// }
/// impl<T, const N: usize> RevTypeFn<[T; N]> for ArrayFn<N> {
///     type Arg = T;
/// }
/// ```
/// 
/// ### Non-injective function
/// 
/// As mentioned above, this trait doesn't make a function [injective].
/// 
/// In the example below, `NonInjective` isn't injective, because it maps different 
/// arguments to the same return value:
/// 
/// ```rust
/// use typewit::{CallFn, RevTypeFn, TypeFn, UncallFn};
/// 
/// let _: CallFn<NonInjective, Vec<u8>> = 3u8;
/// let _: CallFn<NonInjective, String> = 5u8;
/// 
/// let _: UncallFn<NonInjective, u8> = ();
/// 
/// 
/// struct NonInjective;
/// 
/// impl<T> TypeFn<T> for NonInjective {
///     type Output = u8;
/// }
/// 
/// impl RevTypeFn<u8> for NonInjective {
///     type Arg = ();
/// }
/// ```
/// 
/// [injective]: mod@crate::type_fn#injective
pub trait RevTypeFn<Ret: ?Sized>: TypeFn<Self::Arg, Output = Ret> {
    /// The argument to this function with `Ret` as the return value.
    type Arg: ?Sized;
}

/// Queries the argument to a `F: `[`TypeFn`] from its return value.
/// 
/// # Example
/// 
/// ```rust
/// use typewit::UncallFn;
/// 
/// let vect = vec![3u32, 5, 8];
/// let value: UncallFn<VecFn, Vec<u32>> = vect[1];
/// assert_eq!(value, 5u32);
/// 
/// typewit::inj_type_fn!{
///     struct VecFn;
///     impl<T> T => Vec<T>
/// }
/// ```
pub type UncallFn<F, Ret> = <F as RevTypeFn<Ret>>::Arg;


/// [`CallFn`](crate::CallFn) with an additional `F:`[`InjTypeFn<A>`] requirement,
/// which helps with type inference.
///
/// # Example
///
/// ```rust
/// use typewit::{InjTypeFn, CallInjFn};
/// 
/// // inferred return type
/// let inferred_ret = upcast(3u8);
/// assert_eq!(inferred_ret, 3);
/// 
/// // inferred argument type
/// let inferred_arg: u32 = upcast(5);
/// assert_eq!(inferred_arg, 5);
/// 
/// // Because the return type is `CallInjFn<_, I>`,
/// // this can infer `I` from the return type,
/// fn upcast<I>(int: I) -> CallInjFn<Upcast, I>
/// where
///     Upcast: InjTypeFn<I>,
///     CallInjFn<Upcast, I>: From<I>,
/// {
///     int.into()
/// }
/// 
/// 
/// typewit::inj_type_fn!{
///     struct Upcast;
///     
///     impl u8 => u16;
///     impl u16 => u32;
///     impl u32 => u64;
///     impl u64 => u128;
/// }
/// ```
/// 
/// As of October 2023, replacing `CallInjFn` with `CallFn` can cause type inference errors:
/// 
/// ```text
/// error[E0277]: the trait bound `Upcast: TypeFn<{integer}>` is not satisfied
///   --> src/type_fn/injective.rs:132:32
///    |
/// 11 | let inferred_arg: u32 = upcast(5);
///    |                         ------ ^ the trait `TypeFn<{integer}>` is not implemented for `Upcast`
///    |                         |
///    |                         required by a bound introduced by this call
///    |
///    = help: the following other types implement trait `TypeFn<T>`:
///              <Upcast as TypeFn<u16>>
///              <Upcast as TypeFn<u32>>
///              <Upcast as TypeFn<u64>>
///              <Upcast as TypeFn<u8>>
/// ```
pub type CallInjFn<F, A> = <F as InjTypeFn<A>>::Ret;


macro_rules! simple_inj_type_fn {
    (
        impl[$($impl:tt)*] ($arg:ty => $ret:ty) for $func:ty
        $(where[$($where:tt)*])?
    ) => {
        impl<$($impl)*> crate::type_fn::TypeFn<$arg> for $func
        $(where $($where)*)?
        {
            type Output = $ret;
        }

        impl<$($impl)*> crate::type_fn::RevTypeFn<$ret> for $func
        $(where $($where)*)?
        {
            type Arg = $arg;
        }
    };
} pub(crate) use simple_inj_type_fn;


////////////////////////////////////////////////////////////////////////////////

/// Reverses an [`InjTypeFn`], its arguments become return values, 
/// and its return values become arguments.
/// 
/// # Examples
///
/// ### Permutations
///
/// The different ways this function can be combined with [`CallFn`] and 
/// [`UncallFn`] 
///
/// ```rust
/// use typewit::type_fn::{CallFn, FnRev, UncallFn};
/// 
/// let _: CallFn<FnRev<Swap>, Right> = Left;
/// let _: UncallFn<    Swap,  Right> = Left;
/// 
/// let _: CallFn<        Swap,  Up> = Down;
/// let _: UncallFn<FnRev<Swap>, Up> = Down;
/// 
/// typewit::inj_type_fn!{
///     struct Swap;
/// 
///     impl Left => Right;
///     impl Up   => Down;
/// }
/// 
/// struct Left;
/// struct Right;
/// struct Up;
/// struct Down;
/// ```
/// 
/// [`CallFn`]: crate::CallFn
pub struct FnRev<F: ?Sized>(PhantomData<fn() -> F>);


impl<F: ?Sized> FnRev<F> {
    /// Constructs a `FnRev`.
    pub const NEW: Self = Self(PhantomData);
}

impl<F> FnRev<F> {
    /// Constructs a `FnRev` from `&F`
    pub const fn from_ref(_f: &F) -> Self {
        Self::NEW
    }
}

impl<F, A: ?Sized> TypeFn<A> for FnRev<F>
where
    F: RevTypeFn<A>
{
    type Output = UncallFn<F, A>;
}

impl<F, R: ?Sized> RevTypeFn<R> for FnRev<F>
where
    F: InjTypeFn<R>
{
    type Arg = CallInjFn<F, R>;
}

#[test]
fn test_fnrev_equivalence(){
    fn _foo<A, F: InjTypeFn<A>>() {
        let _ = crate::TypeEq::<CallInjFn<FnRev<F>, F::Ret>, UncallFn<F, F::Ret>>::NEW;
        
        let _ = crate::TypeEq::<UncallFn<FnRev<F>, A>, CallInjFn<F, A>>::NEW;
    }
}