SizedNat.agda 984 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. {-# OPTIONS --sized-types --show-implicit #-}
  2. module Termination.Sized.SizedNat where
  3. open import Size
  4. data Nat : {size : Size} -> Set where
  5. zero : {size : Size} -> Nat {↑ size}
  6. suc : {size : Size} -> Nat {size} -> Nat {↑ size}
  7. -- subtraction is non size increasing
  8. sub : {size : Size} -> Nat {size} -> Nat {∞} -> Nat {size}
  9. sub zero n = zero
  10. sub (suc m) zero = suc m
  11. sub (suc m) (suc n) = sub m n
  12. -- div' m n computes ceiling(m/(n+1))
  13. div' : {size : Size} -> Nat {size} -> Nat -> Nat {size}
  14. div' zero n = zero
  15. div' (suc m) n = suc (div' (sub m n) n)
  16. -- one can use sized types as if they were not sized
  17. -- sizes default to ∞
  18. add : Nat -> Nat -> Nat
  19. add (zero ) n = n
  20. add (suc m) n = suc (add m n)
  21. nisse : {i : Size} -> Nat {i} -> Nat {i}
  22. nisse zero = zero
  23. nisse (suc zero) = suc zero
  24. nisse (suc (suc n)) = suc zero
  25. {- Agda complains about duplicate binding
  26. NatInfty = Nat {∞}
  27. {-# BUILTIN NATURAL NatInfty #-}
  28. {-# BUILTIN PLUS add #-}
  29. -}