module Data.Array.Repa.Eval.Elt
(Elt (..))
where
import GHC.Prim
import GHC.Exts
import GHC.Types
import GHC.Word
import GHC.Int
import GHC.Generics
class Elt a where
touch :: a -> IO ()
default touch :: (Generic a, GElt (Rep a)) => a -> IO ()
touch = gtouch . from
zero :: a
default zero :: (Generic a, GElt (Rep a)) => a
zero = to gzero
one :: a
default one :: (Generic a, GElt (Rep a)) => a
one = to gone
class GElt f where
gtouch :: f a -> IO ()
gzero :: f a
gone :: f a
instance GElt U1 where
gtouch _ = return ()
gzero = U1
gone = U1
instance (GElt a, GElt b) => GElt (a :*: b) where
gtouch (x :*: y) = gtouch x >> gtouch y
gzero = gzero :*: gzero
gone = gone :*: gone
instance (GElt a, GElt b) => GElt (a :+: b) where
gtouch (L1 x) = gtouch x
gtouch (R1 x) = gtouch x
gzero = L1 gzero
gone = R1 gone
instance (GElt a) => GElt (M1 i c a) where
gtouch (M1 x) = gtouch x
gzero = M1 gzero
gone = M1 gone
instance (Elt a) => GElt (K1 i a) where
gtouch (K1 x) = touch x
gzero = K1 zero
gone = K1 one
instance Elt Bool where
touch b
= IO (\state -> case touch# b state of
state' -> (# state', () #))
zero = False
one = True
instance Elt Float where
touch (F# f)
= IO (\state -> case touch# f state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Double where
touch (D# d)
= IO (\state -> case touch# d state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Int where
touch (I# i)
= IO (\state -> case touch# i state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Int8 where
touch (I8# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Int16 where
touch (I16# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Int32 where
touch (I32# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Int64 where
touch (I64# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Word where
touch (W# i)
= IO (\state -> case touch# i state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Word8 where
touch (W8# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Word16 where
touch (W16# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Word32 where
touch (W32# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance Elt Word64 where
touch (W64# w)
= IO (\state -> case touch# w state of
state' -> (# state', () #))
zero = 0
one = 1
instance (Elt a, Elt b) => Elt (a, b) where
touch (a, b)
= do touch a
touch b
zero = (zero, zero)
one = (one, one)
instance (Elt a, Elt b, Elt c) => Elt (a, b, c) where
touch (a, b, c)
= do touch a
touch b
touch c
zero = (zero, zero, zero)
one = (one, one, one)
instance (Elt a, Elt b, Elt c, Elt d) => Elt (a, b, c, d) where
touch (a, b, c, d)
= do touch a
touch b
touch c
touch d
zero = (zero, zero, zero, zero)
one = (one, one, one, one)
instance (Elt a, Elt b, Elt c, Elt d, Elt e) => Elt (a, b, c, d, e) where
touch (a, b, c, d, e)
= do touch a
touch b
touch c
touch d
touch e
zero = (zero, zero, zero, zero, zero)
one = (one, one, one, one, one)
instance (Elt a, Elt b, Elt c, Elt d, Elt e, Elt f) => Elt (a, b, c, d, e, f) where
touch (a, b, c, d, e, f)
= do touch a
touch b
touch c
touch d
touch e
touch f
zero = (zero, zero, zero, zero, zero, zero)
one = (one, one, one, one, one, one)