How do I define an NFData instance for a recursive singleton type?
I am using a library singletons
. I have this datatype:
import Control.DeepSeq
import Data.Singletons.Prelude
import Data.Singletons.TH
data T =
A
| B [T]
genSingletons [''T]
I want the generated singleton type to ST
be an instance NFData
. It would be easy if the type was T
not recursive. I tried to write this:
instance NFData (ST a) where
rnf SA = ()
rnf (SB (x `SCons` xs)) = rnf x `seq` rnf xs
but it fails on the last line with the message:
Could not deduce (NFData (Sing n1)) arising from a use of `rnf'
from the context (a ~ 'B n)
bound by a pattern with constructor
SB :: forall (z_azEs :: T) (n_azEt :: [T]).
(z_azEs ~ 'B n_azEt) =>
Sing n_azEt -> Sing z_azEs,
in an equation for `rnf'
or from (n ~ (n0 : n1))
bound by a pattern with constructor
SCons :: forall (a0 :: BOX) (z0 :: [a0]) (n0 :: a0) (n1 :: [a0]).
(z0 ~ (n0 : n1)) =>
Sing n0 -> Sing n1 -> Sing z0,
in an equation for `rnf'
In the second argument of `seq', namely `rnf xs'
In the expression: rnf x `seq` rnf xs
In an equation for `rnf':
rnf (SB (x `SCons` xs)) = rnf x `seq` rnf xs
I understand that GHC wants x
and xs
in the template SB (x ``SCons`` xs))
to be copies NFData
, but I find it difficult to understand exactly how to say it. What should I write in the context of this instance to make it work?
source to share
First, you need to provide instances NFData
for single lists.
instance NFData (SList '[]) where
rnf SNil = ()
instance (NFData (Sing x), NFData (SList xs)) => NFData (SList (x ': xs)) where
rnf (SCons x xs) = rnf x `seq` rnf xs
Note that you cannot solve this in one instance, because that way you cannot provide recursive constraints NFData
:
instance NFData (SList xs) where
rnf SNil = ()
rnf (SCons x xs) = ? -- no way to know if NFData (Sing x)
Likewise, you need to write separate instances for the cases T
:
instance NFData (ST A) where
rnf SA = ()
instance NFData (SList xs) => NFData (ST (B xs)) where
rnf (SB xs) = rnf xs
source to share