13-implicitProofObligations.agda 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. {-# OPTIONS --universe-polymorphism #-}
  2. module InstanceArguments.13-implicitProofObligations where
  3. module Imports where
  4. module L where
  5. open import Agda.Primitive public
  6. using (Level; _⊔_) renaming (lzero to zero; lsuc to suc)
  7. -- extract from Data.Unit
  8. record ⊤ : Set where
  9. constructor tt
  10. -- extract from Data.Empty
  11. data ⊥ : Set where
  12. ⊥-elim : ∀ {w} {Whatever : Set w} → ⊥ → Whatever
  13. ⊥-elim ()
  14. -- extract from Function
  15. id : ∀ {a} {A : Set a} → A → A
  16. id x = x
  17. _$_ : ∀ {a b} {A : Set a} {B : A → Set b} →
  18. ((x : A) → B x) → ((x : A) → B x)
  19. f $ x = f x
  20. _∘_ : ∀ {a b c}
  21. {A : Set a} {B : A → Set b} {C : {x : A} → B x → Set c} →
  22. (∀ {x} (y : B x) → C y) → (g : (x : A) → B x) →
  23. ((x : A) → C (g x))
  24. f ∘ g = λ x → f (g x)
  25. -- extract from Data.Bool
  26. data Bool : Set where
  27. true : Bool
  28. false : Bool
  29. not : Bool → Bool
  30. not true = false
  31. not false = true
  32. T : Bool → Set
  33. T true = ⊤
  34. T false = ⊥
  35. -- extract from Relation.Nullary.Decidable and friends
  36. infix 3 ¬_
  37. ¬_ : ∀ {ℓ} → Set ℓ → Set ℓ
  38. ¬ P = P → ⊥
  39. data Dec {p} (P : Set p) : Set p where
  40. yes : ( p : P) → Dec P
  41. no : (¬p : ¬ P) → Dec P
  42. ⌊_⌋ : ∀ {p} {P : Set p} → Dec P → Bool
  43. ⌊ yes _ ⌋ = true
  44. ⌊ no _ ⌋ = false
  45. False : ∀ {p} {P : Set p} → Dec P → Set
  46. False Q = T (not ⌊ Q ⌋)
  47. -- extract from Relation.Binary.PropositionalEquality
  48. data _≡_ {a} {A : Set a} (x : A) : A → Set a where
  49. refl : x ≡ x
  50. cong : ∀ {a b} {A : Set a} {B : Set b}
  51. (f : A → B) {x y} → x ≡ y → f x ≡ f y
  52. cong f refl = refl
  53. _≢′_ : ∀ {a} {A : Set a} → A → A → Set a
  54. x ≢′ y = ¬ (x ≡ y)
  55. -- wrapper to be able to use instance search
  56. data _≢_ {a} {A : Set a} (x y : A) : Set a where
  57. not-equal : x ≢′ y → x ≢ y
  58. -- extract from Data.Nat
  59. data ℕ : Set where
  60. zero : ℕ
  61. suc : (n : ℕ) → ℕ
  62. {-# BUILTIN NATURAL ℕ #-}
  63. pred : ℕ → ℕ
  64. pred zero = zero
  65. pred (suc n) = n
  66. infixl 6 _+_
  67. _+_ : ℕ → ℕ → ℕ
  68. -- zero + n = n
  69. -- suc m + n = suc (m + n)
  70. zero + n = n
  71. suc m + n = suc (m + n)
  72. _*_ : ℕ → ℕ → ℕ
  73. zero * n = zero
  74. suc m * n = n + (m * n)
  75. _≟_ : (x y : ℕ) → Dec (x ≡ y)
  76. zero ≟ zero = yes refl
  77. suc m ≟ suc n with m ≟ n
  78. suc m ≟ suc .m | yes refl = yes refl
  79. suc m ≟ suc n | no prf = no (prf ∘ cong pred)
  80. zero ≟ suc n = no λ()
  81. suc m ≟ zero = no λ()
  82. -- extract from Data.Fin
  83. data Fin : ℕ → Set where
  84. zero : {n : ℕ} → Fin (suc n)
  85. suc : {n : ℕ} (i : Fin n) → Fin (suc n)
  86. -- A conversion: toℕ "n" = n.
  87. toℕ : ∀ {n} → Fin n → ℕ
  88. toℕ zero = 0
  89. toℕ (suc i) = suc (toℕ i)
  90. -- extract from Data.Product
  91. record Σ {a b} (A : Set a) (B : A → Set b) : Set (a L.⊔ b) where
  92. constructor _,_
  93. field
  94. proj₁ : A
  95. proj₂ : B proj₁
  96. _×_ : ∀ {a b} (A : Set a) (B : Set b) → Set (a L.⊔ b)
  97. A × B = Σ A (λ (_ : A) → B)
  98. -- extract from Data.Nat.DivMod
  99. data DivMod : ℕ → ℕ → Set where
  100. result : {divisor : ℕ} (q : ℕ) (r : Fin divisor) →
  101. DivMod (toℕ r + (q * divisor)) divisor
  102. data DivMod' (dividend divisor : ℕ) : Set where
  103. result : (q : ℕ) (r : Fin divisor)
  104. (eq : dividend ≡ (toℕ r + (q * divisor))) →
  105. DivMod' dividend divisor
  106. postulate
  107. _div_ : (dividend divisor : ℕ) {≢0 : False (divisor ≟ 0)} → ℕ
  108. _divMod_ : (dividend divisor : ℕ) {≢0 : False (divisor ≟ 0)} →
  109. DivMod dividend divisor
  110. _divMod'_ : (dividend divisor : ℕ) {≢0 : False (divisor ≟ 0)} →
  111. DivMod' dividend divisor
  112. open Imports
  113. -- Begin of actual example!
  114. postulate
  115. d : ℕ
  116. d≢0 : d ≢ 0
  117. -- d≢0' : d ≢ 0
  118. fromWitnessFalse : ∀ {p} {P : Set p} {Q : Dec P} → ¬ P → False Q
  119. fromWitnessFalse {Q = yes p} ¬P = ⊥-elim $ ¬P p
  120. fromWitnessFalse {Q = no ¬p} ¬P = tt
  121. ⋯ : {A : Set} → {{v : A}} → A
  122. ⋯ {{v}} = v
  123. _divMod′_ : (dividend divisor : ℕ) {{ ≢0 : divisor ≢ 0 }} → ℕ × ℕ
  124. _divMod′_ a d {{not-equal p}} with _divMod_ a d { fromWitnessFalse p }
  125. _divMod′_ ._ d {{not-equal p}} | (result q r) = q , toℕ r
  126. _div′_ : (dividend divisor : ℕ) {{ ≢0 : divisor ≢ 0 }} → ℕ
  127. _div′_ a b {{_}} with a divMod′ b
  128. a div′ b | (q , _) = q
  129. --Agda can't resolve hiddens
  130. -- test : {d≢0 : False (d ≟ 0)} → ℕ
  131. -- test = 5 div d
  132. -- test2 : {{d≢0 : d ≢ 0}} → ℕ
  133. -- test2 = 5 div′ d
  134. test3 = 5 div 2
  135. test4 = 5 div′ 2
  136. where
  137. instance
  138. nz : 2 ≢ 0
  139. nz = not-equal λ()