Haskell:尝试使用泛型时出现类型错误



我是一个Haskell初学者,当我编译Haskell源代码时,当我试图让一个函数实现一个lazy至少只要

module Main where
import Data.Maybe
main = undefined
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
foldWhilel f acc xs = go acc xs
where
go :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
go acc [] = Just acc
go acc (x:xs) = if isNothing new_acc then Just acc else go (fromJust new_acc) xs
where
new_acc :: Maybe b
new_acc = f x acc

的这个错误讨厌愚蠢的无意义的错误,考虑到(至少就我所知),这应该是完美的工作。因为函数go和函数参数在类型注释

中设计得很好
dist-newstyle/gh.hs:15:19: error:
• Couldn't match type ‘b2’ with ‘b’
‘b2’ is a rigid type variable bound by
the type signature for:
new_acc :: forall b2. Maybe b2
at dist-newstyle/gh.hs:14:9-26
‘b’ is a rigid type variable bound by
the type signature for:
foldWhilel :: forall a b.
(a -> b -> Maybe b) -> b -> [a] -> Maybe b
at dist-newstyle/gh.hs:7:1-56
Expected type: Maybe b2
Actual type: Maybe b
• In the expression: f x acc
In an equation for ‘new_acc’: new_acc = f x acc
In an equation for ‘go’:
go acc (x : xs)
= if isNothing new_acc then Just acc else go (fromJust new_acc) xs
where
new_acc :: Maybe b
new_acc = f x acc
• Relevant bindings include
new_acc :: Maybe b2 (bound at dist-newstyle/gh.hs:15:9)
f :: a -> b -> Maybe b (bound at dist-newstyle/gh.hs:8:12)
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
(bound at dist-newstyle/gh.hs:8:1)
|
15 |         new_acc = f x acc
|                   ^^^^^^^
dist-newstyle/gh.hs:15:21: error:
• Couldn't match expected type ‘a’ with actual type ‘a1’
‘a1’ is a rigid type variable bound by
the type signature for:
go :: forall b1 a1. b1 -> [a1] -> Maybe b1
at dist-newstyle/gh.hs:10:5-29
‘a’ is a rigid type variable bound by
the type signature for:
foldWhilel :: forall a b.
(a -> b -> Maybe b) -> b -> [a] -> Maybe b
at dist-newstyle/gh.hs:7:1-56
• In the first argument of ‘f’, namely ‘x’
In the expression: f x acc
In an equation for ‘new_acc’: new_acc = f x acc
• Relevant bindings include
xs :: [a1] (bound at dist-newstyle/gh.hs:12:15)
x :: a1 (bound at dist-newstyle/gh.hs:12:13)
go :: b1 -> [a1] -> Maybe b1 (bound at dist-newstyle/gh.hs:11:5)
f :: a -> b -> Maybe b (bound at dist-newstyle/gh.hs:8:12)
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
(bound at dist-newstyle/gh.hs:8:1)
|
15 |         new_acc = f x acc
|                     ^
dist-newstyle/gh.hs:15:23: error:
• Couldn't match expected type ‘b’ with actual type ‘b1’
‘b1’ is a rigid type variable bound by
the type signature for:
go :: forall b1 a1. b1 -> [a1] -> Maybe b1
at dist-newstyle/gh.hs:10:5-29
‘b’ is a rigid type variable bound by
the type signature for:
foldWhilel :: forall a b.
(a -> b -> Maybe b) -> b -> [a] -> Maybe b
at dist-newstyle/gh.hs:7:1-56
• In the second argument of ‘f’, namely ‘acc’
In the expression: f x acc
In an equation for ‘new_acc’: new_acc = f x acc
• Relevant bindings include
acc :: b1 (bound at dist-newstyle/gh.hs:12:8)
go :: b1 -> [a1] -> Maybe b1 (bound at dist-newstyle/gh.hs:11:5)
f :: a -> b -> Maybe b (bound at dist-newstyle/gh.hs:8:12)
foldWhilel :: (a -> b -> Maybe b) -> b -> [a] -> Maybe b
(bound at dist-newstyle/gh.hs:8:1)
|
15 |         new_acc = f x acc
|           

修复该错误的最简单方法是删除gonew_acc的类型签名。

你不能这样使用类型签名,因为foldWhilel签名中使用的bnew_acc签名中使用的b是两个完全不同的变量,只是碰巧有相似的名称。最重要的是,go签名中的(a -> b -> Maybe b)是错误的(go没有这样的参数,它是从外部作用域使用f)。

如果您希望在整个函数中使用具有相同含义的b,则需要启用并应用作用域类型变量:

{-# Language ScopedTypeVariables #-}
import Data.Maybe
foldWhilel :: forall a b. (a -> b -> Maybe b) -> b -> [a] -> Maybe b
foldWhilel f acc xs = go acc xs
where
go :: b -> [a] -> Maybe b
go acc [] = Just acc
go acc (x:xs) = if isNothing new_acc then Just acc else go (fromJust new_acc) xs
where
new_acc :: Maybe b
new_acc = f x acc

演示

相关内容

最新更新