• This repository has been archived on 10/Sep/2021
  • Stars
    star
    208
  • Rank 189,015 (Top 4 %)
  • Language
    Common Lisp
  • License
    MIT License
  • Created over 6 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Coalton is (supposed to be) a dialect of ML embedded in Common Lisp.

Deprecated! Moved!

This version of Coalton was a prototype and is here for archival purposes only. It is not maintained. The project is now officially located at this link.


Coalton

Check out the library to see the latest that Coalton can express!

Coalton is a dialect of ML embedded in Common Lisp. It emphasizes practicality and interoperability with Lisp, and is intended to be a DSL that allows one to gradually make their programs safer.

Coalton currently allows one to do the following in Common Lisp:

  • Express statically typed programs similar in spirit to Standard ML, OCaml, and Haskell.
  • Perform compile-time type inference and type checking Γ  la Hindley-Milner type inference.
    • Optionally, global values can be manually declared if desired.
  • Interoperate an ML-like with Common Lisp, in both directions. All data values are native and easy data structures.
  • Define parameterized algebraic data types, including mutually recursive types.
  • Define macros using the standard defmacro facility that is understood by Coalton. (In Coalton, if is a macro that expands into a match on the Boolean type!)

Coalton does not intend to re-define how Lisp is written entirely, but rather it serves to augment Lisp with an additional, available programming dialect.

Coalton is a work-in-progress and has some limitations:

  • [bug] Unit tests aren't at all written. USE AT YOUR OWN RISK!
  • [bug] Redefinitions don't play well in a lot of ways:
    • Types aren't globally re-checked or warned about.
    • The internal type DB is frequently clobbered with new data.
  • [bug] Declared types are used for inference, but are simply believed without question. As such, a bug in a declaration will cause runtime bugs.
  • [bug] Nested patterns, guards, and a more elaborate pattern grammar is not implemented.
  • Generalized algebraic data types, type classes, and high-order types are neither implemented nor supported.
  • ML structures, signatures, or functors are neither implemented nor supported.

Coalton-Lisp Bridge

Coalton within Lisp

Coalton value definitions come in two forms.

;; Coalton
(define f v)
;; Lisp
f   ; an object (possibly a function) in the variable namespace
    ; can be lexically shadowed

;; Coalton
(define (f x ...) v)
;; Lisp
f    ; a function object in the variable namespace
     ; can be lexically shadowed
#'f  ; a function object in the function namespace

Lisp within Coalton

Use the coalton:lisp macro to embed Lisp code inside of a Coalton program. Of course, Coalton will not be able to verify your Lisp code is correct. For example, the following function uses Common Lisp's ~R format directive to write integers as strings.

;; Coalton
(coalton-toplevel
  (declare integer-name (fn Integer -> String))
  (define (integer-name n)
    (lisp String
      (cl:format cl:nil "~R" n))))

Examples

You may want to check out the library for example code. It represents the latest of what Coalton can do, and it also has a lot of definitions that the following depends on. (It is loaded by default as a part of Coalton.)

First we will load the system and get into COALTON-USER. Note that this package currently does not :USE the COMMON-LISP package, so you must qualify CL symbols manually.

CL-USER> (ql:quickload :coalton)
...
CL-USER> (in-package :coalton-user)
#<COMMON-LISP:PACKAGE "COALTON-USER">
COALTON-USER>

We already have a Maybe algebraic data type (ADT) defined in the library. We can immediately construct these values in Common Lisp. We use the convention in Lisp code to capitalize the first letter of constructors and type names. (Otherwise the capitalization has no significance.)

