From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: David Allsopp <dra-news@metastack.com>
Cc: "OCaml List" <caml-list@inria.fr>
Subject: Re: [Caml-list] GADT in an optional parameter
Date: Fri, 19 Jul 2013 15:59:47 +0900 [thread overview]
Message-ID: <37C2C45C-A317-408E-8545-4FD426E3C817@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <000001ce83bf$879e6520$96db2f60$@metastack.com>
On 2013/07/18, at 23:03, David Allsopp <dra-news@metastack.com> wrote:
> I then wanted to try to make the first parameter optional, defaulting to Exn
> (so you'd get the vanilla behaviour of Map.find):
>
> module StringMap =
> struct
> include Map.Make(String)
>
> let find : type s t . ?mode:(s, t) wrap -> string -> s
> Map.Make(String).t -> t = fun ?(mode = Exn) key map ->
> match mode with
> Exn ->
> find key map
> | Option ->
> try
> Some (find key map)
> with Not_found -> None
> end
>
> But I get: This expression has type (s, s) wrap but an expression was
> expected of type (s, t) wrap on the Exn default value.
>
> Now, in my woolly way, I thought that the default value for an optional
> parameter behaved /almost/ like syntactic sugar, but clearly not. Is there a
> way to annotate this to achieve what I'm after?
No, clearly no.
Optional arguments have no impact on typing: the absence of an optional
argument cannot force some extra equation.
I agree this would be interesting, but then we would need a way to express
it in the type. Otherwise, why would the user expect (s=t) when there is no
argument, whereas there is no way to see what the default is?
Something like:
val find : ?mode:(('s,'t) wrap = Exn) -> string -> 's Map.Make(String).t -> 't
It would be good to see whether this has many applications.
In the past, there were some request to be able to default to the identity function.
Jacques
next prev parent reply other threads:[~2013-07-19 6:59 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-07-18 14:03 David Allsopp
2013-07-19 6:59 ` Jacques Garrigue [this message]
2013-07-19 10:37 ` Alain Frisch
2013-07-19 16:33 ` Gabriel Scherer
2013-07-25 9:31 ` David Allsopp
2013-07-25 12:11 ` Alain Frisch
2013-07-19 9:05 ` oleg
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=37C2C45C-A317-408E-8545-4FD426E3C817@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=dra-news@metastack.com \
/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