caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Naming polymorphic variant types
@ 2002-08-22  0:04 Nick Alexander
  2002-08-23 22:38 ` John Max Skaller
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Alexander @ 2002-08-22  0:04 UTC (permalink / raw)
  To: caml-list

> From: "Nick Alexander" <nalexander@amavi.com>
>> Now, can anyone point me at docs about polymorphic records?  I can't
>> find a thing :(  They were mentioned in the 3.05 release notes as'-
>> Support for polymorphic methods and record fields.'
>> which could be an extension of the object-based polymorphic records or
>> a really useful extension like the polymorphic variants.
>
> Oops, you didn't parser the above sentence correctly.
> What 3.05 adds is "polymorphic methods" and "polymorphic record
> fields", but not "polymorphic records" (you still have to simulate them
> with objects).  Polymorphic record fields are shortly described in the
> core language part of the tutorial; they provide explicit
> polymorhism at the record field level, which may be light than going
> through objects and methods.

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.  So I was hoping for
something pretty such as the polymorphic variants.  I don't see how this
situation can be faked with objects -- objects (and classes) are even more
heavy than record types!  By heavy, I mean that I cannot create a new
object type with just the right methods on the fly, I must first declare
the class type, etc.  It seems like polymorphic row variables are _so_
close, but I certainly don't understand enough type/category theory to
extend the system myself.

Am I out of luck?

Nick





-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Naming polymorphic variant types
  2002-08-22  0:04 [Caml-list] Naming polymorphic variant types Nick Alexander
@ 2002-08-23 22:38 ` John Max Skaller
  0 siblings, 0 replies; 6+ messages in thread
From: John Max Skaller @ 2002-08-23 22:38 UTC (permalink / raw)
  To: nalexander; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Naming polymorphic variant types
  2002-08-21 16:02   ` Nick Alexander
@ 2002-08-21 23:50     ` Jacques Garrigue
  0 siblings, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2002-08-21 23:50 UTC (permalink / raw)
  To: nalexander; +Cc: caml-list

From: "Nick Alexander" <nalexander@amavi.com>
> Now, can anyone point me at docs about polymorphic records?  I can't find
> a thing :(  They were mentioned in the 3.05 release notes as'-
> Support for polymorphic methods and record fields.' 
> which could be an extension of the object-based polymorphic records or a
> really useful extension like the polymorphic variants.

Oops, you didn't parser the above sentence correctly.
What 3.05 adds is "polymorphic methods" and "polymorphic record
fields", but not "polymorphic records" (you still have to simulate
them with objects).  Polymorphic record fields are shortly described
in the core language part of the tutorial; they provide explicit
polymorhism at the record field level, which may be light than going
through objects and methods.

Jacques Garrigue
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Naming polymorphic variant types
  2002-08-21  5:27 ` Jacques Garrigue
@ 2002-08-21 16:02   ` Nick Alexander
  2002-08-21 23:50     ` Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Alexander @ 2002-08-21 16:02 UTC (permalink / raw)
  To: garrigue; +Cc: caml-list

> From: "Nick Alexander" <nalexander@amavi.com>
>
>> module type TEST =
>>   sig
>>     type outer = [ `A | `B]
>>     and inner = [ `B]
>>     val x : outer -> outer
>>     val y : inner
>>   end
>> module Test : TEST
>> # let p = Test.y;;
>> val p : Test.inner = `B
>> # let q = Test.x p;;
>>                  ^
>> This expression has type Test.inner = [ `B] but is here used with type
>>   Test.outer = [ `A | `B]
>> The first variant type does not allow tag(s) `A
>>
>> I'm confused.  Can I get an explanation for why the parameter to a
>> function with a sum variant type needs all summands to be possible
>> arguments?
>
> Inner is a subtype of outer, but they cannot be unified (unification
> requires equality, not subtyping).
>
> You can get subtyping by writing explicit coercions:
>
> # let q = Test.x (p :> Test.outer);;
> val q : Test.outer = `A
>
> You can also give polymorphic types to your values in the interface:
>  val x : [< outer] -> outer
> and
>  val y : [> inner]
> are both valid, and one of them is enough to allow (Test.x p) without
> coercion. I.e., [< outer] can be unified with inner, and [> inner] can
> be unified with outer.

