3 ms·
My nomination for what we get wrong over and over again: working under the assumption that there is One True Syntax. There isn't. Different syntaxes are suita
by lisper 10y ago
My nomination for what we get wrong over and over again: working under the assumption that there is One True Syntax. There isn't. Different syntaxes are suitable for different needs.
A single language can support multiple syntaxes if the AST is exposed as a first-class construct. This makes it easy to write new syntactic front-ends. Unfortunately, the only language to date that supports this idea is Lisp, which means that this incredibly powerful idea is conflated in most people's minds with lots of irritating silly parentheses. (One of the reasons for this conflation is that once you start adopting this mindset the parens become a lot less silly and irritating, but that's another story.)
I'm currently working on cryptography code, where algebraic syntax is very convenient. Here's an excerpt from some code I'm currently working on:
(define-method (point-double (ec elliptic-curve a b c q r) x y)
(bb
lambda modp(q, (3*x*x + 2*a*x + b)/(2*y))
x3 modp(q, lambda*lambda - a - 2*x)
y3 modp(q, lambda*(x-x3)-y)
(values x3 y3)))
(define-method (point-add (ec elliptic-curve a b c q r) x1 y1 x2 y2)
(if (and (= x1 x2) (= y1 y2))
(point-double ec x1 y1)
(bb
lambda modp(q, (y1-y2)/(x1-x2))
x3 modp(q, lambda*lambda-a-x1-x2)
y3 modp(q, lambda*(x1-x3)-y1)
(values x3 y3))))
Notice that it looks like Lisp (and it is Lisp) but there's infix code seamlessly embedded. Moreover, the MODP construct automatically converts everything to modular arithmetic operations, so, for example, x/y doesn't actually divide but instead expands into (* x (modular-inverse y p)). But when I code, all I have to do is copy the mathematical formulas more or less verbatim and my compiler takes care of expanding everything out into calculations that use Montgomery reduction or whatever optimizations I want to provide. This approach also has the advantage that I can test the correctness of my protocols completely independently of the correctness of my optimizations.
- err4nt 10y agoHey thanks for taking the time to write this out. I'm new to programming and learning JavaScript, and what you said about exposing the AST and writing new syntaxes for the same language is really intriguing. I try to learn about the history of computer science and I try to understand where languages have come from - and one so far I haven't dabbled with any of the Lisp languages at all. If I was a JS learner curious about Lisp - what's the easiest way for me to get started with some experiments?
- lisper 10y agoI started working on a book a while back targeted specifically towards people who already know a little about programming: https://github.com/rongarret/BWFP https://github.com/rongarret/BWFP It's nowhere near complete but I'm working on chapter 3 now.
- jbpetersen 10y agoTry checking out https://github.com/maryrosecook/littlelisp https://github.com/maryrosecook/littlelisp. It's a relatively very simple Lisp interpreter written in JS. http://languagelog.ldc.upenn.edu/myl/ldc/llog/jmc.pdf http://languagelog.ldc.upenn.edu/myl/ldc/llog/jmc.pdf is another great resource for being introduced to Lisp.
- didibus 10y agoMaybe ClojureScript.
- mroll 10y agotcl is also written directly in the ast. And there is a built in command, expr, for doing infix math
- dnautics 10y agoJulia has AST as a first class object, but I think it does this by secretly running lisp behind the scenes.
- ljw1001 10y agoSmalltalk is another language where the AST is fully accessible by application code.