Coalton Language Manual Overview
main+, /, ==, <=>, and >>=, refer to the Standard Library Reference.repository·main·Indexed 23 days ago
https://github.com/coalton-lang/coaltonCoalton is an efficient, statically typed functional programming language that integrates directly into Common Lisp. It provides strong type guarantees while maintaining Lisp's interactivity. The documentation covers installation, integration patterns using (coalton-toplevel ...), the 'mine' TUI editor and its 'beaming' workflow, and the use of Big-Float for arbitrary precision floating point numbers.
+, /, ==, <=>, and >>=, refer to the Standard Library Reference.The examples/ directory contains several Coalton projects designed to demonstrate language features and serve as real-world test cases. You can explore these projects to understand different application patterns:
small-coalton-programs: A collection of small, illustrative programs useful for educational purposes.quil-coalton: A monadic parser for Quil implemented in Coalton.thih: An implementation of Typing Haskell in Haskell using Coalton.fractal: A Mandelbrot viewer demonstrating integration between Coalton, Common Lisp, and SDL2.The mine TUI has a fixed layout with several key panes:
Ctrl+t to switch to it. Within this pane, use Enter to switch files, c to close, and s to save..asd project. You can open files directly from here.Ctrl+e.Ctrl+r.All elements are accessible via mouse clicks or keyboard shortcuts.
Packages located in the coalton/experimental directory provide useful functionality that is currently in an experimental stage. These features may undergo significant redesigns, syntax changes, or be moved to a lower-level implementation (such as being integrated directly into the compiler) in future versions.
Use these packages if you need specific functionality that is not yet part of the stable Coalton core, but be aware that your code may require updates if the underlying implementation or syntax changes.
Type checking in Coalton is built upon several core calculus operations defined in the src/typechecker/ directory:
mgu and unify (defined in src/typechecker/unify.lisp).apply-substitution (defined in src/typechecker/substitutions.lisp and src/typechecker/expression.lisp).infer-expression-type (defined in src/typechecker/define.lisp).The type checker uses a specific traversal logic based on a traverse-block struct, which contains a mapping of how every AST node should be transformed during traversal. All discovered type information is stored within an environment structure (src/typechecker/environment.lisp).
The forall operator allows you to introduce explicit type-variable binders (also known as scoped type variables) in a type declaration. While Coalton can infer polymorphism automatically, forall is used when you want the specific names of the type variables to be part of the declaration and available for use within the body of the declaration.
forall are available inside related the, declare, and lisp annotations in the corresponding body.define-class, forall expressions are scoped to their specific method definitions in define-instance.forall, declarations are still implicitly quantified, but the names are not scoped into the body.(forall (⟨var⟩...) ⟨type⟩)You can use the Unicode alias ∀ instead of the keyword forall.
(declare keep-first (forall (:left :right) :left -> :right -> :left))Interfacing Coalton with Lisp is similar to a Foreign Function Interface (FFI). Because Lisp is dynamically typed, it is easy to introduce type errors that Coalton cannot catch at compile time.
Recommendations:
(lisp (-> ...)) block. Watch out for numerical contagion, nil returns, or unexpected error conditions.check-type and other assertions when passing values from Lisp into Coalton-managed code to ensure they adhere to the expected types.The progn expression allows sequencing multiple expressions, returning the value of the last one.
Flat let syntax: Inside a progn, you can use a special flat let syntax for variable binding and pattern matching. Note that these flat let expressions are not recursive and do not support polymorphism. If you need a polymorphic let, use the standard let syntax wrapping the progn block.
Function definitions implicitly contain a progn block.
;; Using progn with flat let
(define (f x y)
(progn
(let x_ = (into x))
(let y_ = (into y))
(<> x_ y_)))
;; Pattern matching in flat let
(define (f t)
(let (Tuple fst snd) = t)
(+ fst snd))
;; Standard let for polymorphism (required if flat let fails)
(let ((id (fn (x) x)))
(progn
(id Unit)
(id "hello")))In Coalton, all source functions are fixed-arity.
A * B -> C represents a single function taking two arguments.A -> B -> C denotes a function that takes one argument and returns another function. This is the standard way to represent user-written currying in Coalton.The specialize directive allows you to register a specialized implementation for a generic function. When a call site has matching known types at compile-time, Coalton may rewrite the call to use the specialized function instead of the generic one. This happens automatically during monomorphization.
Important Safety Note: Specialization is not guaranteed. You must ensure that the specialized implementation behaves identically to the original generic definition.
(specialize ⟨generic-fun⟩ ⟨specialized-fun⟩ ⟨specialized-ty⟩)When a constrained function (a function requiring typeclass dictionaries) is treated as a first-class value, the compiler captures the resolved hidden dictionaries in a closure.
This closure's Common Lisp lambda list matches the remaining visible positional and keyword interface. This allows Common Lisp to perform standard runtime arity and keyword checks on the captured function object.
The pipe macro is a convenience tool for threading a value through a sequence of functions from left to right. Instead of nesting function calls like (h (g (f x))), you can use pipe to list the initial expression followed by the transformations in the order they should be executed.
Key characteristics:
pipe is a macro, it performs syntactic transformation rather than acting as a standard higher-order function.(pipe xs
reverse
sort
show)