[Haskell-cafe] Is there a way to make this code compose generic ?

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Mon Apr 7 14:15:49 UTC 2025


Hello, I have this


data DataSourceShape
    = DataSourceShape ([HSize], [Maybe HSize])
    | DataSourceShape'Range !DIM1 !DIM1

a function which combine 2 DataSourceShape and produce an new one (monoid operation ?)

combine'Shape ∷ DataSourceShape → DataSourceShape → DataSourceShape

I use this in this class with familly types.

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

  ds'Shape ∷ MonadSafe m ⇒ DataSourceAcq a → m DataSourceShape

  withDataSourceP ∷ (Location l, MonadSafe m)
                  ⇒ ScanFile l → DataSourcePath a → (DataSourceAcq a → m r) → m r

and here an instance for one of my type DataFrameQCustom (I have plenty of them). they are all constructed the same way.


  data DataSourceAcq DataFrameQCustom
    = DataSourceAcq'DataFrameQCustom
      (DataSourceAcq Attenuation)
      (DataSourceAcq Geometry)
      (DataSourceAcq Image)
      (DataSourceAcq Mask)
      (DataSourceAcq Timestamp)
      (DataSourceAcq Timescan0)
      (DataSourceAcq Scannumber)


  ds'Shape(DataSourceAcq'DataFrameQCustom a g i m idx t0 s)
      = do sa ← ds'Shape a
           sg ← ds'Shape g
           si ← ds'Shape i
           sm ← ds'Shape m
           sidx ← ds'Shape idx
           st0 ← ds'Shape t0
           ss ← ds'Shape s
           pure $ foldl1 combine'Shape [sa, sg, si, sm, sidx, st0, ss]

  withDataSourceP f (DataSourcePath'DataFrameQCustom a g i m idx t0 s) gg =
    withDataSourceP f a $ λa' →
    withDataSourceP f g $ λg' →
    withDataSourceP f i $ λi' →
    withDataSourceP f m $ λm' →
    withDataSourceP f idx $ λidx' →
    withDataSourceP f s $ λs' →
    withDataSourceP f t0 $ λt0' → gg (DataSourceAcq'DataFrameQCustom a' g' i' m' idx' t0' s')


My question is, how should avoid writting by hand all these ds'Shape / withDatasourceP implementations, which seems quite mechanical.

thanks for your help

Frederic


More information about the Haskell-Cafe mailing list