Files
agda-doodles/plfa/Naturals.agda
2020-06-05 09:18:15 +02:00

69 lines
1.1 KiB
Agda
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module Naturals where
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl)
open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; _∎)
data : Set where
zero :
suc :
{-# BUILTIN NATURAL #-}
_+_ :
zero + n = n
suc m + n = suc (m + n)
_*_ :
zero * n = zero
suc m * n = n + (m * n)
_^_ :
n ^ 0 = 1
n ^ suc m = n * (n ^ m)
_∸_ :
m zero = m
zero suc n = zero
suc m suc n = m n
infixl 6 _+_ _∸_
infixl 7 _*_
{-# BUILTIN NATPLUS _+_ #-}
{-# BUILTIN NATTIMES _*_ #-}
{-# BUILTIN NATMINUS _∸_ #-}
-- Bin stretch exercise
data Bin : Set where
⟨⟩ : Bin
_O : Bin Bin
_I : Bin Bin
inc : Bin Bin
inc ⟨⟩ = ⟨⟩ I
inc (m O) = m I
inc (m I) = (inc m) O
_ : inc (⟨⟩ I O I I) ⟨⟩ I I O O
_ = refl
_ : inc (⟨⟩ I) ⟨⟩ I O
_ = refl
to : -> Bin
to 0 = ⟨⟩
to (suc n) = inc (to n)
_ : to 11 ⟨⟩ I O I I
_ = refl
from : Bin ->
from ⟨⟩ = 0
from (m O) = 2 * from m
from (m I) = 1 + 2 * from m
_ : from (⟨⟩ I O I I) 11
_ = refl