agnes 0.3.2

A data wrangling library for Rust
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
/*!
Traits, structs, and type aliases for handling cons-list element labels and associated logic.
*/
use std::collections::VecDeque;
use std::marker::PhantomData;
use std::ops::{Add, BitAnd, BitOr, Not, Sub};

use typenum::{
    bit::{B0, B1},
    marker_traits::Bit,
    operator_aliases::{Add1, And, Or, Sub1},
    type_operators::IsEqual,
    uint::{UInt, UTerm, Unsigned},
};

use access::NRows;
use cons::{cons, Cons, Nil};
use store::DataRef;

/// Trait to provide associated types (table and backing natural) for a field identifier.
///
/// All identifiers in `agnes` exist in a specific table (a marker struct which represents that
/// table). Within the table, identifiers are backed by a type-level natural number (using the
/// `typenum` crate for type-level numbers).
pub trait Identifier {
    /// The [Ident](struct.Ident.html) struct (which should always be
    /// Ident<Self::Table, Self::Natural) for this identifier.
    type Ident: Identifier; // = Ident<Self::Table, Self::Natural>;
    /// The table for this identifier.
    type Table;
    /// The `typenum`-based backing natural number corresponding to this identifier.
    type Natural;
}

/// A label, which is simply an [Identifier](trait.Identifier.html) along with an associated
/// `const` name and type description.
pub trait Label: Identifier {
    /// The label name.
    const NAME: &'static str;
    /// The type description for the data referred to by this label.
    const TYPE: &'static str;
}

/// An basic identifier struct for an identifier within the table `Tbl`, backed by the type-level
/// natural number `Nat`.
#[derive(Debug, Clone)]
pub struct Ident<Tbl, Nat> {
    _marker: PhantomData<(Tbl, Nat)>,
}

impl<Tbl, Nat> Identifier for Ident<Tbl, Nat> {
    type Ident = Self;
    type Table = Tbl;
    type Natural = Nat;
}
/// Helpful type alias to refer to the table in which an identifier exists.
pub type TblOf<T> = <T as Identifier>::Table;
/// Helpful type alias to refer to the backing natural number for an identifier.
pub type NatOf<T> = <T as Identifier>::Natural;

impl Identifier for UTerm {
    type Ident = Ident<Self::Table, Self::Natural>;
    type Table = Local;
    type Natural = Self;
}
impl<U, B> Identifier for UInt<U, B> {
    type Ident = Ident<Self::Table, Self::Natural>;
    type Table = Local;
    type Natural = Self;
}

/// Trait to access name and type description for a label.
pub trait LabelName {
    /// Returns the label name.
    fn name() -> &'static str;
    /// Returns a string specified the type of this data referred to by this label.
    fn str_type() -> &'static str;
}
impl<T> LabelName for T
where
    T: Label,
{
    fn name() -> &'static str {
        T::NAME
    }
    fn str_type() -> &'static str {
        T::TYPE
    }
}

/// Ident-level equality. Leverages `typenum`'s `IsEqual` trait for type-level-number equality,
/// but doesn't use `IsEqual`'s `is_equal` method (since no results of this equality check are
/// intended to be instantiated).
pub trait IdentEq<Other> {
    /// Whether or not these identifiers are equal.
    type Eq: Bit;
}

/// Type alias for the 'true' bit.
pub type True = B1;
/// Type alias for the 'false' bit.
pub type False = B0;

/// Fallback to IsEqual
impl<T, U> IdentEq<U> for T
where
    T: IsEqual<U>,
{
    type Eq = <T as IsEqual<U>>::Output;
}

/// Type-level equality implementation for `Ident`s. Result will be `True` if both table and
/// the type-level natural number backing this label match.
impl<TTable, TNat, UTbl, UNat> IdentEq<Ident<UTbl, UNat>> for Ident<TTable, TNat>
where
    TTable: IsEqual<UTbl>,
    TNat: IsEqual<UNat>,
    <TTable as IsEqual<UTbl>>::Output: BitAnd<<TNat as IsEqual<UNat>>::Output>,
    <<TTable as IsEqual<UTbl>>::Output as BitAnd<<TNat as IsEqual<UNat>>::Output>>::Output: Bit,
{
    type Eq = And<<TTable as IsEqual<UTbl>>::Output, <TNat as IsEqual<UNat>>::Output>;
}

/// Common dummy table for 'local' lookups -- lookups that are not related to the concept of tables
/// (in particular, used for looking up the frame index in a view from a field label)
pub struct Local;
impl IsEqual<Local> for Local {
    type Output = True;
    fn is_equal(self, _rhs: Local) -> True {
        B1
    }
}

/// Trait for determining whether or not the `Self` and `U` labels refer to the same field.
pub trait LabelEq<U> {
    /// Whether or not the two labels refer to the same field.
    type Eq;
}
impl<T, U> LabelEq<U> for T
where
    T: Identifier,
    U: Identifier,
    T::Ident: IdentEq<U::Ident>,
{
    type Eq = <T::Ident as IdentEq<U::Ident>>::Eq;
}

/// Container for a value of type `V` labeled with `L`.
#[derive(Debug, Clone)]
pub struct Labeled<L, V> {
    _label: PhantomData<L>,
    /// The contained value corresponding to this label.
    pub value: V,
}
impl<L, V> From<V> for Labeled<L, V> {
    fn from(orig: V) -> Labeled<L, V> {
        Labeled {
            _label: PhantomData,
            value: orig,
        }
    }
}

/// Trait for labeling an arbitrary value (to construct a [Labeled](struct.Labeled.html)) object).
pub trait IntoLabeled: Sized {
    /// Label this object with label `Label`.
    fn label<Label>(self) -> Labeled<Label, Self>;
}

impl<T> IntoLabeled for T {
    fn label<Label>(self) -> Labeled<Label, T> {
        Labeled::from(self)
    }
}

/// Container for storing the underlying data type `D` (of a field, for example) for a value of
/// type `V`.
#[derive(Debug, Clone)]
pub struct TypedValue<D, V> {
    _dtype: PhantomData<D>,
    value: V,
}
impl<D, V> From<V> for TypedValue<D, V> {
    fn from(orig: V) -> TypedValue<D, V> {
        TypedValue {
            _dtype: PhantomData,
            value: orig,
        }
    }
}

/// Trait for associating an underlying data type with a type.
pub trait Typed {
    /// Associated data type with this type.
    type DType;
}
impl<D, V> Typed for TypedValue<D, V> {
    type DType = D;
}
impl<L, D, V> Typed for Labeled<L, TypedValue<D, V>> {
    type DType = D;
}

/// Type alias for the associated data type.
pub type TypeOf<T> = <T as Typed>::DType;

impl<T> Typed for ::field::FieldData<T> {
    type DType = T;
}
impl<T, DI> Typed for ::frame::Framed<T, DI> {
    type DType = T;
}
impl<T> Typed for ::store::DataRef<T>
where
    T: Typed,
{
    type DType = T::DType;
}

/// Marker trait for an object that can be held in a Labeled<...> or TypedValue<...> container.
pub trait SelfValued {}

macro_rules! impl_selfvalued {
    ($($dtype:ty)*) => {$(
        impl SelfValued for $dtype {}
    )*}
}
impl_selfvalued![
    f32 f64
    i8 i16 i32 i64 i128 isize
    u8 u16 u32 u64 u128 usize
    bool char str String
];
impl<T> SelfValued for ::field::FieldData<T> {}
impl<T, DI> SelfValued for ::frame::Framed<T, DI> {}
impl<T> SelfValued for DataRef<T> {}
impl<T> SelfValued for PhantomData<T> {}

