coalton-toplevel is the main entry point for compiling top-level Coalton definitions and declarations.

Syntax

(coalton-toplevel
  ⟨toplevel-form⟩...)

;; ⟨toplevel-form⟩ := ⟨declare-form⟩
;;                  | ⟨define-form⟩
;;                  | ⟨define-type-form⟩
;;                  | ⟨define-struct-form⟩
;;                  | ⟨define-type-alias-form⟩
;;                  | ⟨define-exception-form⟩
;;                  | ⟨define-resumption-form⟩
;;                  | ⟨define-class-form⟩
;;                  | ⟨define-instance-form⟩
;;                  | ⟨specialize-form⟩
;;                  | ⟨attribute-form⟩
;;                  | (progn ⟨toplevel-form⟩...)
;;                  | ⟨toplevel-macro-form⟩
;;
;; ⟨attribute-form⟩ := (repr :enum | :transparent | :lisp | :native ⟨lisp-type⟩)
;;                   | (derive ⟨class-name⟩+)
;;                   | (monomorphize)
;;                   | (inline)
;;                   | (overlap)

Semantics

  • coalton-toplevel compiles a group of top-level Coalton forms as a single translation unit and installs their definitions into the global Coalton environment.
  • Forms within the same coalton-toplevel may be mutually recursive regardless of the order in which they are written. The compiler processes categories in stages, sorting definitions into strongly connected dependency components (SCCs):
    1. Type, struct, exception, resumption, and type-alias definitions (define-type, define-struct, define-exception, define-resumption, define-type-alias), along with repr and derive attributes.
    2. Type class definitions (define-class).
    3. Explicit and derived class instances (define-instance).
    4. Value and function declarations and definitions (declare, define), along with inline and monomorphize attributes.
    5. Specialization rules (specialize).
  • Attributes (repr, derive, monomorphize, inline, overlap) attach to the immediately following definition or declaration; a trailing attribute with no target form signals a parse error.
  • A top-level (progn ⟨toplevel-form⟩...) splices its subforms into the enclosing coalton-toplevel unit.
  • All defined names, constructors, and methods must be symbols interned in the current Common Lisp *package*.
  • Standalone expressions are not valid top-level forms; use coalton to evaluate Coalton expressions.

Examples

(coalton-toplevel
  (derive Eq Show)
  (repr :enum)
  (define-type Mode
    Development
    Release)

  ;; Mutually recursive functions can appear in any order
  (declare even-count? (UFix -> Boolean))
  (define (even-count? n)
    (if (== n 0)
        True
        (odd-count? (- n 1))))

  (declare odd-count? (UFix -> Boolean))
  (define (odd-count? n)
    (if (== n 0)
        False
        (even-count? (- n 1)))))

Other notes

  • Each compiled coalton-toplevel form emits a load-time check verifying that the FASL was compiled in the same compiler mode (development vs. release) as the running image.
  • Inside library source code when :coalton-lisp-toplevel is present in cl:*features*, coalton-toplevel also accepts (lisp-toplevel () ⟨lisp-form⟩...) to splice raw Common Lisp top-level forms into the generated output.
  • To inspect the Common Lisp code generated by a coalton-toplevel form, replace coalton-toplevel with pprint-coalton-codegen.