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
use super::{
    LCons, LFoldOp, LFoldOpOutput, LNil, LPrependComposeFunctor, LRemoveAtOp, LRemoveAtOpOutput,
    TList,
};
use crate::{
    counter::{Counter, Current, Next},
    functional::{ApplyFunctor, Functor},
};
use std::marker::PhantomData;
use std::ops::Add;
use typenum::{Sum, Unsigned, U0, U1};

// length of list

/// A type operator that gets the length of [TList].
pub trait LLengthOp
where
    Self: TList,
    Self::Output: Unsigned,
{
    type Output;
}

impl LLengthOp for LNil {
    type Output = U0;
}

impl<Head, Tail> LLengthOp for LCons<Head, Tail>
where
    Tail: TList + LLengthOp,
    LLengthOpOutput<Tail>: Add<U1>,
    Sum<LLengthOpOutput<Tail>, U1>: Unsigned,
{
    type Output = Sum<LLengthOpOutput<Tail>, U1>;
}

pub type LLengthOpOutput<List> = <List as LLengthOp>::Output;

/// A [Functor] that returns the length of [TList].
pub struct LLengthFunctor;

pub type LLength<List> = ApplyFunctor<LLengthFunctor, List>;

impl<List> Functor<List> for LLengthFunctor
where
    List: TList + LLengthOp,
{
    type Output = LLengthOpOutput<List>;
}

// set equal

/// Compare if a left-hand-side [TList] has the same set of types
/// with `Rhs` [TList].
///
/// The `Indexes` argument can be left unspecified.
pub trait LSetEqualOp<Rhs, Indexes>
where
    Rhs: TList,
    Indexes: TList,
    Self: TList,
{
    type Output;
}

pub type LSetEqualOpOutput<Lhs, Rhs, Indexes> = <Lhs as LSetEqualOp<Rhs, Indexes>>::Output;

impl LSetEqualOp<LNil, LNil> for LNil {
    type Output = ();
}

impl<LHead, LTail, RHead, RTail, Index, IRemain>
    LSetEqualOp<LCons<RHead, RTail>, LCons<Index, IRemain>> for LCons<LHead, LTail>
where
    Index: Counter,
    IRemain: TList,
    LTail: TList,
    RTail: TList,
    Self: LRemoveAtOp<RHead, Index>,
    LRemoveAtOpOutput<Self, RHead, Index>: LSetEqualOp<RTail, IRemain>,
{
    type Output = LSetEqualOpOutput<LRemoveAtOpOutput<Self, RHead, Index>, RTail, IRemain>;
}

/// A [Functor] that compares if `Lhs` and `Rhs` [TList]s have same set of values.
pub struct LSetEqualFunctor<Rhs, Indexes> {
    _phantom: PhantomData<(Rhs, Indexes)>,
}

pub type LSetEqual<Lhs, Rhs, Indexes> = ApplyFunctor<LSetEqualFunctor<Rhs, Indexes>, Lhs>;

impl<Lhs, Rhs, Indexes> Functor<Lhs> for LSetEqualFunctor<Rhs, Indexes>
where
    Lhs: TList + LSetEqualOp<Rhs, Indexes>,
    Rhs: TList,
    Indexes: TList,
{
    type Output = LSetEqualOpOutput<Lhs, Rhs, Indexes>;
}

// concatenate

/// Concatenates the `Rhs` [TList] to the end of left-hand-side [TList].
pub trait LConcatOp<Rhs>
where
    Self: TList,
    Self::Output: TList,
{
    type Output;
}

impl<Rhs> LConcatOp<Rhs> for LNil
where
    Rhs: TList,
{
    type Output = Rhs;
}

impl<Rhs, Head, Tail> LConcatOp<Rhs> for LCons<Head, Tail>
where
    Rhs: TList,
    Tail: TList + LConcatOp<Rhs>,
{
    type Output = LCons<Head, LConcatOpOutput<Tail, Rhs>>;
}

pub type LConcatOpOutput<Lhs, Rhs> = <Lhs as LConcatOp<Rhs>>::Output;

/// A [Functor] that concatenates input and `Rhs` [TList]s.
pub struct LConcatFunctor<Rhs>
where
    Rhs: TList,
{
    _phantom: PhantomData<Rhs>,
}

pub type LConcat<Lhs, Rhs> = ApplyFunctor<LConcatFunctor<Rhs>, Lhs>;

impl<Lhs, Rhs> Functor<Lhs> for LConcatFunctor<Rhs>
where
    Lhs: TList + LConcatOp<Rhs>,
    Rhs: TList,
{
    type Output = LConcatOpOutput<Lhs, Rhs>;
}

/// A [Functor] that concatenates the input tuple `(Lhs, Rhs)` of [TList]s.
pub struct LConcatComposeFunctor;