COALTON-USER> (cl:list (Just 5) Nothing Nothing)
(#.(JUST 5) #.NOTHING #.NOTHING)
COALTON-USER> cl::(eq (second *) (third *))
COMMON-LISP:T

We can define a type-safe function gg. Definitions must occur within coalton-toplevel forms.

COALTON-USER> (coalton-toplevel
                (define (gg x) (if x (left 1) (right x))))
GG

And we can use gg from Lisp.

COALTON-USER> (cl:funcall gg True)
#.(LEFT 1)

More conveniently, however, we can use the Coalton-to-Lisp bridge using the coalton macro.

COALTON-USER> (coalton (gg True))
#.(LEFT 1)

It's more convenient because it doesn't use funcall, and catches compile-time type errors!

COALTON-USER> (coalton (gg 1))

Type error: The types INTEGER and BOOLEAN don't match.
   [Condition of type COMMON-LISP:SIMPLE-ERROR]

Restarts:
 0: [RETRY] Retry SLIME REPL evaluation request.
 1: [*ABORT] Return to SLIME's top level.
 2: [ABORT] abort thread (#<THREAD "repl-thread" RUNNING {1008E40413}>)

We can inspect what Coalton inferred the type of gg to be using type-of. Note that, unlike in Common Lisp, coalton:type-of takes a symbol naming a global value.

COALTON-USER> (type-of 'gg)
(FN BOOLEAN -> (EITHER INTEGER BOOLEAN))

The Coalton library defines Liszt, whose name will surely change. It's a homogeneous list type, and as usual, we can construct values in Lisp.

COALTON-USER> (coalton-toplevel
               (define l (Kons (Just 1) (Kons Nothing (Kons (Just 2) Knil)))))
#.(KONS #.(JUST 1) #.(KONS #.NOTHING #.(KONS #.(JUST 2) #.KNIL)))

The type inferencer will deduce the type correctly.

COALTON-USER> (type-of 'l)
(LISZT (MAYBE INTEGER))

And it will also catch errors.

COALTON-USER> (coalton (Kons 1 2))

Type error: The types INTEGER and (LISZT INTEGER) don't match.
    [Condition of type SIMPLE-ERROR]

Restarts:
 0: [RETRY] Retry SLIME REPL evaluation request.
 1: [*ABORT] Return to SLIME's top level.
 2: [ABORT] abort thread (#<THREAD "repl-thread" RUNNING {1008E28413}>)

More Repositories

1

cl-algebraic-data-type

Algebraic data types in Common Lisp
Common Lisp
120
star
2

quickutil

The solution to the Utility Library problem.
Common Lisp
87
star
3

lisp-random

A smattering of random Lisp code by Robert Smith
Common Lisp
66
star
4

cl-permutation

Permutations and permutation groups in Common Lisp.
Common Lisp
48
star
5

formulador

Render math formulas in 2D in your terminal!
Common Lisp
44
star
6

quantum-interpreter

A tiny, self-contained, general-purpose quantum interpreter.
Common Lisp
43
star
7

computable-reals

Arbitrary precision, automatic re-computing real numbers in Common Lisp.
Common Lisp
28
star
8

hypergeometrica

Livin' like it's 1813 (or 1988).
Common Lisp
25
star
9

policy-cond

Common Lisp
15
star
10

callback-heaven

Generating and managing callbacks from C into Heaven (Lisp).
Common Lisp
12
star
11

cl-forest

Unofficial bindings to Rigetti Forest in Common Lisp.
Common Lisp
10
star
12

talks

Talks, notes, slides, etc.
TeX
6
star
13

algebraic-data-library

Common Lisp
5
star
14

instructions

Instructions for various things.
Shell
3
star
15

interface

Common Lisp
2
star
16

arpeggiando

Common Lisp
2
star
17

webapp

An example webapp in Common Lisp using Hunchentoot and stuff.
Common Lisp
2
star
18

compsci-with-robert

Common Lisp
2
star
19

clim-color-picker

Sample, minimal, buildable CLIM app.
Common Lisp
2
star
20

map-set

Common Lisp
2
star
21

big-string

Common Lisp
2
star
22

synonyms

Common Lisp
2
star
23

recur

Common Lisp
1
star
24

parameterized-function

Common Lisp
1
star
25

cadrlogue

A book and media cataloguing system.
Common Lisp
1
star
26

cl-stopwatch

Common Lisp
1
star
27

illogical-pathnames

Common Lisp
1
star
28

maxima

Common Lisp
1
star