Skip to content

Instantly share code, notes, and snippets.

@adituv
Created June 22, 2015 04:48
Show Gist options
  • Save adituv/2bed55e290486131043d to your computer and use it in GitHub Desktop.
Save adituv/2bed55e290486131043d to your computer and use it in GitHub Desktop.
Constrained heterogeneous list
{-# 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)
{-# 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