/// Trait for extracting the an associated value of a value-holding container (e.g.
/// [TypedValue](struct.TypedValue.html), [Labeled](struct.Labeled.html)).
pub trait Valued {
    /// The associated value.
    type Value;
    /// Read-only reference to the value.
    fn value_ref(&self) -> &Self::Value;
    /// Mutable reference to the value.
    fn value_mut(&mut self) -> &mut Self::Value;
    /// Take ownership of the value.
    fn value(self) -> Self::Value;
}
impl<T> Valued for T
where
    T: SelfValued,
{
    type Value = Self;
    fn value_ref(&self) -> &Self {
        self
    }
    fn value_mut(&mut self) -> &mut Self {
        self
    }
    fn value(self) -> Self::Value {
        self
    }
}
impl<D, V> Valued for TypedValue<D, V>
where
    V: Valued,
{
    type Value = V::Value;
    fn value_ref(&self) -> &Self::Value {
        &self.value.value_ref()
    }
    fn value_mut(&mut self) -> &mut Self::Value {
        self.value.value_mut()
    }
    fn value(self) -> Self::Value {
        self.value.value()
    }
}
impl<L, V> Valued for Labeled<L, V>
where
    V: Valued,
{
    type Value = V::Value;
    fn value_ref(&self) -> &V::Value {
        self.value.value_ref()
    }
    fn value_mut(&mut self) -> &mut V::Value {
        self.value.value_mut()
    }
    fn value(self) -> V::Value {
        self.value.value()
    }
}

/// Type alias for retrieving the Value of a [Valued](trait.Valued.html) object.
pub type ValueOf<T> = <T as Valued>::Value;

/// Trait for finding the associated marker (non-instantiated type) for a container
/// (e.g. [Labeled](struct.Labeled.html)).
pub trait Marked {
    /// Associated marker.
    type Marker;
}
impl<L, M> Marked for Labeled<L, PhantomData<M>> {
    type Marker = M;
}
impl<L, D, M> Marked for Labeled<L, TypedValue<D, PhantomData<M>>> {
    type Marker = M;
}

/// Type alias for retrieving the marker of a [Marked](trait.Marked.html) object.
pub type MarkerOf<T> = <T as Marked>::Marker;

/// Label-value cons-list
pub type LVCons<L, V, T> = Cons<Labeled<L, V>, T>;
/// Label-only cons-list
pub type LCons<L, T> = LVCons<L, (), T>;
/// Type alias for a label-only cons-list.
pub type LabelCons<L, T> = LCons<L, T>;
/// Label-marker cons-list
pub type LMCons<L, M, T> = LVCons<L, PhantomData<M>, T>;
/// Label-DType-value cons-list
pub type LDVCons<L, D, V, T> = LVCons<L, TypedValue<D, V>, T>;

/// `LabelEq`-based membership test for cons-lists. Specifies whether `E` is a member (based on
/// labels) of Self.
pub trait Member<E> {
    /// [True](type.True.html) or [False](type.False.html).
    type IsMember: Bit;
}

impl<E> Member<E> for Nil {
    type IsMember = False;
}
impl<E, L, V, T> Member<E> for LVCons<L, V, T>
where
    L: LabelEq<E>,
    T: Member<E>,
    <L as LabelEq<E>>::Eq: BitOr<<T as Member<E>>::IsMember>,
    <<L as LabelEq<E>>::Eq as BitOr<<T as Member<E>>::IsMember>>::Output: Bit,
{
    type IsMember = Or<<L as LabelEq<E>>::Eq, <T as Member<E>>::IsMember>;
}

/// Trait to ensure that all labels in `LabeList` are found in cons-list `Self`.
pub trait HasLabels<LabelList> {}
// Everything as the empty label list
impl<T> HasLabels<Nil> for T {}
// make sure the first label is in the haystack, then move on the to rest of the needles
impl<NeedleLbl, NeedleValue, NeedleTail, Haystack>
    HasLabels<LVCons<NeedleLbl, NeedleValue, NeedleTail>> for Haystack
where
    Haystack: Member<NeedleLbl, IsMember = True>,
    Haystack: HasLabels<NeedleTail>,
{
}
/// Convenience implementation for the case where only a single label is provided
impl<Needle, Haystack> HasLabels<Needle> for Haystack
where
    Needle: Label,
    Haystack: Member<Needle, IsMember = True>,
{
}

/// Marker trait for ensuring that the labels of a cons-list constitute a set (no label cardinality
/// greater than 1).
pub trait IsLabelSet {
    /// [True](type.True.html) or [False](type.False.html).
    type IsSet;
}
// Empty set
impl IsLabelSet for Nil {
    type IsSet = True;
}
// Cons-list is a label set if head label isn't found in tail, and tail is a label set
impl<L, V, T> IsLabelSet for LVCons<L, V, T>
where
    T: Member<L>,
    <T as Member<L>>::IsMember: Not,
    <<T as Member<L>>::IsMember as Not>::Output: BitAnd<<T as IsLabelSet>::IsSet>,
    T: IsLabelSet,
{
    type IsSet = And<<<T as Member<L>>::IsMember as Not>::Output, <T as IsLabelSet>::IsSet>;
}

/// Determines the set difference between an [LVCons](type.LVCons.html) label set and another
/// [LVCons](type.LVCons.html) label set `RightSet`.
pub trait SetDiff<RightSet> {
    /// The set of labels that exist in `Self` and not in `RightSet`.
    type Set;
}

// edge case: set difference will null set
impl<LLabel, LValue, LTail> SetDiff<Nil> for LVCons<LLabel, LValue, LTail> {
    type Set = LVCons<LLabel, LValue, LTail>;
}
// edge case: null set difference with anything is null set
impl<RSet> SetDiff<RSet> for Nil {
    type Set = Nil;
}

impl<LLabel, LValue, LTail, RLabel, RValue, RTail> SetDiff<LVCons<RLabel, RValue, RTail>>
    for LVCons<LLabel, LValue, LTail>
where
    Self: SetDiffStep<LVCons<RLabel, RValue, RTail>, LVCons<RLabel, RValue, RTail>>,
{
    type Set =
        <Self as SetDiffStep<LVCons<RLabel, RValue, RTail>, LVCons<RLabel, RValue, RTail>>>::Set;
}

/// Helper trait used [SetDiff](trait.SetDiff.html) to compute the set difference between two label
/// sets. `RightSet` is the set-subtrahend remaining at this point of the process, `FullRightSet`
/// is the original full set-subtrahend.
pub trait SetDiffStep<RightSet, FullRightSet> {
    /// The set of labels that exist in `Self` and not in `RightSet`.
    type Set;
}

// left set exhausted: we're done!
impl<RightSet, FullRightSet> SetDiffStep<RightSet, FullRightSet> for Nil {
    type Set = Nil;
}

// right set exhausted: recurse into left tail, restart with full right set
impl<LLabel, LValue, LTail, FullRightSet> SetDiffStep<Nil, FullRightSet>
    for LVCons<LLabel, LValue, LTail>
where
    LTail: SetDiffStep<FullRightSet, FullRightSet>,
{
    type Set = LVCons<LLabel, LValue, <LTail as SetDiffStep<FullRightSet, FullRightSet>>::Set>;
}

// normal step: check if heads match and use SetDiffMatch
impl<LLabel, LValue, LTail, RLabel, RValue, RTail, FullRightSet>
    SetDiffStep<LVCons<RLabel, RValue, RTail>, FullRightSet> for LVCons<LLabel, LValue, LTail>
where
    LLabel: LabelEq<RLabel>,
    Self:
        SetDiffMatch<LVCons<RLabel, RValue, RTail>, FullRightSet, <LLabel as LabelEq<RLabel>>::Eq>,
{
    type Set = <Self as SetDiffMatch<
        LVCons<RLabel, RValue, RTail>,
        FullRightSet,
        <LLabel as LabelEq<RLabel>>::Eq,
    >>::Set;
}

/// Helper trait used [SetDiff](trait.SetDiff.html) to compute the set difference between two label
/// sets. `RightSet` is the set-subtrahend remaining at this point of the process, `FullRightSet`
/// is the original full set-subtrahend. `Match` denotes whether or not the heads of the two
/// cons-lists (`Self` and `RightSet`) match.
pub trait SetDiffMatch<RightSet, FullRightSet, Match> {
    /// The set of labels that exist in `Self` and not in `RightSet`.
    type Set;
}

// heads of left and right do not match: recurse into right tail
impl<LLabel, LValue, LTail, RLabel, RValue, RTail, FullRightSet>
    SetDiffMatch<LVCons<RLabel, RValue, RTail>, FullRightSet, False>
    for LVCons<LLabel, LValue, LTail>
where
    LVCons<LLabel, LValue, LTail>: SetDiffStep<RTail, FullRightSet>,
{
    type Set = <LVCons<LLabel, LValue, LTail> as SetDiffStep<RTail, FullRightSet>>::Set;
}

// heads of both left and right match: continues with tails of both left and right
impl<LLabel, LValue, LTail, RLabel, RValue, RTail, FullRightSet>
    SetDiffMatch<LVCons<RLabel, RValue, RTail>, FullRightSet, True>
    for LVCons<LLabel, LValue, LTail>
where
    LTail: SetDiffStep<RTail, FullRightSet>,
{
    type Set = <LTail as SetDiffStep<RTail, FullRightSet>>::Set;
}

/// Type alias for the label set that is the set different between `LeftSet` and `RightSet`.
pub type LabelSetDiff<LeftSet, RightSet> = <LeftSet as SetDiff<RightSet>>::Set;

/// Look up an element from a cons-list by `typenum` natural number.
pub trait LookupElemByNat<N> {
    /// Type of looked-up element.
    type Elem;
    /// Look up the element from this cons-list.
    fn elem(&self) -> &Self::Elem;
}

impl<H, T> LookupElemByNat<UTerm> for Cons<H, T> {
    type Elem = H;
    fn elem(&self) -> &Self::Elem {
        &self.head
    }
}

impl<H, T> LookupElemByNat<UInt<UTerm, B1>> for Cons<H, T>
where
    T: LookupElemByNat<UTerm>,
{
    type Elem = <T as LookupElemByNat<UTerm>>::Elem;
    fn elem(&self) -> &Self::Elem {
        self.tail.elem()
    }
}

impl<H, T, N> LookupElemByNat<UInt<N, B0>> for Cons<H, T>
where
    N: Sub<B1>,
    T: LookupElemByNat<UInt<Sub1<N>, B1>>,
{
    type Elem = <T as LookupElemByNat<UInt<Sub1<N>, B1>>>::Elem;
    fn elem(&self) -> &Self::Elem {
        self.tail.elem()
    }
}

impl<H, T, N, B> LookupElemByNat<UInt<UInt<N, B>, B1>> for Cons<H, T>
where
    T: LookupElemByNat<UInt<UInt<N, B>, B0>>,
{
    type Elem = <T as LookupElemByNat<UInt<UInt<N, B>, B0>>>::Elem;
    fn elem(&self) -> &Self::Elem {
        self.tail.elem()
    }
}

/// Lookup a type-level natural number backing label `L`.
pub trait LookupNatByLabel<L> {
    /// The backing type-level natural number for `L`.
    type Nat: Unsigned;
    /// A run-time accessor for `Nat`.
    fn nat(&self) -> usize {
        Self::Nat::to_usize()
    }
}
impl<TargetL, L, V, T> LookupNatByLabel<TargetL> for LVCons<L, V, T>
where
    TargetL: LabelEq<L>,
    LVCons<L, V, T>: LookupNatByLabelMatch<TargetL, <TargetL as LabelEq<L>>::Eq>,
{
    type Nat =
        <LVCons<L, V, T> as LookupNatByLabelMatch<TargetL, <TargetL as LabelEq<L>>::Eq>>::Nat;
}

/// Helper lookup trait for [LookupNatByLabel](trait.LookupNatByLabel.html). Used by
/// `LookupNatByLabel` for computing the backing type-level natural number for label `TargetL`.
///
/// `B` specifies whether or not `TargetL` matches the head value of `Self`.
pub trait LookupNatByLabelMatch<TargetL, B> {
    /// Backing type-level natural number for `TargetL`.
    type Nat: Unsigned;
}
impl<TargetL, L, V, T> LookupNatByLabelMatch<TargetL, True> for LVCons<L, V, T> {
    type Nat = UTerm;
}
impl<TargetL, L, V, T> LookupNatByLabelMatch<TargetL, False> for LVCons<L, V, T>
where
    T: LookupNatByLabel<TargetL>,
    <T as LookupNatByLabel<TargetL>>::Nat: Add<B1>,
    <<T as LookupNatByLabel<TargetL>>::Nat as Add<B1>>::Output: Unsigned,
{
    type Nat = Add1<<T as LookupNatByLabel<TargetL>>::Nat>;
}

/// Look up an element from a cons-list by label `L`.
pub trait LookupElemByLabel<L> {
    /// Type of lookup-up element.
    type Elem;
    /// Look up the element from this cons-list.
    fn elem(&self) -> &Self::Elem;
}
impl<L, T> LookupElemByLabel<L> for T
where
    T: LookupNatByLabel<L>,
    T: LookupElemByNat<<T as LookupNatByLabel<L>>::Nat>,
{
    type Elem = <Self as LookupElemByNat<<Self as LookupNatByLabel<L>>::Nat>>::Elem;
    fn elem(&self) -> &Self::Elem {
        LookupElemByNat::<_>::elem(self)
    }
}

/// Take an element from a cons-list using `typenum` natural number.
pub trait TakeElemByNat<N> {
    /// Type of taken element.
    type Elem;
    /// Type of remaining cons-list after element was taken.
    type Rest;
    /// Take the element from this cons-list.
    fn take_elem(self) -> (Self::Elem, Self::Rest);
}

impl<H, T> TakeElemByNat<UTerm> for Cons<H, T> {
    type Elem = H;
    type Rest = T;
    fn take_elem(self) -> (H, T) {
        (self.head, self.tail)
    }
}

impl<H, T> TakeElemByNat<UInt<UTerm, B1>> for Cons<H, T>
where
    T: TakeElemByNat<UTerm>,
{
    type Elem = <T as TakeElemByNat<UTerm>>::Elem;
    type Rest = Cons<H, <T as TakeElemByNat<UTerm>>::Rest>;
    fn take_elem(self) -> (Self::Elem, Self::Rest) {
        let (elem, rest) = self.tail.take_elem();
        (elem, cons(self.head, rest))
    }
}

impl<H, T, N> TakeElemByNat<UInt<N, B0>> for Cons<H, T>
where
    N: Sub<B1>,
    T: TakeElemByNat<UInt<Sub1<N>, B1>>,
{
    type Elem = <T as TakeElemByNat<UInt<Sub1<N>, B1>>>::Elem;
    type Rest = Cons<H, <T as TakeElemByNat<UInt<Sub1<N>, B1>>>::Rest>;
    fn take_elem(self) -> (Self::Elem, Self::Rest) {
        let (elem, rest) = self.tail.take_elem();
        (elem, cons(self.head, rest))
    }
}

impl<H, T, N, B> TakeElemByNat<UInt<UInt<N, B>, B1>> for Cons<H, T>
where
    T: TakeElemByNat<UInt<UInt<N, B>, B0>>,
{
    type Elem = <T as TakeElemByNat<UInt<UInt<N, B>, B0>>>::Elem;
    type Rest = Cons<H, <T as TakeElemByNat<UInt<UInt<N, B>, B0>>>::Rest>;
    fn take_elem(self) -> (Self::Elem, Self::Rest) {
        let (elem, rest) = self.tail.take_elem();
        (elem, cons(self.head, rest))
    }
}

/// Take an element from a cons-list using label `L`.
pub trait TakeElemByLabel<L> {
    /// Type of taken element.
    type Elem;
    /// Type of remaining cons-list after element was taken.
    type Rest;
    /// Take the element from this cons-list.
    fn take_elem(self) -> (Self::Elem, Self::Rest);
}
impl<L, T> TakeElemByLabel<L> for T
where
    T: LookupNatByLabel<L>,
    T: TakeElemByNat<<T as LookupNatByLabel<L>>::Nat>,
{
    type Elem = <Self as TakeElemByNat<<Self as LookupNatByLabel<L>>::Nat>>::Elem;
    type Rest = <Self as TakeElemByNat<<Self as LookupNatByLabel<L>>::Nat>>::Rest;
    fn take_elem(self) -> (Self::Elem, Self::Rest) {
        TakeElemByNat::<_>::take_elem(self)
    }
}

/// Type alias for an element (as looked up by `Label`) from cons-list `T`.
pub type ElemOf<T, Label> = <T as LookupElemByLabel<Label>>::Elem;

/// Sepcialization of [LookupElemByLabel](trait.LookupElemByLabel.html) where the looked-up element
/// implements [Valued](trait.Valued.html).
pub trait LookupValuedElemByLabel<L>: LookupElemByLabel<L> {
    /// Type of looked-up element.
    type Elem: Valued;

    /// Look up the element from this cons-list.
    fn elem(&self) -> &<Self as LookupValuedElemByLabel<L>>::Elem;
}
impl<T, L> LookupValuedElemByLabel<L> for T
where
    T: LookupElemByLabel<L>,
    ElemOf<Self, L>: Valued,
{
    type Elem = <Self as LookupElemByLabel<L>>::Elem;
    fn elem(&self) -> &<Self as LookupElemByLabel<L>>::Elem {
        <Self as LookupElemByLabel<L>>::elem(self)
    }
}

/// Type alias for an element implementing [Valued](trait.Valued.html) (as looked up by `Label`)
/// from cons-list `T`.
pub type ValuedElemOf<T, Label> = <T as LookupValuedElemByLabel<Label>>::Elem;
/// Type alias for the associated `Value` of an element implementing [Valued](trait.Valued.html)
/// (as looked up by `Label`) from cons-list `T`.
pub type ValueOfElemOf<T, Label> = <<T as LookupValuedElemByLabel<Label>>::Elem as Valued>::Value;

/// Sepcialization of [LookupElemByLabel](trait.LookupElemByLabel.html) where the looked-up element
/// implements [Marked](trait.Marked.html).
pub trait LookupMarkedElemByLabel<L>: LookupElemByLabel<L> {
    /// Marker type of looked-up element.
    type Elem: Marked;
}
impl<T, L> LookupMarkedElemByLabel<L> for T
where
    T: LookupElemByLabel<L>,
    ElemOf<Self, L>: Marked,
{
    type Elem = <Self as LookupElemByLabel<L>>::Elem;
}
/// Type alias for an element implementing [Marked](trait.Marked.html) (as looked up by `Label`)
/// from cons-list `T`.
pub type MarkedElemOf<T, Label> = <T as LookupMarkedElemByLabel<Label>>::Elem;
/// Type alias for the associated `Marker` of an element implementing [Marked](trait.Marked.html)
/// (as looked up by `Label`) from cons-list `T`.
pub type MarkerOfElemOf<T, Label> = <<T as LookupMarkedElemByLabel<Label>>::Elem as Marked>::Marker;

/// Sepcialization of [LookupElemByLabel](trait.LookupElemByLabel.html) where the looked-up element
/// implements [Typed](trait.Typed.html).
pub trait LookupTypedElemByLabel<L>: LookupElemByLabel<L> {
    /// Associated data type of looked-up element.
    type Elem: Typed;
}
impl<T, L> LookupTypedElemByLabel<L> for T
where
    T: LookupElemByLabel<L>,
    ElemOf<Self, L>: Typed,
{
    type Elem = <Self as LookupElemByLabel<L>>::Elem;
}
/// Type alias for an element implementing [Typed](trait.Typed.html) (as looked up by `Label`)
/// from cons-list `T`.
pub type TypedElemOf<T, Label> = <T as LookupTypedElemByLabel<Label>>::Elem;
/// Type alias for the associated `DType` of an element implementing [Typed](trait.Typed.html)
/// (as looked up by `Label`) from cons-list `T`.
pub type TypeOfElemOf<T, Label> = <<T as LookupTypedElemByLabel<Label>>::Elem as Typed>::DType;

/// Trait to find the subset of cons-list `Self` which are labeled with labels in `LabelList`.
///
/// Any labels in `LabelList` not found in `Self` will be ignored (see `HasLabels` for a trait
/// that requires all members of `LabelList` to be found). The labels will be in the same order as
/// in the original cons-list (with those labels not in `LabelList` filtered out).
pub trait LabelSubset<LabelList> {
    /// Subset of `Self` that are labeled with labels in `LabelList`.
    type Output;
}

// End-point. No more list elements to search. We don't care if anything remains or not in
// `LabelList`.
impl<LabelList> LabelSubset<LabelList> for Nil {
    type Output = Nil;
}

// Implementation for `LVCons` cons-lists.
impl<LabelList, L, V, T> LabelSubset<LabelList> for LVCons<L, V, T>
where
    LabelList: Member<L>,
    LVCons<L, V, T>: LabelSubsetPred<LabelList, <LabelList as Member<L>>::IsMember>,
{
    type Output =
        <LVCons<L, V, T> as LabelSubsetPred<LabelList, <LabelList as Member<L>>::IsMember>>::Output;
}

/// Helper filter trait. Used by `Filter` for computing the subset of `Self` cons-list which
/// contains the labels in `LabelList`.
///
/// `IsMember` specifies whether or not the label of the head value of `Self` is a member of
/// `LabelList`.
pub trait LabelSubsetPred<LabelList, IsMember> {
    /// Subset of `Self` that are labeled with labels in `LabelList`.
    type Output;
}

// `LabelSubsetPred` implementation for a cons-list where the head is in `LabelList`.
impl<LabelList, H, T> LabelSubsetPred<LabelList, True> for Cons<H, T>
where
    T: LabelSubset<LabelList>,
{
    // head is in list, so we include it and check the tail
    type Output = Cons<H, <T as LabelSubset<LabelList>>::Output>;
}
// `LabelSubsetPred` implementation for a cons-list where the head isn't in `LabelList`.
impl<LabelList, H, T> LabelSubsetPred<LabelList, False> for Cons<H, T>
where
    T: LabelSubset<LabelList>,
{
    // head isn't in list, so we check the tail
    type Output = <T as LabelSubset<LabelList>>::Output;
}

/// Trait to compute the new ordering of a labeled cons-list using the new ordering
/// `TargetOrdering`.
pub trait Reorder<TargetOrdering> {
    /// The values from `Self`, re-ordered to match the ordering of `TargetOrdering`.
    type Output;

