Issue924.agda 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. -- Andreas, 2013-10-24 Bug reported to me by Christoph-Simon Senjak
  2. module Issue924 where
  3. open import Level
  4. open import Data.Empty
  5. open import Data.Product
  6. open import Data.Sum
  7. open import Data.Bool
  8. open import Data.Maybe.Base
  9. import Data.Nat as ℕ
  10. open import Data.Nat using (ℕ)
  11. import Data.Nat.Divisibility as ℕ
  12. import Data.Nat.Properties as ℕ
  13. open import Data.Nat.DivMod
  14. import Data.Fin as F
  15. open import Data.Fin using (Fin)
  16. import Data.Fin.Properties as F
  17. import Data.List as L
  18. open import Data.List using (List)
  19. open import Data.Vec
  20. open import Relation.Nullary
  21. open import Relation.Binary.PropositionalEquality
  22. open ≡-Reasoning
  23. open import Algebra
  24. open import Algebra.Structures
  25. Byte : Set
  26. Byte = Fin 256
  27. infixr 8 _^_
  28. _^_ : ℕ → ℕ → ℕ
  29. _ ^ 0 = 1
  30. n ^ (ℕ.suc m) = n ℕ.* (n ^ m)
  31. BitVecToNat : {n : ℕ} → Vec Bool n → ℕ
  32. BitVecToNat [] = 0
  33. BitVecToNat (true ∷ r) = ℕ.suc (2 ℕ.* (BitVecToNat r))
  34. BitVecToNat (false ∷ r) = (2 ℕ.* (BitVecToNat r))
  35. postulate ≤lem₁ : {a b : ℕ} → (a ℕ.< b) → (2 ℕ.* a) ℕ.< (2 ℕ.* b)
  36. postulate ≤lem₂ : {a b : ℕ} → (a ℕ.< (ℕ.suc b)) → (ℕ.suc (2 ℕ.* a)) ℕ.< (2 ℕ.* (ℕ.suc b))
  37. postulate ≤lem₃ : {a b c : ℕ} → (c ℕ.+ (a ℕ.* 2) ℕ.< (2 ℕ.* b)) → a ℕ.< b
  38. ¬0<b∧0≡b : {b : ℕ} → 0 ℕ.< b → 0 ≡ b → ⊥
  39. ¬0<b∧0≡b {0} () 0≡b
  40. ¬0<b∧0≡b {ℕ.suc n} 0<b ()
  41. 0<a∧0<b→0<ab : (a b : ℕ) → (0 ℕ.< a) → (0 ℕ.< b) → (0 ℕ.< (a ℕ.* b))
  42. 0<a∧0<b→0<ab a b 0<a 0<b with 1 ℕ.≤? (a ℕ.* b)
  43. ... | yes y = y
  44. ... | no n with (a ℕ.* b) | inspect (λ x → a ℕ.* x) b
  45. ... | ℕ.suc ns | _ = ⊥-elim (n (ℕ.s≤s (ℕ.z≤n {ns})))
  46. ... | 0 | [ eq ] with ℕ.i*j≡0⇒i≡0∨j≡0 a {b} eq
  47. ... | inj₁ a≡0 = ⊥-elim (¬0<b∧0≡b 0<a (sym a≡0))
  48. ... | inj₂ b≡0 = ⊥-elim (¬0<b∧0≡b 0<b (sym b≡0))
  49. 0<s^b : {a b : ℕ} → (0 ℕ.< ((ℕ.suc a) ^ b))
  50. 0<s^b {_} {0} = ℕ.s≤s ℕ.z≤n
  51. 0<s^b {m} {ℕ.suc n} = 0<a∧0<b→0<ab (ℕ.suc m) ((ℕ.suc m) ^ n) (ℕ.s≤s ℕ.z≤n) (0<s^b {m} {n})
  52. <-lemma : (b : ℕ) → (0 ℕ.< b) → ∃ λ a → ℕ.suc a ≡ b
  53. <-lemma (ℕ.suc b) (ℕ.s≤s (ℕ.z≤n {.b})) = ( b , refl )
  54. 2^lem₁ : (a : ℕ) → ∃ λ b → (ℕ.suc b) ≡ (2 ^ a)
  55. 2^lem₁ a = <-lemma (2 ^ a) (0<s^b {1} {a})
  56. BitVecToNatLemma : {n : ℕ} → (s : Vec Bool n) → ((BitVecToNat s) ℕ.< (2 ^ n))
  57. BitVecToNatLemma {0} [] = ℕ.s≤s ℕ.z≤n
  58. BitVecToNatLemma {ℕ.suc n} (false ∷ r) = ≤lem₁ (BitVecToNatLemma r)
  59. BitVecToNatLemma {ℕ.suc n} (true ∷ r) = ret₃ where
  60. q = 2^lem₁ n
  61. t = proj₁ q
  62. s : (ℕ.suc t) ≡ 2 ^ n
  63. s = proj₂ q
  64. ret₁ : (BitVecToNat r) ℕ.< (ℕ.suc t)
  65. ret₁ = subst (λ u → (BitVecToNat r) ℕ.< u) (sym s) (BitVecToNatLemma r)
  66. ret₂ : (ℕ.suc (2 ℕ.* (BitVecToNat r))) ℕ.< (2 ℕ.* (ℕ.suc t))
  67. ret₂ = ≤lem₂ ret₁
  68. ret₃ : (ℕ.suc (2 ℕ.* (BitVecToNat r))) ℕ.< (2 ^ (ℕ.suc n))
  69. ret₃ = subst (λ u → (ℕ.suc (2 ℕ.* (BitVecToNat r))) ℕ.< (2 ℕ.* u)) s ret₂
  70. BitVecToFin : {n : ℕ} → Vec Bool n → Fin (2 ^ n)
  71. BitVecToFin s = F.fromℕ≤ (BitVecToNatLemma s)
  72. FinToBitVec : {n : ℕ} → (m : Fin (2 ^ n)) → ∃ λ (s : Vec Bool n) → (BitVecToFin s ≡ m)
  73. FinToBitVec {0} F.zero = ( [] , refl )
  74. FinToBitVec {0} (F.suc ())
  75. FinToBitVec {ℕ.suc n} k = ( ret₁ , lem₇ ) where
  76. open CommutativeSemiring ℕ.commutativeSemiring using (*-comm)
  77. kn = F.toℕ k
  78. p2^n' = 2^lem₁ (ℕ.suc n)
  79. p2^n = proj₁ p2^n'
  80. p2^nl : ℕ.suc p2^n ≡ 2 ^ (ℕ.suc n)
  81. p2^nl = proj₂ p2^n'
  82. kn<2^n : kn ℕ.< 2 ^ (ℕ.suc n)
  83. kn<2^n = subst (λ d → kn ℕ.< d) p2^nl (subst (λ d → kn ℕ.< ℕ.suc (ℕ.pred d)) (sym p2^nl) (ℕ.s≤s (F.prop-toℕ-≤ k)))
  84. dm = kn divMod 2
  85. quot = DivMod.quotient dm
  86. rem = F.toℕ (DivMod.remainder dm)
  87. Fin2toBool : Fin 2 → Bool
  88. Fin2toBool F.zero = false
  89. Fin2toBool (F.suc F.zero) = true
  90. Fin2toBool (F.suc (F.suc ()))
  91. zl = 2^lem₁ n
  92. lem₀ : rem ℕ.+ quot ℕ.* 2 ℕ.< (2 ^ (ℕ.suc n))
  93. lem₀ = subst (λ d → d ℕ.< (2 ^ (ℕ.suc n))) (DivMod.property dm) kn<2^n
  94. lem₁ : (DivMod.quotient dm) ℕ.< (2 ^ n)
  95. lem₁ = ≤lem₃ {a = quot} {b = 2 ^ n} {c = rem} lem₀
  96. fQuot : Fin (2 ^ n)
  97. fQuot = F.fromℕ≤ lem₁
  98. -- the recursive call
  99. prevRet : ∃ λ (s : Vec Bool n) → (BitVecToFin s ≡ fQuot)
  100. prevRet = FinToBitVec (F.fromℕ≤ lem₁)
  101. prevRetEq : (BitVecToFin (proj₁ prevRet) ≡ fQuot)
  102. prevRetEq = proj₂ prevRet
  103. lem₉ : {n : ℕ} → (r : Vec Bool n) → F.toℕ (BitVecToFin r) ≡ (BitVecToNat r)
  104. lem₉ r = trans (cong F.toℕ refl) (F.toℕ-fromℕ≤ (BitVecToNatLemma r))
  105. prevRetEqℕ : quot ≡ BitVecToNat (proj₁ prevRet)
  106. prevRetEqℕ = trans (trans (sym (F.toℕ-fromℕ≤ lem₁)) (cong F.toℕ (sym prevRetEq))) (lem₉ (proj₁ prevRet))
  107. ret₁ = (Fin2toBool (DivMod.remainder dm)) ∷ (proj₁ prevRet)
  108. lem₃ : (r : Fin 2) → (BitVecToNat ((Fin2toBool r) ∷ (proj₁ prevRet))) ≡ (F.toℕ r) ℕ.+ (2 ℕ.* (BitVecToNat (proj₁ prevRet)))
  109. lem₃ F.zero = refl
  110. lem₃ (F.suc F.zero) = refl
  111. lem₃ (F.suc (F.suc ()))
  112. lem₅ : rem ℕ.+ (2 ℕ.* (BitVecToNat (proj₁ prevRet))) ≡ kn
  113. lem₅ = subst (λ d → rem ℕ.+ d ≡ kn) (*-comm (BitVecToNat (proj₁ prevRet)) 2)
  114. (subst (λ d → rem ℕ.+ d ℕ.* 2 ≡ kn) prevRetEqℕ (sym (DivMod.property dm)))
  115. lem₆ : (BitVecToNat ret₁) ≡ kn
  116. lem₆ =
  117. begin
  118. BitVecToNat ret₁
  119. ≡⟨ lem₃ (DivMod.remainder dm) ⟩
  120. rem ℕ.+ (2 ℕ.* (BitVecToNat (proj₁ prevRet)))
  121. ≡⟨ lem₅ ⟩
  122. kn
  123. -- lem₆ = trans lem₄ lem₅
  124. lem₈ : kn ≡ F.toℕ (F.fromℕ≤ kn<2^n)
  125. lem₈ = sym (cong F.toℕ (F.fromℕ≤-toℕ k kn<2^n))
  126. lem₇ : (BitVecToFin ret₁) ≡ k
  127. lem₇ = trans (F.toℕ-injective (trans (lem₉ ret₁) (trans lem₆ lem₈))) (F.fromℕ≤-toℕ k kn<2^n)
  128. record ByteStream : Set where
  129. constructor bs
  130. field
  131. cBit : Maybe (∃ λ (c : Fin 8) → Vec Bool (ℕ.suc (F.toℕ c)))
  132. str : List Byte
  133. property : (cBit ≡ nothing) → (str ≡ L.[])
  134. byteStreamFromList : List Byte → ByteStream
  135. byteStreamFromList L.[] = bs nothing
  136. L.[]
  137. (λ _ → refl)
  138. byteStreamFromList (a L.∷ x) = bs (just ((F.fromℕ 7) , (proj₁ (FinToBitVec a))))
  139. x
  140. (λ ())
  141. -- Problem WAS:
  142. -- Normalization in unifyConArgs went berserk when checking this
  143. -- absurd lambda. Fixed by more cautious normalization in unifier.