Version française
Home     About     Download     Resources     Contact us    
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 *)
or
	{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, mailto:skaller@ozemail.com.au
snail:10/1 Toxteth Rd, Glebe, NSW 2037, Australia.
voice:61-2-9660-0850


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners