Skip to content

Merge references, arrays, and pointers #405

@gebner

Description

@gebner

Currently, we expose four related types in the Pulse standard library: ref, array, slice, and ArrayPtr.ptr.

Slices and pointers can be straightforwardly converted into each other. But references, arrays, and pointers can't. You can't turn a singleton array into a reference, a reference into a pointer to a singleton slice, etc.

I think we should unify pointers and arrays, and then define references as singleton arrays. The groundwork for this is already laid in the HigherArray module, which defines arrays as subarray pointers (but does not expose this fact).

Concretely, this proposal would:

  1. Expose functions in HigherArray/Array/etc. to split array (references).
  2. Define ref t as larray t 1. (But hide this definition behind conversion functions.)
  3. Define ArrayPtr.ptr t as array t (i.e., remove the module since it's redundant now).

Pros:

  • We reduce a lot of code duplication with arrays/slices/pointers. Operations like sorting/swapping etc. are defined twice.
  • It becomes feasible to model output parameters as mutable references, and you can pass array fields as references.
  • C code makes use of these conversions, and we would support that.

Cons:

  • OCaml does not support this kind of references natively. We could either not support the conversion functions on the OCaml backend, or simulate them.

Alternatives:

  • We could add type classes for array-like and singleton-like references. But that's not really feasible without monomorphization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions