[Haskell-cafe] How to write a Monad instance for this type
monkleyon at gmail.com
Tue Sep 3 17:40:56 UTC 2019
> I end up with this type.
> data Hdf5M a
> = H5Root (Hdf5M a)
> | H5Group ByteString [Hdf5M a] -- A group can contain other groups and/or datasets
> | forall sh b. (NativeType b, Shape sh) => H5Dataset ByteString (Array F sh b)
> And I would like to be able to describe a contain like this
> hdf5 $ do
> group "name" $ do
> dataset "name1" array1
> dataset "name2" array2
> group "other-name" $ do
You don't need a monad instance for this. First of all, you don't even
need do syntax to make something "pretty" similar to this.
group "name" $
[ dataset "name1" array1
, dataset "name2" array2
, group "other-name" $ [
But if you insist, you can just use an existing monad like WriterT. For
data Composite a = Base a | Composite [Composite a]
type CompositeWriter a = Writer [Composite a] ()
base = tell . pure . Base
composite = censor (pure . Composite)
asComposite = Composite . snd . runWriter
test = asComposite $ do
composite $ do
Hope that helps.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe