caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Patrick M Doane <patrick@watson.org>
To: caml-list@inria.fr
Subject: [Caml-list] Typing of default arguments
Date: Thu, 15 Feb 2001 20:54:24 -0500 (EST)	[thread overview]
Message-ID: <Pine.BSF.3.96.1010215204515.49712A-100000@fledge.watson.org> (raw)

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

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


             reply	other threads:[~2001-02-16  1:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-16  1:54 Patrick M Doane [this message]
2001-02-17  7:15 ` Pierre Weis
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=Pine.BSF.3.96.1010215204515.49712A-100000@fledge.watson.org \
    --to=patrick@watson.org \
    --cc=caml-list@inria.fr \
    /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).