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
#[allow(unused_imports)]
use core::fmt::Debug;
use core::ops::{Add, Div, Mul, Neg, Not};

use crate::prelude::*;

/// Id HKT
pub mod hkt {
  use crate::HKT1;

  /// Id HKT
  pub struct Id;

  impl HKT1 for Id {
    type T<A> = super::Id<A>;
  }
}

/// The Identity monad
///
/// This type does nothing more than wrap a `T`.
///
/// Typeclasses that Id impls for all T:
///  * [`FunctorOnce`]
///  * [`MonadOnce`]
///  * [`ApplyOnce`]
///  * [`Applicative`]
///  * [`Alt`] (ignores second Id)
///
/// Typeclasses that Id impls when T impls:
///  * `(Partial)Eq`
///  * `(Partial)Ord`
///  * `append` / `identity` [`Semigroup`] [`Monoid`]
///  * [`Debug`]
///  * [`Clone`], [`Copy`]
///  * [`Default`]
///  * Arithmetic / algebra [`Add`], [`Mul`], [`Div`], [`Neg`], [`Not`]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Id<T>(pub T);

impl<T> Id<T> {
  /// Wrap a type in [`Id`]
  pub fn new(t: T) -> Self {
    Self(t)
  }

  /// Unwrap this [`Id`]
  pub fn get(self) -> T {
    self.0
  }
}

impl<T> Not for Id<T> where T: Not
{
  type Output = Id<<T as Not>::Output>;

  fn not(self) -> Self::Output {
    Id(!self.0)
  }
}

impl<T> Neg for Id<T> where T: Neg
{
  type Output = Id<<T as Neg>::Output>;

  fn neg(self) -> Self::Output {
    Id(-self.0)
  }
}

impl<T> Add for Id<T> where T: Add
{
  type Output = Id<<T as Add>::Output>;

  fn add(self, rhs: Self) -> Self::Output {
    Id(self.0 + rhs.0)
  }
}

impl<T> Mul for Id<T> where T: Mul
{
  type Output = Id<<T as Mul>::Output>;

  fn mul(self, rhs: Self) -> Self::Output {
    Id(self.0 * rhs.0)
  }
}

impl<T> Div for Id<T> where T: Div
{
  type Output = Id<<T as Div>::Output>;

  fn div(self, rhs: Self) -> Self::Output {
    Id(self.0 / rhs.0)
  }
}

impl<T> Semigroup for Id<T> where T: Semigroup
{
  fn append(self, b: Self) -> Self {
    Id(self.0.append(b.0))
  }
}

impl<T> Monoid for Id<T> where T: Monoid
{
  fn identity() -> Self {
    Id(T::identity())
  }
}

impl<T> FunctorOnce<hkt::Id, T> for Id<T> {
  fn fmap1<AB, B>(self, f: AB) -> Id<B>
    where AB: F1Once<T, Ret = B>
  {
    Id(f.call1(self.0))
  }
}
deriving!(impl Functor<hkt::Id, A> for Id<A> {..FunctorOnce});

impl<T> MonadOnce<hkt::Id, T> for Id<T> {
  fn bind1<B, AMB>(self, f: AMB) -> Id<B>
    where AMB: F1Once<T, Ret = Id<B>>
  {
    f.call1(self.0)
  }
}
deriving!(impl Monad<hkt::Id, A> for Id<A> {..MonadOnce});

impl<T> ApplyOnce<hkt::Id, T> for Id<T> {
  fn apply1<A, B>(self, a: Id<A>) -> Id<B>
    where T: F1Once<A, Ret = B>
  {
    Id(self.0.call1(a.0))
  }
}
deriving!(impl Apply<hkt::Id, AB> for Id<AB> {..ApplyOnce});

impl<T> Applicative<hkt::Id, T> for Id<T> {
  fn pure(a: T) -> Id<T> {
    Id(a)
  }
}

impl<T> Alt<hkt::Id, T> for Id<T> {
  fn alt(self, _: Self) -> Self {
    self
  }
}