Thank you Jacques.  I didn't know that you could name < and > types in
type declarations.  Perfect :)
Now, can anyone point me at docs about polymorphic records?  I can't find
a thing :(  They were mentioned in the 3.05 release notes as'- Support for polymorphic methods and record fields.'
which could be an extension of the object-based polymorphic records or a
really useful extension like the polymorphic variants.
Cheers,
Nick


-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Naming polymorphic variant types
  2002-08-21  4:22 Nick Alexander
@ 2002-08-21  5:27 ` Jacques Garrigue
  2002-08-21 16:02   ` Nick Alexander
  0 siblings, 1 reply; 6+ messages in thread
From: Jacques Garrigue @ 2002-08-21  5:27 UTC (permalink / raw)
  To: nalexander; +Cc: caml-list

From: "Nick Alexander" <nalexander@amavi.com>

> module type TEST =
>   sig
>     type outer = [ `A | `B]
>     and inner = [ `B]
>     val x : outer -> outer
>     val y : inner
>   end
> module Test : TEST
> # let p = Test.y;;
> val p : Test.inner = `B
> # let q = Test.x p;;
>                  ^
> This expression has type Test.inner = [ `B] but is here used with type
>   Test.outer = [ `A | `B]
> The first variant type does not allow tag(s) `A
>
> I'm confused.  Can I get an explanation for why the parameter to a
> function with a sum variant type needs all summands to be possible
> arguments? 

Inner is a subtype of outer, but they cannot be unified (unification
requires equality, not subtyping).
 
You can get subtyping by writing explicit coercions:

# let q = Test.x (p :> Test.outer);;
val q : Test.outer = `A

You can also give polymorphic types to your values in the interface:
  val x : [< outer] -> outer
and
  val y : [> inner]
are both valid, and one of them is enough to allow (Test.x p) without
coercion. I.e., [< outer] can be unified with inner, and [> inner] can
be unified with outer.

        Jacques Garrigue
-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Caml-list] Naming polymorphic variant types
@ 2002-08-21  4:22 Nick Alexander
  2002-08-21  5:27 ` Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Alexander @ 2002-08-21  4:22 UTC (permalink / raw)
  To: caml-list

Hi all,

Here's a file...

module type TEST = sig
  type outer = [`A | `B]
  type inner = [`B]

  val x : outer -> outer
  val y : inner
end

module Test : TEST = struct
  type outer = [`A | `B]
  and inner = [`B]

  let x o = match o with
  | `A -> `A
  | `B -> `A

  let y = `B
end

let p = Test.y
let q = Test.x p

That when evaluated, produces...

# #use "C:/Program Files/Objective Caml/test.ml";;
module type TEST =
  sig
    type outer = [ `A | `B]
    and inner = [ `B]
    val x : outer -> outer
    val y : inner
  end
module Test : TEST
val p : Test.inner = `B
File "C:/Program Files/Objective Caml/sql5.ml", line 21, characters 15-16:
This expression has type Test.inner = [ `B] but is here used with type
  Test.outer = [ `A | `B]
The first variant type does not allow tag(s) `A

I'm confused.  Can I get an explanation for why the parameter to a
function with a sum variant type needs all summands to be possible
arguments?  I'm sure I'm misunderstanding the subtyping relationship, but
I can't hack it.  This all grew out of a desire to document code by naming
polymorphic variant types, a questionable capability at best.
Any light appreciated,
Nick Alexander


-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-08-23 22:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-22  0:04 [Caml-list] Naming polymorphic variant types Nick Alexander
2002-08-23 22:38 ` John Max Skaller
  -- strict thread matches above, loose matches on Subject: below --
2002-08-21  4:22 Nick Alexander
2002-08-21  5:27 ` Jacques Garrigue
2002-08-21 16:02   ` Nick Alexander
2002-08-21 23:50     ` Jacques Garrigue

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).