* # and polymorphic variants
@ 2001-02-01 0:37 Juergen Pfitzenmaier
2001-02-01 9:50 ` Andrzej M. Ostruszka
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Juergen Pfitzenmaier @ 2001-02-01 0:37 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: # and polymorphic variants
2001-02-01 0:37 # and polymorphic variants Juergen Pfitzenmaier
@ 2001-02-01 9:50 ` Andrzej M. Ostruszka
2001-02-01 14:31 ` Jacques Garrigue
2001-02-02 1:45 ` Brian Rogoff
2 siblings, 0 replies; 5+ messages in thread
From: Andrzej M. Ostruszka @ 2001-02-01 9:50 UTC (permalink / raw)
To: Caml List
On Thu, Feb 01 (2001), Juergen Pfitzenmaier wrote:
> 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.
What about alias patterns? The following seems to work:
type t1 = [ `A of int ]
type t2 = [ `B of string ]
type t = [ `A of int | `B of string ]
let f1 (`A x) = print_int x
and f2 (`B x) = print_string x
and f (x : t) = match x with
| #t1 as a -> f1 a
| #t2 as b -> f2 b
Best regards
--
____ _ ___
/ | \_/ |/ _ \ Andrzej Marek Ostruszka
/ _ | | (_) | Instytut Fizyki, Uniwersytet Jagiellonski (Cracow)
/_/ L|_|V|_|\___/ (PGP <-- finger ostruszk@order.if.uj.edu.pl)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: # and polymorphic variants
2001-02-01 0:37 # and polymorphic variants 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
2 siblings, 1 reply; 5+ messages in thread
From: Jacques Garrigue @ 2001-02-01 14:31 UTC (permalink / raw)
To: pfitzen; +Cc: caml-list
From: Juergen Pfitzenmaier <pfitzen@informatik.uni-tuebingen.de>
> 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;;
...
> 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 *)
You were pretty close to the solution !
You just have to write
match x with
| #t1 as y -> f1 y
| #t2 as y -> f2 y
logical, no?
Jacques
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: # and polymorphic variants
2001-02-01 0:37 # and polymorphic variants Juergen Pfitzenmaier
2001-02-01 9:50 ` Andrzej M. Ostruszka
2001-02-01 14:31 ` Jacques Garrigue
@ 2001-02-02 1:45 ` Brian Rogoff
2 siblings, 0 replies; 5+ messages in thread
From: Brian Rogoff @ 2001-02-02 1:45 UTC (permalink / raw)
To: Juergen Pfitzenmaier; +Cc: caml-list
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
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: # and polymorphic variants
2001-02-01 14:31 ` Jacques Garrigue
@ 2001-02-04 8:37 ` Julian Assange
0 siblings, 0 replies; 5+ messages in thread
From: Julian Assange @ 2001-02-04 8:37 UTC (permalink / raw)
To: Jacques Garrigue; +Cc: pfitzen, caml-list, proff
Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp> writes:
> > match x with
> > | #t1 -> f1 x (* this is not allowed !! *)
> > | #t2 -> f2 x;;
Where is this hash notation documented, btw?
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2001-02-04 21:06 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-02-01 0:37 # and polymorphic variants 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox