/coalton

Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.

Primary LanguageCommon LispMIT LicenseMIT

GitHub Workflow Status Discord

Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.

Coalton can be written in files:

(in-package #:coalton-user)

(coalton-toplevel
  (define-type Symbol
    (Symbol String))

  (define (symbol-name sym)
    (match sym
      ((Symbol s) s)))

  (define-instance (Eq Symbol)
    (define (== a b)
      (== (symbol-name a) (symbol-name b)))
    (define (/= a b)
      (not (== a b))))

  (define-type Expr
    "A symbolic expression of basic arithmetic."
    (EConst Integer)
    (EVar   Symbol)
    (E+     Expr Expr)
    (E*     Expr Expr))

  (declare diff (Symbol -> Expr -> Expr))
  (define (diff x f)
    "Compute the derivative of F with respect to X."
    (match f
      ((EConst _)   ; c' = 0
       (EConst 0))
      ((EVar s)     ; x' = 1
       (if (== s x) (EConst 1) (EConst 0)))
      ((E+ a b)     ; (a+b)' = a' + b'
       (E+ (diff x a) (diff x b)))
      ((E* a b)     ; (ab)' = a'b + ab'
       (E+ (E* (diff x a) b)
           (E* a          (diff x b))))))

 (declare dt (Expr -> Expr))
 (define dt
   "The time derivative operator."
   (diff (Symbol "t"))))

And at the REPL:

CL-USER> (in-package #:coalton-user)
COALTON-USER> (coalton (dt (E+ (EVar (Symbol "t"))
                               (EConst 1))))
#.(E+ #.(ECONST 1) #.(ECONST 0))

Coalton has not reached "1.0" yet. This means that, from time to time, you may have a substandard user experience. While we try to be ANSI-conforming, Coalton may only work on SBCL 2.1.x.

What's Here?

This repository contains the source code to the Coalton compiler, and the standard library.

It also contains a few example programs, such as:

Lastly and importantly, we maintain a collection of documentation about Coalton in the docs directory, including a standard library reference guide.

Getting Started

Install: Clone this repository into a place your Lisp can see (e.g., ~/quicklisp/local-projects/). (Coalton is not yet on Quicklisp.)

Use: Either run (ql:quickload :coalton), or add #:coalton to your ASD's :depends-on list.

Test: Run (ql:quickload :coalton/tests) to download the dependencies, then run (asdf:test-system :coalton).

Learn: We recommend starting with the Intro to Coalton document, and then taking a peek in the examples directory. It may also be helpful to check out the introductory blog post.

Get Involved

Want to ask a question about Coalton, propose a feature, or share a cool program you wrote? Try posting in the GitHub Discussions page!

We welcome contributions of all forms, especially as we stabilize toward a 1.0 release. We would be grateful to receive:

  • bug reports (filed as issues),
  • bug fixes and typo corrections (filed as pull requests),
  • small example programs, and
  • user experience troubles.