类型族导致模棱两可的变量错误

Type families causes ambiguous variable error

用例:我正在 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

如果您有一个 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