gpt4 book ai didi

sqlite - Haskell 变量 "not in scope: type variable ' m'"

转载 作者:行者123 更新时间:2023-12-04 14:02:59 28 4
gpt4 key购买 nike

我不清楚为什么类型声明无法编译并且我无法解释编译器错误,我错过了什么?下面是带有类型定义和编译器错误的代码。

{-# 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)
| ^

最佳答案

在类型同义词的右侧,您不能像在大多数其他地方那样突然组成类型变量名。您需要通过指定 forall m 来显式量化它,如下所示:

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

还有两种替代解决方案。一种方法是将 m 作为参数传递给类型同义词,然后更改 runDB' 的类型以返回 RunDB m:

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

另一种方法是去掉类型同义词,然后这样写:

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

关于sqlite - Haskell 变量 "not in scope: type variable ' m'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69431119/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com