Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Separate class and instance in different modules in Haskell?

In one module, I define a class:

class Monad m => MonadTime m where
    currentTime :: m UTCTime

The MonadTime is exported.

In the different module, I wanted to create an instance:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

-- MonadTime is imported

instance MonadTime IO where
    currentTime :: IO UTCTime
    currentTime = Data.Time.Clock.getCurrentTime

However, the issue is:

‘currentTime’ is not a (visible) method of class ‘MonadTime’

If I move the instance to the first module, where class is defined, everything works. But my idea was to provide the implementation in main module.

How should I resolve this?

>Solution :

You need to export it with the method, so:

module MyModule(MonadTime(currentTime)) where

class Monad m => MonadTime m where
    currentTime :: m UTCTime

and in the other module:

module MyOtherModule where

import MyModule(MonadTime(currentTime))

instance MonadTime IO where
    currentTime :: IO UTCTime
    currentTime = Data.Time.Clock.getCurrentTime
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading