Created
June 22, 2015 04:48
-
-
Save adituv/2bed55e290486131043d to your computer and use it in GitHub Desktop.
Constrained heterogeneous list
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 ConstraintKinds, GADTs, RankNTypes #-} | |
module CList where | |
infixr 5 :> | |
data CList c where | |
Nil :: CList c | |
(:>) :: forall c t. (c t) => t -> CList c -> CList c | |
-- "Monomorphic map instance" (ish) for CList. Converts to normal list. | |
-- Possible improvement: OverloadedLists, hence to (IsList a => a)? | |
cmap :: (forall t. (c t) => t -> r) -> CList c -> [r] | |
cmap _ Nil = [] | |
cmap f (x :> xs) = (f x):(cmap f xs) |
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 CPP #-} | |
import CList | |
import Data.Typeable | |
#ifdef FUNC_SHOW | |
import Text.Show.Functions | |
#endif | |
exampleList1 :: CList Show | |
exampleList1 = 'a' :> "b" :> (1.0 :: Double) :> True :> Nil | |
exampleList2 :: CList Typeable | |
exampleList2 = (1 :: Int) :> (2.0 :: Double) :> not :> Nil | |
-- Compilation error if command line flag -DFUNC_SHOW not present | |
exampleList3 :: CList Show | |
exampleList3 = 'a' :> not :> Nil | |
main :: IO () | |
main = do | |
print $ cmap show exampleList1 | |
print $ cmap (show . typeOf) exampleList2 | |
print $ cmap show exampleList3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment