Can I name a function signature?
Asked Answered
A

1

10

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?

Angele answered 17/6, 2011 at 21:37 Comment(0)
R
17

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
Rosettarosette answered 17/6, 2011 at 21:40 Comment(6)
@Tim Perry: It's understandable. The meaning of newtype is surprisingly subtle and it's easy to mix it up with the others, especially when most explanations emphasize the operational characteristics (which mostly imply the conceptual differences, but that's not immediately obvious).Rosettarosette
Do you think this question is worth leaving lurking on StackOverflow? Your elucidation of data, type and newtype was very helpful to me. Otherwise I'd vote to delete this. I guess I didn't fully grasp that newtype needs a constructor. I was hung up on the fact that "stuff" defined with newtype was equivalent to the type it contains after compilation. I didn't realize that the "stuff" was a new data type and thus needed a constructor. I really needed a type synonym. I guess I've talked myself into leaving this question up. Thanks @camccann, as always, for your detailed answer.Angele
@Tim Perry: Why does any question get left, once the solution is provided? In some ways, seemingly-silly mistakes made by people who are neither beginners nor experts have the most useful answers, because they indicate concepts where existing material may not be clear enough. Imagine someone makes the same mistake you did, and uses Google to search for "parse error on input" newtype. That query already brings up this answer as the second result!Rosettarosette
Cool -- my question is almost famous!Angele
I wonder why does Haskell provide the newtype keyword at all... It's equivalent to data with a single strict field, isn't it?Frei
@Rotsor: My impression is that newtype was far more distinct/different once, when the language was first defined, and it's not really something that can easily just be removed. But I'm fairly new to Haskell, relatively speaking, so you'd be better off asking someone who's been around for more than a couple years.Rosettarosette

© 2022 - 2024 — McMap. All rights reserved.