首页 > 解决方案 > 未将类型识别为变压器堆栈内的单子的关联类型

问题描述

我一般都在玩 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期望 aWindowParameters ctx作为它的第二个参数,它是ContextHandler类的关联类型。但是由于某种原因,编译器没有看到这GLFW.WindowConfigWindowParameters为了这个堆栈。StateT从堆栈中扔掉Processor(如我正在学习的教程中一样)有效,编译:

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

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

标签: haskellmonad-transformers

解决方案


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))

推荐阅读