Skip to content

Instantly share code, notes, and snippets.

@mjhopkins
Created June 8, 2017 01:55
Show Gist options
  • Save mjhopkins/6571ca4c33ef436b417d0ad38306417c to your computer and use it in GitHub Desktop.
Save mjhopkins/6571ca4c33ef436b417d0ad38306417c to your computer and use it in GitHub Desktop.
Idris code from Sydney Type Theory meeting 5 June 2017
module EvenOdd
data Even : (n : Nat) -> Type where
ZeroIsEven : Even 0
SuccSuccEven : Even n -> Even (S (S n))
data Odd : (n : Nat) -> Type where
OneIsOdd : Odd 1
SuccSuccOdd : Odd n -> Odd (S (S n))
total
evenOrOdd : (n : Nat) -> Either (Even n) (Odd n)
evenOrOdd Z = Left ZeroIsEven
evenOrOdd (S Z) = Right OneIsOdd
evenOrOdd (S (S k)) = case evenOrOdd k of
Left l => Left (SuccSuccEven l)
Right r => Right (SuccSuccOdd r)
thereExistsAnOddNatural : (n : Nat ** Odd n)
thereExistsAnOddNatural = (5 ** (SuccSuccOdd (SuccSuccOdd OneIsOdd)))
--------------------------------------------------------------------------------
mutual
data Even' : (n : Nat) -> Type where
ZeroEven : Even' 0
SuccOdd : Odd' n -> Even' (S n)
data Odd' : (n : Nat) -> Type where
SuccEven : Even' n -> Odd' (S n)
total
f : (n : Nat) -> Either (Even' n) (Odd' n) -> Either (Even' (S n)) (Odd' (S n))
f Z (Left ZeroEven) = Right (SuccEven ZeroEven)
f (S k) (Left (SuccOdd x)) = Right (SuccEven (SuccOdd x))
f (S n) (Right r) = Left (SuccOdd r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment