Typed Lisp, A Primer (2019)

by tagfowufeon 1/31/23, 1:20 PMwith 30 comments
by PaulHouleon 1/31/23, 5:28 PM

I have hacked off and on on this

https://github.com/paulhoule/ferocity

which lets you write extended Java in a lispy syntax. It generates stubs for the standard library and other packages you choose, unerasing types by putting them into method names. It works pretty well with IDEs but there are still problems w/ type erasure such that some kinds of type checking can't be done by the compiler working directly on the lispy Java, probably I wouldn't implement newer features such as pattern matching that are dependent on type inference to work, though lambda definitions are feasible if you give specific types.

The 'extended' bit was almost discovered instead of invented in that it is pretty obvious that you need quote and eval functions such that you can write lispy Java programs that manipulate Java expressions. Said expressions can be evaled at runtime with a primitive interpreter or incorporated into classes that are compiled w/ Javac. The motivation of the thing was to demonstrate Java-embedded-in-Java (an ugly kind of homoiconicity) and implement syntactic macros from Java which I think that prototype proves is possible but there is a lot more to be done on it to be really useful. Enough has been implemented in it right now in that you can use it to write the code generator that builds stubs. It might be good for balls-to-the-walls metaprogramming in Java but I think many will think it combines all the worse features of Java and Lisp.

by shadowgovton 1/31/23, 8:58 PM

Article makes one error that is mostly correct but can trip someone up in corner cases. In section 2.5, article makes the assertion `(the τ e) ≈ (or (check-type e τ) e)`

This may be true in some implementations, but the actual defintion of the `(the)` special operator is that if e is not type Ï„, the behavior of `(the Ï„ e)` is undefined! Crashing when the type is determined to mismatch is one allowed result, but `the` is also the language's "escape hatch" to permit implementations to improve performance by throwing away runtime type information, at which point abusing types will crash in surprising ways.

by vindarelon 1/31/23, 3:52 PM

> Augment Lisp with functional Haskell-like type declarations ;-)

Since the article's publication, this is now possible with the industrial-grade Coalton: https://github.com/coalton-lang/coalton/

by aidenn0on 1/31/23, 6:53 PM

With the disclaimer that cl:satisfies makes the tun-time type-system turing complete (and no implementation I know of chacks cl:satisfies at compile time), the compiler-available types are strictly non-recursive.

For example, you cannot define a type that means "A List of type X" because that requires recursion i.e. this is not allowed:

    (deftype typed-list (x) `(cons ,x (or (typed-list ,x) null)))
So one must either declare the type of the loop-local variable(s) (when iterating) or the first N item(s) of a list (when recursing), which is a bit unfortunate. SBCL, in particular, does a great job of inferring types at compile time with just a few annotations, but code that is optimizer-friendly is made significantly more ugly by this. Or, you know, people just end up using arrays for everything.

by runevaulton 1/31/23, 3:47 PM

I've been thinking about typed lisp a bit lately since I started messing with CL again, and I admit the point about lists as code hadn't crossed my mind as something you have to deal with for any typing attempts. If you tried to make a "fully static typed lisp" likely you'd need a few holes for areas like that where they don't make sense. Well that or do the shenanigans things like c# do for param arrays where it is just an array of objects.

Mind you I feel like over time we are seeing dynamic and static typing converging more and more. Python type annotations are a thing to some degree I know, while c# a while back added the dynamic type.

by Pet_Anton 1/31/23, 2:32 PM

There is a similar project for Clojure: https://github.com/typedclojure/typedclojure

by retracon 1/31/23, 4:49 PM

Yes. With macros and runtime logic it's possible to implement just about any type system in Lisp, even an ML-like system. Box everything and use macros to transparently pick it apart and reconstruct your wrapped type objects when you use them. My initial reaction to that whole idea was horror. The inefficiency! But maybe not necessarily. A compiler that understood the type system used, could optimize most of it away. Just like how Haskell or SML compilers do.

by neilvon 1/31/23, 4:42 PM

The Racket platform has Typed Racket, by Sam Tobin-Hochstadt, et al.

https://docs.racket-lang.org/ts-guide/beginning.html

by dangon 1/31/23, 6:45 PM

Related:

Typed Lisp, a Primer (2019) - https://news.ycombinator.com/item?id=23878612 - July 2020 (26 comments)

by TOGoSon 1/31/23, 4:20 PM

> type's have always been there

As have excessive apostrophe's.