我可以命名函数签名吗?
我正在传递一个部分应用的函数。完整的签名是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在这里混淆了
type
和newtype
?使用
type
定义一个类型同义词,这似乎是您想要做的,而newtype
创建一个需要构造函数名称的新类型,就像使用data
。换句话说,您可能想要这样:
...或者可能这样:
后者显然需要“展开”才能应用该功能。
供参考:
data
定义了一种新的代数数据类型,它可以是递归的,具有不同的类型类实例,引入了可能的惰性的额外层,等等。newtype
定义了一个数据类型,它带有一个带有单个参数的构造函数,它可以是递归的并且具有不同的实例,但仅用于类型检查;编译后,它相当于它包含的类型。type
定义一个类型同义词,它不能递归或具有不同的实例,在类型检查时完全展开,相当于一个宏。如果您想了解
data
和newtype
之间在“额外惰性”方面的语义区别,请比较这两种类型以及它们可能具有的值:例如,您认为这些函数如果分别应用于
undefined
与DCon undefined
和NCon undefined
会做什么?Are you confusing
type
andnewtype
here?Using
type
defines a type synonym, which is what you seem to be trying to do, whereasnewtype
creates a new type that needs a constructor name, like withdata
.In other words, you probably want this:
...or maybe this:
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
andnewtype
where "extra laziness" is concerned, compare these two types and the possible values they can have:For instance, what do you think these functions will do if applied to
undefined
vs.DCon undefined
andNCon undefined
, respectively?