caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David Allsopp" <dra-news@metastack.com>
To: "OCaml List" <caml-list@inria.fr>
Subject: [Caml-list] GADT in an optional parameter
Date: Thu, 18 Jul 2013 15:03:05 +0100	[thread overview]
Message-ID: <000001ce83bf$879e6520$96db2f60$@metastack.com> (raw)

So, continuing my forays into sections of type theory I clearly don't
properly understand! Although I was trying to use it for something else, my
problem here is equivalent to a rather over-discussed problem on this list -
namely, raising exceptions vs wrapping results in 'a option

I defined the following GADT:

type ('a, 'b) wrap = Option : ('a, 'a option) wrap
                   | Exn : ('a, 'a) wrap

I then created the following alternate implementation of Map where find
takes that GADT as its first parameter:

module StringMap =
  struct
    include Map.Make(String)

    let find : type s t . (s, t) wrap -> string -> s Map.Make(String).t -> t
= fun mode key map ->
      match mode with
        Exn ->
          find key map
      | Option ->
          try
            Some (find key map)
          with Not_found -> None
  end

So far so good, as we can have:

StringMap.find Option "foo" StringMap.empty;; (* None *)
StringMap.find Exn "foo" StringMap.empty;;    (* Not_found raised *)

which is quite nice (ignoring any quite optimisable and probably negligible
overhead of the extra match clause verses having two functions find and
find_option).

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? 


David 


             reply	other threads:[~2013-07-18 14:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-18 14:03 David Allsopp [this message]
2013-07-19  6:59 ` Jacques Garrigue
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='000001ce83bf$879e6520$96db2f60$@metastack.com' \
    --to=dra-news@metastack.com \
    --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).