What is the best way to make
type Configuration = Array DIM1 (Double, Double, Double)
an instance of Read? So later I could derive
data SimulationData = SD Configuration Double StdGen Int
to be an instance of Read too.
Such an instance will be an orphan instance, which you should generally avoid. However, it's fairly simple to write it:
{-# LANGUAGE TypeOperators #-}
import Data.Array.Repa (Array, Shape, Elt, Z(..), (:.)(..))
import qualified Data.Array.Repa as R
instance Read Z where
readsPrec _ r = do
("Z", s) <- lex r
return (Z, s)
instance (Read tail, Read head) => Read (tail :. head) where
readsPrec d =
readParen (d > prec) $ \r -> do
(tl, s) <- readsPrec (prec + 1) r
(":.", t) <- lex s
(hd, u) <- readsPrec (prec + 1) t
return (tl :. hd, u)
where prec = 3
instance (Shape sh, Read sh, Elt a, Read a) => Read (Array sh a) where
readsPrec d =
readParen (d > app) $ \r -> do
("Array", s) <- lex r
(sh, t) <- readsPrec (app + 1) s
(xs, u) <- readsPrec (app + 1) t
return (R.fromList sh xs, u)
where app = 10
If you use the StandaloneDeriving
extension, the first two instances can be simplified:
deriving instance Read Z
deriving instance (Read tail, Read head) => Read (tail :. head)
These instances should probably be in repa itself; I just based them on the example instance given in Text.Show and repa's show
output. I suggest making a feature request on repa's bug tracker, and putting these instances into a module of your program for now (unless you want to avoid orphan instances entirely, in which case you'll have to solve the problem another way altogether).
That said, you should probably consider simply converting your data to a list (with toList
) and using that; it avoids the orphan instance, and shouldn't have any downsides. You might also want to consider using a "real" serialisation library like cereal if you're more interested in processing the data with code than having it be human-readable; Read
is generally considered to be of rather limited use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With