Re: varying number of arguments restriction

2001-10-31 Thread Fergus Henderson

On 30-Oct-2001, Hal Daume <[EMAIL PROTECTED]> wrote:
> I'm curious why the following code is invalid (from a language design
> point of view):
> 
> > foo :: [(Int, String)] -> String
> > foo [] = ""
> > foo = snd . head
> 
> ghc complains:
> 
> Varying number of arguments for function `foo'
> 
> I don't understand why this should be invalid?

While it would be possible to define a semantics for such code,
I suspect that this sort of thing commonly arises as a result
of accidental errors in Haskell programs.

Allowing such programs would therefore (a) let some unintended errors
slip past the compiler and (b) result in much more obscure error messages
(from the type checker) for some other unintended errors.

Since it's easy for the user to write function definitions so that all
clauses have the same number of arguments, and since requiring this helps
the compiler give better error messages and/or catch errors earlier,
I think it makes good sense to do so.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  | "... it seems to me that 15 years of
The University of Melbourne | email is plenty for one lifetime."
WWW:   | -- Prof. Donald E. Knuth

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: varying number of arguments restriction

2001-10-30 Thread Brian Boutel

Ashley Yakeley wrote:
> 
> At 2001-10-30 11:01, Hal Daume wrote:
> 
> >obviously i can rewrite:
> >
> >foo [] = ""
> >foo s  = (snd . head) s
> >
> >but this is uglier.
> 
> I'm not sure. I actually prefer it written out so that the number of
> arguments in the cases matches (as GHC enforces).
> 

It's defined in the Report, not a GHC idiosyncracy. As to why, I don't
really remember, but I suspect it had to do with a desire by some
members of the Haskell Committee to require that the patterns in all
clauses of a function binding were disjoint, so that reasoning about
programs could deal with each clause independently. This was not
adopted, and the alternative top-to-bottom, left-to-right, semantics
were, but there was still a feeling that good style demanded
disjointness.

In that style, the second clause could be written

foo ((x,y):xs) = y

I don't know whether this is still true, but it used to be argued that
this was likely to be more efficient because compilers could produce
really good pattern-matching code.

--brian

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: varying number of arguments restriction

2001-10-30 Thread Ashley Yakeley

At 2001-10-30 11:01, Hal Daume wrote:

>obviously i can rewrite:
>
>foo [] = ""
>foo s  = (snd . head) s
>
>but this is uglier.

I'm not sure. I actually prefer it written out so that the number of 
arguments in the cases matches (as GHC enforces).

-- 
Ashley Yakeley, Seattle WA


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell