-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.idr
268 lines (201 loc) · 6.17 KB
/
Main.idr
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
module Main
import Language.Reflection
import Data.Vect
%flag C "-O3"
%flag C "-fomit-frame-pointer"
%default total
-- Mostly a translation of
-- https://github.com/HoTT/book/blob/master/coq_introduction/Reading_HoTT_in_Coq.v
-- https://github.com/tzakian/homotopy_type_theory/blob/master/C.agda
infix 4 .=
-- | Agda-style, homogeneous equality.
data (.=) : {A : Type} -> A -> A -> Type where
Hrefl : x .= x
-- These are here to allow the use of (.=) in proofs.
hetero : (a .= b) -> (a = b)
hetero Hrefl = Refl
homo : (a = b) -> (a .= b)
homo Refl = Hrefl
basedPathInd : {A : Type} ->
(a : A) ->
(C : (x : A) -> (a .= x) -> Type) ->
C a Hrefl -> ((x : A) -> (p : a .= x) -> C x p)
basedPathInd a C c .a Hrefl = c
pathInd : {A : Type} ->
(C : (x, y : A) -> (x .= y) -> Type) ->
(c : (x : A) -> C x x Hrefl) ->
(x, y : A) ->
(p : x .= y) ->
C x y p
pathInd C c x y p = basedPathInd x (C x) (c x) y p
----
Paths : Type -> Type -> Type
Paths a b = a = b
myPaths : Paths Int Int
myPaths = Refl
----
Sect : {A, B : Type} ->
(s : A -> B) ->
(r : B -> A) ->
Type
Sect {A} s r = (x : A) -> r (s x) .= x
----
-- | "Every path has an inverse".
-- ("equality is symmetric".)
inverse : x .= y -> y .= x
inverse Hrefl = Hrefl
-- inverse = sym
myInv : Int .= Int
myInv = inverse Hrefl
-- | "Paths concatenate".
-- ("Equality is transitive".)
concat : x .= y -> y .= z -> x .= z
concat Hrefl Hrefl = Hrefl
-- concat = trans
infixr 8 .-
(.-) : x .= y -> y .= z -> x .= z
(.-) Hrefl Hrefl = Hrefl
concatAssoc : (p : x .= y) -> (q : y .= z) -> (r : z .= t) ->
(p .- q) .- r .= p .- (q .- r)
concatAssoc Hrefl Hrefl Hrefl = Hrefl
inverseInvl : (p : x .= y) -> inverse p .- p .= Hrefl
inverseInvl Hrefl = Hrefl
myConcat : Int .= Int
myConcat = concat Hrefl Hrefl
concatEq : x -> x .= y -> y .= z -> x .= z
concatEq _ = concat
syntax [x] "=|" [p] "|#" [q] = concatEq x p q
--syntax [x] "=[|" [p] "|]" [q] = concatEq x p q
--syntax [x] "=|" [p] "|#" [q] = concatEq x p q
testEq : Int .= Int
testEq = 12 =| Hrefl |# Hrefl
transport : {A : Type} -> (f : A -> Type) -> x .= y -> f x -> f y
transport _ Hrefl x = x
myTransport : List Int
myTransport = transport List Hrefl [1,2,3]
----
ap : {A, B : Type} -> {x, y : A} -> (f : A -> B) -> x .= y -> f x .= f y
ap _ Hrefl = Hrefl
-- ap = cong {f}?
myAp : List Int .= List Int
myAp = ap List Hrefl
apD : {A : Type} -> {B : A -> Type} -> {x, y : A} ->
(f : (a : A) -> B a) -> (p : x .= y) ->
transport B p (f x) .= f y
apD pair Hrefl = Hrefl
Vec : Type -> Nat -> Type
Vec a n = Vect n a
myApD : transport {A = Nat} (Vec Int) Hrefl Nil .= Nil
myApD = apD {B = Vec Int} (\n => replicate n 12) Hrefl
----
inCtx : {A, B : Type} -> {x, y : A} ->
x .= y -> (f : A -> B) -> f x .= f y
inCtx p f = ap f p
-- This is hard, so I just made it an axiom...
-- TODO: Actually prove it.
postulate adj : {A, B : Type} ->
(f : A -> B) -> (g : B -> A) ->
(gf : (b : B) -> f (g b) .= b) -> -- Sect g f
(fg : (a : A) -> g (f a) .= a) -> -- Sect f g
(a : A) -> gf (f a) .= ap f (fg a)
unitR : {A : Type} -> {x, y : A} ->
(q : x .= y) -> q .- Hrefl .= q
unitR Hrefl = Hrefl
unitL : {A : Type} -> {x, y : A} ->
(q : x .= y) -> Hrefl .- q .= q
unitL Hrefl = Hrefl
antiWhiskerR : {A : Type} -> {x, y, z : A} -> {q, r : x .= y} ->
(p : y .= z) -> (q .- p .= r .- p -> q .= r)
antiWhiskerR {q=q} {r=r} Hrefl h =
let unitq = unitR q
unitr = unitR r
in ?antiWR
Main.antiWR = proof
intro
intro
intro
intro
intro
intro
rewrite (hetero h)
intros
rewrite (hetero unitr)
rewrite sym (hetero unitq)
exact Hrefl
htpyNatural : {A, B : Type} -> {x, y : A} -> {f, g : A -> B} ->
(p : (x : A) -> (f x .= g x)) -> (q : x .= y) -> ap f q .- p y .= p x .- ap g q
htpyNatural {x} p Hrefl =
let fg = p x
in ?htpyNatural_p
Main.htpyNatural_p = proof
intros
rewrite sym (hetero (unitR (p x)))
rewrite sym (hetero (unitL (p x)))
exact Hrefl
idf : (A : Type) -> (A -> A)
idf A = \x => x
apIdf : {A : Type} -> {u, v : A} ->
(p : u .= v) -> ap (idf A) p .= p
apIdf Hrefl = Hrefl
-- infix 4 ~
-- (~) : {A : Type} ->
-- {B : Type} ->
-- (f, g : A -> B) ->
-- Type
-- (~) {A} f g = (x : A) -> f x .= g x
data QInv : {A, B : Type} -> (f : A -> B) -> Type where
MkQInv : {A, B : Type} ->
(g : B -> A) ->
(a : Sect g f) ->
(b : Sect f g) ->
QInv f
idQInv : QInv id
idQInv = MkQInv id (\_ => Hrefl) (\_ => Hrefl)
----
data IsEquiv : {A, B : Type} -> (f : A -> B) -> Type where
MkIsEquiv : {A, B : Type} ->
(g : B -> A) ->
(gf : Sect g f) ->
(fg : Sect f g) ->
(adj : (a : A) -> gf (f a) .= ap f (fg a)) ->
IsEquiv f
equiv : QInv f -> IsEquiv f
equiv {f} (MkQInv g gf fg) = MkIsEquiv g gf fg (adj f g gf fg)
----
infix 4 ~=
(~=) : (A, B : Type) -> Type
A ~= B = (a : A -> B ** IsEquiv a)
ide : {A : Type} -> A ~= A
ide = (id ** equiv idQInv)
equivPath : {A, B : Type} -> A .= B -> A ~= B
equivPath {A} {B} p =
pathInd (\A', B' => const (A' ~= B'))
(\_ => ide) A B p
----
postulate univalence : (a ~= b) -> (a .= b)
postulate univalenceEquiv : (a .= b) ~= (a ~= b)
-- Doing an actual proof. We will prove that NatClone
-- is equivalent to Nat, then use the univalence axiom
-- to conclude that they are equial.
data NatClone = CZ | CS NatClone
natToClone : Nat -> NatClone
natToClone (S n) = CS (natToClone n)
natToClone Z = CZ
cloneToNat : NatClone -> Nat
cloneToNat (CS n) = S (cloneToNat n)
cloneToNat CZ = Z
leftSect : Sect cloneToNat natToClone
leftSect CZ = Hrefl
leftSect (CS n) = ap CS (leftSect n)
rightSect : Sect natToClone cloneToNat
rightSect Z = Hrefl
rightSect (S n) = ap S (rightSect n)
isEquivNatClone : IsEquiv natToClone
isEquivNatClone = MkIsEquiv cloneToNat leftSect rightSect
(adj natToClone cloneToNat leftSect rightSect)
equivNatClone : Nat ~= NatClone
equivNatClone = (natToClone ** isEquivNatClone)
equalNatClone : Nat .= NatClone
equalNatClone = univalence equivNatClone
hfiber : {A, B : Type} -> (f : A -> B) -> (y : B) -> Type
hfiber {A} f y = (x : A ** f x .= y)