-
Notifications
You must be signed in to change notification settings - Fork 0
/
functoriality.txt
41 lines (30 loc) · 854 Bytes
/
functoriality.txt
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
Ex. 1
-----
instance Bifunctor Pair where
bimap f g (Pair x y) = Pair (f x) (g y)
first f (Pair x y) = Pair (f x) y
second g (Pair x y) = Pair x (g y)
first f = bimap f id
first f (Pair x y) = Pair (f x) y
bimap f id (Pair x y) = Pair (f x) y
similarly for second g
bimap f g (Pair x y) = first f . second g (Pair x y) = first f (Pair x (g y)) = Pair (f x) (g y)
Ex. 2
-----
SugaredFromRaw x =
match x
Left (Const () _) => Nothing
Right (Identity y) => Just y
RawFromSugared x =
match x
Nothing => Left (Const () a)
Just y => Right (Identity y)
SugaredFromRaw RawFromSugared x =
match x
Nothing => SugaredFromRaw (Left (Const () a)) = Nothing
Just y => SugaredFromRaw (Right (Identity y)) = Just y
Ex. 3
-----
instance Bifunctor PreList where
bimap f g Nil = Nil
bimap f g (Cons a b) = Cons (f a) (g b)