caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: skaller <skaller@users.sourceforge.net>
To: Chris King <colanderman@gmail.com>
Cc: David Allsopp <dra-news@metastack.com>,
	OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] Re: Polymorphic variants question
Date: Sat, 02 Sep 2006 05:00:28 +1000	[thread overview]
Message-ID: <1157137228.22787.13.camel@rosella.wigram> (raw)
In-Reply-To: <875c7e070609011133udd506c7v223d8ece952b95ae@mail.gmail.com>

On Fri, 2006-09-01 at 14:33 -0400, Chris King wrote:
> On 9/1/06, David Allsopp <dra-news@metastack.com> wrote:
> > Now, if I try to constrain it to what I'm after with
> > 
> > let (f : [`A | `C] -> bool * [`A | `B | `C]) = fun x -> ...
> > 
> > then I get a type error unless I change
> > 	(false, x)
> > to
> > 	(false, id x)
> > with 
> > 	let id = function `A -> `A | `C -> `C
> > 
> > Is there a better way of writing this?
> 
> Yes, you can use the coercion operator to tell the compiler to expand the type of x to include `B:
> 
> # let f (x: [`A | `C]) = if x = `A then (true, `B) else (false, (x :> [`A | `B | `C]))
> val f : [ `A | `C ] -> bool * [ `A | `B | `C ] = <fun>
> 
> Perhaps someone else can clarify how this accomplishes the same thing as 
> your id function, as my understanding of type theory is somewhat rudimentary.

The problem is `C not `B. Looking at this expression:

if x = `A then (true, `B) else (false, x)

we know the result is a bool followed by:

  1. `B in the true case
  2. x in the false case

But we know x might be `A from the comparison 

   x = `A

so now we know the result is bool followed by `A, `B,
or something else, we know not what, which x might be. 

Note that the type of x must include the case `A, 
since if this were not so the comparison:

  x = `A

could not be between the same types .. even though x is
only returned when it is *not* `A -- that's a flow
of control issue not a typing issue.

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 :)

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


  reply	other threads:[~2006-09-01 19:00 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   ` skaller [this message]
2006-09-01 19:57     ` [Caml-list] " David Allsopp
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=1157137228.22787.13.camel@rosella.wigram \
    --to=skaller@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    --cc=colanderman@gmail.com \
    --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;
as well as URLs for NNTP newsgroup(s).