English 中文(简体)
函数无法匹配类型
原标题:function could not match type

我的职责如下:

    foo :: Int -> a -> [a]
    foo n v = bar n
      where
        bar :: Int -> [a]
        bar n = take n $ repeat v

使用 ghci 报告此错误 :

    Couldn t match type `a  with `a1 
          `a  is a rigid type variable bound by
              the type signature for foo :: Int -> a -> [a] at hs99.hs:872:1
          `a1  is a rigid type variable bound by
              the type signature for bar :: Int -> [a1] at hs99.hs:875:9
    Expected type: [a1]
        Actual type: [a]
    In the expression: take n $ repeat v
    In an equation for `bar : bar n = take n $ repeat v

如果删除条形声明类型, 代码可以毫无错误地编译 。 这里的条形声明类型是什么? 为什么会发生错误, 因为条形声明类型比条形定义( 条形声明与某类相联) 更加通用?

谢谢你的帮助!

最佳回答

a in

foo :: Int -> a -> [a]

a in

    bar :: Int -> [a]

具有相同名称的不同类型变量。

要获得您期待的行为, 请打开“ 范围 Type 可变可定义扩展名( 例如, 在源文件顶部插入 < code_ - # LANGUAGE 范围 Type可变性 #- / code> ), 并更改 < code> foo 的类型符号

foo :: forall a. Int -> a -> [a]

当未启用“ ServedTypeVariables” 时, 它仿佛您原代码是像这样写入的 :

foo :: forall a. Int -> a -> [a]
foo n v = bar n
  where
    bar :: forall a. Int -> [a]
    bar n = take n $ repeat v

如果忽略了 bar 的批注类型,则不能说ghci隐含地使用ClosedTypeVariables。

相反,您给 bar 提供的批注类型与 ghci 推论类型有冲突 -- -- 您所主张的 bar 具有一种ghci知道它不能拥有的类型 。

删除批注类型时,将排除冲突。

范围 type 可更改您所提供的类型说明的含义。 它不会影响 ghc 的推断类型 。

问题回答

刚刚发现这条线索也有一个很好的解释:http://www.haskell.org/pipermail/haskell-cafe/2008-June/044617.html





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签