Mercurial > hg > Members > atton > agda-proofs
annotate sandbox/FunctorExample.agda @ 8:a3509dbb9e49
Example for implicit-level functor
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 18 Jan 2015 20:06:33 +0900 |
parents | c11c259916b7 |
children | 4a0841123810 |
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 | |
8
a3509dbb9e49
Example for implicit-level functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
7
diff
changeset
|
47 fmap-to-nest-list : {l ll : Level} {A : Set l} {B : Set l} {fl : {l' : Level} -> Functor {l'} List} |
a3509dbb9e49
Example for implicit-level functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
7
diff
changeset
|
48 -> (A -> B) -> (List (List A)) -> (List (List B)) |
a3509dbb9e49
Example for implicit-level functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
7
diff
changeset
|
49 fmap-to-nest-list {_} {_} {_} {_} {fl} f xs = Functor.fmap fl (Functor.fmap fl f) xs |
7
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 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
|
52 identity : A -> Identity 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-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
|
55 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
|
56 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
57 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
|
58 identity-preserve-id (identity x) = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
59 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
60 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
|
61 (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
|
62 identity-covariant f g (identity x) = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
63 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
64 identity-is-functor : {l : Level} -> Functor Identity |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
65 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
|
66 preserve-id = identity-preserve-id ; |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
67 covariant = identity-covariant } |
6 | 68 |
69 | |
70 | |
71 | |
7
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
72 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
|
73 (functorF : Functor F) |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
74 (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
|
75 field |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
76 natural-transformation : {A : Set l} -> F A -> G A |
6 | 77 field |
7
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
78 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
|
79 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
|
80 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
81 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
|
82 tail nil = nil |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
83 tail (cons _ xs) = xs |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
84 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
85 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
|
86 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
|
87 tail-commute f nil = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
88 tail-commute f (cons x xs) = refl |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
89 |
c11c259916b7
Example for natural transformation
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
6
diff
changeset
|
90 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
|
91 tail-is-natural-transformation = record { natural-transformation = tail; |
8
a3509dbb9e49
Example for implicit-level functor
Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
parents:
7
diff
changeset
|
92 commute = tail-commute} |