Binary.agda 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. {-
  2. FP Lunch, Nottingham
  3. July 27, 2007
  4. Conor McBride
  5. -}
  6. module Binary where
  7. data Bit : Set where
  8. O : Bit
  9. I : Bit
  10. infixl 80 _◃_
  11. data Pos : Set where
  12. ◃I : Pos
  13. _◃_ : Pos -> Bit -> Pos
  14. bsuc : Pos -> Pos
  15. bsuc ◃I = ◃I ◃ O
  16. bsuc (n ◃ O) = n ◃ I
  17. bsuc (n ◃ I) = bsuc n ◃ O
  18. data Peano : Pos -> Set where
  19. pI : Peano ◃I
  20. psuc : {n : Pos} -> Peano n -> Peano (bsuc n)
  21. pdouble : {n : Pos} -> Peano n -> Peano (n ◃ O)
  22. pdouble pI = psuc pI
  23. pdouble (psuc p) = psuc (psuc (pdouble p))
  24. peano : (n : Pos) -> Peano n
  25. peano ◃I = pI
  26. peano (n ◃ O) = pdouble (peano n)
  27. peano (n ◃ I) = psuc (pdouble (peano n))
  28. -- Slow addition (yay!)
  29. _+_ : Pos -> Pos -> Pos
  30. _+_ n m = peano n ⊕ m
  31. where
  32. _⊕_ : {n : Pos} -> Peano n -> Pos -> Pos
  33. pI ⊕ m = bsuc m
  34. psuc p ⊕ m = bsuc (p ⊕ m)
  35. infixl 60 _+_
  36. infix 40 _==_
  37. data _==_ {A : Set}(x : A) : A -> Set where
  38. refl : x == x
  39. test : (◃I ◃ I ◃ O ◃ O ◃ O) == (◃I ◃ I ◃ O ◃ I) + (◃I ◃ O ◃ I ◃ I)
  40. test = refl