1

I am a beginner in Haskell and have a problem with the def of functions in Haskell. Values are functions in Haskell, right? (+3) 3 = 6 but what does (+3) (+3) mean. Does (+3) counts as an value ?

5
  • 4
    What? Read some material first. Commented May 14, 2015 at 17:12
  • 1
    (+3) (+3) won't work, unless you've made a -> a a Num instance. That being said, if you're interested in definitions, you should have a look at the Haskell report. Commented May 14, 2015 at 17:12
  • const x y = x negate x = -x we should calc const const(negate1)(negate2)3 At the first step my Tutor calculate const const(negate1) to const but Commented May 14, 2015 at 17:20
  • 2
    @EugeneSh. Have you ever wondered what was your mindstate before learning something? Perhaps when you are writing teaching material. I have and this questions allow me to see a little bit of myself in the past. And it is a valid question anyway, so why not let the search engines decide how relevant/good question it is. Commented May 14, 2015 at 17:41
  • 1
    @LayGonzález The right mind state before learning something should be the curiosity. I guess we have it here. But then comes the learning skill, that can be learned by itself. The very first learning skill should be: first things first. Commented May 14, 2015 at 17:48

2 Answers 2

3

(+3) isn't what you probably think it is. In other languages, this means the numerical value positive‑three. In Haskell it's the partial application of the + operator, and is a function taking one argument (the other number to add). It adds three to whatever it's given. So (+3) x is the application of the function (+3) to the value x and returns x+3. However, (+3) (+3) tries to add 3 to the function (+3) which doesn't make sense and gives a type error.

It might help to imagine replacing + with a regular function called plus:

plus x y = y + x

Then, (+3) is equivalent to plus 3, and (+3) 3 to (plus 3) 3 which is the same as plus 3 3. However, (+3) (+3) is equivalent to (plus 3) (plus 3) or plus 3 (plus 3) which doesn't make sense.

You might want to think about what (+3) . (+3) means. This chains together two applications of adding three, and is a single-argument function that adds six.

Sign up to request clarification or add additional context in comments.

Comments

1

(+3) (+3) is simply a type error and, therefore, it won't compile.

The type of (+3) is Int -> Int, which means that when applying an Int, we get another Int. It also means that you can only apply values of type Int!

So, you can't apply to (+3) a value of type Int -> Int, only values of type Int.

So yes, (+3) is value, but not of the right type to apply it to a function that expects a value of type Int.

Int can't be unified with Int -> Int because they have different type constructor, which makes them different types, as pointed out by @DanielWagner. The outermost constructor of Int -> Int is -> while the outermost constructor of Int is simply Int. It is sufficient that two types have different outermost constructor for considering them different.

10 Comments

thats make sense. but why does compose = foldr (.) id work foldr::(a -> b -> b) -> b -> [a] -> b but id type is b->b
The type of (.) is (b -> c) -> (a -> b) -> a -> c, which is the same as (b -> c) -> (a -> b) -> (a -> c). It makes sense, function compositions, takes 2 functions and returns a function. The type of foldr is (a -> b -> c) -> b -> [a] -> b. To remove ambiguity, lets rename the type variables to this: foldr :: (fa -> fb -> fb) -> fb -> [fa] -> fb. (.) :: (cb -> cc) -> (ca -> cb) -> (ca -> cc). Then, it's clear that the application foldr (.) has this type: (ca -> cb) -> [cb -> cb] -> (ca -> cb). Also: (.) :: x -> x. and finally, foldr (.) idis: [x -> x] -> (x -> x).
The key idea that you seem to be missing is that types of this kind: (cb -> cc) can be unified with type variables like fa; what I did was to exactly replace all fas with (cb -> cc). And something similar for fb&(ca->cb).
And also, if you have types like a=(x -> y) and b=(x -> z) and you unify a with b then, that unification will force y=z. That is why we ended with only 1 type variable, x; because the unifications that where done forced to all type variables to belong to the same type.
@LayGonzález Int -> Int does not have kind * -> *; it has kind *, just like Int. But the outer-most (type) constructor of Int -> Int is (->), while the outer-most constructor of Int is Int, and only type with the same outer-most constructor may unify.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.