Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Constraint Kinds - default constraint for default implementation

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?

like image 622
Impredicative Avatar asked Jul 28 '14 14:07

Impredicative


1 Answers

You can achieve this using DefaultSignatures:

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DefaultSignatures #-}

import GHC.Exts (Constraint)

class Foo a where
  type Ctx a :: Constraint
  type Ctx a = Show a

  foo :: (Ctx a) => a -> String

  default foo :: Show a => a -> String
  foo = show

main :: IO ()
main = putStrLn "Compiles!"

From my perspective, it should be using the default constraint of Show, which would let this compile.

The reason your approach doesn't work is that the user of your class should be able to override any number of defaults. Your code would break if someone tried to override Ctx but not foo.

like image 150
Roman Cheplyaka Avatar answered Nov 02 '22 13:11

Roman Cheplyaka