Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. open import Relation.Binary.PropositionalEquality
  2. open import Data.Sum
  3. open import Data.Product
  4. open import Relation.Nullary.Negation
  5. open import Relation.Nullary.Decidable
  6. open import Relation.Binary.Core
  7. open import Relation.Nullary
  8. open import Data.Nat using (ℕ) renaming (_+_ to _⊹_)
  9.  
  10. data Digit : Set where
  11. zero : Digit
  12. one : Digit
  13. two : Digit
  14.  
  15. data Digits : Set
  16. last : Digits → Digit
  17.  
  18. infixl 3 [_] _⟨_⟩∷_
  19. data Digits where
  20. [_] : Digit → Digits
  21. _⟨_⟩∷_ : (ds : Digits) → (nice : last ds ≢ zero) → (d : Digit) → Digits
  22.  
  23. last [ x ] = x
  24. last (ds ⟨ nice ⟩∷ d) = last ds
  25.  
  26.  
  27. suc' : Digits → (∃ λ ds → last ds ≢ zero)
  28. suc' [ zero ] = [ one ] , λ ()
  29. suc' [ one ] = [ two ] , λ ()
  30. suc' [ two ] = ([ one ] ⟨ (λ ()) ⟩∷ zero) , λ ()
  31. suc' (ds ⟨ nice ⟩∷ zero) = (ds ⟨ nice ⟩∷ one) , nice
  32. suc' (ds ⟨ nice ⟩∷ one) = (ds ⟨ nice ⟩∷ two) , nice
  33. suc' (ds ⟨ nice ⟩∷ two) = ((proj₁ deeper) ⟨ proj₂ deeper ⟩∷ zero) , proj₂ deeper
  34. where
  35. deeper = suc' ds
  36.  
  37.  
  38. suc : Digits → Digits
  39. suc ds = proj₁ (suc' ds)
  40.  
  41.  
  42. infixl 4 _+_
  43. _+_ : (a b : Digits) → Digits
  44. help : (a b : Digits) → last a ≢ zero → last b ≢ zero → last (a + b) ≢ zero
  45.  
  46. [ zero ] + x = x
  47. [ one ] + x = suc x
  48. [ two ] + x = suc (suc x)
  49. x + [ zero ] = x
  50. x + [ one ] = suc x
  51. x + [ two ] = suc (suc x)
  52. (a ⟨ nice ⟩∷ zero) + (b ⟨ nice₁ ⟩∷ d) = ((a + b) ⟨ help a b nice nice₁ ⟩∷ d)
  53. (a ⟨ nice ⟩∷ d) + (b ⟨ nice₁ ⟩∷ zero) = ((a + b) ⟨ help a b nice nice₁ ⟩∷ d)
  54. (a ⟨ nice ⟩∷ one) + (b ⟨ nice₁ ⟩∷ one) = ((a + b) ⟨ help a b nice nice₁ ⟩∷ two)
  55. (a ⟨ nice ⟩∷ one) + (b ⟨ nice₁ ⟩∷ two) = (proj₁ deeper ⟨ proj₂ deeper ⟩∷ zero)
  56. where
  57. deeper = suc' (a + b)
  58. (a ⟨ nice ⟩∷ two) + (b ⟨ nice₁ ⟩∷ one) = (proj₁ deeper ⟨ proj₂ deeper ⟩∷ zero)
  59. where
  60. deeper = suc' (a + b)
  61. (a ⟨ nice ⟩∷ two) + (b ⟨ nice₁ ⟩∷ two) = (proj₁ deeper ⟨ proj₂ deeper ⟩∷ one)
  62. where
  63. deeper = suc' (suc (a + b))
  64.  
  65.  
  66. help [ zero ] b x y = y
  67. help [ one ] b x y = proj₂ (suc' b)
  68. help [ two ] b x y = proj₂ (suc' (suc b))
  69. help (a ⟨ nice ⟩∷ d) [ zero ] x y = x
  70. help (a ⟨ nice ⟩∷ d) [ one ] x y = proj₂ (suc' (a ⟨ nice ⟩∷ d))
  71. help (a ⟨ nice ⟩∷ d) [ two ] x y = proj₂ (suc' (suc (a ⟨ nice ⟩∷ d)))
  72. help (a ⟨ nice ⟩∷ zero) (b ⟨ nice₁ ⟩∷ d) x y = help a b x y
  73. help (a ⟨ nice ⟩∷ one) (b ⟨ nice₁ ⟩∷ zero) x y = help a b x y
  74. help (a ⟨ nice ⟩∷ one) (b ⟨ nice₁ ⟩∷ one) x y = help a b x y
  75. help (a ⟨ nice ⟩∷ one) (b ⟨ nice₁ ⟩∷ two) x y = proj₂ (suc' (a + b))
  76. help (a ⟨ nice ⟩∷ two) (b ⟨ nice₁ ⟩∷ zero) x y = help a b x y
  77. help (a ⟨ nice ⟩∷ two) (b ⟨ nice₁ ⟩∷ one) x y = proj₂ (suc' (a + b))
  78. help (a ⟨ nice ⟩∷ two) (b ⟨ nice₁ ⟩∷ two) x y = proj₂ (suc' (suc (a + b)))
  79.  
  80.  
  81. infixl 5 _|*_
  82. _|*_ : Digit → Digits → Digits
  83. zero |* b = [ zero ]
  84. one |* b = b
  85. two |* b = b + b
  86.  
  87.  
  88. step' : Digits → Digits → Digits
  89. step' xs [ x ] = x |* xs
  90. step' xs (ys ⟨ nice ⟩∷ x) = step' (x |* xs) ys
  91.  
  92. step : Digits → Digits
  93. step = step' [ one ]
  94.  
  95.  
  96.  
  97. contains0 :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement