Skip to content

Allow custom constructor patterns for opaque data types #1590

@stylewarning

Description

@stylewarning

Sometimes we write code like

(repr :native foo)
(define-type Foo)

and Foo naturally has an algebraic structure, but it's just opaque to Coalton. For example, consider

(repr :native (cl:unsigned-byte 32))
(define-type RGBA)

which holds an RGBA value in an 8-bit quadruplet. We would want to write code like

(match c
  ((RGBA r g b a) ...))

The goal of this task is to allow one to define the above.

If we want to go the 90% of the way there, we should allow arbitrary discriminated unions:

(repr :native (cl:or cl:single-float (cl:unsigned-byte 32)))
(define-type Color)

(define (f c)
  (match c
    ((RGBA r g b a) ...)
    ((Grayscale p) ...)))

If we want to go the full mile, we should allow discrimination based off of a property of the value, not just its type:

(repr :native (cl:unsigned-byte 64))
(define-type Color)

;; RGB: first byte is #x00
;; Gray: first byte is #x01
;; HSL: first byte is #x02
;; etc.

(define (f c)
  (match c
    ((RGB r g b) ...)
    ((Grayscale g) ...)
    ((HSL h s l) ...)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegenCode generation matters (Coalton -> Lisp)enhancementNew feature or requestperformance

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions