[Haskell-cafe] bracket and type family

PICCA Frederic-Emmanuel frederic-emmanuel.picca at synchrotron-soleil.fr
Mon Mar 14 13:13:19 UTC 2022


Hello

I try to have a generic way to open and close ressources for my software

class DataResource a where
    data OpenedDataResource a :: *
    acquireDataResource :: a -> IO (OpenedDataResource a)
    releaseDataResource :: OpenedDataResource a -> IO ()

the idea is to have a generic method like this

withDataResource :: DataResource a => a -> (OpenedDataResource a -> IO r) -> IO r
withDataResource r = bracket (acquireDataResource r) releaseDataResource

and later another one for the bracket of Pipes.

So I try with something simple

--  File

newtype H5FilePath = H5FilePath FilePath

instance DataResource H5FilePath where
  data OpenedDataResource H5FilePath = File
  acquireDataResource (H5FilePath f) = openFile (pack f) [ReadOnly] Nothing
  releaseDataResource = closeFile 

but I end up with this sort of error


src/Hkl/H5.hs:231:40: error:
    • Couldn't match type ‘File’ with ‘OpenedDataResource H5FilePath’
      Expected type: IO (OpenedDataResource H5FilePath)
        Actual type: IO File
    • In the expression: openFile (pack f) [ReadOnly] Nothing
      In an equation for ‘acquireDataResource’:
          acquireDataResource (H5FilePath f)
            = openFile (pack f) [ReadOnly] Nothing
      In the instance declaration for ‘DataResource H5FilePath’
    |
231 |   acquireDataResource (H5FilePath f) = openFile (pack f) [ReadOnly] Nothing
    |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/Hkl/H5.hs:232:25: error:
    • Couldn't match type ‘OpenedDataResource H5FilePath’ with ‘File’
      Expected type: OpenedDataResource H5FilePath -> IO ()
        Actual type: File -> IO ()
    • In the expression: closeFile
      In an equation for ‘releaseDataResource’:
          releaseDataResource = closeFile
      In the instance declaration for ‘DataResource H5FilePath’
    |
232 |   releaseDataResource = closeFile 
    |                         ^^^^^^^^^
cabal: Failed to build hkl-0.1.0.0 (which is required by exe:binoculars-ng
from hkl-0.1.0.0).

I do not understand why ? and more seriously, I do not know how to fix this.

thanks for your help

Frederic


More information about the Haskell-Cafe mailing list