caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: "Seth J. Fogarty" <sfogarty@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Question on Variant Types
Date: Thu, 29 Jun 2006 12:37:18 +0900 (JST)	[thread overview]
Message-ID: <20060629.123718.71847699.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <c7ee61120606281739g27fc344bt855cde4bd6a89797@mail.gmail.com>

On 6/29/06, Seth J. Fogarty <sfogarty@gmail.com> wrote:
> If you have time, I have one more question I can't seem to solve. Which
> quite possibly has as simple an answer as the other two. You've been very
> helpful so far, and I don't want to impose, so feel free to let me know if
> you've not the time.
>
> type foo = [`A of int | `B | 'D of foo]
> type bar = [`A of int | `C of foo * bar | 'D of bar]
>
> let rec occurs i x =
>    match x with
>    |`A j -> i = j
>    |`C (foo, bar) -> (occurs i foo) or (occurs i bar)
>    |_ -> false
>
> I would like occurs to work on bars and foos. But as it is, occurs won't
> work on either, because it assigns the `C variant the type "`C of 'b * 'b".
> Even if I spell out `D and `B, I cannot get it to accept.

As long as foo and bar are two subtypes of a common type, you can
still solve the problem by defining that type and using subtyping:

type foobar = [`A of int | `B | `C of foobar * foobar | `D of foobar]
let occurs_foo i x = occurs i (x : foo :> foobar)
let occurs_bar i x = occurs i (x : bar :> foobar)

Jacques Garrigue


  parent reply	other threads:[~2006-06-29  4:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-28 16:27 Seth J. Fogarty
2006-06-28 16:47 ` [Caml-list] " Jonathan Roewen
2006-06-28 16:48   ` Jonathan Roewen
2006-06-28 23:44     ` Seth J. Fogarty
2006-06-28 23:51       ` Jonathan Roewen
     [not found]         ` <c7ee61120606281739g27fc344bt855cde4bd6a89797@mail.gmail.com>
2006-06-29  3:09           ` Jonathan Roewen
2006-06-29  3:37           ` Jacques Garrigue [this message]
2006-06-29 21:27 ` Another question on variant types and matching (was: Re: [Caml-list] Question on Variant Types) Richard Jones
2006-06-29 21:51   ` Jonathan Roewen

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=20060629.123718.71847699.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@yquem.inria.fr \
    --cc=sfogarty@gmail.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).