module Generics.Regular.Functions.Seq (
DeepSeq (..), Seq(..), gdseq
) where
import Data.List
import Generics.Regular.Base
class Seq f where
gseq :: (a -> b -> b) -> f a -> b -> b
instance Seq I where
gseq f (I x) = f x
instance (DeepSeq a) => Seq (K a) where
gseq _ (K x) = dseq x
instance Seq U where
gseq _ U = id
instance (Seq f, Seq g) => Seq (f :+: g) where
gseq f (L x) = gseq f x
gseq f (R y) = gseq f y
instance (Seq f, Seq g) => Seq (f :*: g) where
gseq f (x :*: y) = gseq f x . gseq f y
instance Seq f => Seq (C c f) where
gseq f (C x) = gseq f x
instance Seq f => Seq (S s f) where
gseq f (S x) = gseq f x
gdseq :: (Regular a, Seq (PF a)) => a -> b -> b
gdseq p = gseq gdseq (from p)
class DeepSeq a where
dseq :: a -> b -> b
dseq = seq
instance DeepSeq Int
instance DeepSeq Integer
instance DeepSeq Char
instance DeepSeq Float
instance DeepSeq Double
instance DeepSeq ()
instance DeepSeq a => DeepSeq [a] where
dseq xs b = foldl' (flip dseq) b xs
instance DeepSeq a => DeepSeq (Maybe a) where
dseq Nothing b = b
dseq (Just a) b = dseq a b
instance (DeepSeq a, DeepSeq b) => DeepSeq (Either a b) where
dseq (Left x) b = dseq x b
dseq (Right x) b = dseq x b