Mercurial > hg > Members > atton > agda-proofs
annotate sandbox/FunctorExample.agda @ 7:c11c259916b7
Example for natural transformation
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 17 Jan 2015 22:13:47 +0900 |
parents | 90abb3f53c03 |
children | a3509dbb9e49 |
rev | line source |
---|---|
6 | 1 open import Level |
2 open import Relation.Binary.PropositionalEquality | |
3 open ≡-Reasoning | |
4 | |
5 | |
6 module FunctorExample where | |
7 | |
8 id : {l : Level} {A : Set l} -> A -> A | |
9 id x = x | |
10 | |
11 _∙_ : {l ll lll : Level} {A : Set l} {B : Set ll} {C : Set lll} -> (B -> C) -> (A -> B) -> (A -> C) | |
12 f ∙ g = \x -> f (g x) | |
13 | |
14 | |
15 | |
16 record Functor {l : Level} (F : Set l -> Set (suc l)) : (Set (suc l)) where | |
17 field | |
18 fmap : ∀{A B} -> (A -> B) -> (F A) -> (F B) | |
19 field | |
20 preserve-id : ∀{A} (Fa : F A) → fmap id Fa ≡ id Fa | |
21 covariant : ∀{A B C} (f : A → B) → (g : B → C) → (x : F A) | |
22 → fmap (g ∙ f) x ≡ fmap g (fmap f x) | |
23 | |
24 data List {l : Level} (A : Set l) : (Set (suc l)) where | |
25 nil : List A | |
26 cons : A -> List A -> List A | |
27 | |
28 list-fmap : {l ll : Level} {A : Set l} {B : Set ll} -> (A -> B) -> List A -> List B | |
29 list-fmap f nil = nil | |
30 list-fmap f (cons x xs) = cons (f x) (list-fmap f xs) | |
31 | |
32 list-preserve-id : {l : Level} {A : Set l} -> (xs : List A) -> list-fmap id xs ≡ id xs | |
33 list-preserve-id nil = refl | |
34 list-preserve-id (cons x xs) = cong (\li -> cons x li) (list-preserve-id xs) | |
35 | |
36 list-covariant : {l ll lll : Level} {A : Set l} {B : Set ll} {C : Set lll} -> | |
37 (f : A -> B) → (g : B -> C) → (x : List A) → list-fmap (g ∙ f) x ≡ list-fmap g (list-fmap f x) | |
38 list-covariant f g nil = refl | |
39 list-covariant f g (cons x xs) = cong (\li -> cons (g (f x)) li) (list-covariant f g xs) | |
40 | |
41 | |
42 list-is-functor : {l : Level} -> Functor List | |
43 list-is-functor {l} = record { fmap = list-fmap ; | |
44 preserve-id = list-preserve-id ; | |
45 covariant = list-covariant {l}} | |
46 | |
7
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
47 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
48 data Identity {l : Level} (A : Set l) : Set (suc l) where |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
49 identity : A -> Identity A |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
50 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
51 identity-fmap : {l ll : Level} {A : Set l} {B : Set ll} -> (A -> B) -> Identity A -> Identity B |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
52 identity-fmap f (identity a) = identity (f a) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
53 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
54 identity-preserve-id : {l : Level} {A : Set l} -> (x : Identity A) -> identity-fmap id x ≡ id x |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
55 identity-preserve-id (identity x) = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
56 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
57 identity-covariant : {l ll lll : Level} {A : Set l} {B : Set ll} {C : Set lll} -> |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
58 (f : A -> B) → (g : B -> C) → (x : Identity A) → identity-fmap (g ∙ f) x ≡ identity-fmap g (identity-fmap f x) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
59 identity-covariant f g (identity x) = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
60 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
61 identity-is-functor : {l : Level} -> Functor Identity |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
62 identity-is-functor {l} = record { fmap = identity-fmap {l}; |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
63 preserve-id = identity-preserve-id ; |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
64 covariant = identity-covariant } |
6 | 65 |
66 | |
67 | |
68 | |
7
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
69 record NaturalTransformation {l ll : Level} (F G : Set l -> Set (suc l)) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
70 (functorF : Functor F) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
71 (functorG : Functor G) : Set (suc (l ⊔ ll)) where |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
72 field |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
73 natural-transformation : {A : Set l} -> F A -> G A |
6 | 74 field |
7
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
75 commute : ∀ {A B} -> (function : A -> B) -> (x : F A) -> |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
76 natural-transformation (Functor.fmap functorF function x) ≡ Functor.fmap functorG function (natural-transformation x) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
77 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
78 tail : {l : Level} {A : Set l} -> List A -> List A |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
79 tail nil = nil |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
80 tail (cons _ xs) = xs |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
81 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
82 tail-commute : {l ll : Level} {A : Set l} {B : Set ll} -> (f : A -> B) -> (xs : List A) -> |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
83 tail (list-fmap f xs) ≡ list-fmap f (tail xs) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
84 tail-commute f nil = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
85 tail-commute f (cons x xs) = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
86 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
87 tail-is-natural-transformation : {l ll : Level} -> NaturalTransformation {l} {ll} List List list-is-functor list-is-functor |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
88 tail-is-natural-transformation = record { natural-transformation = tail; |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
89 commute = tail-commute} |