[Haskell-beginners] infinite type
Julian Rohrhuber
julian.rohrhuber at musikundmedien.net
Sun Jan 3 19:46:19 UTC 2016
Hi all, than you for your helpful replies.
>
> On 03.01.2016, at 16:03, Imants Cekusins <imantc at gmail.com> wrote:
>
> Hello Julian,
>
> Prelude> let f g = g . g
> Prelude> let sum x y = x + y
> Prelude> f sum
>
> If you are trying to call 'f' and see the result, args are missing.
>
> If you are trying to create new type, use 'let'.
>
> Prelude> (\x y -> x + y) . (\x y -> x + y)
>
> Similar story. Let or args
Hello Imants – same with “let” here:
Prelude> let f g = g . g
Prelude> let sum x y = x + y
Prelude> let sum' = f sum
<interactive>:43:14:
Occurs check: cannot construct the infinite type: a ~ a -> a
> On 03.01.2016, at 14:42, Joel Neely <joel.neely at gmail.com> wrote:
> I'm curious as to the intended meaning of
>
> let f g = g . g
>
> Without reading further down, I immediately thought: "composing g onto g must require that the argument and result types (domain and range) of g must be identical”.
yes, that domain and range are the same is implied when you rise a function "to a power” (which is what f does). I just assumed that arity and type are two different things in Haskell.
> Then, when I read
>
> f sum
>
> I don't know what that means, given that sum is [1] "a function of two arguments" yielding a single value (or, if I want to have my daily dose of curry, sum is [2] "a function of one argument yielding a (function of one argument yielding a single value)".
>
> With either reading, I don't know how to reconcile sum with the expectation on the argument to f.
As you wondered what I was trying to express, I was thinking along reading [2]. With:
Prelude> let z = sum 2
Prelude> let z' = f z
z' is a function with one argument, and yes, this works because z was one with a single argument.
But assuming that function composition (.) is a function too, I was curious what would happen if I applied it partially.
So an intermediate step would have been this:
let k = (\x y -> x + y) . (\x -> x + 1)
which is fine. It leaves a binary op function as I expected.
By contrast,
let k = (\x y -> x + y) . (\x y -> x + y)
returns Non type-variable argument in the constraint: Num (a -> a)
Instead of something like following which I had dared to expect:
let k = (\z -> (\x y -> x + y + z))
What really made me wonder then was how come that
f sum
would return not a "Non type-variable argument”, but "cannot construct the infinite type: a ~ a -> a”
Where does the infinity come from?
More information about the Beginners
mailing list