    /// Reorder this cons-list according to the new ordering `TargetOrdering`.
    fn reorder(self) -> Self::Output;
}
// Verifies that the label sets are equivalent, and calls Reordering.
impl<L, V, T, TargetL, TargetV, TargetT> Reorder<LVCons<TargetL, TargetV, TargetT>>
    for LVCons<L, V, T>
where
    LVCons<L, V, T>: HasLabels<LVCons<TargetL, TargetV, TargetT>>,
    LVCons<TargetL, TargetV, TargetT>: HasLabels<LVCons<L, V, T>>,
    LVCons<TargetL, TargetV, TargetT>: Reordering<Self>,
{
    type Output = <LVCons<TargetL, TargetV, TargetT> as Reordering<Self>>::Output;

    fn reorder(self) -> Self::Output {
        <LVCons<TargetL, TargetV, TargetT> as Reordering<Self>>::reorder(self)
    }
}

/// Trait for a labeled cons-list which describes a reordering of the labels in `Original`.
pub trait Reordering<Original> {
    /// The values from `Original`, re-ordered to match the ordering of `Self`.
    type Output;

    /// Reorder `Original` according to the ordering of `Self`.
    fn reorder(orig: Original) -> Self::Output;
}
impl Reordering<Nil> for Nil {
    type Output = Nil;

    fn reorder(_: Nil) -> Nil {
        Nil
    }
}
impl<L, V, T, TargetL, TargetV, TargetT> Reordering<LVCons<L, V, T>>
    for LVCons<TargetL, TargetV, TargetT>
where
    LVCons<L, V, T>: TakeElemByLabel<TargetL>,
    <LVCons<L, V, T> as TakeElemByLabel<TargetL>>::Elem: Valued,
    TargetT: Reordering<<LVCons<L, V, T> as TakeElemByLabel<TargetL>>::Rest>,
{
    type Output = LVCons<
        TargetL,
        <<LVCons<L, V, T> as TakeElemByLabel<TargetL>>::Elem as Valued>::Value,
        <TargetT as Reordering<<LVCons<L, V, T> as TakeElemByLabel<TargetL>>::Rest>>::Output,
    >;

    fn reorder(orig: LVCons<L, V, T>) -> Self::Output {
        let (elem, rest) = TakeElemByLabel::<TargetL>::take_elem(orig);
        LVCons {
            head: Labeled::from(elem.value()),
            tail: <TargetT as Reordering<_>>::reorder(rest),
        }
    }
}

macro_rules! subset_apply {
    (
        $req_trait:tt $req_fn:tt ($($req_fn_output:tt)*)
        $trait_name:tt $fn_name:tt
        $trait_name_pred:tt $fn_name_pred:tt
    ) => {

        /// Trait for calling function `$req_fn` of trait `$req_trait` for all values of a cons-list
        /// which match a specified `LabelList`.
        ///
        /// Any labels in `LabelList` not found in `Self` will be ignored (see `HasLabels` for a
        /// trait that requires all members of `LabelList` to be found). The labels will be in the
        /// same order as in the original cons-list (with those labels not in `LabelList` filtered
        /// out).
        pub trait $trait_name<LabelList> {
            /// Output of applying `$req_fn` to values in this cons-list which match labels in
            /// `LabelList`.
            type Output;

            /// Apply `$req_fn` to value in the head of this cons-list if label of head matches
            /// labels of `LabelList`.
            fn $fn_name(&self) -> Self::Output;
        }

        // Base-case (Nil) implementation
        impl<LabelList> $trait_name<LabelList> for Nil {
            type Output = Nil;

            fn $fn_name(&self) -> Nil {
                Nil
            }
        }

        // Implementation for `LVCons` cons-lists.
        impl<LabelList, L, V, T> $trait_name<LabelList> for LVCons<L, V, T>
        where
            LabelList: Member<L>,
            Self: $trait_name_pred<LabelList, <LabelList as Member<L>>::IsMember>
        {
            type Output = <LVCons<L, V, T> as $trait_name_pred<
                LabelList,
                <LabelList as Member<L>>::IsMember,
            >>::Output;

            fn $fn_name(&self) -> Self::Output {
                self.$fn_name_pred()
            }
        }

        /// Helper trait. Used by `$trait_name` for computing the subset of `Self` cons-list which
        /// contains the labels in `LabelList`, and applying `$req_fn` to that subset.
        ///
        /// `IsMember` specifies whether or not the label of the head value of `Self` is a member of
        /// `LabelList`.
        pub trait $trait_name_pred<LabelList, IsMember> {
            /// Output of applying `$req_fn` to values in this cons-list if `IsMember` is `True`.
            type Output;

            /// Apply `$req_fn` to value in the head of this cons-list if `IsMember` is `True`.
            fn $fn_name_pred(&self) -> Self::Output;
        }

        // `$trait_name_pred` implementation for a cons-list where the head is in `LabelList`.
        impl<LabelList, H, T> $trait_name_pred<LabelList, True> for Cons<H, T>
        where
            T: $trait_name<LabelList>,
            H: $req_trait,
        {
            type Output = Cons<$($req_fn_output)*, <T as $trait_name<LabelList>>::Output>;

            fn $fn_name_pred(&self) -> Self::Output {
                Cons {
                    head: self.head.$req_fn(),
                    tail: self.tail.$fn_name()
                }
            }
        }

        // `$trait_name_pred` implementation for a cons-list where the head isn't in `LabelList`.
        impl<LabelList, H, T> $trait_name_pred<LabelList, False> for Cons<H, T>
        where
            T: $trait_name<LabelList>,
        {
            type Output = <T as $trait_name<LabelList>>::Output;

            fn $fn_name_pred(&self) -> Self::Output {
                self.tail.$fn_name()
            }
        }

    }
}

subset_apply![
    Clone clone (H)
    SubsetClone subset_clone
    SubsetClonePred subset_clone_pred
];

/// Generates a [LabelCons](type.LabelCons.html)-list with the labels associated with this
/// cons-list.
pub trait AssocLabels {
    /// [LabelCons](type.LabelCons.html)-list of labels associated with the `Self` cons-list.
    type Labels;
}
impl<Label, Value, Tail> AssocLabels for LVCons<Label, Value, Tail>
where
    Tail: AssocLabels,
{
    type Labels = LabelCons<Label, Tail::Labels>;
}
impl AssocLabels for Nil {
    type Labels = Nil;
}

//TODO: figure out how to have this return an array
/// Trait for generating a collection (`VecDeque`) of string labels for the labels associated with
/// the `Self` cons-list.
pub trait StrLabels {
    /// Returns the labels (as strings) for the labels associated with `Self`.
    fn labels<'a>() -> VecDeque<&'a str>;
    /// Returns the labels (as strings) for the labels associated with `Self`, collected in a
    /// `Vec` struct.
    fn labels_vec<'a>() -> Vec<&'a str> {
        Self::labels().iter().map(|&s| s).collect::<Vec<_>>()
    }
}
impl StrLabels for Nil {
    fn labels<'a>() -> VecDeque<&'a str> {
        VecDeque::new()
    }
}
impl<L, V, T> StrLabels for LVCons<L, V, T>
where
    L: LabelName,
    T: StrLabels,
{
    fn labels<'a>() -> VecDeque<&'a str> {
        let mut previous = T::labels();
        previous.push_front(L::name());
        previous
    }
}

impl NRows for Nil {
    fn nrows(&self) -> usize {
        0
    }
}
impl<L, V, Tail> NRows for LVCons<L, V, Tail>
where
    V: Valued,
    ValueOf<V>: NRows,
{
    fn nrows(&self) -> usize {
        self.head.value_ref().nrows()
    }
}

