Headline: I would like to provide a default implementation for a class method parametrised over a constraint, which uses the default instance for that constraint.
Consider the following:
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
import GHC.Exts (Constraint)
class Foo a where
type Ctx a :: Constraint
type Ctx a = Show a
foo :: (Ctx a) => a -> String
foo = show
main :: IO ()
main = putStrLn "Compiles!"
This fails to compile with the error of:
Could not deduce (Show a) arising from a use of ‘show’ from the context (Foo a)
From my perspective, it should be using the default constraint of Show
, which would let this compile. Is there any reason this doesn't work, or can anyone suggest a good way to achieve this?
Ctx
but notfoo
to refuse to compile. Still, this looks like a good solution! – Daune