-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEither.fm
39 lines (33 loc) · 1.39 KB
/
Either.fm
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
// A container for either a value of type A or a value of type B.
T Either<A: Type, B: Type>
| Either.left(value: A);
| Either.right(value: B);
Either.bind<A: Type, B: Type, C: Type>(e: Either(A,B), f: B -> Either(A,C)): Either(A,C)
case e:
| Either.left<A, C>(e.value);
| f(e.value);
// Either(A) functor
// =================
Either.functor<A: Type>: Functor(Either(A))
Functor.new<Either(A)>(Either.map<A>)
Either.functor.verified<A: Type>: VerifiedFunctor(Either(A), Either.functor<A>)
VerifiedFunctor.new<Either(A), Either.functor<A>>(Either.map.id<A>, Either.map.comp<A>)
Either.map<A: Type, B: Type, C: Type>(f: B -> C, e: Either(A, B)): Either(A, C)
case e:
| Either.left<A, C>(e.value);
| Either.right<A, C>(f(e.value));
Either.map.id<A: Type, B: Type>(e: Either(A, B)): Equal(Either(A, B), Either.map<A, B, B>(Function.id<B>, e), e)
case e:
| _;
| _;
: Equal(_, Either.map<A,B,B>(Function.id<>, e.self), e.self);
Either.map.comp<A: Type, B: Type, C: Type, D: Type>(e: Either(A, B), g: (C -> D), h: (B -> C))
: Equal(Either(A, D),
Either.map<A, B, D>(Function.comp<B, C, D>(g, h), e),
Function.comp<Either(A, B), Either(A, C), Either(A, D)>(Either.map<A, C, D>(g), Either.map<A, B, C>(h), e))
case e:
| _;
| _;
: Equal(_,
Either.map<,,>(Function.comp<,,>(g, h), e.self),
Function.comp<,,>(Either.map<,,>(g), Either.map<,,>(h))(e.self));