/// Trait for generating a collection (`VecDeque`) of string descriptions for the types associated
/// with the `Self` cons-list.
pub trait StrTypes {
    /// Returns the string descriptions of the types associated with `Self`.
    fn str_types<'a>() -> VecDeque<&'a str>;
}
impl StrTypes for Nil {
    fn str_types<'a>() -> VecDeque<&'a str> {
        VecDeque::new()
    }
}
impl<L, V, T> StrTypes for LVCons<L, V, T>
where
    L: LabelName,
    T: StrTypes,
{
    fn str_types<'a>() -> VecDeque<&'a str> {
        let mut previous = T::str_types();
        previous.push_front(L::str_type());
        previous
    }
}

/// Declares a set of data tables that all occupy the same tablespace (i.e. can be merged or
/// joined together). This macro should be used at the beginning of any `agnes`-using code, to
/// declare the various source and constructed table field labels.
///
/// Calls to this macro should include one or more `table` declarations, which have similar syntax
/// to `struct` definitions: a comma-separated list of `name: type` pairs for each member of the
/// table. Like a `struct` declaration, a `table` declaration can be preceded by a visibility
/// modifier (e.g. `pub`).
///
/// This macro will declare a module for each table specified (with the appropriate visibility) and
/// constructs label marker structs for each field specified within the table.
///
/// # Example
///
/// The following macro call declares two tables: `employee` and `department`. The `employee` table
/// has three fields (two `u64` fields and one `String` field), and the `department` table has
/// two fields (one `u64` field and one `String` field).
///
///
/// ```
/// # #[macro_use] extern crate agnes;
/// tablespace![
///     pub table employee {
///         EmpId: u64,
///         DeptId: u64,
///         EmpName: String,
///     }
///     table department {
///         DeptId: u64,
///         DeptName: String,
///     }
/// ];
/// # fn main() {}
/// ```
/// As a result of calling this macro, two modules will be declared --`namespace` and `department`
/// -- as well as the specified field labels within those modules. In this case, the `employee`
/// table will have public visibility, while the `department` table will be private. After declaring
/// these modules, you can refer to the labels as you would a normal type; e.g., `employee::EmpId`.
#[macro_export]
macro_rules! tablespace {
    (@fields() -> ($($out:tt)*)) => {
        declare_fields![Table; $($out)*];
        /// `FieldCons` cons-list of fields in this table.
        pub type Fields = Fields![$($out)*];
    };
    (@fields(,) -> ($($out:tt)*)) => {
        declare_fields![Table; $($out)*];
        /// `FieldCons` cons-list of fields in this table.
        pub type Fields = Fields![$($out)*];
    };

    (@fields
        (,$field_name:ident: $field_ty:ident = {$str_name:expr} $($rest:tt)*)
        ->
        ($($out:tt)*)
    ) => {
        tablespace![@fields
            ($($rest)*)
            ->
            ($($out)* $field_name: $field_ty = $str_name,)
        ];
    };
    (@fields
        (,$field_name:ident: $field_ty:ident $($rest:tt)*)
        ->
        ($($out:tt)*)
    ) => {
        tablespace![@fields
            ($($rest)*)
            ->
            ($($out)* $field_name: $field_ty = stringify![$field_name],)
        ];
    };

    (@body($($body:tt)*)) => {
        tablespace![@fields(,$($body)*) -> ()];
    };

    (@construct($vis:vis $tbl_name:ident)($nat:ty)($($body:tt)*)) => {
        $vis mod $tbl_name {
            #![allow(dead_code)]
            /*!
                Type aliases defining what is contained within table $tbl_name.
            */

            /// Type-level backing natural number for this table. This type connects all tables
            /// within a tablespace together.
            pub type Table = $nat;

            /// Type alias for a `DataStore` composed of the fields
            /// referenced in this table definition.
            pub type Store = $crate::store::DataStore<Fields>;
            /// Extra type alias for `Store`.
            pub type DataStore = Store;
            /// Type alias for a `DataView` composed of the fields
            /// referenced in this table definition.
            pub type View = <Store as $crate::store::IntoView>::Output;
            /// Extra type alias for `View`.
            pub type DataView = View;

            tablespace![@body($($body)*)];
        }
    };

    // end case
    (@continue($prev_tbl:ty)) => {};

    // non-initial case
    (@continue($prev_tbl:ty)
        $vis:vis table $tbl_name:ident {
            $($body:tt)*
        }
        $($rest:tt)*
    ) => {
        tablespace![@construct($vis $tbl_name)($prev_tbl)($($body)*)];
        tablespace![@continue($crate::typenum::Add1<$prev_tbl>) $($rest)*];
    };

    // entry point
    (
        $vis:vis table $tbl_name:ident {
            $($body:tt)*
        }
        $($rest:tt)*
    ) => {
        tablespace![@construct($vis $tbl_name)($crate::typenum::U0)($($body)*)];
        tablespace![@continue($crate::typenum::Add1<$crate::typenum::U0>) $($rest)*];
    }
}

/// Macro for defining a single label and its backing natural. Used by
/// [next_label](macro.next_label.html) and
/// [first_label](macro.first_label.html) macros.
#[macro_export]
macro_rules! nat_label {
    ($label:ident, $tbl:ty, $nat:ty, $dtype:ty, $name:expr) => {
        /// Unit struct representing the field $label.
        #[derive(Debug, Clone)]
        pub struct $label;

        impl $crate::label::Identifier for $label {
            type Ident = $crate::label::Ident<$tbl, $nat>;
            type Table = $tbl;
            type Natural = $nat;
        }
        impl $crate::label::Label for $label {
            const NAME: &'static str = $name;
            const TYPE: &'static str = stringify![$dtype];
        }
        impl $crate::label::Typed for $label {
            type DType = $dtype;
        }
    };
}

/// Macro for handling creation of the first label in a table. Used by
/// [declare_fields](macro.declare_fields.html).
#[macro_export]
macro_rules! first_label {
    ($label:ident, $tbl:ty, $dtype:ty) => {
        first_label![$label, $tbl, $dtype, stringify![$label]];
    };
    ($label:ident, $tbl:ty, $dtype:ty, $name:expr) => {
        nat_label![$label, $tbl, $crate::typenum::consts::U0, $dtype, $name];
    };
}

/// Macro for handling creation of the subsequent (non-initial) labels in a table. Used by
/// [declare_fields](macro.declare_fields.html).
#[macro_export]
macro_rules! next_label {
    ($label:ident, $prev:ident, $dtype:ty) => {
        next_label![$label, $prev, $dtype, stringify![$label]];
    };
    ($label:ident, $prev:ident, $dtype:ty, $name:expr) => {
        nat_label![
            $label,
            $crate::label::TblOf<$prev>,
            $crate::typenum::Add1<$crate::label::NatOf<$prev>>,
            $dtype,
            $name
        ];
    };
}

/// Create a [LabelCons](label/type.LabelCons.html) cons-list based on a list of provided labels.
/// Used to specify a list of field labels to operate over.
///
/// # Example
/// ```ignore
/// let subdv = dv.v::<Labels![emp_table::EmpId, dept_table::DeptId, emp_table::EmpName]>();
/// ```
#[macro_export]
macro_rules! Labels {
    (@labels()) => { $crate::cons::Nil };
    (@labels($label:ident, $($rest:tt,)*)) =>
    {
        $crate::label::LCons<$label, Labels![@labels($($rest,)*)]>
    };
    (@labels($label:path, $($rest:tt,)*)) =>
    {
        $crate::label::LCons<$label, Labels![@labels($($rest,)*)]>
    };
    ($($label:ident),*$(,)*) =>
    {
        Labels![@labels($($label,)*)]
    };
    ($($label:path),*$(,)*) =>
    {
        Labels![@labels($($label,)*)]
    }
}

