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
//! A newtype wrapper that reverses the order of a [`Semigroup`](crate::classes::Semigroup)'s operation.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! functions::*,
//! types::Dual,
//! };
//!
//! let x = Dual("hello ".to_string());
//! let y = Dual("world".to_string());
//! // Dual reverses append order: append(Dual(a), Dual(b)) = Dual(append(b, a))
//! assert_eq!(append(x, y), Dual("worldhello ".to_string()));
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::classes::*,
fp_macros::*,
};
/// A newtype wrapper that reverses the order of a [`Semigroup`]'s operation.
///
/// `append(Dual(a), Dual(b))` is equivalent to `Dual(append(b, a))`.
///
/// If the inner type is a [`Monoid`], `Dual` is also a [`Monoid`] with the
/// same identity element.
#[document_examples]
///
/// ```
/// use fp_library::{
/// functions::*,
/// types::Dual,
/// };
///
/// let x = Dual("ab".to_string());
/// let y = Dual("cd".to_string());
/// assert_eq!(append(x, y), Dual("cdab".to_string()));
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Dual<A>(
/// The wrapped value.
pub A,
);
#[document_type_parameters("The semigroup type.")]
impl<A: Semigroup> Semigroup for Dual<A> {
/// Combines two values in reverse order.
///
/// `append(Dual(a), Dual(b))` computes `Dual(append(b, a))`.
#[document_signature]
///
#[document_parameters("The first dual value.", "The second dual value.")]
///
#[document_returns("The reversed combination wrapped in `Dual`.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// functions::*,
/// types::Dual,
/// };
///
/// assert_eq!(append(Dual("ab".to_string()), Dual("cd".to_string())), Dual("cdab".to_string()));
/// ```
fn append(
a: Self,
b: Self,
) -> Self {
Dual(A::append(b.0, a.0))
}
}
#[document_type_parameters("The monoid type.")]
impl<A: Monoid> Monoid for Dual<A> {
/// Returns `Dual(empty())`.
#[document_signature]
///
#[document_returns("The identity element wrapped in `Dual`.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// functions::*,
/// types::Dual,
/// };
///
/// assert_eq!(empty::<Dual<String>>(), Dual(String::new()));
/// ```
fn empty() -> Self {
Dual(A::empty())
}
}
}
pub use inner::*;
#[cfg(test)]
mod tests {
use {
super::*,
crate::functions::*,
quickcheck_macros::quickcheck,
};
#[quickcheck]
fn semigroup_associativity(
a: String,
b: String,
c: String,
) -> bool {
let x = Dual(a);
let y = Dual(b);
let z = Dual(c);
append(x.clone(), append(y.clone(), z.clone())) == append(append(x, y), z)
}
#[quickcheck]
fn monoid_left_identity(a: String) -> bool {
let x = Dual(a);
append(empty::<Dual<String>>(), x.clone()) == x
}
#[quickcheck]
fn monoid_right_identity(a: String) -> bool {
let x = Dual(a);
append(x.clone(), empty::<Dual<String>>()) == x
}
}