Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Polymorphic variants question
Date: Fri, 1 Sep 2006 20:26:39 +0100	[thread overview]
Message-ID: <200609012026.40067.jon@ffconsultancy.com> (raw)
In-Reply-To: <012901c6cdec$64edf490$6a7ba8c0@treble>

On Friday 01 September 2006 18:31, David Allsopp wrote:
> let f x = if x = `A then (true, `B) else (false, x);;

You probably want:

# let f = function `A -> true, `B | `C -> false, `C;;
val f : [< `A | `C ] -> bool * [> `B | `C ] = <fun>

> then I get a type error unless I change
> 	(false, x)
> to
> 	(false, id x)

Use coercion :> instead of an ad-hoc function:

# let (f : [`A | `C] -> bool * [`A | `B | `C]) =
    fun x -> if x = `A then (true, `B) else (false, (x :> [`A|`B|`C]));;
val f : [ `A | `C ] -> bool * [ `A | `B | `C ] = <fun>

> Is there a better way of writing this? I'm using this in the context of
> several interrelated lexers where `A, `B and `C are high-level states and
> certain lexers can only be called in a subset of those states but each
> lexer may yield any value for the next-state. I'd quite like to eliminate
> the id x bit since it's only there to "separate" x from the return value
> for the type-checker.

Note that the type of your id function is not 'a -> 'a:

# let id = function `A -> `A | `C -> `C;;
val id : [< `A | `C ] -> [> `A | `C ] = <fun>

So it accepts a subset of {A, C} and returns a type that unifies with any 
superset of {A, C}.

To leverage static type checking you must provide as much information as 
possible to the static type checker. In this case, rather than using "if" to 
denote `A or not `A, you can just as easily use pattern matching to imply 
specifically `A or `C.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


  parent reply	other threads:[~2006-09-01 19:26 UTC|newest]

Thread overview: 17+ 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
2006-09-01 20:40       ` Jacques Carette
2006-09-01 18:40 ` [Caml-list] " Olivier Andrieu
2006-09-01 19:26 ` Jon Harrop [this message]
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
  -- strict thread matches above, loose matches on Subject: below --
2001-07-20  1:05 [Caml-list] polymorphic variants question John Max Skaller

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=200609012026.40067.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.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