[Haskell-cafe] data familly and `sort of` alternative

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Thu Apr 13 20:36:27 UTC 2023


Hello,

I have this class with two data type familly

class DataSource a where
  data DataSourcePath a :: Type
  data DataSourceAcq a :: Type

  withDataSourceP :: (Location l, MonadSafe m) => l -> DataSourcePath a -> (DataSourceAcq a -> m r) -> m r

instance DataSource Double where
  data DataSourcePath Double
    = DataSourcePath'Double (Hdf5Path Z Double)
    | DataSourcePath'Double'Const Double
    deriving (Generic, Show, FromJSON, ToJSON)

  data DataSourceAcq Double
    = DataSourceAcq'Double Dataset
    | DataSourceAcq'Double'Const Double

  withDataSourceP f (DataSourcePath'Double p) g = withHdf5PathP f p $ \ds -> g (DataSourceAcq'Double ds)
  withDataSourceP _ (DataSourcePath'Double'Const a) g = g (DataSourceAcq'Double'Const a)


the purpose of this is to let our users gives  multiple way to extract a value from different sources.
In the previous case, from an hdf5 file or from a constante value. The withHdf5PathP method can fail  and raise an Exception.
So I would like a way to provide a fallback like

(pseudo code)
withDataSourceP f (datapath `or` fallback) g = try datapath and if it fails fallback

My question is howto write this `or` method, it is not clear to me and it seems to me that this method should be usable by all the instance.

I am also not use if this is the right way to design all this.

Thanks for your help.

Frederic


More information about the Haskell-Cafe mailing list