diff --git a/docs/intro-to-coalton.md b/docs/intro-to-coalton.md index f218eb745..7c768f770 100644 --- a/docs/intro-to-coalton.md +++ b/docs/intro-to-coalton.md @@ -272,6 +272,41 @@ Type definitions introduce type constructors. For example, we may construct a so We'll see how to unpack these types using `match` later in this document. +## Type Aliases + +Coalton allows the definition of parametric type aliases. Type aliases can be defined on primitive types and types created with `define-type` or `define-alias`. + +```lisp +(coalton-toplevel + ;; New aliases are created with the DEFINE-ALIAS operator + (define-alias Coordinate Integer) + (define-alias (Pair :a) (Tuple :a :a)) + (define-alias Translation (Pair Coordinate -> Pair Coordinate)) + + (declare shift-right Translation) + (define (shift-right (Tuple x y)) + (Tuple (1+ x) y)) + + (define shifted-coordinate (shift-right (Tuple 0 0)))) +``` + +Outside of a Coalton expression, `describe-type-of` displays the type of a symbol, including its aliases, and returns the type. `describe-alias` displays the alias along with its base type and returns the base type. + +```lisp +COALTON-USER> shifted-coordinate +#.(TUPLE 1 0) + +COALTON-USER> (type-of 'shifted-coordinate) +(TUPLE INTEGER INTEGER) + +COALTON-USER> (describe-type-of 'shifted-coordinate) +[(PAIR COORDINATE) := (TUPLE [COORDINATE := INTEGER] [COORDINATE := INTEGER])] + +COALTON-USER> (describe-alias 'Pair) +[(PAIR :A) := (TUPLE :A :A)] +``` + + ### Structs diff --git a/src/debug.lisp b/src/debug.lisp index 0519d3786..666f6f3ad 100644 --- a/src/debug.lisp +++ b/src/debug.lisp @@ -184,7 +184,8 @@ "Lookup the type represented by the alias SYMBOL in the global environment" (let ((tc:*pprint-aliases* t) (type (tc:alias-entry-type (tc:lookup-alias entry:*global-environment* symbol)))) - (format t "~S~%" type) + (tc:with-pprint-variable-context () + (format t "~S~%" type)) type)) (defun coalton:kind-of (symbol)