无法将类型识别为变压器堆栈内单子的关联类型



我正在玩OpenGL,特别是名为GPipe的Haskell库。我有一个底部有IO的 monad 变压器堆栈,然后是库中的ContextT变压器,然后是StateT,因为需要某种状态,最后是一个newtype Processor,因为简单的type会为这样的堆栈生成可怕的错误消息。这是一般的想法。但是,下面的代码不会键入检查:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Lens
import Control.Monad.State
import Control.Monad.Trans
import Control.Monad.Except
import qualified "GPipe" Graphics.GPipe as GP
import qualified "GPipe-GLFW" Graphics.GPipe.Context.GLFW as GLFW
---- State and Processor types ----
class ArtState os as | as -> os where
event :: GP.ContextHandler ctx => as -> Processor ctx os (as, Maybe e)
present :: GP.ContextHandler ctx => as -> Processor ctx os as
window :: Lens' as (WindowType os)
data ProgramState = ProgramState
newtype GP.ContextHandler ctx => Processor ctx os a = Processor {
runProcessor :: StateT ProgramState (GP.ContextT ctx os IO) a
}
---- MenuArt things ----
type WindowType os = GP.Window os GP.RGBFloat GP.Depth
data MenuArt os = MenuArt {
_maWindow :: WindowType os
}
makeLenses ''MenuArt
instance ArtState os (MenuArt os) where
event ms = Processor $ return (ms, Nothing)
present ms = Processor $ return ms
window = maWindow
initMenuArt :: (ArtState os a, GP.ContextHandler ctx) =>
Maybe a 
-> Processor ctx os (Either String (MenuArt os))
initMenuArt Nothing = Processor $ do
win <- lift $ GP.newWindow (GP.WindowFormatColorDepth GP.RGB8 GP.Depth16)
(GLFW.defaultWindowConfig "foobar")
return $ Right $ MenuArt {
_maWindow = win
}
initMenuArt (Just from) = Processor $ do
return $ Right $ MenuArt {
_maWindow = from ^. window
}
---- events ----
data UserEvent = CloseWindow

错误消息如下:

/tmp/testing/app/Main.hs:49:33: error:
• Couldn't match expected type ‘GP.WindowParameters ctx’
with actual type ‘GLFW.WindowConfig’
• In the second argument of ‘GP.newWindow’, namely
‘(GLFW.defaultWindowConfig "foobar")’
In the second argument of ‘($)’, namely
‘GP.newWindow
(GP.WindowFormatColorDepth GP.RGB8 GP.Depth16)
(GLFW.defaultWindowConfig "foobar")’
In a stmt of a 'do' block:
win <- lift
$ GP.newWindow
(GP.WindowFormatColorDepth GP.RGB8 GP.Depth16)
(GLFW.defaultWindowConfig "foobar")
• Relevant bindings include
initMenuArt :: Maybe a
-> Processor ctx os (Either String (MenuArt os))
(bound at app/Main.hs:47:1)
|
49 |                                (GLFW.defaultWindowConfig "foobar")
|                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

据我所知,newWindow期望WindowParameters ctx作为其第二个参数,这是ContextHandler类的关联类型。但是由于某种原因,编译器没有看到此堆栈WindowParametersGLFW.WindowConfig。从堆栈中丢弃StateTProcessor(如我正在完成的教程中(有效,这将编译:

main :: IO ()
main = do
GP.runContextT GLFW.defaultHandleConfig $ do
win <- GP.newWindow (GP.WindowFormatColor GP.RGB8) (GLFW.defaultWindowConfig "foobar")
return ()
return ()

我做错了什么,但不知道是什么。

initMenuArt正在使用GLFW.defaultWindowConfig,这是一个GLFW函数。

GPipe定义由ctx类型参数化的接口,GPipe-GLFW通过使用GLFW.Handle实例化ctx来实现该接口。

因此,initMenuArt应相应地进行专业化:

initMenuArt
:: (ArtState os a)
=> Maybe a 
-> Processor GLFW.Handle os (Either String (MenuArt os))

最新更新