gpt4 book ai didi

haskell - 添加 ghci 的推断类型签名会导致错误

转载 作者:行者123 更新时间:2023-12-03 15:10:43 24 4
gpt4 key购买 nike

编辑:这是一个非常简单的例子。下面这个例子的动机。

这编译:

{-# LANGUAGE TypeFamilies #-}

type family F a b

f :: a -> F a b
f = undefined

f' [a] = f a

ghci 报告说:
*Main> :t f'
f' :: [a] -> F a b

但是如果我们将这个类型签名添加到上面的文件中,它会提示:
*Main> :r
[1 of 1] Compiling Main ( test.hs, interpreted )

test.hs:9:14:
Couldn't match type `F a b0' with `F a b'
NB: `F' is a type function, and may not be injective
In the return type of a call of `f'
In the expression: f a
In an equation for `f'': f' [a] = f a
Failed, modules loaded: none.

是什么赋予了?

动机:

看到后 this question ,我以为我会成为一个聪明的亚历克并写一个小笑话解决方案。攻击计划是从类型级别的数字开始(像往常一样),然后编写一个小的类型级别函数 Args n a c产生采用 n 的函数类型 a 的副本并产生 c .然后我们可以为各种 n 编写一个小类型类。并在路上。这是我想写的:
{-# LANGUAGE TypeFamilies #-}

data Z = Z
data S a = S a

type family Args n a c
type instance Args Z a c = c
type instance Args (S n) a c = a -> Args n a c

class OnAll n where
onAll :: n -> (b -> a) -> Args n a c -> Args n b c

instance OnAll Z where
onAll Z f c = c

instance OnAll n => OnAll (S n) where
onAll (S n) f g b = onAll n f (g (f b))

我惊讶地发现这没有进行类型检查!

最佳答案

这是一个 GHC 错误,可以通过以下甚至进一步简化的示例来证明:

type family F a

f :: b -> F a
f = undefined

f' :: b -> F a
f' a = f a

我建议向 GHC 总部报告。

关于haskell - 添加 ghci 的推断类型签名会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8531997/

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