gpt4 book ai didi

haskell - 我怎样才能弄清楚 p0 是什么?

转载 作者:行者123 更新时间:2023-12-03 09:45:53 25 4
gpt4 key购买 nike

我试图理解引用类型变量 p0 的编译器错误消息.在大多数情况下,错误消息会告诉我编译器正在调用什么 p0 ,类似于“p0 是受...约束的刚性类型变量”,但在这种情况下不是。

一般来说,如果编译器错误消息是指它已分配的类型变量(而不是我在类型签名中引用的类型变量),并且它没有告诉我类型变量绑定(bind)的位置,我该如何计算它出去?

{-# LANGUAGE TypeFamilies, FlexibleContexts, MultiParamTypeClasses #-}
import Data.List (minimumBy)
import Data.Ord (comparing)
import qualified Math.Geometry.Grid as G (Grid(..))
import qualified Math.Geometry.GridMap as GM (GridMap(..))
import Prelude hiding (lookup)

class Pattern p where
type Metric p
difference ∷ p → p → Metric p
makeSimilar ∷ p → Metric p → p → p

data SOM gm k p = SOM
{
sGridMap :: gm p,
sLearningFunction :: Int -> Int -> Metric p,
sCounter :: Int
}

foo
:: (Pattern p, Ord v, v ~ Metric p, GM.GridMap gm p, GM.GridMap gm v,
k ~ G.Index (GM.BaseGrid gm p), k ~ G.Index (GM.BaseGrid gm v)) =>
SOM gm k p -> p -> [(k, v)]
foo s p = GM.toList . GM.map (p `difference`) . sGridMap $ s

bar :: (Pattern p, Ord v, v ~ Metric p) => [(k, v)] -> k
bar ds = fst . minimumBy (comparing snd) $ ds

wombat
:: (Pattern p, Ord v, v ~ Metric p, GM.GridMap gm p, GM.GridMap gm v,
k ~ G.Index (GM.BaseGrid gm p), k ~ G.Index (GM.BaseGrid gm v)) =>
SOM gm k p -> p -> (k, [(k, v)])
wombat s p = (bar diffs, diffs)
where diffs = foo s p

这是错误:
λ> :l ../amy.hs
[1 of 1] Compiling Main ( ../amy.hs, interpreted )

../amy.hs:33:19:
Could not deduce (v ~ Metric p0)
from the context (Pattern p,
Ord v,
v ~ Metric p,
GM.GridMap gm p,
GM.GridMap gm v,
k ~ G.Index (GM.BaseGrid gm p),
k ~ G.Index (GM.BaseGrid gm v))
bound by the type signature for
wombat :: (Pattern p, Ord v, v ~ Metric p, GM.GridMap gm p,
GM.GridMap gm v, k ~ G.Index (GM.BaseGrid gm p),
k ~ G.Index (GM.BaseGrid gm v)) =>
SOM gm k p -> p -> (k, [(k, v)])
at ../amy.hs:(30,10)-(32,40)
`v' is a rigid type variable bound by
the type signature for
wombat :: (Pattern p, Ord v, v ~ Metric p, GM.GridMap gm p,
GM.GridMap gm v, k ~ G.Index (GM.BaseGrid gm p),
k ~ G.Index (GM.BaseGrid gm v)) =>
SOM gm k p -> p -> (k, [(k, v)])
at ../amy.hs:30:10
In the expression: bar diffs
In the expression: (bar diffs, diffs)
In an equation for `wombat':
wombat s p
= (bar diffs, diffs)
where
diffs = foo s p
Failed, modules loaded: none.

最佳答案

这有点猜测,但这里有:
p0p 重命名在 bar的类型签名。

bar :: (Pattern p, Ord v, v ~ Metric p) => [(k, v)] -> k

这里 p仅出现在 => 的左侧.仅限 kv可以从调用站点推断。可能有很多类型 pMetric 时给出相同的结果,并且编译器不能假设 pbarp 相同在 wombat , 即使 Metric p在这两种情况下都是一样的。

在这种情况下,我会将类型签名更改为
bar :: Ord v => [(k, v)] -> k

bar不使用任何其他约束。

如果在您的真实代码中 bar确实使用其他约束,我会 add a proxy argument (可能是 p 类型,如果我有一个适当类型的值,如 wombat 那样,或者 a -> pp -> a 等)来帮助类型检查器。

关于haskell - 我怎样才能弄清楚 p0 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15614379/

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