Skip to content

Instantly share code, notes, and snippets.

@ndtimofeev
Created November 3, 2017 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndtimofeev/c46b28e9c9a4ee9d689cbc9d753a6d08 to your computer and use it in GitHub Desktop.
Save ndtimofeev/c46b28e9c9a4ee9d689cbc9d753a6d08 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeOperators, TypeInType, GADTs, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
data HList (xs :: [k]) where
Z :: HList '[]
(:.) :: a -> HList xs -> HList (a ': xs)
infixr 5 :.
class Filter a xs where
typeFilter :: HList xs -> [a]
instance Filter a '[] where
typeFilter _ = []
instance Filter a xs => Filter a (a ': xs) where
typeFilter (v :. xs) = v : typeFilter xs
instance Filter a xs => Filter a (b ': xs) where
typeFilter (v :. xs) = typeFilter xs
newtype Name = Name { getName :: Name }
newtype Phone = Phone { getPhone :: Int }
data Selection = Selection Int Int
data PhoneContact = PhoneContact Name Phone
data PhoneWithSelectionContact = PhoneWithSelectionContact Name Phone Selection
class IsContact a
instance IsContact PhoneContact
instance IsContact PhoneWithSelectionContact
filterSelections :: Filter PhoneWithSelectionContact xs => HList xs -> [PhoneWithSelectionContact]
filterSelections = typeFilter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment