镜头变焦模糊变量

Lens zoom ambiguous variable

我在使用 Control.Lens 提供的 zoom 函数时遇到困难。使用我的自定义 monad 转换器 HearthMonad,我不知道如何满足 GHC 的 "ambiguous type" 投诉。

有问题的代码在 drawCard.

我该如何解决这个问题?我是否必须创建自己的自定义缩放运算符来处理 Hearth m 中的 Monad m


{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TemplateHaskell #-}


module EngineZoom where


--------------------------------------------------------------------------------


import Control.Applicative
import Control.Lens
import Control.Monad.State
import Data.List
import Data.Maybe


--------------------------------------------------------------------------------


type PlayerHandle = String


data Card = Card String
    deriving (Show, Eq, Ord)


data Player = Player {
    _playerHandle :: PlayerHandle,
    _hand :: [Card]
} deriving (Show, Eq, Ord)
makeLenses ''Player


data GameState = GameState {
    _gamePlayers :: [Player]
} deriving (Show, Eq, Ord)
makeLenses ''GameState


newtype Hearth m a = Hearth {
    unHearth :: StateT GameState m a
} deriving (Functor, Applicative, Monad, MonadState GameState, MonadIO, MonadTrans)


type HearthMonad = MonadIO


runHearth :: (HearthMonad m) => m ()
runHearth = evalStateT (unHearth runGame) mkGameState


mkGameState :: GameState
mkGameState = GameState { _gamePlayers = map mkPlayer ["Bob", "Joe"] }


mkPlayer :: PlayerHandle -> Player
mkPlayer handle = Player { _playerHandle = handle, _hand = [] }


runGame :: (HearthMonad m) => Hearth m ()
runGame = do
    card <- drawCard "Bob"
    liftIO $ print card


getPlayer :: PlayerHandle -> Lens' GameState Player
getPlayer handle f st = fmap put' get'
    where
        players = st^.gamePlayers
        put' player = let
            g p = case p^.playerHandle == handle of
                True -> player
                False -> p
            in set gamePlayers (map g players) st
        get' = f $ fromJust $ find (\p -> p^.playerHandle == handle) players


drawCard :: (HearthMonad m) => PlayerHandle -> Hearth m Card
drawCard handle = do
    let card = Card "Yeti"
    --getPlayer handle.hand <>= [card]
    zoom (getPlayer handle) $ hand <>= [card]
    return card

EngineZoom.hs:86:5:
    Could not deduce (Control.Lens.Internal.Zoom.Zoomed (Hearth m)
                      ~ Control.Lens.Internal.Zoom.Zoomed m0)
    from the context (HearthMonad m)
      bound by the type signature for
                 drawCard :: HearthMonad m => PlayerHandle -> Hearth m Card
      at EngineZoom.hs:82:13-60
    NB: `Control.Lens.Internal.Zoom.Zoomed' is a type function, and may not be injective
    The type variable `m0' is ambiguous
    Relevant bindings include
      drawCard :: PlayerHandle -> Hearth m Card
        (bound at EngineZoom.hs:83:1)
    In the expression: zoom (getPlayer handle)
    In a stmt of a 'do' block:
      zoom (getPlayer handle) $ hand <>= [card]
    In the expression:
      do { let card = Card "Yeti";
           zoom (getPlayer handle) $ hand <>= [card];
           return card }

问题是你的新类型只能保持一种状态,即GameState。变焦实质上将状态更改为镜头目标,但由于 Hearth 不能将 Player 作为状态,因此 zoom (getPlayer handle) 不能与 Hearth.[=17 一起使用=]

简单的解决方案是用 type Hearth = StateT GameState 替换 newtype 并且缩放有效。如果你想要一个新类型,你需要将状态参数化,这是一个例子:

import Control.Lens.Internal.Zoom

newtype HearthS s m a = Hearth {
    unHearth :: StateT s m a
} deriving (Functor, Applicative, Monad, MonadState s, MonadIO, MonadTrans)

type Hearth = HearthS GameState

type instance Zoomed (HearthS s m) = Focusing m
instance Monad z => Zoom (HearthS s z) (HearthS t z) s t where
  zoom l (Hearth m) = Hearth (zoom l m)