pub type LConcatCompose<Lhs, Rhs> = ApplyFunctor<LConcatComposeFunctor, (Lhs, Rhs)>;

impl<Lhs, Rhs> Functor<(Lhs, Rhs)> for LConcatComposeFunctor
where
    Lhs: TList + LConcatOp<Rhs>,
    Rhs: TList,
{
    type Output = LConcatOpOutput<Lhs, Rhs>;
}

// split list

/// A type operator that splits a [TList] at `Target`.
pub trait LSplitOp<Target, Index>
where
    Index: Counter,
    Self: TList,
    Self::FormerOutput: TList,
    Self::LatterOutput: TList,
{
    type FormerOutput;
    type LatterOutput;
}

pub type LSplitOpFormerOutput<List, Target, Index> =
    <List as LSplitOp<Target, Index>>::FormerOutput;
pub type LSplitOpLatterOutput<List, Target, Index> =
    <List as LSplitOp<Target, Index>>::LatterOutput;

impl<Target, Tail> LSplitOp<Target, Current> for LCons<Target, Tail>
where
    Tail: TList,
{
    type FormerOutput = LNil;
    type LatterOutput = Self;
}

impl<Target, Index, NonTarget, Tail> LSplitOp<Target, Next<Index>> for LCons<NonTarget, Tail>
where
    Index: Counter,
    Tail: TList + LSplitOp<Target, Index>,
{
    type FormerOutput = LCons<NonTarget, LSplitOpFormerOutput<Tail, Target, Index>>;
    type LatterOutput = LSplitOpLatterOutput<Tail, Target, Index>;
}

/// A [Functor] that splits input [TList] at `Target`.
pub struct LSplitFunctor<Target, Index>
where
    Index: Counter,
{
    _phantom: PhantomData<(Target, Index)>,
}

pub type LSplit<List, Target, Index> = ApplyFunctor<LSplitFunctor<Target, Index>, List>;

impl<List, Target, Index> Functor<List> for LSplitFunctor<Target, Index>
where
    List: TList + LSplitOp<Target, Index>,
    Index: Counter,
{
    type Output = (
        LSplitOpFormerOutput<List, Target, Index>,
        LSplitOpLatterOutput<List, Target, Index>,
    );
}

// reverse

/// A [Functor] that reverses a [TList].
pub struct LReverseFunctor {}

impl<List> Functor<List> for LReverseFunctor
where
    List: LFoldOp<LNil, LPrependComposeFunctor>,
    LFoldOpOutput<List, LNil, LPrependComposeFunctor>: TList,
{
    type Output = LFoldOpOutput<List, LNil, LPrependComposeFunctor>;
}

pub type LReverse<List> = ApplyFunctor<LReverseFunctor, List>;

// into vector of integers

/// The trait builds a concrete `Vec<usize>` from a [TList]
///
/// It provides [to_usize_vec](LToUsizeVec::to_usize_vec) method to
/// produces a vector of integers. It assumes all contained types implement
/// [Unsigned](typenum::Unsigned) trait.
pub trait LToUsizeVec {
    fn to_usize_vec() -> Vec<usize>;
    fn append_usize_vec(values: &mut Vec<usize>);
}

impl LToUsizeVec for LNil {
    fn to_usize_vec() -> Vec<usize> {
        vec![]
    }

    fn append_usize_vec(_values: &mut Vec<usize>) {}
}

impl<Value, Tail> LToUsizeVec for LCons<Value, Tail>
where
    Value: Unsigned,
    Tail: TList + LToUsizeVec,
{
    fn to_usize_vec() -> Vec<usize> {
        let mut values = vec![];
        Self::append_usize_vec(&mut values);
        values
    }

    fn append_usize_vec(values: &mut Vec<usize>) {
        values.push(Value::USIZE);
        Tail::append_usize_vec(values);
    }
}

// tests

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{control::IfSameOutput, TListType};

    type AssertSame<Lhs, Rhs> = IfSameOutput<(), Lhs, Rhs>;

    struct A;
    struct B;
    struct C;
    struct D;
    struct E;

    type SomeList = TListType![A, B, C];
    type AnotherList = TListType![D, E];

    // split
    type Assert1<Index> = AssertSame<LSplit<SomeList, B, Index>, (TListType![A], TListType![B, C])>;

    // reverse list
    type Assert10 = AssertSame<LReverse<SomeList>, TListType![C, B, A]>;

    // assert identical set of items
    type Assert11<Idx> = LSetEqual<SomeList, TListType![C, A, B], Idx>;

    // concat
    type Assert12 = AssertSame<LConcat<SomeList, AnotherList>, TListType![A, B, C, D, E]>;

    #[test]
    fn tlist_misc_test() {
        let _: Assert1<_> = ();
        let _: Assert10 = ();
        let _: Assert11<_> = ();
        let _: Assert12 = ();
    }
}