@@ -14,19 +14,41 @@ section lemmas
14
14
15
15
open function
16
16
17
- variables {f : Type u → Type v}
18
- variables [applicative f ] [is_lawful_applicative f ]
17
+ variables {F : Type u → Type v}
18
+ variables [applicative F ] [is_lawful_applicative F ]
19
19
variables {α β γ σ : Type u}
20
20
21
21
attribute [functor_norm] seq_assoc pure_seq_eq_map map_pure seq_map_assoc map_seq
22
22
23
- lemma applicative.map_seq_map (g : α → β → γ) (h : σ → β) (x : f α) (y : f σ) :
24
- (g <$> x) <*> (h <$> y) = (flip (∘) h ∘ g ) <$> x <*> y :=
23
+ lemma applicative.map_seq_map (f : α → β → γ) (g : σ → β) (x : F α) (y : F σ) :
24
+ (f <$> x) <*> (g <$> y) = (flip (∘) g ∘ f ) <$> x <*> y :=
25
25
by simp [flip] with functor_norm
26
26
27
- lemma applicative.pure_seq_eq_map' (g : α → β) :
28
- (<*>) (pure g : f (α → β)) = (<$>) g :=
29
- by funext; simp with functor_norm
27
+ lemma applicative.pure_seq_eq_map' (f : α → β) :
28
+ (<*>) (pure f : F (α → β)) = (<$>) f :=
29
+ by ext; simp with functor_norm
30
+
31
+ theorem applicative.ext {F} : ∀ {A1 : applicative F} {A2 : applicative F}
32
+ [@is_lawful_applicative F A1] [@is_lawful_applicative F A2]
33
+ (H1 : ∀ {α : Type u} (x : α),
34
+ @has_pure.pure _ A1.to_has_pure _ x = @has_pure.pure _ A2.to_has_pure _ x)
35
+ (H2 : ∀ {α β : Type u} (f : F (α → β)) (x : F α),
36
+ @has_seq.seq _ A1.to_has_seq _ _ f x = @has_seq.seq _ A2.to_has_seq _ _ f x),
37
+ A1 = A2
38
+ | {to_functor := F1, seq := s1, pure := p1, seq_left := sl1, seq_right := sr1}
39
+ {to_functor := F2, seq := s2, pure := p2, seq_left := sl2, seq_right := sr2} L1 L2 H1 H2 :=
40
+ begin
41
+ have : @p1 = @p2, {funext α x, apply H1}, subst this ,
42
+ have : @s1 = @s2, {funext α β f x, apply H2}, subst this ,
43
+ cases L1, cases L2,
44
+ have : F1 = F2,
45
+ { resetI, apply functor.ext, intros,
46
+ exact (L1_pure_seq_eq_map _ _).symm.trans (L2_pure_seq_eq_map _ _) },
47
+ subst this ,
48
+ congr; funext α β x y,
49
+ { exact (L1_seq_left_eq _ _).trans (L2_seq_left_eq _ _).symm },
50
+ { exact (L1_seq_right_eq _ _).trans (L2_seq_right_eq _ _).symm }
51
+ end
30
52
31
53
end lemmas
32
54
@@ -35,63 +57,64 @@ namespace comp
35
57
open function (hiding comp)
36
58
open functor
37
59
38
- variables {f : Type u → Type w} {g : Type v → Type u}
60
+ variables {F : Type u → Type w} {G : Type v → Type u}
39
61
40
- variables [applicative f ] [applicative g ]
62
+ variables [applicative F ] [applicative G ]
41
63
42
- protected def seq {α β : Type v} : comp f g (α → β) → comp f g α → comp f g β
43
- | ⟨h⟩ ⟨x⟩ := ⟨has_seq.seq < $> h <*> x⟩
64
+ protected def seq {α β : Type v} : comp F G (α → β) → comp F G α → comp F G β
65
+ | (comp.mk f) (comp.mk x) := comp.mk $ (<*>) < $> f <*> x
44
66
45
- instance : has_pure (comp f g ) :=
46
- ⟨λ _ x, ⟨ pure $ pure x⟩ ⟩
67
+ instance : has_pure (comp F G ) :=
68
+ ⟨λ _ x, comp.mk $ pure $ pure x⟩
47
69
48
- instance : has_seq (comp f g ) :=
70
+ instance : has_seq (comp F G ) :=
49
71
⟨λ _ _ f x, comp.seq f x⟩
50
72
51
- @[simp]
52
- protected lemma run_pure {α : Type v} :
53
- ∀ x : α, (pure x : comp f g α).run = pure (pure x)
73
+ @[simp] protected lemma run_pure {α : Type v} :
74
+ ∀ x : α, (pure x : comp F G α).run = pure (pure x)
54
75
| _ := rfl
55
76
56
- @[simp]
57
- protected lemma run_seq {α β : Type v} :
58
- ∀ (h : comp f g (α → β)) (x : comp f g α),
59
- (h <*> x).run = (<*>) <$> h.run <*> x.run
60
- | ⟨_⟩ ⟨_⟩ := rfl
77
+ @[simp] protected lemma run_seq {α β : Type v} (f : comp F G (α → β)) (x : comp F G α) :
78
+ (f <*> x).run = (<*>) <$> f.run <*> x.run := rfl
61
79
62
- variables [is_lawful_applicative f] [is_lawful_applicative g]
80
+ instance : applicative (comp F G) :=
81
+ { map := @comp.map F G _ _,
82
+ seq := @comp.seq F G _ _,
83
+ ..comp.has_pure }
84
+
85
+ variables [is_lawful_applicative F] [is_lawful_applicative G]
63
86
variables {α β γ : Type v}
64
87
65
- lemma map_pure (h : α → β) (x : α) : (h <$> pure x : comp f g β) = pure (h x) :=
66
- by ext; simp
88
+ lemma map_pure (f : α → β) (x : α) : (f <$> pure x : comp F G β) = pure (f x) :=
89
+ comp. ext $ by simp
67
90
68
- lemma seq_pure (h : comp f g (α → β)) (x : α) :
69
- h <*> pure x = (λ g : α → β, g x) <$> h :=
70
- by ext; simp [(∘)] with functor_norm
91
+ lemma seq_pure (f : comp F G (α → β)) (x : α) :
92
+ f <*> pure x = (λ g : α → β, g x) <$> f :=
93
+ comp. ext $ by simp [(∘)] with functor_norm
71
94
72
- lemma seq_assoc (x : comp f g α) (h₀ : comp f g (α → β)) (h₁ : comp f g (β → γ)) :
73
- h₁ <*> (h₀ <*> x) = (@function.comp α β γ <$> h₁ ) <*> h₀ <*> x :=
74
- by ext; simp [(∘)] with functor_norm
95
+ lemma seq_assoc (x : comp F G α) (f : comp F G (α → β)) (g : comp F G (β → γ)) :
96
+ g <*> (f <*> x) = (@function.comp α β γ <$> g ) <*> f <*> x :=
97
+ comp. ext $ by simp [(∘)] with functor_norm
75
98
76
- lemma pure_seq_eq_map (h : α → β) (x : comp f g α) :
77
- pure h <*> x = h <$> x :=
78
- by ext; simp [applicative.pure_seq_eq_map'] with functor_norm
99
+ lemma pure_seq_eq_map (f : α → β) (x : comp F G α) :
100
+ pure f <*> x = f <$> x :=
101
+ comp. ext $ by simp [applicative.pure_seq_eq_map'] with functor_norm
79
102
80
- instance {f : Type u → Type w} {g : Type v → Type u}
81
- [applicative f] [applicative g] :
82
- applicative (comp f g) :=
83
- { map := @comp.map f g _ _,
84
- seq := @comp.seq f g _ _,
85
- ..comp.has_pure }
103
+ instance : is_lawful_applicative (comp F G) :=
104
+ { pure_seq_eq_map := @comp.pure_seq_eq_map F G _ _ _ _,
105
+ map_pure := @comp.map_pure F G _ _ _ _,
106
+ seq_pure := @comp.seq_pure F G _ _ _ _,
107
+ seq_assoc := @comp.seq_assoc F G _ _ _ _ }
86
108
87
- instance {f : Type u → Type w} {g : Type v → Type u}
88
- [applicative f] [applicative g]
89
- [is_lawful_applicative f] [is_lawful_applicative g] :
90
- is_lawful_applicative (comp f g) :=
91
- { pure_seq_eq_map := @comp.pure_seq_eq_map f g _ _ _ _,
92
- map_pure := @comp.map_pure f g _ _ _ _,
93
- seq_pure := @comp.seq_pure f g _ _ _ _,
94
- seq_assoc := @comp.seq_assoc f g _ _ _ _ }
109
+ theorem applicative_id_comp {F} [AF : applicative F] [LF : is_lawful_applicative F] :
110
+ @comp.applicative id F _ _ = AF :=
111
+ @applicative.ext F _ _ (@comp.is_lawful_applicative id F _ _ _ _) _
112
+ (λ α x, rfl) (λ α β f x, rfl)
113
+
114
+ theorem applicative_comp_id {F} [AF : applicative F] [LF : is_lawful_applicative F] :
115
+ @comp.applicative F id _ _ = AF :=
116
+ @applicative.ext F _ _ (@comp.is_lawful_applicative F id _ _ _ _) _
117
+ (λ α x, rfl) (λ α β f x, show id <$> f <*> x = f <*> x, by rw id_map)
95
118
96
119
end comp
97
120
open functor
0 commit comments