Quantcast

Change in type that I don't understand

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Change in type that I don't understand

Jan Erik Moström
Hi,

Here is something that I don't understand, why does the named value have
a different type than just the value itself?

Prelude> let f x = x * x
Prelude> :t f
f :: Num a => a -> a
Prelude> :t [f]
[f] :: Num a => [a -> a]
Prelude> let g = [f]
Prelude> :t g
g :: [Integer -> Integer]

= jem
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change in type that I don't understand

Daniel Trstenjak-2

Hi Jan,

> Here is something that I don't understand, why does the named value have a
> different type than just the value itself?

Looks like a bug in your ghci version.

With version 7.10.2 I'm getting:

> :t g
g :: Num a => [a -> a]


Grettings,
Daniel
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change in type that I don't understand

David McBride
In reply to this post by Jan Erik Moström
If you have the Monomorphism restriction set, it will choose types
based on the type defaulting rules (Num changes to Integer).  This is
because polymorphic code is slower than code that has concrete types.

Try :set -XNoMonomorphismRestriction, then test it again.

It may be that different versions of ghci have a different default for
that pragma.

On Fri, Jan 27, 2017 at 6:23 AM, Jan Erik Moström <[hidden email]> wrote:

> Hi,
>
> Here is something that I don't understand, why does the named value have a
> different type than just the value itself?
>
> Prelude> let f x = x * x
> Prelude> :t f
> f :: Num a => a -> a
> Prelude> :t [f]
> [f] :: Num a => [a -> a]
> Prelude> let g = [f]
> Prelude> :t g
> g :: [Integer -> Integer]
>
> = jem
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change in type that I don't understand

Daniel Trstenjak-2

On Fri, Jan 27, 2017 at 08:35:08AM -0500, David McBride wrote:
> If you have the Monomorphism restriction set, it will choose types
> based on the type defaulting rules (Num changes to Integer).  This is
> because polymorphic code is slower than code that has concrete types.

But why didn't the defaulting rules have been applied for both: 'f' and 'g'?

Greetings,
Daniel
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change in type that I don't understand

David McBride
Because f has an argument x, and g doesn't.

On Fri, Jan 27, 2017 at 10:17 AM, Daniel Trstenjak
<[hidden email]> wrote:

>
> On Fri, Jan 27, 2017 at 08:35:08AM -0500, David McBride wrote:
>> If you have the Monomorphism restriction set, it will choose types
>> based on the type defaulting rules (Num changes to Integer).  This is
>> because polymorphic code is slower than code that has concrete types.
>
> But why didn't the defaulting rules have been applied for both: 'f' and 'g'?
>
> Greetings,
> Daniel
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change in type that I don't understand

David McBride
In reply to this post by Daniel Trstenjak-2
Sorry let me elaborate.  The type of f depends on its argument.  The
argument could be anything that the caller passes in so long as it is
an instance of Num.  If the user passes in Int or Integer or Float, it
has to handle all those cases.  It can't just type restrict the
argument to Int, that is likely not what the user wanted.  If he had
he would have type restricted it himself.

g on the other hand has complete control over its own type.  If mm
restriction is enabled and there is no type declaration then it is
reasonable for it to default to a concrete type so that it only has to
generate only one possible code path.

If it isn't enabled then it will try to be as polymorphic as possible,
at the cost of being flexible enough to return any type that is an
instance of Num.

On Fri, Jan 27, 2017 at 10:17 AM, Daniel Trstenjak
<[hidden email]> wrote:

>
> On Fri, Jan 27, 2017 at 08:35:08AM -0500, David McBride wrote:
>> If you have the Monomorphism restriction set, it will choose types
>> based on the type defaulting rules (Num changes to Integer).  This is
>> because polymorphic code is slower than code that has concrete types.
>
> But why didn't the defaulting rules have been applied for both: 'f' and 'g'?
>
> Greetings,
> Daniel
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Change in type that I don't understand

Daniel Trstenjak-2

Thank you for the nice explanation!

Greetings,
Daniel
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...