haskell - How can I define NFData instance for recursive singleton type? -


i'm using singletons library. have data type:

import control.deepseq import data.singletons.prelude import data.singletons.th  data t =           | b [t]  gensingletons [''t] 

i want generated singleton type st instance of nfdata. straightforward if type t wouldn't recursive. tried write this:

instance nfdata (st a)     rnf sa = ()     rnf (sb (x `scons` xs)) = rnf x `seq` rnf xs 

but fails on last line message:

could not deduce (nfdata (sing n1)) arising use of `rnf' context (a ~ 'b n)   bound pattern constructor              sb :: forall (z_azes :: t) (n_azet :: [t]).                    (z_azes ~ 'b n_azet) =>                    sing n_azet -> sing z_azes,            in equation `rnf' or (n ~ (n0 : n1))   bound pattern constructor              scons :: forall (a0 :: box) (z0 :: [a0]) (n0 :: a0) (n1 :: [a0]).                       (z0 ~ (n0 : n1)) =>                       sing n0 -> sing n1 -> sing z0,            in equation `rnf' in second argument of `seq', namely `rnf xs' in expression: rnf x `seq` rnf xs in equation `rnf':     rnf (sb (x `scons` xs)) = rnf x `seq` rnf xs 

i understand ghc wants x , xs in pattern sb (x ``scons`` xs)) instances of nfdata, have trouble figuring out how tell this. should write in context of instance make work?

first, need provide nfdata instances singleton lists.

instance nfdata (slist '[])   rnf snil = ()  instance (nfdata (sing x), nfdata (slist xs)) => nfdata (slist (x ': xs))   rnf (scons x xs) = rnf x `seq` rnf xs 

note can't solve in single instance, because way couldn't provide recursive nfdata constraints:

instance nfdata (slist xs)   rnf snil = ()   rnf (scons x xs) = ? -- no way know if nfdata (sing x) 

similarly, have write separate instances t cases:

instance nfdata (st a)   rnf sa = ()  instance nfdata (slist xs) => nfdata (st (b xs))   rnf (sb xs) = rnf xs 

Comments