/// Macro for declaring field labels. Used by [tablespace](macro.tablespace.html) macro.
#[macro_export]
macro_rules! declare_fields
{
    // end case
    (@step($tbl:ty)($prev_label:ident)()) => {};

    // non-initial label
    (@step
        ($tbl:ty)
        ($prev_label:ident)
        ($label:ident: $dtype:ident = $name:expr, $($rest:tt)*)
    )
        =>
    {
        next_label![$label, $prev_label, $dtype, $name];
        declare_fields![@step
            ($tbl)
            ($label)
            ($($rest)*)
        ];
    };
    // handle non-trailing comma
    (@step($tbl:ty)($prev_label:ident)($label:ident: $dtype:ident = $name:expr))
        =>
    {
        declare_fields![@step($tbl)($prev_label)($label: $dtype,)]
    };

    // initial label
    (@start
        ($tbl:ty)
        ($label:ident: $dtype:ident = $name:expr, $($rest:tt)*)
    )
        =>
    {
        first_label![$label, $tbl, $dtype, $name];
        declare_fields![@step
            ($tbl)
            ($label)
            ($($rest)*)
        ];
    };
    // handle non-trailing comma
    (@start($tbl:ty)($label:ident: $dtype:ident = $name:expr))
        =>
    {
        declare_fields![@step($tbl)($label: $dtype = $name,)]
    };

    // entry point
    ($tbl:ty; $($fields:tt)*) => {
        declare_fields![@start($tbl)($($fields)*)];
    };
}

/// Create a [FieldCons](fieldlist/type.FieldCons.html) cons-list based on a list of provided labels
/// and data types. Used by [tablespace](macro.tablespace.html) macro.
#[macro_export]
macro_rules! Fields {
    (@fields()) => { $crate::cons::Nil };
    (@fields(
        $label:ident: $dtype:ident $(= $name:expr)*,
        $($rest_label:ident: $rest_dtype:ident $(= $rest_name:expr)*,)*)
    )
        =>
    {
        $crate::fieldlist::FieldCons<
            $label,
            $dtype,
            Fields![@fields($($rest_label: $rest_dtype,)*)]
        >
    };
    ($($label:ident: $dtype:ident $(= $name:expr)*),*$(,)*) =>
    {
        Fields![@fields($($label: $dtype,)*)]
    };
    ($existing:ident .. $($label:ident: $dtype:ident),*$(,)*) =>
    {
        <$existing as $crate::cons::Append<Fields![@fields($($label: $dtype,)*)]>>::Appended
    };
    ($($label:ident: $dtype:ident),*$(,)* .. $existing:ident) =>
    {
        <Fields![@fields($($label: $dtype,)*)] as $crate::cons::Append<$existing>>::Appended
    }
}

/// Creates a data table with supplied data.
///
/// `table` handles creating the underlying [DataStore](store/struct.DataStore.html) to store the
/// data, and returns a [DataView](view/struct.DataView.html).
///
/// This macro should be called with a semi-colon separated list of
/// '`<label> = [<comma-separated list of data];`' field data definitions. Each field should have
/// the same number of data entries.
///
/// # Example
///
/// In this usage, we are creating a data table for the `employee` table, as defined with the
/// [tablespace](macro.tablespace.html) macro. We add 7 data points to each of the `EmpId`,
/// `DeptId`, and `EmpName` fields:
/// ```
/// # #[macro_use] extern crate agnes;
/// tablespace![
///     pub table employee {
///         EmpId: u64,
///         DeptId: u64,
///         EmpName: String,
///     }
/// ];
/// fn main() {
///     let emp_table = table![
///         employee::EmpId = [0, 1, 2, 3, 4, 5, 6];
///         employee::DeptId = [0, 2, 1, 1, 1, 0, 1];
///         employee::EmpName =
///             ["Astrid", "Bob", "Calvin", "Deborah", "Eliza", "Franklin", "Gunther"];
///     ];
///     assert_eq!((emp_table.nrows(), emp_table.nfields()), (7, 3));
///     println!("{}", emp_table);
/// }
/// ```
/// This will produce the output:
/// ```text
///  EmpId | DeptId | EmpName
/// -------+--------+----------
///  0     | 0      | Astrid
///  1     | 2      | Bob
///  2     | 1      | Calvin
///  3     | 1      | Deborah
///  4     | 1      | Eliza
///  5     | 0      | Franklin
///  6     | 1      | Gunther
/// ```
#[macro_export]
macro_rules! table {
    ($($label:ty = [$($value:expr),*];)*) => {{
        $crate::store::DataStore::<$crate::cons::Nil>::empty()
        $(
            .push_back_field::<$label, _>(
                $crate::field::FieldData::from_boxed_slice(Box::new([$($value),*]))
            )
        )*
            .into_view()
    }}
}

#[cfg(test)]
mod tests {
    use super::*;
    use cons::*;
    use typenum::{
        consts::{U0, U1, U2, U3, U4},
        Bit,
    };

    pub type SampleTable = U0;
    first_label![ImALabel, U0, u64];
    next_label![ImAnotherLabel, ImALabel, u64];

    #[test]
    fn type_eq() {
        assert!(<U1 as IdentEq<U1>>::Eq::to_bool());
        assert!(!<U1 as IdentEq<U4>>::Eq::to_bool());
        assert!(<ImALabel as LabelEq<ImALabel>>::Eq::to_bool());
        assert!(!<ImALabel as LabelEq<ImAnotherLabel>>::Eq::to_bool());
    }

    pub type NumberTable = Add1<SampleTable>;
    first_label![F0, NumberTable, u64];
    next_label![F1, F0, f64];
    next_label![F2, F1, i64];
    next_label![F3, F2, String];
    next_label![F4, F3, f32];
    next_label![F5, F4, f32];
    next_label![F6, F5, f32];
    next_label![F7, F6, f32];

