On this page:
Algebraic Data Types for compilers

Algebraic Data Types for compilers

 (require phc-adt) package: phc-adt-lib

This library is implmented using literate programming. The implementation details are presented in Algebraic Data Types for compilers: Implementation.

This library defines tagged structures, untagged structures, constructors and variants. While uses of Racket’s struct need the struct to be declared before they are used, the structures implemented by this library can be used anonymously, without declaring them in advance. All uses of a tagged structure with the same tag name and set of fields are equivalent, even if they are declared in separate files. It is also possible to access a field on a tagged structure instance without specifying the tagged structure’s tag name. This is used in a separate library (not published yet) to implement the dotted notation commonly used in object-oriented languages for field accesses, instance.field.

This library works by saving across compilations the list of all tagged structures used across the program. The tag name and list of field names for each tagged structure is written to a file. That file is, by default, located in the same directory as the source file, and it is called "adt-pre-declarations.rkt", but this can be changed using the optional argument to adt-init. These "remembered" tagged structure definitions are used to pre-declare the structs corresponding to each tagged structure, so that all the code using the same "adt-pre-declarations.rkt" file sees the same structure definitions.



(adt-init pre-declarations-file)
  pre-declarations-file : string?
The (adt-init) macro has to be called in a module context, after the (require phc-adt) but before using any of the identifiers provided by this library.

The pre-declarations-file (which defaults to "adt-pre-declarations.rkt") should be the string representation of a path, relative to the directory containing the file which uses adt-init.

The adt-init macro creates the pre-declarations-file if it does not exist, and requires it. The pre-declarations-file contains a list of remembered tagged structures (specifically, the tag name followed by a list of field names). It uses the #lang phc-adt/declarations which pre-declares the structs corresponding to each remembered tagged structure.

It then makes all the identifiers defined by this library available (the identifiers cannot otherwise be used before calling adt-init).

This #lang is used by the pre-declarations-file (see adt-init), and is not intended to be used in other situations.

    1 Tagged structures

    2 Untagged structures

    3 Constructors

    4 Variants