Haskell 个类型方案的声明
Haskell declarations to mach a type scheme
我想知道什么 Haskell 类型声明会匹配下面的类型方案?
∀a,b.H(a → b) ⇒ b
本文来自4.1 Unambiguity / A Theory of Overloading
稍后根据 chi 的回答进行编辑。
我试过这段代码,但我不能让它失败,因为它应该
class H f where
g :: f -> Bool
instance H (Integer -> Bool) where
g f = f 0
instance H (Char -> Bool) where
g f = f '1'
g (\x -> if x > 10 then True else False)
g (\x -> if x == '0' then True else False)
此外,我意识到对于此代码,无法通过添加 | b -> a
之类的函数依赖项来匹配此传播规则来使类型明确无误
(FH) H (a —> b),H (a' —> b) => a = a'
由于H
是类型类名,它可以接受多个实例
instance H (Int -> Bool) where
instance H (Char -> Bool) where
...
在这种情况下,如果我们有一个术语
x :: ∀a,b. H(a → b) ⇒ b
我们需要计算
x && True
我们需要进行类型检查x :: Bool
,但这只能确定b = Bool
,而a = Int
和a = Char
都可以使用。因此类型不明确。
我想知道什么 Haskell 类型声明会匹配下面的类型方案?
∀a,b.H(a → b) ⇒ b
本文来自4.1 Unambiguity / A Theory of Overloading
稍后根据 chi 的回答进行编辑。 我试过这段代码,但我不能让它失败,因为它应该
class H f where
g :: f -> Bool
instance H (Integer -> Bool) where
g f = f 0
instance H (Char -> Bool) where
g f = f '1'
g (\x -> if x > 10 then True else False)
g (\x -> if x == '0' then True else False)
此外,我意识到对于此代码,无法通过添加 | b -> a
之类的函数依赖项来匹配此传播规则来使类型明确无误
(FH) H (a —> b),H (a' —> b) => a = a'
由于H
是类型类名,它可以接受多个实例
instance H (Int -> Bool) where
instance H (Char -> Bool) where
...
在这种情况下,如果我们有一个术语
x :: ∀a,b. H(a → b) ⇒ b
我们需要计算
x && True
我们需要进行类型检查x :: Bool
,但这只能确定b = Bool
,而a = Int
和a = Char
都可以使用。因此类型不明确。