类型族会导致不明确的变量错误



用例:我在使用基本相同状态的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]
}

每个玩家由我们和ussr代表。每个玩家都有一只手牌,从服务器的角度来看,它是全能的,并且知道玩家手中的每一张牌。但从美国玩家的角度来看,游戏状态看起来更像这个

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和serverToPlayerCEvent的方法。

问题是您的类型族不是内射的:例如,知道USHand aPrivate并不能确切地告诉您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,例如,在您希望aInt的情况下,您可以编写f (Proxy :: Proxy Int)

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

{-# 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 

最新更新