Skip to content

Commit

Permalink
add tutorial documentation for type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarin Heffes committed Oct 16, 2024
1 parent 9dda5c2 commit 314ff0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
35 changes: 35 additions & 0 deletions docs/intro-to-coalton.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/debug.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 314ff0b

Please sign in to comment.