Skip to content

Instantly share code, notes, and snippets.

@gallais
Created November 3, 2016 13:15
Show Gist options
  • Save gallais/e415b6e9b3bafc8070f16ecb35fb2bc3 to your computer and use it in GitHub Desktop.
Save gallais/e415b6e9b3bafc8070f16ecb35fb2bc3 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
module ShowBoth where
import Data.Proxy
type family IsTuple (a :: *) :: Bool where
IsTuple (a, b) = 'True
IsTuple a = 'False
class ShowBoth (a :: *) (b :: Bool) where
showBoth :: Proxy b -> a -> String
instance (Show a, Show b) => ShowBoth (a, b) 'False where
showBoth _ (a, b) = concat
[ "The first element is: `"
, show a
, "` and the second element is: `"
, show b
, "`"
]
instance (Show a, Show b) => ShowBoth (a, (b, c)) 'True where
showBoth _ (a, (b, _)) = concat
[ "The first element is: `"
, show a
, "` and the second element is: `"
, show b
, "`"
]
showBoth' :: forall a b. ShowBoth (a, b) (IsTuple b) => (a, b) -> String
showBoth' = showBoth (Proxy :: Proxy (IsTuple b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment