使用案例:我在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的方法用于检查并且有用的方式。
答案 0 :(得分:5)
问题在于您的类型系列不是唯一的:知道USHand a
Private
例如并不能确切地告诉您a
是什么:它可能是USSR
但它也可能是Observer
,因为两个实例都声明:
type USHand USSR = Private
type USHand Observer = Private
因为函数toUS
的类型为Public -> USHand a
,所以必须以某种方式猜测a
,我们只是看到它不可能。
为了解决这个问题,您需要引入代理。代理是一种简单的数据类型,定义如下:
data Proxy a = Proxy
如果您有一个函数f :: F a
,Haskell无法猜测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