Created
February 18, 2014 10:47
-
-
Save myuon/9068515 to your computer and use it in GitHub Desktop.
(Show a) => HeteroList a, (Real a) => HeteroList a -- with ConstraintKinds Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, DataKinds, TypeFamilies, ConstraintKinds, TypeOperators, UndecidableInstances #-} | |
import GHC.Prim (Constraint) | |
import Data.Ratio ((%)) | |
type family All (cx :: * -> Constraint) (xs :: [*]) :: Constraint | |
type instance All cx '[] = () | |
type instance All cx (x ': xs) = (cx x, All cx xs) | |
data HList (as :: [*]) where | |
Nil :: HList '[] | |
Cons :: a -> HList as -> HList (a ': as) | |
instance (All Show as) => Show (HList as) where | |
show Nil = "[]" | |
show (Cons x xs) = show x ++ ":" ++ show xs | |
sumRational :: (All Real as) => HList as -> Rational | |
sumRational Nil = 0 | |
sumRational (Cons x xs) = toRational x + sumRational xs | |
main = do | |
print $ Cons 100 (Cons 'q' Nil) -- 100:'q':[] | |
print $ sumRational $ Cons (199 :: Int) $ Cons (1.200 :: Float) $ Cons (3 % 4 :: Rational) $ Nil -- 842845389 % 4194304 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment