Setoid.agda 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. {-# OPTIONS --no-positivity-check
  2. --no-termination-check
  3. #-}
  4. -- A universe setoids
  5. module Setoid where
  6. import Chain
  7. record True : Set where
  8. data False : Set where
  9. Rel : Set -> Set1
  10. Rel A = A -> A -> Set
  11. Pred : Set -> Set1
  12. Pred A = A -> Set
  13. Resp : {A : Set} -> Rel A -> {B : Set} -> Rel B -> Pred (A -> B)
  14. Resp _R_ _S_ f = forall x y -> x R y -> f x S f y
  15. _∋_ : (A : Set) → A → A
  16. A ∋ x = x
  17. mutual
  18. infixr 10 _,_
  19. infix 40 _=El=_ _=S=_ _=Fam=_
  20. infix 60 _!_
  21. data Setoid : Set where
  22. nat : Setoid
  23. Π : (A : Setoid)(F : Fam A) -> Setoid
  24. Σ : (A : Setoid)(F : Fam A) -> Setoid
  25. data Fam (A : Setoid) : Set where
  26. fam : (F : El A -> Setoid) ->
  27. Resp _=El=_ _=S=_ F -> Fam A
  28. data El : Setoid -> Set where
  29. zero : El nat
  30. suc : El nat -> El nat
  31. ƛ : {A : Setoid}{F : Fam A}
  32. (f : (x : El A) -> El (F ! x)) ->
  33. ((x y : El A) -> x =El= y -> f x =El= f y) -> El (Π A F)
  34. _,_ : {A : Setoid}{F : Fam A}(x : El A) -> El (F ! x) -> El (Σ A F)
  35. data _=S=_ : (A B : Setoid) -> Set where
  36. eqNat : nat =S= nat
  37. eqΠ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂} ->
  38. A₂ =S= A₁ -> F₁ =Fam= F₂ -> Π A₁ F₁ =S= Π A₂ F₂
  39. eqΣ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂} ->
  40. A₁ =S= A₂ -> F₁ =Fam= F₂ -> Σ A₁ F₁ =S= Σ A₂ F₂
  41. data _=El=_ : {A B : Setoid} -> El A -> El B -> Set where
  42. eqInNat : {n : El nat} -> n =El= n
  43. eqInΠ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂}
  44. {f₁ : (x : El A₁) -> El (F₁ ! x)}
  45. {pf₁ : (x y : El A₁) -> x =El= y -> f₁ x =El= f₁ y}
  46. {f₂ : (x : El A₂) -> El (F₂ ! x)} ->
  47. {pf₂ : (x y : El A₂) -> x =El= y -> f₂ x =El= f₂ y} ->
  48. A₂ =S= A₁ ->
  49. ((x : El A₁)(y : El A₂) -> x =El= y -> f₁ x =El= f₂ y) ->
  50. (El (Π A₁ F₁) ∋ ƛ f₁ pf₁) =El= (El (Π A₂ F₂) ∋ ƛ f₂ pf₂)
  51. eqInΣ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂}
  52. {x₁ : El A₁}{y₁ : El (F₁ ! x₁)}
  53. {x₂ : El A₂}{y₂ : El (F₂ ! x₂)} ->
  54. F₁ =Fam= F₂ -> x₁ =El= x₂ -> y₁ =El= y₂ -> (El (Σ A₁ F₁) ∋ (x₁ , y₁ )) =El= (El (Σ A₂ F₂) ∋ (x₂ , y₂))
  55. data _=Fam=_ {A B : Setoid}(F : Fam A)(G : Fam B) : Set where
  56. eqFam : B =S= A -> (forall x y -> x =El= y -> F ! x =S= G ! y) -> F =Fam= G
  57. _!_ : {A : Setoid} -> Fam A -> El A -> Setoid
  58. fam F _ ! x = F x
  59. -- Inversions
  60. famEqDom : {A B : Setoid}{F : Fam A}{G : Fam B} -> F =Fam= G -> B =S= A
  61. famEqDom (eqFam p _) = p
  62. famEqCodom : {A B : Setoid}{F : Fam A}{G : Fam B} -> F =Fam= G ->
  63. (x : El A)(y : El B) -> x =El= y -> F ! x =S= G ! y
  64. famEqCodom (eqFam _ p) = p
  65. eqΠ-inv₁ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂} ->
  66. Π A₁ F₁ =S= Π A₂ F₂ -> A₂ =S= A₁
  67. eqΠ-inv₁ (eqΠ p _) = p
  68. eqΠ-inv₂ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂} ->
  69. Π A₁ F₁ =S= Π A₂ F₂ -> F₁ =Fam= F₂
  70. eqΠ-inv₂ (eqΠ _ p) = p
  71. eqΣ-inv₁ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂} ->
  72. Σ A₁ F₁ =S= Σ A₂ F₂ -> A₁ =S= A₂
  73. eqΣ-inv₁ (eqΣ p _) = p
  74. eqΣ-inv₂ : {A₁ A₂ : Setoid}{F₁ : Fam A₁}{F₂ : Fam A₂} ->
  75. Σ A₁ F₁ =S= Σ A₂ F₂ -> F₁ =Fam= F₂
  76. eqΣ-inv₂ (eqΣ _ p) = p
  77. -- Equivalence proofs and casts
  78. mutual
  79. -- _=Fam=_ is an equivalence
  80. =Fam=-refl : {A : Setoid}{F : Fam A} -> F =Fam= F
  81. =Fam=-refl {F = fam _ p} = eqFam =S=-refl p
  82. =Fam=-sym : {A B : Setoid}{F : Fam A}{G : Fam B} -> F =Fam= G -> G =Fam= F
  83. =Fam=-sym (eqFam B=A F=G) = eqFam (=S=-sym B=A) \x y x=y -> =S=-sym (F=G _ _ (=El=-sym x=y))
  84. =Fam=-trans : {A B C : Setoid}{F : Fam A}{G : Fam B}{H : Fam C} ->
  85. F =Fam= G -> G =Fam= H -> F =Fam= H
  86. =Fam=-trans (eqFam B=A F=G) (eqFam C=B G=H) =
  87. eqFam (=S=-trans C=B B=A) \x y x=y ->
  88. =S=-trans (F=G x (B=A << x) (cast-id _))
  89. (G=H _ y (=El=-trans (=El=-sym (cast-id _)) x=y))
  90. -- _=S=_ is an equivalence
  91. =S=-refl : {A : Setoid} -> A =S= A
  92. =S=-refl {nat} = eqNat
  93. =S=-refl {Π A F} = eqΠ =S=-refl =Fam=-refl
  94. =S=-refl {Σ A F} = eqΣ =S=-refl =Fam=-refl
  95. =S=-sym : {A B : Setoid} -> A =S= B -> B =S= A
  96. =S=-sym eqNat = eqNat
  97. =S=-sym (eqΠ pA pF) = eqΠ (=S=-sym pA) (=Fam=-sym pF)
  98. =S=-sym (eqΣ pA pF) = eqΣ (=S=-sym pA) (=Fam=-sym pF)
  99. =S=-trans : {A B C : Setoid} -> A =S= B -> B =S= C -> A =S= C
  100. =S=-trans eqNat eqNat = eqNat
  101. =S=-trans (eqΠ B=A F=G) (eqΠ C=B G=H) = eqΠ (=S=-trans C=B B=A) (=Fam=-trans F=G G=H)
  102. =S=-trans (eqΣ A=B F=G) (eqΣ B=C G=H) = eqΣ (=S=-trans A=B B=C) (=Fam=-trans F=G G=H)
  103. -- _=El=_ is an equivalence
  104. =El=-refl : {A : Setoid}{x : El A} -> x =El= x
  105. =El=-refl {nat} = eqInNat
  106. =El=-refl {Π A F}{ƛ f pf} = eqInΠ =S=-refl pf
  107. =El=-refl {Σ A F}{x , y} = eqInΣ =Fam=-refl =El=-refl =El=-refl
  108. =El=-sym : {A B : Setoid}{x : El A}{y : El B} -> x =El= y -> y =El= x
  109. =El=-sym eqInNat = eqInNat
  110. =El=-sym (eqInΠ B=A p) = eqInΠ (=S=-sym B=A) \x y x=y -> =El=-sym (p y x (=El=-sym x=y))
  111. =El=-sym (eqInΣ pF px py) = eqInΣ (=Fam=-sym pF) (=El=-sym px) (=El=-sym py)
  112. =El=-trans : {A B C : Setoid}{x : El A}{y : El B}{z : El C} ->
  113. x =El= y -> y =El= z -> x =El= z
  114. =El=-trans eqInNat eqInNat = eqInNat
  115. =El=-trans (eqInΠ B=A f=g) (eqInΠ C=B g=h) =
  116. eqInΠ (=S=-trans C=B B=A) \x y x=y ->
  117. =El=-trans (f=g x (B=A << x) (cast-id _))
  118. (g=h _ y (=El=-trans (=El=-sym (cast-id _)) x=y))
  119. =El=-trans (eqInΣ F₁=F₂ x₁=x₂ y₁=y₂) (eqInΣ F₂=F₃ x₂=x₃ y₂=y₃) =
  120. eqInΣ (=Fam=-trans F₁=F₂ F₂=F₃) (=El=-trans x₁=x₂ x₂=x₃) (=El=-trans y₁=y₂ y₂=y₃)
  121. -- Casting. Important: don't look at the proof!
  122. infix 50 _<<_ _>>_
  123. _<<_ : {A B : Setoid} -> A =S= B -> El B -> El A
  124. _<<_ {nat}{Π _ _} () _
  125. _<<_ {nat}{Σ _ _} () _
  126. _<<_ {Π _ _}{nat} () _
  127. _<<_ {Π _ _}{Σ _ _} () _
  128. _<<_ {Σ _ _}{nat} () _
  129. _<<_ {Σ _ _}{Π _ _} () _
  130. _<<_ {nat}{nat} p x = x
  131. _<<_ {Π A₁ F₁}{Π A₂ F₂} p (ƛ f pf) = ƛ g pg
  132. where
  133. g : (x : El A₁) -> El (F₁ ! x)
  134. g x = let A₂=A₁ = eqΠ-inv₁ p
  135. F₁=F₂ = famEqCodom (eqΠ-inv₂ p) x _ (cast-id _)
  136. in F₁=F₂ << f (A₂=A₁ << x)
  137. pg : (x y : El A₁) -> x =El= y -> g x =El= g y
  138. pg x y x=y = cast-irr _ _ (pf _ _ (cast-irr _ _ x=y))
  139. _<<_ {Σ A₁ F₁}{Σ A₂ F₂} p (x , y) = eqΣ-inv₁ p << x , F₁=F₂ << y
  140. where
  141. F₁=F₂ : F₁ ! (eqΣ-inv₁ p << x) =S= F₂ ! x
  142. F₁=F₂ = famEqCodom (eqΣ-inv₂ p) _ _ (=El=-sym (cast-id _))
  143. _>>_ : {A B : Setoid} -> Fam A -> A =S= B -> Fam B
  144. fam F pF >> A=B = fam G pG
  145. where
  146. G : El _ -> Setoid
  147. G y = F (A=B << y)
  148. pG : forall x y -> x =El= y -> G x =S= G y
  149. pG x y x=y = pF _ _ (cast-irr _ _ x=y)
  150. cast-id : {A B : Setoid}{x : El A}(p : B =S= A) -> x =El= p << x
  151. cast-id eqNat = eqInNat
  152. cast-id {x = x , y } (eqΣ A=B F=G) = eqInΣ (=Fam=-sym F=G) (cast-id _) (cast-id _)
  153. cast-id {x = ƛ f pf} (eqΠ B=A F=G) =
  154. eqInΠ (=S=-sym B=A) \x y x=y ->
  155. proof f x
  156. ≡ f (_ << y) by pf _ _ (=El=-trans x=y (cast-id _))
  157. ≡ _ << f (_ << y) by cast-id _
  158. qed
  159. where
  160. open Chain El _=El=_ (\x -> =El=-refl) (\x y z -> =El=-trans)
  161. cast-irr : {A₁ A₂ B₁ B₂ : Setoid}{x : El A₁}{y : El A₂}
  162. (p₁ : B₁ =S= A₁)(p₂ : B₂ =S= A₂) -> x =El= y -> p₁ << x =El= p₂ << y
  163. cast-irr {x = x}{y = y} p q x=y =
  164. proof p << x
  165. ≡ x by =El=-sym (cast-id _)
  166. ≡ y by x=y
  167. ≡ q << y by cast-id _
  168. qed
  169. where
  170. open Chain El _=El=_ (\x -> =El=-refl) (\x y z -> =El=-trans)
  171. -- Let's do some overloading
  172. data EqFam : Set -> Set where
  173. el : EqFam Setoid
  174. setoid : EqFam True
  175. fam : EqFam Setoid
  176. [_] : {I : Set} -> EqFam I -> I -> Set
  177. [ el ] A = El A
  178. [ setoid ] _ = Setoid
  179. [ fam ] A = Fam A
  180. infix 5 _==_
  181. _==_ : {I : Set}{eqf : EqFam I}{i j : I} -> [ eqf ] i -> [ eqf ] j -> Set
  182. _==_ {eqf = el } x y = x =El= y
  183. _==_ {eqf = setoid} A B = A =S= B
  184. _==_ {eqf = fam } F G = F =Fam= G
  185. refl : {I : Set}{eqf : EqFam I}{i : I}{x : [ eqf ] i} -> x == x
  186. refl {eqf = el} = =El=-refl
  187. refl {eqf = setoid} = =S=-refl
  188. refl {eqf = fam} = =Fam=-refl
  189. sym : {I : Set}{eqf : EqFam I}{i j : I}{x : [ eqf ] i}{y : [ eqf ] j} -> x == y -> y == x
  190. sym {eqf = el} = =El=-sym
  191. sym {eqf = setoid} = =S=-sym
  192. sym {eqf = fam} = =Fam=-sym
  193. trans : {I : Set}{eqf : EqFam I}{i j k : I}{x : [ eqf ] i}{y : [ eqf ] j}{z : [ eqf ] k} ->
  194. x == y -> y == z -> x == z
  195. trans {eqf = el} = =El=-trans
  196. trans {eqf = setoid} = =S=-trans
  197. trans {eqf = fam} = =Fam=-trans
  198. open module EqChain {I : Set}{eqf : EqFam I} =
  199. Chain ([ eqf ]) _==_ (\x -> refl) (\x y z -> trans)
  200. homo : {A B : Setoid}{x : El A}{y : El B} -> x == y -> A == B
  201. homo eqInNat = eqNat
  202. homo (eqInΠ B=A p) = eqΠ B=A (eqFam B=A \x y x=y -> homo (p x y x=y))
  203. homo (eqInΣ F=G p q) = eqΣ (homo p) F=G
  204. cast-id' : {A B C : Setoid}{x : El A}{y : El B}(p : C == B) -> x == y -> x == p << y
  205. cast-id' C=B x=y = trans x=y (cast-id _)
  206. -- Some helper stuff
  207. K : {A : Setoid} -> Setoid -> Fam A
  208. K B = fam (\_ -> B) (\_ _ _ -> refl)
  209. infixr 20 _==>_
  210. infixl 70 _·_ _∘_ _○_
  211. infixl 90 _#_
  212. !-cong : {A B : Setoid}(F : Fam A)(G : Fam B){x : El A}{y : El B} ->
  213. F == G -> x == y -> F ! x == G ! y
  214. !-cong F G (eqFam B=A F=G) x=y = F=G _ _ x=y
  215. !-cong-R : {A : Setoid}(F : Fam A){x y : El A} ->
  216. x == y -> F ! x == F ! y
  217. !-cong-R F x=y = !-cong F F refl x=y
  218. _==>_ : Setoid -> Setoid -> Setoid
  219. A ==> B = Π A (K B)
  220. _#_ : {A : Setoid}{F : Fam A} -> El (Π A F) -> (x : El A) -> El (F ! x)
  221. ƛ f _ # x = f x
  222. #-cong : {A B : Setoid}{F : Fam A}{G : Fam B}
  223. (f : El (Π A F))(g : El (Π B G)){x : El A}{y : El B} ->
  224. f == g -> x == y -> f # x == g # y
  225. #-cong ._ ._ (eqInΠ _ f=g) x=y = f=g _ _ x=y
  226. #-cong-R : {A : Setoid}{F : Fam A}(f : El (Π A F)){x y : El A} ->
  227. x == y -> f # x == f # y
  228. #-cong-R f p = #-cong f f refl p
  229. id : {A : Setoid} -> El (A ==> A)
  230. id = ƛ (\x -> x) (\_ _ p -> p)
  231. -- Family composition
  232. _○_ : {A B : Setoid} -> Fam A -> El (B ==> A) -> Fam B
  233. F ○ f = fam (\x -> F ! (f # x)) (\x y x=y -> !-cong-R F (#-cong-R f x=y))
  234. lem-○-id : {A : Setoid}{F : Fam A} -> F ○ id == F
  235. lem-○-id {F = F} = eqFam refl \x y x=y -> !-cong-R F x=y
  236. _∘_ : {A B : Setoid}{F : Fam B} -> El (Π B F) -> (g : El (A ==> B)) -> El (Π A (F ○ g))
  237. f ∘ g = ƛ (\x -> f # (g # x)) \x y x=y -> #-cong-R f (#-cong-R g x=y)
  238. lem-∘-id : {A : Setoid}{F : Fam A}(f : El (Π A F)) -> f ∘ id == f
  239. lem-∘-id (ƛ f pf) = eqInΠ refl pf
  240. lem-id-∘ : {A B : Setoid}(f : El (A ==> B)) -> id ∘ f == f
  241. lem-id-∘ (ƛ f pf) = eqInΠ refl pf
  242. -- Simply type composition (not quite a special case of ∘ because of proof relevance)
  243. _·_ : {A B C : Setoid} -> El (B ==> C) -> El (A ==> B) -> El (A ==> C)
  244. f · g = eqΠ refl (eqFam refl \_ _ _ -> refl) << f ∘ g
  245. fst : {A : Setoid}{F : Fam A} -> El (Σ A F) -> El A
  246. fst (x , y) = x
  247. snd : {A : Setoid}{F : Fam A}(p : El (Σ A F)) -> El (F ! fst p)
  248. snd (x , y) = y
  249. fst-eq : {A B : Setoid}{F : Fam A}{G : Fam B}
  250. {x : El (Σ A F)}{y : El (Σ B G)} -> x == y -> fst x == fst y
  251. fst-eq (eqInΣ _ x₁=x₂ _) = x₁=x₂
  252. snd-eq : {A B : Setoid}{F : Fam A}{G : Fam B}
  253. {x : El (Σ A F)}{y : El (Σ B G)} -> x == y -> snd x == snd y
  254. snd-eq (eqInΣ _ _ y₁=y₂) = y₁=y₂
  255. η : {A : Setoid}{F : Fam A}(f : El (Π A F)){pf : (x y : El A) -> x == y -> f # x == f # y} ->
  256. f == ƛ {F = F} (_#_ f) pf
  257. η (ƛ f pf) = eqInΠ refl pf