caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* anonymous function + lable bug?
@ 2000-11-05 13:11 Chris Hecker
  2000-11-07  2:52 ` Jacques Garrigue
  2000-11-07 13:40 ` Sven LUTHER
  0 siblings, 2 replies; 6+ messages in thread
From: Chris Hecker @ 2000-11-05 13:11 UTC (permalink / raw)
  To: caml-list


Is this a bug?

# let f = fun ~x -> x;;
val f : x:'a -> 'a = <fun>

# let g = function ~x -> x;;
Characters 17-18:
Syntax error

I thought these two were equivalent?

Chris



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: anonymous function + lable bug?
  2000-11-05 13:11 anonymous function + lable bug? Chris Hecker
@ 2000-11-07  2:52 ` Jacques Garrigue
  2000-11-07 19:03   ` Chris Hecker
  2000-11-07 13:40 ` Sven LUTHER
  1 sibling, 1 reply; 6+ messages in thread
From: Jacques Garrigue @ 2000-11-07  2:52 UTC (permalink / raw)
  To: checker; +Cc: caml-list

From: Chris Hecker <checker@d6.com>

> Is this a bug?
> 
> # let f = fun ~x -> x;;
> val f : x:'a -> 'a = <fun>
> 
> # let g = function ~x -> x;;
> Characters 17-18:
> Syntax error
> 
> I thought these two were equivalent?

They are not:

Similar to the above example,
# let f = fun x y -> x;;
val f : 'a -> 'b -> 'a = <fun>
# let f = function x y -> x;;
Syntax error       ^

and the other way round,
# let f = fun Some x -> x;;
              ^^^^
The constructor Some expects 1 argument(s),
but is here applied to 0 argument(s)
# let f = function Some x -> x;;
val f : 'a option -> 'a = <fun>


The equivalence (in the no-label case) is between
        function p1 -> e1 | ... | pn -> en
and
        fun x -> match x with p1 -> e1 | ... | pn -> en

fun and function only become equivalent when the matching is trivial,
and there is no need to use an explicit match ... with
This is a true equivalence: the compiled code is identical, so you can
really think of function as a shorthand for the explicit form with
match, when you take only a single non-labelled argument.

It would be possible to add special rules to allow
        function ~l:p1 -> e1 | ... | ~l:pn -> en
This was actually the case in olabl, but it was very rarely used, and
complicates the parser and type checker.

Jacques
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: anonymous function + lable bug?
  2000-11-05 13:11 anonymous function + lable bug? Chris Hecker
  2000-11-07  2:52 ` Jacques Garrigue
@ 2000-11-07 13:40 ` Sven LUTHER
  2000-11-08 19:14   ` Chris Hecker
  1 sibling, 1 reply; 6+ messages in thread
From: Sven LUTHER @ 2000-11-07 13:40 UTC (permalink / raw)
  To: Chris Hecker; +Cc: caml-list

On Sun, Nov 05, 2000 at 05:11:11AM -0800, Chris Hecker wrote:
> 
> Is this a bug?
> 
> # let f = fun ~x -> x;;
> val f : x:'a -> 'a = <fun>
> 
> # let g = function ~x -> x;;
> Characters 17-18:
> Syntax error
> 
> I thought these two were equivalent?

No, function has only one argument, and can do more kind of polymorphism than 
fun which can take more than one argument as in :

let f = fun x y z -> x + y + z
f 5 6 7 evaluates to 18.

I think the use of function is preferable over fun, which is there for
compatibility reasons.

not sure what the ~ does, i think it is one of the label stuffs.

Try :

# let f ~x = x ;;
val f : x:'a -> 'a = <fun>

Don't know why there is a problem with the above though.

Friendly,

Sven Luther



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: anonymous function + lable bug?
  2000-11-07  2:52 ` Jacques Garrigue
@ 2000-11-07 19:03   ` Chris Hecker
  2000-11-08  1:01     ` Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Chris Hecker @ 2000-11-07 19:03 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: caml-list


>> I thought these two were equivalent?
>They are not:

Right, I didn't mean to say they're completely equivalent (I've read the FAQ), just that I expected equivalence in that case.

>It would be possible to add special rules to allow
>        function ~l:p1 -> e1 | ... | ~l:pn -> en

Ah, now I see, it's because the parameter in function is actually the pattern in an implicit match?

Is there ever a reason to use function (assuming you don't mind parenthesizing constructors and whatnot in fun arguments)?  Does the implied match allow some simplification or optimization or something?  Why do they both exist?

As I said, I've read the FAQ, but I can't see why you'd ever use function when you've got fun...

Thanks,
Chris




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: anonymous function + lable bug?
  2000-11-07 19:03   ` Chris Hecker
@ 2000-11-08  1:01     ` Jacques Garrigue
  0 siblings, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2000-11-08  1:01 UTC (permalink / raw)
  To: checker; +Cc: caml-list

From: Chris Hecker <checker@d6.com>

> >> I thought these two were equivalent?
> >They are not:
> 
> Right, I didn't mean to say they're completely equivalent (I've read
> the FAQ), just that I expected equivalence in that case.
> 
> >It would be possible to add special rules to allow
> >        function ~l:p1 -> e1 | ... | ~l:pn -> en
> 
> Ah, now I see, it's because the parameter in function is actually
> the pattern in an implicit match?

Exactly.

> Is there ever a reason to use function (assuming you don't mind
> parenthesizing constructors and whatnot in fun arguments)?  Does the
> implied match allow some simplification or optimization or
> something?  Why do they both exist?

function is not necessary: you can replace it everywhere with fun and
match, and the code is exactly optimized the same way. Actually some
pass of the compiler expands function in fun+match.

However, in many cases function is a nice and intuitive short-hand for
directly matching on an argument. The reason for it to be here seems
mostly historical: one can define let in terms of fun (+ special
typing), and match in terms of function (no special typing
required). As I remember it, SML (/NJ?) was actually seeing case
(=match) as syntactic sugar for fn(=function).

Jacques Garrigue



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: anonymous function + lable bug?
  2000-11-07 13:40 ` Sven LUTHER
@ 2000-11-08 19:14   ` Chris Hecker
  0 siblings, 0 replies; 6+ messages in thread
From: Chris Hecker @ 2000-11-08 19:14 UTC (permalink / raw)
  To: Sven LUTHER; +Cc: caml-list


>No, function has only one argument, and can do more kind of polymorphism than 
>fun which can take more than one argument as in :

Is this actually true that function can be more polymorphic?  What's an example?

Chris




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2000-11-09 18:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-05 13:11 anonymous function + lable bug? Chris Hecker
2000-11-07  2:52 ` Jacques Garrigue
2000-11-07 19:03   ` Chris Hecker
2000-11-08  1:01     ` Jacques Garrigue
2000-11-07 13:40 ` Sven LUTHER
2000-11-08 19:14   ` Chris Hecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).