Home > OS >  Haskell variable "not in scope: type variable 'm'"
Haskell variable "not in scope: type variable 'm'"

Time:10-04

It is not clear to me why the type declaration is failing to compile and I am not able to interpret the compiler error, what am I missing? Below is the code with the type definition and the compiler error.

{-# LANGUAGE RankNTypes, FlexibleContexts #-}
module API.Models.RunDB where

import Control.Monad.IO.Class (MonadIO, liftIO)
import Database.Persist.Sql (ConnectionPool, runSqlPool, SqlPersistT)
import Web.Scotty (ActionM)

type RunDB = MonadIO m => (forall a. SqlPersistT IO a -> m a)

runDB' :: ConnectionPool -> RunDB
runDB' pool q = liftIO $ runSqlPool q pool
RunDB.hs:8:22: error:
    Not in scope: type variable ‘m’
  |
8 | type RunDB = MonadIO m => (forall a. SqlPersistT IO a -> m a)
  |                      ^

CodePudding user response:

In the right-hand side of type synonyms, you can't just make up type variable names out of the blue the way you can in most other places. You'd need to explicitly quantify it by specifying forall m, like this:

type RunDB = forall m. MonadIO m => (forall a. SqlPersistT IO a -> m a)

There are two alternative solutions, too. One would be to pass m as a parameter to the type synonym, and then change runDB''s type to return a RunDB m instead:

type RunDB m = MonadIO m => (forall a. SqlPersistT IO a -> m a)
runDB' :: ConnectionPool -> RunDB m

Another would be to just get rid of the type synonym and write this:

runDB' :: MonadIO m => ConnectionPool -> SqlPersistT IO a -> m a
  • Related