Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Last active October 25, 2019 18:00
Show Gist options
  • Save pedrominicz/6c3dfe3fc35a5c3f7115f561fd0ac25d to your computer and use it in GitHub Desktop.
Save pedrominicz/6c3dfe3fc35a5c3f7115f561fd0ac25d to your computer and use it in GitHub Desktop.
Programming Language Foundations in Agda: Naturals
module Naturals where
-- Funny, but the "Naturals" module only contains "Bin", because the other
-- exercises in this chapter were too trivial and boring to be worth doing.
open import Data.Nat using (ℕ; zero; suc; _+_; _*_)
data Bin : Set where
<> : Bin
_O : Bin → Bin
_I : Bin → Bin
infixl 10 _O
infixl 10 _I
inc : Bin → Bin
inc <> = <> I
inc (x O) = x I
inc (x I) = (inc x) O
to : ℕ → Bin
to zero = <> O
to (suc x) = inc (to x)
from : Bin → ℕ
from <> = zero
from (x O) = 2 * from x
from (x I) = 1 + 2 * from x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment