Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Explicit specification of function types
(Brandon S. Allbery KF8NH)
2. Re: Explicit specification of function types (Sterling Clover)
3. Re: Explicit specification of function types (Tom Poliquin)
4. Re: Explicit specification of function types (Alexander Dunlap)
5. Re: Explicit specification of function types (Michael Mossey)
6. Re: Explicit specification of function types
(Brandon S. Allbery KF8NH)
7. Re: Explicit specification of function types (Zachary Turner)
----------------------------------------------------------------------
Message: 1
Date: Mon, 23 Mar 2009 23:10:58 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: Zachary Turner <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Skipped content of type multipart/alternative-------------- next part
--------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20090323/73ca8e72/PGP-0001.bin
------------------------------
Message: 2
Date: Mon, 23 Mar 2009 23:22:43 -0400
From: Sterling Clover <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: Zachary Turner <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed
I'd argue that this is good practice for beginners especially, but as
you get to be more practiced, your mileage may vary. Certainly, if
you're only writing first-order code, this is perhaps less useful.
But the moment you begin to pass around higher-order functions with
abandon, it becomes quite useful.
Say, for example, that I want to write a function to abstract a data-
handling pattern I've noticed. I have a number of records I want to
query. Each time, I first restrict them to those where a certain
field is present. Then, I sort them by some arbitrary ordering.
Finally, I return a list of records grouped by some key, as processed
by some combining function. Now there's a bunch of steps there to
think through, and I might not even have a good name for the function
yet to express what it does, but I can write the type straight off
the bat:
foo :: (Ord c) => (a -> Maybe b) -> (a -> a -> Ordering) -> (a -> c) -
> ([a] -> d) -> [d]
This at least begins to articulate the intent of this (admittedly,
made up, but not actually all that made-up) function, and for me at
least, helps me to think about what to write next.
Another way to think about this is sometimes you know the type of the
function you want simply from context: say that you have some list of
data, and you know you want to express a foldr over it. You may not
know where to begin with the function that you want to foldr, but you
do know, at least, that it needs to be of type a -> a -> b, and if
you figure out what the a is and what the b is, you're much closer to
being able to write the function itself.
Another advantage of this is that, once you've specified a correct
type signature, then rather than having a function that produces
something totally off the wall, but which you only realize elsewhere
in the program, you'll get a nice helpful type error right at the
point where you did something weird.
Cheers,
S.
On Mar 23, 2009, at 10:02 PM, Zachary Turner wrote:
> Everything I've read has said that it's generally considered good
> practice to specify the full type of a function before the
> definition. Why is this? It almost seems to go against the
> principles of type inference. Why let the compiler infer types if
> you're just going to tell it what types to use for everything? Ok
> well, not really for everything, you don't typically specify types
> for local bindings, but still why the inconsistency? I have a
> little experience with ML and F# and there you're encouraged to -
> not- specify types explicitly, and let the compiler infer them for
> you. In fact, it seems like it would be fairly common where you
> specify a type that is -less- general than the type that the
> compiler would infer for you, because the most general type might
> involve a combination of type classes used in various ways, and it
> may not always be obvious to the programmer how to specify it
> correctly.
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Mon, 23 Mar 2009 19:08:28 -0700
From: Tom Poliquin <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-15"
Zachary Turner wrote:
> Everything I've read has said that it's generally considered good practice
> to specify the full type of a function before the definition. Why is this?
I'm still a newbie but using (and leaving) the type definitions
helps me quickly recall what a routine is doing and also provides
handy documentation when I have to hack (uhh ... I mean modify)
the code weeks later.
I also agree that when coding having the definitions 'localize'
the type error messages resulting in faster development.
Tom
> It almost seems to go against the principles of type inference. Why let
> the compiler infer types if you're just going to tell it what types to use
> for everything? Ok well, not really for everything, you don't typically
> specify types for local bindings, but still why the inconsistency? I have
> a little experience with ML and F# and there you're encouraged to -not-
> specify types explicitly, and let the compiler infer them for you. In
> fact, it seems like it would be fairly common where you specify a type that
> is -less- general than the type that the compiler would infer for you,
> because the most general type might involve a combination of type classes
> used in various ways, and it may not always be obvious to the programmer
> how to specify it correctly.
------------------------------
Message: 4
Date: Mon, 23 Mar 2009 20:57:00 -0700
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: Tom Poliquin <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Mar 23, 2009 at 7:08 PM, Tom Poliquin <[email protected]> wrote:
> Zachary Turner wrote:
>> Everything I've read has said that it's generally considered good practice
>> to specify the full type of a function before the definition. Why is this?
>
> I'm still a newbie but using (and leaving) the type definitions
> helps me quickly recall what a routine is doing and also provides
> handy documentation when I have to hack (uhh ... I mean modify)
> the code weeks later.
I agree with this. It's quite useful to be able to just look at a
function and see what its type is without having to go into GHCi and
query it. This becomes less useful for local-er variables because
their type can often be inferred from context and they are not
generally used elsewhere in the function.
Alex
------------------------------
Message: 5
Date: Mon, 23 Mar 2009 21:21:42 -0700
From: Michael Mossey <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I'm a beginner just learning about type inference (through getting lots
of error messages and using :t a lot). I find it fascinating, and it
seems a shame to disregard this power by declaring all types. However I
realize that it's the more global or critical types that should be
declared, and more local areas can be left to inference. Perhaps also
the compiler's ability to infer is related to its ability to detect
compile-time type errors. In other words, if you program a compiler to
be sensitive enough to detect all type errors at compile time, then by
necessity you are giving it the smarts to infer types. Is that right?
Thanks,
Mike
------------------------------
Message: 6
Date: Tue, 24 Mar 2009 00:28:17 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: Michael Mossey <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On 2009 Mar 24, at 0:21, Michael Mossey wrote:
> I'm a beginner just learning about type inference (through getting
> lots of error messages and using :t a lot). I find it fascinating,
> and it seems a shame to disregard this power by declaring all types.
> However I realize that it's the more global or critical types that
> should be declared, and more local areas can be left to inference.
> Perhaps also the compiler's ability to infer is related to its
> ability to detect compile-time type errors. In other words, if you
> program a compiler to be sensitive enough to detect all type errors
> at compile time, then by necessity you are giving it the smarts to
> infer types. Is that right?
It may have the knowledge but not the mechanism (consider C++).
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20090324/857a507d/PGP-0001.bin
------------------------------
Message: 7
Date: Mon, 23 Mar 2009 23:41:57 -0500
From: Zachary Turner <[email protected]>
Subject: Re: [Haskell-beginners] Explicit specification of function
types
To: "Brandon S. Allbery KF8NH" <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
2009/3/23 Brandon S. Allbery KF8NH <[email protected]>
> On 2009 Mar 24, at 0:21, Michael Mossey wrote:
>
>> I'm a beginner just learning about type inference (through getting lots of
>> error messages and using :t a lot). I find it fascinating, and it seems a
>> shame to disregard this power by declaring all types. However I realize that
>> it's the more global or critical types that should be declared, and more
>> local areas can be left to inference. Perhaps also the compiler's ability to
>> infer is related to its ability to detect compile-time type errors. In other
>> words, if you program a compiler to be sensitive enough to detect all type
>> errors at compile time, then by necessity you are giving it the smarts to
>> infer types. Is that right?
>>
>
>
> It may have the knowledge but not the mechanism (consider C++).
>
I guess now would be a bad time to mention that C++0x has type inference :D
Off topic, but couldn't resist. Anyway, back to the original topic (kind
of), would you consider it bad advice to omit type annotations in languages
like ML or F# as well? I can identify with both arguments, and I just
wonder why one group of people would recommend omitting them, and another
would recommend supplying them, with the only difference being the
language. I know in something like F# you have a fairly advanced IDE, so
you can simply hover the mouse over a function name and get its inferred
type signature. I know you mentioned the performance penalty associated
with a type being "too generic", in the sense that typeclass lookups will
constantly have to be done at runtime if something is too generic. Perhaps
the fact that these other languages don't support typeclasses and hence
don't have the same performance penalty is part of the reason why it's ok
(or at the very least, not as bad) to do this in these languages as it is in
Haskell?
I'm mostly just trying to understand why it's good in one language, and bad
in another. I'm sure there must be a number of tangible advantages in each
language, that either don't apply or are mapped to disadvantages in the
other.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090323/3e69ce72/attachment.htm
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 9, Issue 28
****************************************