Created
November 3, 2016 13:15
-
-
Save gallais/e415b6e9b3bafc8070f16ecb35fb2bc3 to your computer and use it in GitHub Desktop.
Show Both in Haskell (cf. https://gist.github.com/Qqwy/e092a68d508268f0f25cb5d1ffb3ad72)
This file contains hidden or 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 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