gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:28:53 26 4
gpt4 key购买 nike

用例:我正在 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 aa必须以某种方式进行猜测,但我们刚刚发现这是不可能的。

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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com