Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: [Caml-list] Naming polymorphic variant types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Max Skaller <skaller@o...>
Subject: Re: [Caml-list] Naming polymorphic variant types
Nick Alexander wrote:

> Heh, that's what I thought.  Oh well... maybe there is a way to do what I
> want anyway.  Let's say I have a record, say {a:int; b:int; c:int}.  I
> wish to have a function that returns sub-records, say {a:int; b:int} or
> {b:int; c:int}.  However, I don't want to (and can't, if I recall, due to
> name clashes) declare all the sub record types.  

> I certainly don't understand enough type/category theory to
> extend the system myself.
> Am I out of luck?

AFAIK there's no reason it can't be implemented, but the
result would not be compatible with the existing
system. For example:

	type t = {a:int; b:int}
	let f x = x.a + 1

The type has to be assumed to be

	{>a:int} -> int (* #1 *)
	{a:int} -> int (* #2 *)

with polymorphic records (the declaration of type
t is irrelevant). But the existing system deduces

	t -> int

For polymorphic variants, a new syntax was used to
get around the equivalent problem: that seems to be the main
obstacle to me. The obvious candidate is:

	let f x = x.`a

just like polymorphic variants. `a means
polymorphic record label after a . or
inside {} in leading position (after '{'
or ';')

It's not clear whether this style,
with automatic conversions (#1):

	let f x = x.`a in
	ignore(f {`a=1});
	ignore(f {`a=1; `b=2}) (* #1 *)

would be better than requiring explicit conversion (* #2 *)

	f( {`a=1; `b=2) :> {`a:int} ) )(* #2 *)

but clearly a physical conversion (actual
run time operation) is required, so that the field
is at the right offset.	With polymorphic
variants no physical conversion is required,
it's a compile time typing thing only.

[The typing in presence of mutable fields
may complicate things]

John Max Skaller,
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: