typed-racket-datatype
define-datatype
8.12

typed-racket-datatype🔗ℹ

Alex Knauth

Source code: https://github.com/AlexKnauth/typed-racket-datatype.

Provides define-datatype, a form for defining Algebraic Data Types in Typed Racket.

syntax

(define-datatype header (variant-id field ...) ...)

 
header = name-id
  | (name-id param-id ...)
     
field = [field-id : field-type]
Defines name-id as an Algebraic Data Type with the given variant-ids defined as structs with their respective fields. If param-ids are provided, the name-id type and the variant-id structs are all polymorphic with those param-ids, otherwise they are monomorphic.

Each variant struct includes a predicate and field accessors, and define-datatype also generates a predicate name-id? that accepts values that pass any of the variant predicates.

For example
(define-datatype name-id
  (variant-id [field-id : field-type] ...)
  ...)
is equivalent to
(struct variant-id ([field-id : field-type] ...) #:transparent)
...
(define-type name-id (U variant-id ...))
(define (name-id? x) (or (variant-id? x) ...))