caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@best.com>
To: Juergen Pfitzenmaier <pfitzen@informatik.uni-tuebingen.de>
Cc: caml-list@inria.fr
Subject: Re: # and polymorphic variants
Date: Thu, 1 Feb 2001 17:45:00 -0800 (PST)	[thread overview]
Message-ID: <Pine.BSF.4.21.0102011736190.17147-100000@shell5.ba.best.com> (raw)
In-Reply-To: <200102010037.BAA12641@sunstroke.informatik.uni-tuebingen.de>

You need to use a "let rec" so that the function f1 is defined in f (or
replace the "and"s with "let"s) and use aliases. The first should be
obvious, the need for aliases seems necessary to get the coercion. Is 
this what you want?

type t1 = [ `A of int ];;
type t2 = [ `B of string ];;
type t = [ `A of int | `B of string ];;

let rec f1 (`A (x : int)) =
        print_int x 
and f2 (`B (x : string)) =
        print_string x 
and f (x : t) =
        match x with
        | #t1 as x -> f1 x 
        | #t2 as x -> f2 x;;

-- Brian

On Thu, 1 Feb 2001, Juergen Pfitzenmaier wrote:

> Dear ocaml users,
> consider the following example:
> 
> type t1 = [ `A of int ];;
> type t2 = [ `B of string ];;
> type t = [ `A of int | `B of string ];;
> 
> let f1 (`A (x : int)) =
> 	print_int x
> and f2 (`B (x : string)) =
> 	print_string x
> and f (x : t) =
> 	match x with
> 	| #t1 -> f1 x      (* this is not allowed !! *)
> 	| #t2 -> f2 x;;
> 
> The compiler can't constrain the type t of x to t1/t2 in the call to f1/f2.
> And an coercion like in
>     ...
> 	match x with
> 	| #t1 -> f1 (x :> t1)
>     ...
> is not allowed. So I see only one solution:
>     ...
> 	match x with
> 	| #t1 -> f1 (Obj.magic(x) :> t1)   (* I don't like magic *)
>     ...
> But I would like to see something clean like giving a name to the # pattern
>     ...
> 	match x with
> 	| #t1 y -> f1 y    (* (y : t1) would have the same value as x *)
>     ...
> or like
>     ...
> 	match x with
> 	| #t1 -> f1 x      (* x is constrained to type t1 *)
>     ...
> 
> Any comments ?
> 
> -- pfitzen
> 
> 
> 



      parent reply	other threads:[~2001-02-02 15:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-02-01  0:37 Juergen Pfitzenmaier
2001-02-01  9:50 ` Andrzej M. Ostruszka
2001-02-01 14:31 ` Jacques Garrigue
2001-02-04  8:37   ` Julian Assange
2001-02-02  1:45 ` Brian Rogoff [this message]

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=Pine.BSF.4.21.0102011736190.17147-100000@shell5.ba.best.com \
    --to=bpr@best.com \
    --cc=caml-list@inria.fr \
    --cc=pfitzen@informatik.uni-tuebingen.de \
    /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).