[Haskell-cafe] Unary functions and infix notation
Brandon Allbery
allbery.b at gmail.com
Fri Sep 6 17:37:01 CEST 2013
On Fri, Sep 6, 2013 at 11:04 AM, Johannes Emerich <johannes at emerich.de>wrote:
> Desugaring of an equivalent source file shows that id is applied to the
> anonymous function, which is then applied to 1.
>
> The following example of a function that is not polymorphic in its return
> type behaves closer to what I would have expected: It does not work.
>
> Prelude> let z = (\y -> True) :: a -> Bool
> Prelude> :t (`z` True)
>
> <interactive>:1:2:
> The operator `z' takes two arguments,
> but its type `a0 -> Bool' has only one
> In the expression: (`z` True)
>
> What is the purpose/reason for this behaviour?
>
Coming from another language, where functions aren't first class, you will
probably be used to the notion that a function type is somehow different
from any other type. You'll need to unlearn that for functional languages:
function types are just as "real" as (Integer) is, and if I have a type
variable somewhere which doesn't have constraints otherwise preventing it,
that type variable can end up being (Integer) or (a -> a) or (Num c => c ->
c -> c) or (Maybe [x]) or (Maybe (a -> a)) or any other (rank-1, i.e. no
internal "forall"s) type.
(id) has the type (a -> a); in the use mentioned in the first quoted
paragraph, this has unified (a) with (b -> b) to produce (id :: (b -> b) ->
(b -> b)) in order for the whole expression to be typeable. In your second
example, you don't have polymorphism "where it's needed" so it can't infer
a type that will work.
--
brandon s allbery kf8nh sine nomine associates
allbery.b at gmail.com ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130906/62616c0b/attachment.html>
More information about the Haskell-Cafe
mailing list