From: Yaron Minsky <yminsky@gmail.com>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Width subtyping
Date: Mon, 1 Jun 2009 07:34:47 -0400 [thread overview]
Message-ID: <891bd3390906010434r145b38b9pe7d9ddbf223da3db@mail.gmail.com> (raw)
In-Reply-To: <411083.97835.qm@web111504.mail.gq1.yahoo.com>
[-- Attachment #1: Type: text/plain, Size: 1987 bytes --]
On Sun, May 31, 2009 at 7:08 PM, Dario Teixeira <darioteixeira@yahoo.com>wrote:
>
> I also meant "heavier" in terms of efficiency. And like David said, it
> does
> feel wrong to carry the performance penalty of the object system solely
> because
> I need the structural subtyping features.
If you're willing to give up some of the syntactic niceties of records (and
the ability to pattern-match) you can get what you want using an abstract
type.
module type S =
sig
type 'a
t
val create_t1 : a : int -> b : int -> c : int -> [ `a | `b | `c ]
t
val create_t2 : a : int -> b : int -> c : int -> d :
int
-> [ `a | `b | `c | `d ]
t
val create_t3 : b : int -> c : int -> d : int -> [ `b | `c | `d ]
t
val a : [> `a ] t ->
int
val b : [> `b ] t ->
int
val c : [> `c ] t ->
int
val d : [> `d ] t ->
int
end
module M : S =
struct
type u = { a: int; b: int; c :int; d: int
}
type 'a t =
u
let default = { a = 0; b = 0; c = 0; d = 0
}
let create_t1 ~a ~b ~c = { default with a = a; b = b; c = c
}
let create_t2 ~a ~b ~c ~d = { a = a; b=b; c=c; d=d;
}
let create_t3 ~b ~c ~d = { default with b=b; c=c; d=d
}
let a t =
t.a
let b t =
t.b
let c t =
t.c
let d t =
t.d
end
let f x = M.a x + M.b
x
let g () = f (M.create_t1 ~a:0 ~b:0 ~c:0) (* accepted by compiler
*)
let g () = f (M.create_t2 ~a:0 ~b:0 ~c:0 ~d:0) (* accepted by compiler
*)
let g () = f (M.create_t3 ~b:0 ~c:0 ~d:0) (* rejected by compiler *)
The compiler error you get on that last line is this:
Error: This expression has type [ `b | `c | `d ]
M.t
but is here used with type [> `a | `b ]
M.t
The first variant type does not allow tag(s) `a
Here, we've chosen to use a default value for fields that we don't fill in.
We could just as well have used options here. The performance of the above
will be roughly the same as the performance of a simple record. Obviously,
all of the different "subtypes" have the full record stored at a physical
level.
y
[-- Attachment #2: Type: text/html, Size: 7447 bytes --]
next prev parent reply other threads:[~2009-06-01 11:34 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-05-31 23:08 Dario Teixeira
2009-06-01 11:34 ` Yaron Minsky [this message]
-- strict thread matches above, loose matches on Subject: below --
2009-06-01 13:56 Dario Teixeira
2009-06-01 14:21 ` David Allsopp
2009-06-01 17:04 ` Peng Zang
2009-05-30 15:36 Dario Teixeira
2009-05-31 5:18 ` Dave Benjamin
2009-05-31 7:34 ` David Allsopp
2009-06-01 4:21 ` Jacques Garrigue
2009-05-29 15:50 Dario Teixeira
2009-05-29 15:45 Dario Teixeira
2009-05-29 16:06 ` Till Varoquaux
2009-05-29 15:38 Dario Teixeira
2009-05-29 14:10 Dario Teixeira
2009-05-29 14:21 ` [Caml-list] " Jacques Carette
2009-05-29 14:43 ` David Allsopp
2009-05-29 15:33 ` Richard Jones
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=891bd3390906010434r145b38b9pe7d9ddbf223da3db@mail.gmail.com \
--to=yminsky@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=darioteixeira@yahoo.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