If I want to declare a newtype
such that type type of the value is constrained to have an instance for a type-class, it seems like I can do that with:
{-# LANGUAGE RankNTypes #-}
newtype ShowBox = ShowBox (forall a. Show a => a)
GHC compiles that just fine, but when I try and actually use ShowBox
with
ShowBox "hello"
I get a compiler error
<interactive>:1:18:
Could not deduce (a ~ [Char])
from the context (Show a)
bound by a type expected by the context: Show a => a
at <interactive>:1:10-24
`a' is a rigid type variable bound by
a type expected by the context: Show a => a at <interactive>:1:10
In the first argument of `ShowBox', namely `"hello"'
In the expression: ShowBox "hello"
In an equation for `a': a = ShowBox "hello"
Is there a way to make this work?
ShowBox
can only be applied to values that are precisely of typeShow a => a
. I am quite interested to see what is the answer to this question. – Daniel Pratt