[Haskell-beginners] How to link two Types

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Wed Feb 22 16:40:47 UTC 2017


> It is hard to tell from your code what you intend, but it works
> however you want it to, so long as it type checks.

> class Frame a where
>   type Whatever a
>   len :: Whatever a -> IO (Maybe Int)
>   row :: Whatever a -> MaybeT IO (DifTomoFrame a DIM1)

> instance Frame DataFrameH5Path  where
>   type Whatever DataFrameH5Path = DataFrameH5
>   len = undefined -- :: DataFrameH5 -> IO (Maybe Int)
>   row = undefined -- :: DataFrameH5 -> Int -> MaybeT (DifTomoFrame
> DataFrameH5Path DIM1)


In fact what I try realy to do is this.

data DataFrameH5Path
    = DataFrameH5Path
      (DataItem H5) -- image
      (DataItem H5) -- gamma
      (DataItem H5) -- delta
      (DataItem H5) -- wavelength
    deriving (Show)

data DataFrameH5 a
    = DataFrameH5
      (Nxs a) -- Nexus file
      (DataSource H5) -- gamma
      (DataSource H5) -- delta
      (DataSource H5) -- wavelength
      PoniGenerator -- ponie generator

withDataFrameH5 :: (Frame a, MonadSafe m) => File -> Nxs (Key a) -> PoniGenerator -> (a -> m r) -> m r
withDataFrameH5 h nxs'@(Nxs _ _ (DataFrameH5Path _ g d w)) gen = bracket (liftIO before) (liftIO . after)
  where
    -- before :: File -> DataFrameH5Path -> m DataFrameH5
    before :: IO a
    before =  DataFrameH5
              <$> return nxs'
              <*> openDataSource h g
              <*> openDataSource h d
              <*> openDataSource h w
              <*> return gen

    after :: a -> IO ()
    after (DataFrameH5 _ g' d' w' _) = do
      closeDataSource g'
      closeDataSource d'
      closeDataSource w'


I open and hdf5 file and I need to read a bunch of data from this file.
the DataFrameH5 is a sort of resource like a File handler.
I need a location in the file in order to acce the data, then I need to close the file
So I store in the H5 type these resource, that I can release at the end.


Ideally I would like to have only The H5Path type and hide the H5 one but I do not know how to do this.

I have in fact different H5Path types which necessitate each time there corresponding H5 type.

So I want a one for one relation between the H5 <-> H5Path type.


Cheers

Frederic


More information about the Beginners mailing list