Skip to content

Instantly share code, notes, and snippets.

@liarokapisv
Created July 9, 2020 16:15
Show Gist options
  • Save liarokapisv/da16ae4fd5af7fe19c4c723d6c2b6a06 to your computer and use it in GitHub Desktop.
Save liarokapisv/da16ae4fd5af7fe19c4c723d6c2b6a06 to your computer and use it in GitHub Desktop.
data NatE = NZero | NDouble NatG
deriving Show
data NatO = NSucc NatE
deriving Show
type NatG = Either NatE NatO
toInt0 :: NatE -> Int
toInt0 NZero = 0
toInt0 (NDouble n) = 2 * (toInt n)
toInt1 :: NatO -> Int
toInt1 (NSucc n) = 1 + toInt0 n
incE :: NatE -> NatO
incE g = NSucc g
incO :: NatO -> NatE
incO (NSucc NZero) = NDouble $ Right $ NSucc NZero
incO (NSucc (NDouble (Left e))) = NDouble $ Right $ incE e
incO (NSucc (NDouble (Right o))) = NDouble $ Left $ incO o
toInt :: NatG -> Int
toInt n = either toInt0 toInt1 n
toNatG :: Int -> NatG
toNatG 0 = Left $ NZero
toNatG n = case (toNatG (n-1)) of
Left e -> Right $ incE e
Right o -> Left $ incO o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment