嵌套函数定义中的 Haskell 范围使用 where [英] Haskell scoping in nested function definitions using where

查看:31
本文介绍了嵌套函数定义中的 Haskell 范围使用 where的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell 在 where 定义中的范围界定有问题.当我有以下函数 f 时,我想将 x 传递给本地定义的函数 f1 而无需明确将其用作参数,我收到一条错误消息,指出 x 的类型与 f1 输出中的类型不兼容,尽管它应该是相同的:

<前>f :: 等式 a => a -> [a]f x = f1 x在哪里f1 :: 等式 a => a -> [a]f1 y = [ x, y ]

错误如下:

<前>无法将预期类型a1"与推断类型a"匹配`a1' 是一个刚性类型变量,由test.hs:4:11 中‘f1’的类型签名`a' 是一个刚性类型变量,由test.hs:1:8 中‘f’的类型签名在表达式中: x在表达式中:[x, y]在`f1'的定义中:f1 y = [x, y]失败,加载的模块:无.

然而,当我将 x 作为附加参数传递时,就像我在以下代码中使用 g 函数所做的那样,它工作正常:

<前>g :: 等式 a => a -> [a]g x = g1 x x在哪里g1 :: 等式 a => a -> a -> [a]g1 x y = [ x, y ]

有没有办法使f中的a类型与a(或a1) 在 f1?

解决方案

您的代码的问题是本地范围的 f1 类型签名.它指定 f1 可以采用任何类型

f1 :: Eq a =>->[a]

即使这是一个本地函数,您也已将这个函数概括为能够采用 f 中不存在的类型,无论此函数接收到的任何内容都必须来自 f,因此类型签名是不必要的.

只需删除 f1 类型签名.

把我的帖子读回自己,有点不清楚.f1 中的 a 是一个可以接受任何东西的参数化类型,但是传递给它的参数已经在 f 中绑定了.所以这个函数只能接收它的父函数接收的东西,你给它的类型签名打破了这个规则.希望这样说得更清楚一点.

I have a problem with Haskell's scoping in where definitions. When I have the following function f, where I want to pass the x to the locally defined function f1 without explicitely using it as a parameter, I get an error saying that the type of x is incompatible with the one in the output of f1, although it should be the same:

f :: Eq a => a -> [a]
f x = f1 x
    where
        f1 :: Eq a => a -> [a]
        f1 y = [ x, y ]

The error is the following:

    Couldn't match expected type `a1' against inferred type `a'
      `a1' is a rigid type variable bound by
           the type signature for `f1' at test.hs:4:11
      `a' is a rigid type variable bound by
          the type signature for `f' at test.hs:1:8
    In the expression: x
    In the expression: [x, y]
    In the definition of `f1': f1 y = [x, y]
Failed, modules loaded: none.

When I however pass the x as an additional parameter, as I did in the following code with the function g, it works fine:

g :: Eq a => a -> [a]
g x = g1 x x
    where
        g1 :: Eq a => a -> a -> [a]
        g1 x y = [ x, y ]

Is there a way to make the type a in f compatible to the type a (or a1) in f1?

解决方案

The problem with your code is the locally scoped f1 type signature. It specifies that f1 can take any type

f1 :: Eq a => a -> [a]

Even though this is a local function, you've generalized this function to be able to take a type that won't exist within f, whatever this function receives HAS to come from f, so the type signature is unnecessary.

Just remove the f1 type signature.

Edit: Read my post back to myself, it's a bit unclear. a in f1 is a parameterized type that can take anything, but the arguments passed to it are already bound in f. So this function can only receive what its parent function receives, the type signature you're giving it breaks that rule. Hope that's a little more clear.

这篇关于嵌套函数定义中的 Haskell 范围使用 where的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