    #[test]
    fn lookup() {
        let list = LVCons {
            head: Labeled::<F0, _>::from(6u64),
            tail: LVCons {
                head: Labeled::<F1, _>::from(5.3f64),
                tail: LVCons {
                    head: Labeled::<F2, _>::from(-3i64),
                    tail: LVCons {
                        head: Labeled::<F3, _>::from("Hello".to_string()),
                        tail: LVCons {
                            head: Labeled::<F4, _>::from(3.2f32),
                            tail: Nil,
                        },
                    },
                },
            },
        };

        assert_eq!(LookupElemByNat::<U0>::elem(&list).value, 6u64);
        assert_eq!(LookupElemByNat::<U1>::elem(&list).value, 5.3);
        assert_eq!(LookupElemByNat::<U2>::elem(&list).value, -3i64);
        assert_eq!(
            LookupElemByNat::<U3>::elem(&list).value,
            "Hello".to_string()
        );
        assert_eq!(LookupElemByNat::<U4>::elem(&list).value, 3.2f32);

        assert_eq!(LookupNatByLabel::<F0>::nat(&list), 0);
        assert_eq!(LookupNatByLabel::<F1>::nat(&list), 1);
        assert_eq!(LookupNatByLabel::<F2>::nat(&list), 2);
        assert_eq!(LookupNatByLabel::<F3>::nat(&list), 3);
        assert_eq!(LookupNatByLabel::<F4>::nat(&list), 4);

        assert_eq!(LookupElemByLabel::<F0>::elem(&list).value, 6u64);
        assert_eq!(LookupElemByLabel::<F1>::elem(&list).value, 5.3);
        assert_eq!(LookupElemByLabel::<F2>::elem(&list).value, -3i64);
        assert_eq!(
            LookupElemByLabel::<F3>::elem(&list).value,
            "Hello".to_string()
        );
        assert_eq!(LookupElemByLabel::<F4>::elem(&list).value, 3.2f32);

        let list = LVCons {
            head: Labeled::<F5, _>::from(3u32),
            tail: list,
        };

        assert_eq!(LookupNatByLabel::<F0>::nat(&list), 1);
        assert_eq!(LookupNatByLabel::<F1>::nat(&list), 2);
        assert_eq!(LookupNatByLabel::<F2>::nat(&list), 3);
        assert_eq!(LookupNatByLabel::<F3>::nat(&list), 4);
        assert_eq!(LookupNatByLabel::<F4>::nat(&list), 5);
        assert_eq!(LookupNatByLabel::<F5>::nat(&list), 0);

        assert_eq!(LookupElemByLabel::<F0>::elem(&list).value, 6u64);
        assert_eq!(LookupElemByLabel::<F1>::elem(&list).value, 5.3);
        assert_eq!(LookupElemByLabel::<F2>::elem(&list).value, -3i64);
        assert_eq!(
            LookupElemByLabel::<F3>::elem(&list).value,
            "Hello".to_string()
        );
        assert_eq!(LookupElemByLabel::<F4>::elem(&list).value, 3.2f32);
        assert_eq!(LookupElemByLabel::<F5>::elem(&list).value, 3u32);
    }

    #[test]
    fn filter() {
        type SampleLabels = LVCons<
            F0,
            u64,
            LVCons<F1, f64, LVCons<F2, i64, LVCons<F3, String, LVCons<F4, f32, Nil>>>>,
        >;

        {
            // null case
            type Filtered = <SampleLabels as LabelSubset<Labels![]>>::Output;
            // empty filter, length should be 0
            assert_eq!(length![Filtered], 0);
        }
        {
            // other null case
            type Filtered = <Nil as LabelSubset<Labels![F1, F3]>>::Output;
            // empty cons-list, so filtered length should be 0
            assert_eq!(length![Filtered], 0);
        }
        {
            type Filtered = <SampleLabels as LabelSubset<Labels![F3]>>::Output;
            // we only filtered 1 label, so length should be 1
            assert_eq!(length![Filtered], 1);
        }
        {
            type Filtered = <SampleLabels as LabelSubset<Labels![F1, F2, F4]>>::Output;
            // we only filtered 3 labels, so length should be 3
            assert_eq!(length![Filtered], 3);

            {
                type Refiltered = <Filtered as LabelSubset<Labels![F1, F2, F4]>>::Output;
                // filtered same labels, so length should stay at 3
                assert_eq!(length![Refiltered], 3);
            }
            {
                type Refiltered = <Filtered as LabelSubset<Labels![F1, F2]>>::Output;
                // filtered 2 labels that should exist `Filtered`, so length should be 2
                assert_eq!(length![Refiltered], 2);
            }
            {
                type Refiltered = <Filtered as LabelSubset<Labels![F3, F0]>>::Output;
                // filtered 2 labels that should not exist `Filtered`, so length should be 0
                assert_eq!(length![Refiltered], 0);
            }
            {
                type Refiltered = <Filtered as LabelSubset<Labels![F0, F1, F2, F3, F4]>>::Output;
                // `F0 and `F3` don't exist in `Filtered`, so length should be 3
                assert_eq!(length![Refiltered], 3);
            }
        }
        {
            type Filtered = <SampleLabels as LabelSubset<Labels![F1, F2, F4, F5]>>::Output;
            // F5 doesn't exist in SampleLabels, so we still should only have 3
            assert_eq!(length![Filtered], 3);
        }
        {
            type Filtered = <SampleLabels as LabelSubset<Labels![F5, F6, F7]>>::Output;
            // None of these labels exist in SampleLabels, so we should have 0
            assert_eq!(length![Filtered], 0);
        }
        {
            // check for problems cause by duplicated in label list
            type Filtered = <SampleLabels as LabelSubset<Labels![F2, F2, F2]>>::Output;
            // we only filtered 1 label (even if it was duplicated), so length should be 1
            assert_eq!(length![Filtered], 1);
        }
        {
            // check for problems cause by duplicated in label list
            type Filtered = <SampleLabels as LabelSubset<Labels![F2, F2, F3]>>::Output;
            // we only filtered 2 label (albeit with some duplication), so length should be 2
            assert_eq!(length![Filtered], 2);
        }
    }

    #[test]
    fn reorder() {
        type LSet = LVCons<F0, u64, LVCons<F1, f64, LVCons<F2, i64, Nil>>>;
        assert_eq!(["F0", "F1", "F2"], <LSet as StrLabels>::labels_vec()[..]);

        type NewOrder = Labels![F0, F2, F1];
        type Reordered = <LSet as Reorder<NewOrder>>::Output;
        assert_eq!(
            ["F0", "F2", "F1"],
            <Reordered as StrLabels>::labels_vec()[..]
        );

        let orig = LVCons {
            head: Labeled::<F0, _>::from(6u64),
            tail: LVCons {
                head: Labeled::<F1, _>::from(5.3f64),
                tail: LVCons {
                    head: Labeled::<F2, _>::from(-3i64),
                    tail: Nil,
                },
            },
        };
        assert_eq!(LookupElemByNat::<U0>::elem(&orig).value, 6u64);
        assert_eq!(LookupElemByNat::<U1>::elem(&orig).value, 5.3);
        assert_eq!(LookupElemByNat::<U2>::elem(&orig).value, -3i64);

        let reordered: Reordered = Reorder::<NewOrder>::reorder(orig);
        assert_eq!(LookupElemByNat::<U0>::elem(&reordered).value, 6u64);
        assert_eq!(LookupElemByNat::<U1>::elem(&reordered).value, -3i64);
        assert_eq!(LookupElemByNat::<U2>::elem(&reordered).value, 5.3);
    }

    #[test]
    fn set_diff() {
        type LSet1 = LCons<F0, LCons<F1, LCons<F2, Nil>>>;
        type LSet2 = LCons<F4, LCons<F1, LCons<F2, Nil>>>;
        type LSet3 = LCons<F1, Nil>;
        type LSet4 = LCons<F4, Nil>;

        assert_eq!(
            ["F0"],
            <LabelSetDiff<LSet1, LSet2> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F4"],
            <LabelSetDiff<LSet2, LSet1> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F0", "F2"],
            <LabelSetDiff<LSet1, LSet3> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F4", "F2"],
            <LabelSetDiff<LSet2, LSet3> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F1", "F2"],
            <LabelSetDiff<LSet2, LSet4> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F0", "F1", "F2"],
            <LabelSetDiff<LSet1, LSet4> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            [] as [&str; 0],
            <LabelSetDiff<LSet3, LSet1> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F4"],
            <LabelSetDiff<LSet4, LSet1> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            [] as [&str; 0],
            <LabelSetDiff<LSet4, LSet2> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            [] as [&str; 0],
            <LabelSetDiff<Nil, Nil> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            [] as [&str; 0],
            <LabelSetDiff<Nil, LSet2> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            ["F4", "F1", "F2"],
            <LabelSetDiff<LSet2, Nil> as StrLabels>::labels_vec()[..]
        );
        assert_eq!(
            [] as [&str; 0],
            <LabelSetDiff<LSet2, LSet2> as StrLabels>::labels_vec()[..]
        );
    }
}