Skip to content

Instantly share code, notes, and snippets.

@ddrone
Created July 28, 2014 11:49
Show Gist options
  • Save ddrone/2bed9b74f579aa7cb564 to your computer and use it in GitHub Desktop.
Save ddrone/2bed9b74f579aa7cb564 to your computer and use it in GitHub Desktop.
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Typelevel where
import Prelude hiding (sum)
-- type-level natural numbers
data Zero = Zero
data Succ a = Succ a
class ToInt a where
toInt :: a -> Int
class Sum a b c | a b -> c, a c -> b where
instance Sum Zero b b where
instance Sum a b c => Sum (Succ a) b (Succ c) where
two = Succ (Succ Zero)
three = Succ two
sum :: Sum a b c => a -> b -> c
sum _ _ = undefined :: c
-- load this into ghci and input ":t sum two three"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment