Last active
December 11, 2018 23:00
-
-
Save andrevidela/b51d2671959be185e4bb6fe2c7ce6788 to your computer and use it in GitHub Desktop.
How to select the correct instance for an indexed typeclass?
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 AllowAmbiguousTypes #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE ScopedTypeVariables#-} | |
{-# LANGUAGE TypeApplications#-} | |
{-# LANGUAGE TypeFamilies #-} | |
data ValueKind = First | Second deriving (Eq, Show) | |
class DisplayClass (v :: ValueKind) where | |
type FromValue v :: * | |
makeValue :: FromValue v | |
displayName :: FromValue v -> String | |
instance DisplayClass First where | |
type FromValue First = Int | |
makeValue = 1 | |
displayName = show | |
instance DisplayClass Second where | |
type FromValue Second = String | |
makeValue = "Second" | |
displayName = id | |
makeAndPrint :: forall v. (DisplayClass v) => IO () | |
makeAndPrint = putStrLn $ displayName @v (makeValue @v) | |
main :: IO () | |
main = do | |
let firstValue = First | |
let secondValue = Second | |
-- makeAndPrint --using firstValue Instance | |
-- makeAndPrint --ysing secondValue Instance | |
pure () |
mstksg
commented
Dec 11, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment