我正在使用OpenGL,特别是名为GPipe的Haskell库。我有一个monad变换器堆栈,底部是IO
,然后是库中的ContextT
转换器,然后是StateT
,因为需要一些状态,最后是newtype Processor
因为simple 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
类的关联类型。但由于某种原因,编译器没有看到此堆栈的GLFW.WindowConfig
为WindowParameters
。从堆栈中丢弃StateT
和Processor
(如我正在编写的教程中),这可以编译:
main :: IO ()
main = do
GP.runContextT GLFW.defaultHandleConfig $ do
win <- GP.newWindow (GP.WindowFormatColor GP.RGB8) (GLFW.defaultWindowConfig "foobar")
return ()
return ()
我做错了什么,但无法弄清楚是什么。
答案 0 :(得分:3)
initMenuArt
正在使用GLFW.defaultWindowConfig
,这是一个GLFW函数。
GPipe
定义了由ctx
类型参数化的接口,GPipe-GLFW
通过使用ctx
实例化GLFW.Handle
来实现该接口。
因此initMenuArt
应该是专门的:
initMenuArt
:: (ArtState os a)
=> Maybe a
-> Processor GLFW.Handle os (Either String (MenuArt os))