caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <Pierre.Weis@inria.fr>
To: patrick@watson.org (Patrick M Doane)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Typing of default arguments
Date: Sat, 17 Feb 2001 08:15:11 +0100 (MET)	[thread overview]
Message-ID: <200102170715.IAA11563@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.BSF.3.96.1010215204515.49712A-100000@fledge.watson.org> from Patrick M Doane at "Feb 15, 101 08:54:24 pm"

> I am having difficulty understanding the typing rules for default
> arguments. Here are some test cases:
> 
>   (* No default arguments: all of these work *)
>   let f ~g x = g x
> 
>   let a = f (fun x -> x) ()
>   let b = f (fun x -> (x,x)) ()
> 
>   let c = f ~g:(fun x -> x) ()
>   let d = f ~g:(fun x -> (x,x)) ()
> 
>   (* Default argument: last one fails to type-check *)
>   let f' ?(g = (fun x -> x)) x = g x
> 
>   let a' = f' (fun x -> x) ()
>   let b' = f' (fun x -> (x,x)) ()
> 
>   let c' = f' ~g:(fun x -> x) ()
>   let d' = f' ~g:(fun x -> (x,x)) ()
> 
> If I remove the offending case at the end, I notice that the difference
> between f and f' is:
> 
>   val f : g:('a -> 'b) -> 'a -> 'b
>   val f' : ?g:('a -> 'a) -> 'a -> 'a
> 
> So why does the definition for b' typecheck properly? The type of the
> first argument is not 'a -> 'a but rather 'a -> 'a * 'a. 
> 
> It's unfortunate that supplying a default argument restricts the
> polymorphism and reuse of the function. I would like someway to provide
> the default without losing that polymorphic capability. I think this
> example shows that is should at least be legal to do (from the declaration
> of b').
> 
> Any ideas?
> 
> Thanks,
> Patrick Doane

If f' (fun x -> (x,x)) () can typecheck it is not for a strange magic,
but just because of polymorphism and of the regular default value
treatment.

Let's see the evaluation process:

- the expression f' (fun x -> (x,x)) does not provide any argument
labelled g, hence g gets it default value (fun x -> x), hence it
returns the expression (fun x -> g x) with  g being (fun x -> x ),
hence it returns (fun x -> x), which is applied in turn to
(fun x -> (x,x)); this application returns (fun x -> (x,x)), which is
the final result.

- now the expression f' (fun x -> (x,x)) () naturally returns the result
of (f' (fun x -> (x,x))) applied to (), hence (fun x -> (x,x)) applied
to (), hence ((), ()), as observed.

Now the typing process:

f' : ?g:('a -> 'a) -> 'a -> 'a = <fun>

f' (fun x -> (x,x)) () : (f' : 'a -> 'a) (fun x -> (x,x)) () 
                                                           (default value rule)
                       : (f' : 'a -> 'a)
                         (fun x -> (x,x) : 'b -> 'b * 'b) ()
                       : (f' : ('b -> 'b * 'b) -> ('b -> 'b * 'b))
                         (fun x -> (x,x) : 'b -> 'b * 'b) ()
                       : (f' (fun x -> (x,x)) : ('b -> 'b * 'b)) ()
                       : (f' (fun x -> (x,x)) : unit -> unit * unit) (():unit)
                       : (f' (fun x -> (x,x)) ()) : (unit * unit)

This is regular typing, just an amazing side effect of automatic
curryfication and polymorphism.

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  reply	other threads:[~2001-02-17  7:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-16  1:54 Patrick M Doane
2001-02-17  7:15 ` Pierre Weis [this message]
2001-02-17 20:07   ` Patrick M Doane
2001-02-19  0:01     ` Jacques Garrigue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200102170715.IAA11563@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=patrick@watson.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).