Last active
May 7, 2018 06:06
-
-
Save louispan/f340a7f77623aeda3fd8927f00adcf66 to your computer and use it in GitHub Desktop.
Haskell - Advanced Overlap
This file contains 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 DataKinds #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
-- https://wiki.haskell.org/GHC/AdvancedOverlap | |
-- when using closed type family, data kinds, and proxy | |
module Main where | |
import Data.Proxy | |
class Print' flag a where | |
print' :: Proxy flag -> a -> IO () | |
class Print a where | |
print :: a -> IO () | |
-- Is there a way to avoid this explicitness? | |
type family ShowPred a where | |
ShowPred Int = 'True | |
ShowPred Bool = 'True | |
ShowPred [a] = ShowPred a | |
ShowPred a = 'False | |
instance Show a => Print' 'True a where | |
print' _ x = putStrLn (show x) | |
instance Print' 'False a where | |
print' _ _ = putStrLn "No show method" | |
instance (Print' (ShowPred a) a) => Print a where | |
print a = print' (flag a) a | |
where | |
flag :: a -> Proxy (ShowPred a) | |
flag _ = Proxy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://kseo.github.io/posts/2017-02-05-avoid-overlapping-instances-with-closed-type-families.html
https://gist.github.com/phadej/cae76444548b9cffa10d9675e013847b