Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007534OCamllanguage featurespublic2017-05-18 10:272018-04-08 09:32
Reporterfrisch 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityhave not tried
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0007534: Extensible record types
DescriptionWe already have extensible sum types. What about adding extensible record types? This might be very useful to represent "property bags" in an efficient way.

This could look like:

  type t = { .. }
  type t += { x: int = 0 }
  let x : t = { }
  let y : t = { x = 42 }
  let f (r : t) = r.x
  let g (r : t) = {r with x = 42}
  let h : r -> int = function {x; _} -> x

  module M : sig type t += {x: int} end = struct type t += { x: int = 0 } end
  module N : sig type t += {x: string} end = struct type t += { x: string = "" } end
  let r = {M.x = 42; N.x = "hello"}


On the implementation side:

 - Each extension label could be represented as a block with Object_tag (as for extension constructors), also holding the default value.

 - An extensible record value could be represented roughly as an object where the index of fields in the "vtable" would not be obtained by hashing the name, but using directly the unique integer stored in the extension label block. We would reuse the existing machinery for caching lookups.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0017803)
lpw25 (developer)
2017-05-18 11:49

Since you bring it up I had a design for this lying around in some notes somewhere. It follows pretty closely with what you suggest (even using the same syntax). The main difference was that it was mixed with a notion "optional label", so in addition to the "default argument" style of your proposal:

  type t += { x: int = 0 }
  r.x
  function {x; _} -> x

it also supported:

  type t += { ?y: int }
  r.?y
  function {?y; _} -> y

with

  r.y

as short-hand for:

  (let { ?y = Some y' } = r in y')

(including the ensuing warning -- I include it for syntactic consistency rather than usefulness)

and

  function {y; _} -> y

as shorthand for:

  function {?y = Some y} -> y
(0018424)
xleroy (administrator)
2017-09-30 18:14

You will need *very* convincing use cases to justify the implementation complexity for this extension.
(0018935)
frisch (developer)
2018-03-06 22:52

A typical use case would be an extensible application that can be extended with addins, where addins would need to attach custom data to objects in the application and retrieve the data later. This could be implemented using a technique such as the one you described in http://caml.inria.fr/pub/ml-archives/caml-list/2001/05/670222a47a96487f236602e00781f497.en.html [^] but would seem much more idiomatic (and could be made much more efficient) with some language-level support.

A concrete example would be a compiler where optimization passes could be loaded dynamically; some passes could store custom data on each AST node and other passes, later in the compilation process, could react on such data.

FWIW, we have much more uses for such "property lists" (currently using a library) than for extensible sum types in our code base.
(0018936)
lpw25 (developer)
2018-03-07 05:48

I would say that at least 50% of the (non-exception) uses of extensible variants I've seen are really emulating extensible records. They usually use a hash table combined with an extensible GADT for the key -- which needs to be both used as the key and stored in the result.

Of course, uses of extensible variants are pretty rare, so I'm not sure how convincing these examples are, but there is at least some use for this feature in the wild.

- Issue History
Date Modified Username Field Change
2017-05-18 10:27 frisch New Issue
2017-05-18 11:49 lpw25 Note Added: 0017803
2017-09-30 18:14 xleroy Note Added: 0018424
2017-09-30 18:14 xleroy Status new => acknowledged
2018-03-06 22:52 frisch Note Added: 0018935
2018-03-07 05:48 lpw25 Note Added: 0018936


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker