From: "David Allsopp" <dra-news@metastack.com>
To: "'skaller'" <skaller@users.sourceforge.net>
Cc: "'OCaml List'" <caml-list@yquem.inria.fr>,
"'Chris King'" <colanderman@gmail.com>
Subject: RE: [Caml-list] Re: Polymorphic variants question
Date: Fri, 1 Sep 2006 20:57:18 +0100 [thread overview]
Message-ID: <015101c6ce00$d47c36f0$6a7ba8c0@treble> (raw)
In-Reply-To: <1157137228.22787.13.camel@rosella.wigram>
-----Original Message-----
From: skaller [mailto:skaller@users.sourceforge.net]
Sent: 01 September 2006 20:00
> On Fri, 2006-09-01 at 14:33 -0400, Chris King wrote:
> > On 9/1/06, David Allsopp <dra-news@metastack.com> wrote:
<snip>
> By using:
>
> let id = function `A -> `A | `C -> `C
>
> the compiler knows (id x) can include `A
> and it can include `C, the case
>
> (true, `B)
>
> being returned says the return type can also be `B.
> So the return type can be bool followed by `A, `B, or `C.
>
> Hope that makes sense :)
Which is my understanding too - the type of id is [< `A | `C] -> [> `A | `C]
which allows x to be "used" in a [> `A | `B | `C] context without actually
changing the type of x while type inference is going on. Which, I guess I
should've spotted, means I can write:
let f (x : [`A | `C]) : bool * [`A | `B | `C] =
if x = `A
then (true, `B)
else (false, (x :> [`A | `B | `C]))
Which eliminates id and keeps the whole thing done in the type system
(whilst an identity function I'm sure is spotted and removed by the
compiler, it's a shame to have it there just to keep the type checker
happy!).
Incidentally, if I use an annotation free version
let f x = if x = `A then (true, `B) else (false, (x : [< `A | `C] :> [> `A |
`C]))
then the inferred type of x is [< `A | `C > `A ] ... what does that mean?
> --
> John Skaller <skaller at users dot sf dot net>
> Felix, successor to C++: http://felix.sf.net
Thanks for the help! (incidentally, I don't call labels A, B, C, ... in real
code, honest!)
next prev parent reply other threads:[~2006-09-01 19:57 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-01 17:31 David Allsopp
2006-09-01 18:33 ` Chris King
2006-09-01 19:00 ` [Caml-list] " skaller
2006-09-01 19:57 ` David Allsopp [this message]
2006-09-01 20:40 ` Jacques Carette
2006-09-01 18:40 ` [Caml-list] " Olivier Andrieu
2006-09-01 19:26 ` Jon Harrop
2006-09-01 19:29 ` skaller
2006-09-01 20:49 ` Andres Varon
2006-09-02 11:16 ` Jacques Garrigue
2006-09-03 1:22 ` Andres Varon
2006-09-03 9:08 ` Jacques Garrigue
2006-09-03 15:00 ` Andres Varon
2006-09-03 23:18 ` Jacques Garrigue
2006-09-03 0:48 ` [Caml-list] Polymorphic variants problem skaller
2006-09-03 1:12 ` Andres Varon
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='015101c6ce00$d47c36f0$6a7ba8c0@treble' \
--to=dra-news@metastack.com \
--cc=caml-list@yquem.inria.fr \
--cc=colanderman@gmail.com \
--cc=skaller@users.sourceforge.net \
/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