-> constructs function types, lisp return-type specifications, arrow kinds in forall binders, and functional dependencies in define-class.
Syntax
;; Function type with positional and optional keyword inputs
(⟨input-types⟩ [&key (:⟨kw⟩ ⟨type⟩)*] -> ⟨output-spec⟩)
;; Function type with zero positional inputs
(Void -> ⟨output-spec⟩)
(&key (:⟨kw⟩ ⟨type⟩)+ -> ⟨output-spec⟩)
;; Inline `lisp` return type
(lisp (-> ⟨output-spec⟩) (⟨var-spec⟩*) ⟨lisp-form⟩+)
;; Arrow kind in a `forall` binder
(forall ((:⟨var⟩ (⟨kind-1⟩ -> ⟨kind-2⟩))) ⟨qualified-type⟩)
;; Functional dependency in `define-class`
(define-class (⟨class-name⟩ ⟨type-var⟩+ (⟨type-var⟩+ -> ⟨type-var⟩+)) ...)
;; ⟨input-types⟩ := ⟨type⟩ | ⟨input-types⟩ * ⟨type⟩
;; ⟨output-spec⟩ := Void
;; | ⟨type⟩
;; | ⟨type⟩ * ⟨type⟩ [* ⟨type⟩ ...]
;; | ⟨input-spec⟩ -> ⟨output-spec⟩
Semantics
- In function types:
A -> Bis the type of a unary function takingAand returningB.- Multiple positional arguments are separated by
*on the left of->:A * B -> Cis a fixed-arity two-argument function called as(f a b). ->is right-associative in output position:A -> B -> CmeansA -> (B -> C), a unary function returning a unary function, called as((f a) b).Void -> Tis a zero-argument function called as(f). When keyword arguments are present on a function with zero positional inputs, omitVoidand write(&key (:k K) -> T).T -> Voidis a function that returns zero values (such as(values)).T -> A * Bis a function that returns multiple values viavalues.
- In
lispforms:(-> ⟨output-spec⟩)declares the return type(s) of the embedded Common Lisp body; nothing may appear to the left of->.
- In
forallkind annotations:(⟨kind-1⟩ -> ⟨kind-2⟩)denotes the kind of a type constructor. Kind arrows associate to the right:(Type -> Type -> Type)is(Type -> (Type -> Type)).
- In
define-classfunctional dependencies:(⟨from-var⟩+ -> ⟨to-var⟩+)declares that the class type variables on the left uniquely determine those on the right.
Examples
(coalton-toplevel
;; Multi-argument function vs. curried function
(declare add-pair (Integer * Integer -> Integer))
(define (add-pair x y)
(+ x y))
(declare curried-add (Integer -> Integer -> Integer))
(define (curried-add x)
(fn (y) (+ x y)))
;; Nullary function returning multiple values
(declare origin (Void -> Integer * Integer))
(define (origin)
(values 0 0))
;; Kind arrow in `forall`
(declare map-id (forall ((:f (Type -> Type)) (:a Type))
(Functor :f => (:f :a) -> (:f :a))))
(define (map-id xs)
(map id xs)))