Is there a way to make this code compose generic ?

PICCA Frederic-Emmanuel <[email protected]>
Newsgroups gmane.comp.lang.haskell.cafe
Message-ID <1691274023.93414477.1744035349527.JavaMail.zimbra@synchrotron-soleil.fr>
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
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.