用例:我正在ghcjs前端和后端中编写游戏,它们使用基本相同的状态,以便可以在两侧编码游戏规则并与状态变化进行通信。为此,游戏状态看起来像

data GameState = GameState {
  gameTurn :: Int,            -- | Everyone sees
  gamePhase :: GamePhase,     -- | this
  boardState :: BoardState,   -- | stuff
  -- a lot more stuff everyone can see, followed by
  usHand :: [Card],
  ussrHand :: [Card]
}


每个玩家由我们和苏联代表。每个玩家都有一手牌,从服务器角度看,它是无所不能的,并且知道两个玩家手里的每张牌。但是从美国玩家的角度来看,游戏状态看起来更像这样

data GameState = GameState {
  -- public stuff
  usHand :: [Card],
  ussrHand :: Int
}


也就是说,他可以看到自己的手,但是只能看到对手拥有多少张牌。观察者看到的甚至更少。游戏规则很复杂,并且可能会发生很多事情,因此最好对规则进行一次编码,这样会影响玩家手部的规则,例如发新牌,强迫玩家出示新的规则。诸如此类的纸牌会影响每只手,这取决于他们是谁。为此,我最终使用类型族编写了以下内容,但这是行不通的

{-# LANGUAGE  TypeFamilies, RankNTypes #-}
module Test where

data Card = Card
data BoardState = BoardState
data GamePhase = GamePhase
data Country
data Player = PUS | PUSSR

data US
data USSR
data Observer
data Server

data Private = Private Int
data Public = Public [Card]

class HandType a where
  type USHand   a :: *
  type USSRHand a :: *
  toUS :: Public -> USHand a
--   toUSSR :: Public -> USSRHand a -- TODO

instance HandType Server where
  type USHand Server = Public
  type USSRHand Server = Public
  toUS (Public cs) = Public cs

instance HandType US where
  type USHand US = Public
  type USSRHand US = Private
  toUS (Public cs) = Public cs

instance HandType USSR where
  type USHand USSR = Private
  type USSRHand USSR = Public
  toUS (Public cs) = Private (length cs)

instance HandType Observer where
  type USHand Observer = Private
  type USSRHand Observer = Private
  toUS (Public cs) = Private (length cs)

data GameState a = GameState {
  gameTurn :: Int,            -- | Everyone sees
  gamePhase :: GamePhase,     -- | this
  boardState :: BoardState,   -- | stuff

  usHand :: USHand a,
  ussrHand :: USSRHand a
}

data Event a =
    PlaceInfluence Player Int Country -- | Most plays don't affect
  | PlayCard Player Card              -- | either hand
  | DealCards (USHand a) (USSRHand a) -- | This one does

-- Works
obsEvents :: [Event US]
obsEvents = [PlayCard PUS Card, PlayCard PUSSR Card, DealCards (Public [Card]) (Private 3)]

-- Works
serverEvents :: [Event Server]
serverEvents = [PlayCard PUS Card, PlayCard PUSSR Card, DealCards (Public [Card, Card]) (Public [Card])]

-- The server must send out the gamestate modified for the player's consumption.
-- serverToPlayerGS :: GameState Server -> GameState a
serverToPlayerGS (GameState turn phase bs us ussr) =
  GameState turn phase bs (toUS us) undefined -- | <- Doesn't work (line 75)

-- serverToPlayerEvent :: Event Server -> Event a
serverToPlayerEvent (PlaceInfluence p amt c) = PlaceInfluence p amt c
serverToPlayerEvent (PlayCard p c) = PlayCard p c
serverToPlayerEvent (DealCards us ussr) =
  DealCards (toUS us) undefined -- | <- Doesn't work (line 78)


第75行和第78行的错误均与

Couldn't match expected type ‘USHand a’
            with actual type ‘USHand a0’
NB: ‘USHand’ is a type function, and may not be injective
The type variable ‘a0’ is ambiguous
Relevant bindings include
  serverToPlayerGS :: GameState Server -> GameState a
    (bound at src/Test4.hs:74:1)
In the fourth argument of ‘GameState’, namely ‘(toUS us)’
In the expression: GameState turn phase bs (toUS us) undefined


或者如果我省略类型声明

Could not deduce (USHand a0 ~ USHand a1)
from the context (HandType a1,
                  USHand a1 ~ USHand a,
                  USHand t ~ Public)
  bound by the inferred type for ‘serverToPlayerGS’:
             (HandType a1, USHand a1 ~ USHand a, USHand t ~ Public) =>
             GameState t -> GameState a
  at src/Test4.hs:(74,1)-(75,45)
NB: ‘USHand’ is a type function, and may not be injective
The type variable ‘a0’ is ambiguous
Expected type: USHand a
  Actual type: USHand a0
When checking that ‘serverToPlayerGS’ has the inferred type
  serverToPlayerGS :: forall t a a1.
                      (HandType a1, USHand a1 ~ USHand a, USHand t ~ Public) =>
                      GameState t -> GameState a
Probable cause: the inferred type is ambiguous


我在网站上看到了其他一些类似的答案,但是我不确定所解释的修补程序最终将如何导致我希望得到的答案,这是一种以类型检查和有用的方式编写serverToPlayerGS和serverToPlayerEvent的方法。

最佳答案

问题是您的类型族不是内射性的:例如知道USHand aPrivate并不能告诉您确切的a是什么:它可能是USSR但也可能是Observer,因为两者实例声明:

type USHand USSR = Private
type USHand Observer = Private


由于函数toUS的类型为Public -> USHand a,因此必须以某种方式猜测a,而我们发现这是不可能的。

为了解决此问题,您将需要引入代理。代理是一种简单的数据类型,定义为:

data Proxy a = Proxy


如果您具有Haskell无法为其猜测f :: F a的函数a,则可以将其转换为f :: Proxy a -> F a,以便能够在调用站点上指定要编写的a含义。如果希望f (Proxy :: Proxy Int)a,则为实例Int

您将需要作用域类型变量,因为将与a一起使用的toUs来自函数的类型注释。因此,您应该在文件顶部添加以下两行:

{-# LANGUAGE ScopedTypeVariables #-}
import Data.Proxy


然后将toUS的类型从Public -> USHand a更改为:

toUS :: Proxy a -> Public -> USHand a


不要忘记在_的所有实例声明中添加虚拟参数toUs。最后,您可以像这样修补serverToPlayerGS的定义:

serverToPlayerGS :: forall a. HandType a => GameState Server -> GameState a
serverToPlayerGS (GameState turn phase bs us ussr) =
  GameState turn phase bs (toUS (Proxy :: Proxy a) us) undefined

关于haskell - 类型族导致模棱两可的变量错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36268734/

10-11 22:34
查看更多