Skip to content

Instantly share code, notes, and snippets.

@phadej
Created June 12, 2016 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/75f9c8ce4958102242e3646d3cc885ae to your computer and use it in GitHub Desktop.
Save phadej/75f9c8ce4958102242e3646d3cc885ae to your computer and use it in GitHub Desktop.
newtype Two f a = Two (f (f a))
data Sq f a = M a (f a) | E (Sq (Two f) a)
class NFData a where
rnf :: a -> ()
instance NFData () where
rnf () = ()
instance (NFData a, NFData b) => NFData (a, b) where
rnf (a, b) = rnf a `seq` rnf b
-------------------------------------------------------------------------------
-- transformers-0.5
-------------------------------------------------------------------------------
class NFData1 f where
rnfWith :: (a -> ()) -> f a -> ()
instance NFData1 f => NFData1 (Two f) where
rnfWith r (Two t) = rnfWith (rnfWith r) t
instance NFData1 f => NFData1 (Sq f) where
rnfWith r (M a f) = rnf (r a, rnfWith r f)
rnfWith r (E sq) = rnfWith r sq
-------------------------------------------------------------------------------
-- transformers-0.4
-------------------------------------------------------------------------------
class NFData1' f where
rnf1 :: NFData a => f a -> ()
{-
instance NFData1' f => NFData1' (Two f) where
rnf1 (Two t) = rnf1 t
-- Could not deduce (NFData (f a)) arising from a use of ‘rnf1’
-- from the context (NFData1' f)
-}
data X f a = X { getX :: f a }
instance Functor f => Functor (X f) where
fmap f (X x) = X (fmap f x)
instance NFData1' f => NFData1' (X f) where
rnf1 (X x) = rnf1 x
instance (NFData1' f, NFData a) => NFData (X f a) where
rnf = rnf1
instance (NFData1' f, Functor f) => NFData1' (Two f) where
rnf1 (Two t) = rnf1 (fmap X t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment