caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Another polymorphism puzzle
@ 2005-04-01  2:02 Yaron Minsky
  2005-04-01  2:32 ` [Caml-list] " Karl Zilles
  2005-04-01  2:52 ` Manos Renieris
  0 siblings, 2 replies; 5+ messages in thread
From: Yaron Minsky @ 2005-04-01  2:02 UTC (permalink / raw)
  To: Caml Mailing List

The last example I posted about had to do with variant types.  Here's
another interseting little typing surprise having to do with record
types:

# type 'a t = { foo: 'a };;
type 'a t = { foo : 'a; }
# let f x = { x with foo = 3 };;
val f : 'a -> int t = <fun>

I can kind of see how this happened, since after all, the result of (f
x) does not depend in any way on x.  And yet, it seems like the
unification rules should force f to have time ('a t -> int t).

y


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

* Re: [Caml-list] Another polymorphism puzzle
  2005-04-01  2:02 Another polymorphism puzzle Yaron Minsky
@ 2005-04-01  2:32 ` Karl Zilles
  2005-04-01  2:52 ` Manos Renieris
  1 sibling, 0 replies; 5+ messages in thread
From: Karl Zilles @ 2005-04-01  2:32 UTC (permalink / raw)
  To: yminsky; +Cc: Caml Mailing List

Yaron Minsky wrote:
> The last example I posted about had to do with variant types.  Here's
> another interseting little typing surprise having to do with record
> types:
> 
> # type 'a t = { foo: 'a };;
> type 'a t = { foo : 'a; }
> # let f x = { x with foo = 3 };;
> val f : 'a -> int t = <fun>
> 
> I can kind of see how this happened, since after all, the result of (f
> x) does not depend in any way on x.  And yet, it seems like the
> unification rules should force f to have time ('a t -> int t).

Wow.  This is pretty cool.  Also

# type 'a t = { foo: 'a; bar: int };;
type 'a t = { foo : 'a; bar : int; }

# let f x = { x with foo = 3 };;
val f : 'a t -> int t = <fun>
(now the domain is limited to 'a t)

# f { foo= "blah"; bar=100 };;
- : int t' = {foo = 3; bar = 100}


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

* Re: [Caml-list] Another polymorphism puzzle
  2005-04-01  2:02 Another polymorphism puzzle Yaron Minsky
  2005-04-01  2:32 ` [Caml-list] " Karl Zilles
@ 2005-04-01  2:52 ` Manos Renieris
  2005-04-01  4:29   ` Jacques Garrigue
  1 sibling, 1 reply; 5+ messages in thread
From: Manos Renieris @ 2005-04-01  2:52 UTC (permalink / raw)
  To: yminsky; +Cc: Caml Mailing List

Worse:

# let f x = { () with foo = 3};;                    
val f : 'a -> int t = <fun>
# let f x = { 42 with foo = 3};; 
val f : 'a -> int t = <fun>

I think this qualifies as a bug. Especially since the documentation
says the expr on the left hand side of with has to be a record.

Thu, Mar 31, 2005 at 09:02:14PM -0500, Yaron Minsky wrote:
> The last example I posted about had to do with variant types.  Here's
> another interseting little typing surprise having to do with record
> types:
> 
> # type 'a t = { foo: 'a };;
> type 'a t = { foo : 'a; }
> # let f x = { x with foo = 3 };;
> val f : 'a -> int t = <fun>
> 
> I can kind of see how this happened, since after all, the result of (f
> x) does not depend in any way on x.  And yet, it seems like the
> unification rules should force f to have time ('a t -> int t).
> 
> y
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


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

* Re: [Caml-list] Another polymorphism puzzle
  2005-04-01  2:52 ` Manos Renieris
@ 2005-04-01  4:29   ` Jacques Garrigue
  2005-04-01  9:45     ` Alex Baretta
  0 siblings, 1 reply; 5+ messages in thread
From: Jacques Garrigue @ 2005-04-01  4:29 UTC (permalink / raw)
  To: er; +Cc: yminsky, caml-list

From: Manos Renieris <er@cs.brown.edu>
> Worse:
> 
> # let f x = { () with foo = 3};;                    
> val f : 'a -> int t = <fun>
> # let f x = { 42 with foo = 3};; 
> val f : 'a -> int t = <fun>
> 
> I think this qualifies as a bug. Especially since the documentation
> says the expr on the left hand side of with has to be a record.

What do you mean by worse? You just exhibit an immediate consequence
of the typing of f below.

> Thu, Mar 31, 2005 at 09:02:14PM -0500, Yaron Minsky wrote:
> > # type 'a t = { foo: 'a };;
> > type 'a t = { foo : 'a; }
> > # let f x = { x with foo = 3 };;
> > val f : 'a -> int t = <fun>

Now, why does it happen?
The reason is simply that the typing of {x with fields} follows
closely its semantics:
  build a record from the given fields, extracting values from x
  when a field is missing.
Consequence: if you provide all the fields, then there is no missing
field, so x can be completely ignored.
Following the manual, the typing could be more restrictive, but the
current typing is perfectly sound.

Jacques Garrigue


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

* Re: [Caml-list] Another polymorphism puzzle
  2005-04-01  4:29   ` Jacques Garrigue
@ 2005-04-01  9:45     ` Alex Baretta
  0 siblings, 0 replies; 5+ messages in thread
From: Alex Baretta @ 2005-04-01  9:45 UTC (permalink / raw)
  To: Ocaml

Jacques Garrigue wrote:
> From: Manos Renieris <er@cs.brown.edu>
> 
>>Worse:
>>
>># let f x = { () with foo = 3};;                    
>>val f : 'a -> int t = <fun>
>># let f x = { 42 with foo = 3};; 
>>val f : 'a -> int t = <fun>
>>
>>I think this qualifies as a bug. Especially since the documentation
>>says the expr on the left hand side of with has to be a record.
> 
> 
> What do you mean by worse? You just exhibit an immediate consequence
> of the typing of f below.

I think that "worse" here stands for "completely counterintuitive".

> Following the manual, the typing could be more restrictive, but the
> current typing is perfectly sound.
> 
> Jacques Garrigue

I doubt that any one of us has every written buggy code like the 
examples in this thread, yet, it it ever happens out of pure thermal 
agitation in the PS2 cable, I would expect the type checker to track 
down such "unsound" code, even if it would not cause a segfault.

Alex

-- 
*********************************************************************
http://www.barettadeit.com/
Baretta DE&IT
A division of Baretta SRL

tel. +39 02 370 111 55
fax. +39 02 370 111 54

Our technology:

The Application System/Xcaml (AS/Xcaml)
<http://www.asxcaml.org/>

The FreerP Project
<http://www.freerp.org/>


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

end of thread, other threads:[~2005-04-01 10:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-01  2:02 Another polymorphism puzzle Yaron Minsky
2005-04-01  2:32 ` [Caml-list] " Karl Zilles
2005-04-01  2:52 ` Manos Renieris
2005-04-01  4:29   ` Jacques Garrigue
2005-04-01  9:45     ` Alex Baretta

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