Erased-cubical.agda 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. -- Possible improvements:
  2. -- * Parts of the code are not reachable from main.
  3. -- * The following primitives are not used at all: primPOr, primComp,
  4. -- primHComp, prim^glueU and prim^unglueU.
  5. {-# OPTIONS --erased-cubical --save-metas #-}
  6. -- The code from Agda.Builtin.Cubical.Glue should not be compiled.
  7. open import Agda.Builtin.Cubical.Glue
  8. open import Agda.Builtin.Cubical.HCompU
  9. open import Agda.Builtin.Cubical.Id
  10. open import Agda.Builtin.Cubical.Path
  11. open import Agda.Builtin.Cubical.Sub
  12. open import Agda.Builtin.IO
  13. open import Agda.Builtin.Nat
  14. open import Agda.Builtin.Sigma
  15. open import Agda.Builtin.String
  16. open import Agda.Builtin.Unit
  17. open import Agda.Primitive.Cubical
  18. open import Erased-cubical-Cubical
  19. postulate
  20. putStr : String → IO ⊤
  21. {-# FOREIGN GHC import qualified Data.Text.IO #-}
  22. {-# COMPILE GHC putStr = Data.Text.IO.putStr #-}
  23. {-# COMPILE JS putStr =
  24. function (x) {
  25. return function(cb) {
  26. process.stdout.write(x); cb(0); }; } #-}
  27. i₁ : I
  28. i₁ = primIMax (primIMax (primINeg i0) i1) (primIMin i1 i0)
  29. i₁-1 : IsOne i₁
  30. i₁-1 = IsOne1 (primIMax (primINeg i0) i1) (primIMin i1 i0)
  31. (IsOne2 (primINeg i0) i1 itIsOne)
  32. p₁ : Partial i1 Nat
  33. p₁ = λ _ → 12
  34. p₂ : PartialP i1 (λ _ → Nat)
  35. p₂ = λ _ → 12
  36. p₃ : PartialP i0 (λ _ → Nat)
  37. p₃ = isOneEmpty
  38. p₄ : 12 ≡ 12
  39. p₄ = λ _ → 12
  40. n₁ : Nat
  41. n₁ = p₄ i0
  42. s : Sub Nat i1 (λ _ → 13)
  43. s = inc 13
  44. n₂ : Nat
  45. n₂ = primSubOut s
  46. i₂ : I
  47. i₂ = primFaceForall (λ _ → i1)
  48. i₃ : Id 12 12
  49. i₃ = conid i0 p₄
  50. i₄ : I
  51. i₄ = primDepIMin i1 (λ _ → i0)
  52. i₅ : I
  53. i₅ = primIdFace i₃
  54. p₅ : 12 ≡ 12
  55. p₅ = primIdPath i₃
  56. n₃ : Nat
  57. n₃ = primIdJ (λ _ _ → Nat) 14 i₃
  58. n₄ : Nat
  59. n₄ = primIdElim (λ _ _ → Nat) (λ _ _ _ → 14) i₃
  60. infix 2 _⊎_
  61. data _⊎_ (A B : Set) : Set where
  62. inj₁ : A → A ⊎ B
  63. inj₂ : B → A ⊎ B
  64. data ⊥ : Set where
  65. ⊥-elim : {A : Set} → ⊥ → A
  66. ⊥-elim ()
  67. -- Some "standard" path functions.
  68. refl : {A : Set} (x : A) → x ≡ x
  69. refl x = λ _ → x
  70. sym : {A : Set} {x y : A} → x ≡ y → y ≡ x
  71. sym p i = p (primINeg i)
  72. cong : {A B : Set} {x y : A} (f : A → B) → x ≡ y → f x ≡ f y
  73. cong f p i = f (p i)
  74. J :
  75. {A : Set} {x y : A}
  76. (P : (x y : A) → x ≡ y → Set) →
  77. (∀ x → P x x (refl x)) →
  78. (x≡y : x ≡ y) → P x y x≡y
  79. J {x = x} P p x≡y =
  80. primTransp (λ i → P _ _ (λ j → x≡y (primIMin i j))) i0 (p x)
  81. subst :
  82. {A : Set} {x y : A}
  83. (P : A → Set) → x ≡ y → P x → P y
  84. subst P eq p = primTransp (λ i → P (eq i)) i0 p
  85. subst-refl :
  86. {A : Set} {x : A}
  87. (P : A → Set) {p : P x} →
  88. subst P (refl x) p ≡ p
  89. subst-refl {x = x} P {p = p} i =
  90. primTransp (λ _ → P x) i p
  91. -- The following definitions are perhaps less standard when paths are
  92. -- used.
  93. trans : {A : Set} {x y z : A} → x ≡ y → y ≡ z → x ≡ z
  94. trans x≡y y≡z = subst (_ ≡_) y≡z x≡y
  95. trans-refl-refl :
  96. {A : Set}
  97. (x : A) → trans (refl x) (refl x) ≡ refl x
  98. trans-refl-refl x = subst-refl (x ≡_)
  99. trans-sym :
  100. {A : Set} {x y : A} (x≡y : x ≡ y) →
  101. trans x≡y (sym x≡y) ≡ refl x
  102. trans-sym =
  103. J (λ x y x≡y → trans x≡y (sym x≡y) ≡ refl x)
  104. trans-refl-refl
  105. -- Propositions and sets.
  106. Is-proposition : Set → Set
  107. Is-proposition A = (x y : A) → x ≡ y
  108. Is-set : Set → Set
  109. Is-set A = (x y : A) → Is-proposition (x ≡ y)
  110. -- Following Hedberg's "A coherence theorem for Martin-Löf's type
  111. -- theory".
  112. decidable-equality→is-set :
  113. {A : Set} →
  114. ((x y : A) → x ≡ y ⊎ (x ≡ y → ⊥)) →
  115. Is-set A
  116. decidable-equality→is-set dec =
  117. constant⇒set (λ x y → decidable⇒constant (dec x y))
  118. where
  119. Constant : {A B : Set} → (A → B) → Set
  120. Constant f = ∀ x y → f x ≡ f y
  121. _Left-inverse-of_ : {A B : Set} → (B → A) → (A → B) → Set
  122. g Left-inverse-of f = ∀ x → g (f x) ≡ x
  123. proposition : {A : Set} →
  124. (f : Σ (A → A) Constant) →
  125. Σ _ (_Left-inverse-of (fst f)) →
  126. Is-proposition A
  127. proposition (f , constant) (g , left-inverse) x y =
  128. trans (sym (left-inverse x))
  129. (trans (cong g (constant x y)) (left-inverse y))
  130. left-inverse :
  131. {A : Set}
  132. (f : (x y : A) → x ≡ y → x ≡ y) →
  133. ∀ {x y} → Σ _ (_Left-inverse-of (f x y))
  134. left-inverse {A = A} f {x = x} {y = y} =
  135. (λ x≡y → trans x≡y (sym (f y y (refl y))))
  136. , J (λ x y x≡y → trans (f x y x≡y) (sym (f y y (refl y))) ≡ x≡y)
  137. (λ _ → trans-sym _)
  138. constant⇒set :
  139. {A : Set} →
  140. ((x y : A) → Σ (x ≡ y → x ≡ y) Constant) →
  141. Is-set A
  142. constant⇒set constant x y =
  143. proposition (constant x y)
  144. (left-inverse λ x y → fst (constant x y))
  145. decidable⇒constant :
  146. {A : Set} →
  147. A ⊎ (A → ⊥) →
  148. Σ (A → A) Constant
  149. decidable⇒constant (inj₁ x) =
  150. (λ _ → x) , (λ _ _ → refl x)
  151. decidable⇒constant (inj₂ ¬A) =
  152. (λ x → x) , (λ x → ⊥-elim (¬A x))
  153. if_then_else_ : {A : Set₁} → Bool → A → A → A
  154. if true then x else y = x
  155. if false then x else y = y
  156. Bool-set : Is-set Bool
  157. Bool-set = decidable-equality→is-set λ where
  158. false false → inj₁ λ _ → false
  159. true true → inj₁ λ _ → true
  160. false true → inj₂ λ eq →
  161. primTransp (λ i → if eq i then ⊥ else ⊤) i0 _
  162. true false → inj₂ λ eq →
  163. primTransp (λ i → if eq i then ⊤ else ⊥) i0 _
  164. data ∥_∥ᴱ (A : Set) : Set where
  165. ∣_∣ : A → ∥ A ∥ᴱ
  166. @0 trivial : Is-proposition ∥ A ∥ᴱ
  167. recᴱ : {A B : Set} → @0 Is-proposition B → (A → B) → ∥ A ∥ᴱ → B
  168. recᴱ p f ∣ x ∣ = f x
  169. recᴱ p f (trivial x y i) = p (recᴱ p f x) (recᴱ p f y) i
  170. -- Following Vezzosi, Mörtberg and Abel's "Cubical Agda: A Dependently
  171. -- Typed Programming Language with Univalence and Higher Inductive
  172. -- Types".
  173. data _/_ (A : Set) (R : A → A → Set) : Set where
  174. [_] : A → A / R
  175. []-respects-relation : (x y : A) → R x y → [ x ] ≡ [ y ]
  176. is-set : Is-set (A / R)
  177. rec :
  178. {A B : Set} {R : A → A → Set} →
  179. Is-set B →
  180. (f : A → B) →
  181. ((x y : A) → R x y → f x ≡ f y) →
  182. A / R → B
  183. rec s f g [ x ] = f x
  184. rec s f g ([]-respects-relation x y r i) = g x y r i
  185. rec s f g (is-set x y p q i j) =
  186. s (rec s f g x) (rec s f g y)
  187. (λ k → rec s f g (p k)) (λ k → rec s f g (q k)) i j
  188. const-true : I → Bool
  189. const-true i =
  190. rec
  191. {R = _≡_}
  192. Bool-set
  193. (λ x → x)
  194. (λ _ _ x≡y → x≡y)
  195. (is-set
  196. _ _
  197. ([]-respects-relation true true (refl true))
  198. (refl _)
  199. i i)
  200. main : IO ⊤
  201. main =
  202. putStr
  203. (recᴱ
  204. easy
  205. (λ where
  206. true → "Success\n"
  207. false → "Failure\n")
  208. ∣ const-true i0 ∣)
  209. -- It should be possible to mention things that are not compiled in
  210. -- type signatures.
  211. u₁ : Not-compiled → ⊤
  212. u₁ _ = tt
  213. @0 A : Set₁
  214. A = Set
  215. u₂ : A → ⊤
  216. u₂ _ = tt