我可以命名函数签名吗?

发布于 2024-11-16 12:09:38 字数 1055 浏览 2 评论 0原文

我正在传递一个部分应用的函数。完整的签名是:

import Data.Map as Map 
-- Update the correct bin of the histogram based on the min value, bin width,
-- the histogram stored as a map, and the actual value we are interested in.
updateHist :: Double -> Double -> Map.Map Bin Double -> Double -> 
              Map.Map Bin Double

该函数更新存储直方图数据的 Map。前两个参数给出了我们感兴趣的数据的下限,接下来是直方图的箱宽度。我在程序启动时填写这些值,并将部分应用的函数传递到整个模块。这意味着我有大量具有如下签名的函数:

-- Extra the data out of the string and update the histogram (in the Map) with it.
doSomething :: String -> (Map.Map Bin Double -> Double -> Map.Map Bin Double) -> 
               Map.Map Bin Double

这一切都很好,但编写“(Map.Map Bin Double -> Double -> Map.Map Bin Double)”相当冗长。我想将它们全部替换为“UpdateHistFunc”作为类型,但由于某种原因我一直失败。

我尝试过:

newtype UpdateHistFunc = Map.Map Bin Double -> Double -> Map.Map Bin Double

失败并出现以下错误:

HistogramForColumn.hs:84:44:输入“->”时解析错误

我做错了什么?

I'm passing around a partially applied function. The full signature is:

import Data.Map as Map 
-- Update the correct bin of the histogram based on the min value, bin width,
-- the histogram stored as a map, and the actual value we are interested in.
updateHist :: Double -> Double -> Map.Map Bin Double -> Double -> 
              Map.Map Bin Double

The function updates a Map which stores data for a histogram. The first two parameters give the bottom bounds of data we are interested, the next is the bin width for the histogram. I fill these values in when the program starts up and pass the partially applied function all over the module. This means I have a ton of functions with a signature like:

-- Extra the data out of the string and update the histogram (in the Map) with it.
doSomething :: String -> (Map.Map Bin Double -> Double -> Map.Map Bin Double) -> 
               Map.Map Bin Double

This is all fine and dandy, but writing "(Map.Map Bin Double -> Double -> Map.Map Bin Double)" is rather verbose. I'd like to replace them all with "UpdateHistFunc" as a type but for some reason I keep failing.

I tried:

newtype UpdateHistFunc = Map.Map Bin Double -> Double -> Map.Map Bin Double

This failed with the error:

HistogramForColumn.hs:84:44: parse error on input `->'

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2024-11-23 12:09:38

您是否在这里混淆了 typenewtype

使用 type 定义一个类型同义词,这似乎是您想要做的,而 newtype 创建一个需要构造函数名称的新类型,就像使用 data

换句话说,您可能想要这样:

type UpdateHistFunc = Map.Map Bin Double -> Double -> Map.Map Bin Double

...或者可能这样:

newtype UpdateHistFunc = UpdateHistFunc (Map.Map Bin Double -> Double -> Map.Map Bin Double)

后者显然需要“展开”才能应用该功能。


供参考:

  • data 定义了一种新的代数数据类型,它可以是递归的,具有不同的类型类实例,引入了可能的惰性的额外层,等等。
  • newtype 定义了一个数据类型,它带有一个带有单个参数的构造函数,它可以是递归的并且具有不同的实例,但仅用于类型检查;编译后,它相当于它包含的类型。
  • type 定义一个类型同义词,它不能递归或具有不同的实例,在类型检查时完全展开,相当于一个宏。

如果您想了解 datanewtype 之间在“额外惰性”方面的语义区别,请比较这两种类型以及它们可能具有的值:

data DType = DCon DType

newtype NType = NCon NType

例如,您认为这些函数如果分别应用于 undefinedDCon undefinedNCon undefined 会做什么?

fd (DCon x) = x
fn (NCon x) = x

Are you confusing type and newtype here?

Using type defines a type synonym, which is what you seem to be trying to do, whereas newtype creates a new type that needs a constructor name, like with data.

In other words, you probably want this:

type UpdateHistFunc = Map.Map Bin Double -> Double -> Map.Map Bin Double

...or maybe this:

newtype UpdateHistFunc = UpdateHistFunc (Map.Map Bin Double -> Double -> Map.Map Bin Double)

The latter obviously needs to be "unwrapped" in order to apply the function.


For reference:

  • data defines a new algebraic data type, which can be recursive, have distinct instances of type classes, introduces an extra layer of possible laziness, all that stuff.
  • newtype defines a data type with a single constructor taking a single argument, which can be recursive and have distinct instances, but only for type checking; after compilation, it's equivalent to the type it contains.
  • type defines a type synonym, which can't be recursive or have distinct instances, is fully expanded when type checking, and amounts to little more than a macro.

If you're wondering about the semantic distinction between data and newtype where "extra laziness" is concerned, compare these two types and the possible values they can have:

data DType = DCon DType

newtype NType = NCon NType

For instance, what do you think these functions will do if applied to undefined vs. DCon undefined and NCon undefined, respectively?

fd (DCon x) = x
fn (NCon x) = x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文