argument counts...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

argument counts...

Gregory Guthrie

I have a simple function:

       revFn (x:xs) = (revFn xs) . (x:)

 

Of course GHCi correctly infers the type as:   revFn :: [a] -> [a] -> c

 

Adding the base case:

    revFn [] xs = xs

 

Now gives an error;

  “Equations for ‘revF’ have different numbers of arguments”

 

Of course this can be “fixed” by either adding the cancelled argument to the first clause, or converting the base case to only have one explicit argument, and a RHS of a lambda or identity function.

 

But since the interpreter already correctly inferred that the first clause has two arguments (with only one explicit), why does it then ignore this and give an error when the second clause shows two explicit arguments? The types are all correct in either case – why require explicit arguments?

 

Or, perhaps I am missing something simple?


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: argument counts...

Zemyla
You need to turn the second case into a lambda:

revFn [] = \xs -> xs

Or more succinctly,

revFn [] = id

On Mon, Nov 20, 2017 at 11:13 AM, Gregory Guthrie <[hidden email]> wrote:

> I have a simple function:
>
>        revFn (x:xs) = (revFn xs) . (x:)
>
>
>
> Of course GHCi correctly infers the type as:   revFn :: [a] -> [a] -> c
>
>
>
> Adding the base case:
>
>     revFn [] xs = xs
>
>
>
> Now gives an error;
>
>   “Equations for ‘revF’ have different numbers of arguments”
>
>
>
> Of course this can be “fixed” by either adding the cancelled argument to the
> first clause, or converting the base case to only have one explicit
> argument, and a RHS of a lambda or identity function.
>
>
>
> But since the interpreter already correctly inferred that the first clause
> has two arguments (with only one explicit), why does it then ignore this and
> give an error when the second clause shows two explicit arguments? The types
> are all correct in either case – why require explicit arguments?
>
>
>
> Or, perhaps I am missing something simple?
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: argument counts...

Neil Mayhew
In reply to this post by Gregory Guthrie
On 2017-11-20 10:13 AM, Gregory Guthrie wrote:

But since the interpreter already correctly inferred that the first clause has two arguments (with only one explicit), why does it then ignore this and give an error when the second clause shows two explicit arguments? The types are all correct in either case – why require explicit arguments?


Actually, the first clause doesn't have two arguments. It's a single-argument function that returns a one-argument function. Strictly speaking, all multi-argument functions are one-argument functions that return other functions, but the compiler makes a distinction, perhaps to help guard against mistakes. So it's a requirement that all clauses of a function definition have the same number of explicit arguments.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: argument counts...

Arjen
In reply to this post by Gregory Guthrie
On Mon, 2017-11-20 at 11:13 -0600, Gregory Guthrie wrote:

> I have a simple function:
>        revFn (x:xs) = (revFn xs) . (x:)
>  
> Of course GHCi correctly infers the type as:   revFn :: [a] -> [a] ->
> c
>  
> Adding the base case:
>     revFn [] xs = xs
>  
> Now gives an error;
>   “Equations for ‘revF’ have different numbers of arguments”
>  
> Of course this can be “fixed” by either adding the cancelled argument
> to the first clause, or converting the base case to only have one
> explicit argument, and a RHS of a lambda or identity function.
>  
> But since the interpreter already correctly inferred that the first
> clause has two arguments (with only one explicit), why does it then
> ignore this and give an error when the second clause shows two
> explicit arguments? The types are all correct in either case – why
> require explicit arguments?
>  
> Or, perhaps I am missing something simple?
>
The semantics are based on Term Rewriting Systems. The number of
arguments determines when the left side gets rewritten (evaluated) to
the right side. This makes a difference in time and space properties. I
think that is why Haskell (GHC) makes it explicit, and requires all
parts of the function definition to have the same number of arguments.

hope this helps, Arjen
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: argument counts...

Albert Y. C. Lai
In reply to this post by Gregory Guthrie
On 2017-11-20 12:13 PM, Gregory Guthrie wrote:
> I have a simple function:
>
>         revFn (x:xs) = (revFn xs) . (x:)
[...]
>
>      revFn [] xs = xs
>
> Now gives an error;
>
>    “Equations for ‘revF’ have different numbers of arguments”

Please see Haskell 2010 section 4.4.3.1 "Function bindings" and look for
"and the number of patterns in each clause must be the same".

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.