declare specifies the static type of a top-level definition or local binding.
Syntax
;; Top-level declaration (inside coalton-toplevel)
⟨opt-attribute⟩*
(declare ⟨name⟩ ⟨qualified-type⟩)
;; Local declaration (inside let, let*, rec, for, or for* binding list)
(declare ⟨name⟩ ⟨qualified-type⟩)
;; ⟨opt-attribute⟩ := (inline) | (monomorphize) | (abi ⟨convention⟩)
;; ⟨convention⟩ := :auto | :lisp | :unpacked | :unboxed
Semantics
- At top level inside
coalton-toplevel,declarespecifies the type scheme of adefineform with the same⟨name⟩in that translation unit. - Inside the binding list of
let,let*,rec,for, orfor*,(declare ⟨name⟩ ⟨qualified-type⟩)constrains the local binding⟨name⟩in that same binding list. - Every
declaremust pair with a definition in the same scope, and at most onedeclaremay be given per binding; orphan or duplicate declarations are compile-time errors. ⟨qualified-type⟩may be a monomorphic type, a constrained type using=>, or an explicitly quantified type usingforall.- Unquantified type variables (
:a,:b, …) in⟨qualified-type⟩are implicitly universally quantified over the declaration, except in local declarations where type variables already in scope from an enclosingforallrefer to those outer scoped type variables. - When
⟨qualified-type⟩begins with an explicitforall, the listed type variables are brought into lexical scope across the corresponding definition body (for use in innerdeclare,the, andlispforms). - The declared type is verified against the inferred type of the definition:
- The declared type may specialize a more general inferred type (for example, declaring
Integer -> Integerfor(+ x 1)), but it cannot be more general than the inferred type. - All non-defaultable typeclass constraints required by the body must be entailed by the declared constraint context.
- The declared type may specialize a more general inferred type (for example, declaring
- A binding cannot be declared with a bare
Voidor multi-value packT1 * T2type; declarations must be single-value types or function types (such asVoid -> VoidorInteger -> Integer * Integer).
Examples
(coalton-toplevel
;; Constrained polymorphic declaration
(declare clamp (Ord :a => :a * :a * :a -> :a))
(define (clamp lo hi x)
(max lo (min hi x)))
;; Explicit `forall` brings `:a` into scope inside the body
(declare singleton-vec (forall (:a) :a -> (Vector :a)))
(define (singleton-vec x)
(the (Vector :a) [x]))
;; Local `declare` inside `let` and `for`
(declare sum-squares (UFix -> UFix))
(define (sum-squares n)
(let ((declare scale UFix)
(scale 2))
(for ((declare i UFix)
(declare acc UFix)
(i 0 (+ i 1))
(acc 0 (+ acc (* scale (* i i)))))
:returns acc
:while (< i n)))))
Other notes
- Exported top-level
defineforms without a correspondingdeclareemit acoalton:deprecation-warning(planned to become a compile-time error in a future release). - Top-level optimization attributes
inline,monomorphize, andabimay be placed immediately before either thedeclareform or thedefineform.