From: Goswin von Brederlow <goswin-v-b@web.de>
To: Pal-Kristian Engstad <pal_engstad@naughtydog.com>
Cc: "yminsky@gmail.com" <yminsky@gmail.com>,
"caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] pattern matching and records vs tuples
Date: Thu, 16 Apr 2009 18:05:53 +0200 [thread overview]
Message-ID: <87tz4opori.fsf@frosties.localdomain> (raw)
In-Reply-To: <49E53C8A.1090601@naughtydog.com> (Pal-Kristian Engstad's message of "Tue, 14 Apr 2009 18:46:50 -0700")
Pal-Kristian Engstad <pal_engstad@naughtydog.com> writes:
> Honestly, I'd prefer to have to annotate non-exhaustive records:
> let { foo = foo; bar = bar } = x
> should only match { foo; bar }, but
> let { foo = foo; bar = bar; .. } = x,
> can match records with more labels.
> PKE.
> Yaron Minsky wrote:
I would actually like to propose that to work on a type
level. Currently we have:
----------------------------------------------------------------------
# type base = { x : int }
let get_x r = r.x
type more = { x : int; y : int };;
type base = { x : int; }
val get_x : base -> int = <fun>
type more = { x : int; y : int; }
# get_x { x=1; y=2; };;
Error: This expression has type more but is here used with type base
----------------------------------------------------------------------
I propose that records can be subtyped if their prefix matches and
".." would be used to denote the subtype of any record with this prefix:
# type base = { x : int }
let get_x r = r.x
type more = { x : int; y : int };;
type base = { x : int; }
val get_x : base -> int = <fun>
type more = { x : int; y : int; }
# get_x ({ x=1; y=2; } :> base);;
- : int = 1
One could also declare get_x to accept supertypes:
# let get_x (r : { x : int; .. }) = r.x;
val get_x : { x : int; .. } -> int = <fun>
# get_x { x=1; y=2; };;
- : int = 1
I think there is only one thing that would need to be changed for the
compiled code with record subtypes like the above. Copying and
modifying a record would need allocate a block matching the size of
the input record and not the size of the subtype. Think about this
code:
# let change_x (r : { x : int; .. }) x = { r with x = x }
val change_x : { x : int; .. } as 'a -> int -> 'a = <fun>
# change_x { x=1; y=2; } 2;;
- : more = {x = 2; y = 2}
All other changes would only affect the grammar and type inference I think.
Now what would happen with your example?
# type record = { foo : int; bar : float; baz : string }
type record = { foo : int; bar : float; baz : string; }
# let f x = let { foo = foo; bar = bar; .. } = x in (foo, bar)
What type is that?
1) val f : { foo : int; bar : float; baz : string; .. } -> int * float = <fun>
2) val f : record -> int * float = <fun>
3) val f : { foo : int; bar : float; .. } -> int * float = <fun>
4) val f : { foo : 'a; bar : 'b; .. } -> 'a * 'b = <fun>
I would say it should result in 3. And the type should always be the
minimum prefix required to match all occuring fields:
# let f x = let { foo = foo; baz = baz; .. } = x in (foo, baz)
val f : { foo : int; bar : float; baz : string; .. } -> int * string = <fun>
As a further extention the following syntax would be usefull:
# type base = { x : int }
type more = { base with y : int };;
type base = { x : int; }
type more = { x : int; y : int; }
With this syntax more would always be a supertype of base even if the
base type gets altered.
MfG
Goswin
next prev parent reply other threads:[~2009-04-16 16:05 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-04-14 14:12 Yoann Padioleau
2009-04-14 16:00 ` [Caml-list] " Christophe TROESTLER
2009-04-14 16:40 ` Goswin von Brederlow
2009-04-14 16:58 ` Yoann Padioleau
2009-04-14 20:01 ` Christophe Raffalli
2009-04-15 0:44 ` Yaron Minsky
2009-04-15 1:46 ` Pal-Kristian Engstad
2009-04-15 2:37 ` Yaron Minsky
2009-04-15 2:40 ` Ashish Agarwal
2009-04-16 16:05 ` Goswin von Brederlow [this message]
2009-04-16 16:59 ` David Allsopp
2009-04-17 0:26 ` Jacques Garrigue
2009-04-17 21:12 ` Goswin von Brederlow
2009-04-15 7:41 ` blue storm
2009-04-15 9:30 ` Martin Jambon
2009-04-15 11:01 ` Yaron Minsky
2009-04-15 12:04 ` Martin Jambon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87tz4opori.fsf@frosties.localdomain \
--to=goswin-v-b@web.de \
--cc=caml-list@inria.fr \
--cc=pal_engstad@naughtydog.com \
--cc=yminsky@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).