bitwise shrub/kav / lib / either.kav
  1use <prelude.kav>
  2use <maybe.kav> as maybe
  3use <list.kav> as list
  4
  5/%
  6A linear sum type: a value that is either a `Left` holding an `a`
  7or a `Right` holding a `b`.
  8
  9Neither side is privileged, this is a generic sum type, not an error type.
 10%/
 11pub linear datatype Either[a: type, b: type] {
 12    | Left(a)
 13    | Right(b)
 14}
 15
 16/%
 17Deallocates an either.
 18%/
 19pub fn free[a: utype, b: utype](e: Either[a, b]) -> one {
 20    case e of {
 21    | ~Left(_) => one
 22    | ~Right(_) => one
 23    }
 24}
 25
 26/%
 27Returns a copy of an either.
 28%/
 29pub fn copy[a: utype, b: utype](e: !Either[a, b]) -> Either[a, b] {
 30    case e of {
 31    | Left(x) => Left(x)
 32    | Right(y) => Right(y)
 33    }
 34}
 35
 36/%
 37Tests whether an either is a `Left`.
 38%/
 39pub fn isleft[a: type, b: type](e: !Either[a, b]) -> bool {
 40    case e of {
 41    | Left(_) => true
 42    | Right(_) => false
 43    }
 44}
 45
 46/%
 47Tests whether an either is a `Right`.
 48%/
 49pub fn isright[a: type, b: type](e: !Either[a, b]) -> bool {
 50    case e of {
 51    | Left(_) => false
 52    | Right(_) => true
 53    }
 54}
 55
 56/%
 57Swaps the sides of an either: `Left(x)` becomes `Right(x)` and vice versa.
 58%/
 59pub fn swap[a: type, b: type](e: Either[a, b]) -> Either[b, a] {
 60    case e of {
 61    | ~Left(x) => Right(x)
 62    | ~Right(y) => Left(y)
 63    }
 64}
 65
 66/%
 67Applies `f` to the content of a `Left`, leaving a `Right` untouched.
 68%/
 69pub fn mapleft[a: type, b: type, c: type](e: Either[a, b], f: fn(a) -> c) -> Either[c, b] {
 70    case e of {
 71    | ~Left(x) => Left(f(x))
 72    | ~Right(y) => Right(y)
 73    }
 74}
 75
 76/%
 77Applies `f` to the content of a `Right`, leaving a `Left` untouched.
 78%/
 79pub fn mapright[a: type, b: type, c: type](e: Either[a, b], f: fn(b) -> c) -> Either[a, c] {
 80    case e of {
 81    | ~Left(x) => Left(x)
 82    | ~Right(y) => Right(f(y))
 83    }
 84}
 85
 86/%
 87Applies `f` if the either is a `Left` and `g` if it is a `Right`,
 88keeping the side.
 89%/
 90pub fn map[a: type, b: type, c: type, d: type](e: Either[a, b], f: fn(a) -> c, g: fn(b) -> d) -> Either[c, d] {
 91    case e of {
 92    | ~Left(x) => Left(f(x))
 93    | ~Right(y) => Right(g(y))
 94    }
 95}
 96
 97/%
 98Collapses an either into a single value.
 99
100Applies `f` if the either is a `Left` and `g` if it is a `Right`.
101%/
102pub fn fold[a: type, b: type, c: type](e: Either[a, b], f: fn(a) -> c, g: fn(b) -> c) -> c {
103    case e of {
104    | ~Left(x) => f(x)
105    | ~Right(y) => g(y)
106    }
107}
108
109/%
110Tests whether two eithers are equal.
111
112Two eithers are equal if they are on the same side and their contents are
113equal according to `eqleft` (for `Left`) or `eqright` (for `Right`).
114%/
115pub fn equal[a: utype, b: utype](e1: !Either[a, b], e2: !Either[a, b], eqleft: fn(a, a) -> bool, eqright: fn(b, b) -> bool) -> bool {
116    case (e1, e2) of {
117    | (Left(x), Left(y)) => eqleft(x, y)
118    | (Right(x), Right(y)) => eqright(x, y)
119    | (Left(_), Right(_)) => false
120    | (Right(_), Left(_)) => false
121    }
122}