79 points · by afshinmeh · 8y ago · 67 comments
It's a what? Can someone ELI5 this for a lowly Python and JS dev - why would I want this?
----
I watched a talk about Scala's future [0] recently, in which the presenter compared the taglines of a few different languages (Go, Rust, Erlang, Scala), and how they relate to business pains or non-academic problems.
His conclusion about Scala's tagline - "Scala combines object-oriented and functional programming in one concise, high-level language" is that "no engineering manager in the history of software development ever thought to themselves 'hmmm, if only I had a language that fused OO and FP, I'd be able to solve my business problems'. That hasn't happened; that will never happen. This is an academic novelty that has zero relevance to any of us as professional software developers [...] this is not a business pain, this is an academic interest."
I felt a little bit like that when I read Egison's tagline: why should I care about this language?
[0] "The Last Hope for Scala's Infinity War" https://youtu.be/v8IQ-X2HkGE?t=15m8s
Nah, but I can ELI a person with some programming experience.
Pattern matching is super useful for writing code that spends a lot of time building and deconstructing syntactic data structures.
Non-linear pattern matching is useful when de-constructing large-ish expressions that might contain multiple occurances of the same sub-term.
Nonfree datatypes allow you to encode some constraints about your datatypes; e.g., "lists that are equal modulo permutation".
Supporting type-checked non-linear matching over nonfree datatypes allows you to encode a lot of constraints in the type system and syntax that would otherwise be encoded with lots of nested if-statements and only checked by tests, not by static type checking.
This combination of features could be very useful when implementing a lot of mathematical code that operates over algebraic structures. Which is why the language's homepage mentions the implementation of (features for) computer algebra systems as a sort of killer app.
> this is not a business pain, this is an academic interest
Not all innovation is motivated by the business pains of today's engineering managers. And often the solutions to those pains are not recognizable; e.g., thousands engineering manager in 1950 could've beneifted from CAD software but I bet very feew were sitting around saying "I could really use a good piece of CAD software right now".
Scala is another good example of that, despite a marketing complaints about messaging. No one asked for FP+OO, but Scala took off because the combination of FP and OO solved a lot of the pain caused by Java.
Substantially improving the ease of implementing new types of computer algebra systems could easily create a lot of business opportunities e.g. in providing improved analysis/synthesis tools for all sorts of CAD tools.
that said, I don't think this language hits the target. See the hodge operator example...
Egison appears to be a self-consciously academic language, which strikes me as a perfectly legitimate.
In particular, the goal seems to be to provide a more intuitive way of writing algorithms using extensible pattern matching. Now, I have worked on commercial software that has highly algorithmic parts (graph traversals, etc.) written in Erlang (a language that supports a powerful, but not extensible, form of pattern matching). I found the pattern matching facilities to be quite helpful in producing readable, concise, correct code.
The use case is narrow, but strikes me as real nonetheless.
It's a bit subtler than that. Scala is an industry programming language perfectly suited to business applications, but with a big academic and research input (remember that Odersky, its creator, was a big contributor to Java, an industry language) and with an initial emphasis on research. The mentioned talk is criticizing Scala's marketing blurb because it was written almost entirely from a research angle, and doesn't accurately and convincingly portray Scala's benefits for business. See the revised blurb at the end of the talk [1]. It's a question of being clever about marketing.
----
[1] revised blurb: "Scala is an open source programming language that helps you write correct code and modify code safely, while seamlessly reusing existing JVM libraries."
Egison features a customizable pattern-matching facility. Pattern-matching methods for each data type and pattern can be customizable by users. There might be a possibility that you thought syntax sugar is not syntax sugar.
This language is clearly an academic endeavor.
Disclaimer: I don't know what non-linear pattern matching on non-free data means either, but the fact that it's not explained to me is proof enough they aren't meaning to cater to non-academic users.
Egison makes programming dramatically simple!
(define $twin-primes
(match-all primes (list integer)
[<join _ <cons $p <cons ,(+ p 2) _>>>
[p (+ p 2)]]))
Uh, ok?So what it does is matching of such elements (named p) that the next element (see cons) equals p+2, which would be a twin prime, since p is a prime. Then it generates an element consisting of a pair [p, p+2]. The pattern apparently matches such a list comprised of elements (see <join ... >) that are [p, p+2] (the inner <cons ...>).
You definitely remember that Lisp lists are singly-linked lists, and (cons head tail) prepends a new head element to the list tail. This is why it is matched, pertty similarly to how it's done in Haskell.
print [(x,x+2) for x in primes if (x+2 in primes)]
print [(x,y) for x, y in zip(primes, primes[1:]) if x+2 == y]
The simpler one doesn't even require the primes list to be sorted, so I thought the second one would be more equivalent. It's still a lot easier to read and write than Egison one. my @primes = (1..∞).grep: *.is-prime;
my @twin_primes = map { ($_, $_+2) }, @primes.grep: (*+2).is-prime;Pattern-matching would be even more obvious is a piece of Haskell:
pair :: [x] -> Maybe (x, x)
pair p:q:rest = if q = p + 2 then Just (p, q) else Nothing
pair _ = Nothing
findTwins primes = [x | x <- map pair primes]
I suppose that Eigson's power lies in matching of more complex structures, as they mention non-linear matching, matching with multiple results, matching with lexical scoping.Why should I write (+ p 2) when most people think (p+2)? This difference gets worse with more complex expressions. For the masses (and I mean a lot of very capable people) this is just garbage. For some it's wonderful. I can't say it's wrong, just not my thing. Perhaps it's the Blub paradox - I've never had the LISP epiphany so I can't say. I have had the python epiphany, and it's quite the opposite of this.
In Lisp you actually don't write parse trees. Lisp uses a serialized hierarchical data format of a tokenizer. S-expressions are not Lisp syntax trees, but a data format.
Someday we'll resurrect it all...
Reader macros blew my mind when I first read about them. Being able to change the very laws of the universe from within my simple little programs... Insane!
Actually, no - it's the other way around: more complex expressions really benefit from the uniform of syntax and using macros to simplify the code.
Not all macros are worthwhile, and some are detrimental to readability (and not writing those is one of the first Lisp lessons), but there are also macros (syntax extensions), which are elegant and powerful.
The common example is the `->` or `thread-first` macro, which chains a list of function calls, in the following manner:
(-> (list 3) (prepend 2) (prepend 1) print) ;; -> '(1 2 3)
into: (print (prepend (prepend (list 3) 2) 1))
With longer expressions, or longer chains of function applications, the difference becomes even more visible, with `->` version having way less parens (possibly less than in Python for equivalent expression) and less nesting, which makes it easier to read and modify.But it doesn't end there - this is just one macro, out of many powerful syntax extensions, which make for much clearer code. There are macros for lazy evaluation, both sequences (like generators) or expressions (more like `lazy` in OCaml); for partial evaluation of functions, for changing name resolution policy, for declaring classes, for looping, for pattern-matching, for error handling, for FFI, for logic statements, for transactional memory, for async control flow, and so on and on.
Some of these are trivial to implement, but some are hard or just complicated, with many cases covered; so you're not necessarily expected to write them yourself. If a macro's semantics are well-defined - as in most popular examples - importing a package and reading docs is enough to use them to a good effect. You don't really have to use any of them, but they all exist to make certain patterns in code simpler, more readable, and more convenient to work with. Other languages often lack many of them.
Anyway, what I want to say is that the more complex the problem, or the larger codebase, the more readable Lisp becomes, at some point surpassing most other languages. This is what makes Lisp fans to be so good at "recursion and condescension", and is also something you can't see in a `(+ p 2)` kind of examples.
just an observation. most people don’t think this naturally. most people learned (p+2). maybe there’s an argument for whatever is more natural, but the point is that people forget that what they view as natural was once unnatural and had to be learned.
as a small anecdote, i did a calculator competition in high school. we used those hp rpn calculators. it was unnatural at first but then i flew once i gained the skill in using rpn.
Lisp has plenty of syntax, it's just extensible and largely defined in operators. Commonly used macros like DEFUN, WITH-OPEN-FILE, and LOOP all add their own syntax to the language; that's what macros are for. When you put them together, typical Lisp code ends up looking like
(defun count-words (filespec)
(with-open-file (f filespec)
(loop with counts = (make-hash-table :test 'equal)
for line = (read-line f nil)
while line do
(loop for word in (cl-ppcre:split "\\s+" line) do
(incf (gethash word counts 0)))
finally (return counts))))
Which I don't think is that alien. It translates pretty cleanly into Python: def count_words(filespec):
with open(filespec) as f:
counts = collections.defaultdict(int)
for line in f:
for word in line.split():
counts[word] += 1
return counts
Do you find the former significantly harder to understand? I don't see how
it's any more like "writing a parse tree directly."> Why should I write (+ p 2) when most people think (p+2)? This difference gets worse with more complex expressions.
As mentioned in other comments, there are infix packages available for when you're writing math-heavy code (or if you just need infix operators), but I also think that a lot of people don't write very much code with complicated inline arithmetic in it, and overstate how much the awkward math syntax would affect them (there are of course plenty of projects that are math-heavy and where it does make a large difference). When you're defining variables/functions/classes/etc or calling functions or looping or whatever, there's not much difference between Lisp and most other languages aside from whether the bracket is { or ( and which side of the keyword it goes on; in my experience, that sort of code tends to make up the large bulk of most codebases in most languages.
edit: Another place where Lisp's default syntax is significantly different from a lot of languages is something like object.f(x).g(y).h(z), which in Lisp looks like (h (g (f object x) y) z). This can be remedied with common macros like -> that let you write (-> object (f x) (g y) (h z)).
IDictionary<string, int> CountWords(string f) => File.ReadAllText(f).Split().ToLookup(w => w).ToDictionary(kv => kv.Key, kv => kv.Count());
Or a bit more concise in F#: let wordsCount f = File.ReadAllText(f).Split() |> Array.groupBy id |> Array.map (fun x |> fst x, (snd x).Length) |> dictTo see why it's wrong, think about Lisp and Haskell. Users of both languages are sure that they're at the top of the power curve, they're sure that they're looking down when they look at the other language, and they're sure why they're looking down. "How can you get anything done in [Haskell|Lisp]? It doesn't even have [macros|a decent type system]!" But if both are sure they're looking down, then languages can't be well-ordered by "power".
Next, that syntax. I suspect that that kind of syntax "clicks" with some people, and not with others. (Almost everyone could learn it, but that's not the same thing.) And I think you're right that "the masses" - the vast majority of people - are people to whom it doesn't "click". This may be the real flaw of Lisp (and Haskell) - the syntax is just wrong for the large majority of programmers.
Note well: This is my pet theory. I have no data. All predictions guaranteed wrong or your money back.
With a decent macro system you can implement a type-checker (CL, Clojure and Racket do), as sophisticated as you want. But you can also write a type-level interpreter (of Lisp, if you want) which would evaluate programs during compilation (I can't find the post anymore, someone was describing their job interview gone... weird...). The difference between the two, more than with other such comparisons, comes down to aesthetics and cultures.
IOW, you can't infer the lack of ordering based on two items having an ex aequo position.
I guess you could say that jargon is just another form of syntax.
I’ve also noticed that it “pivoted” to focus more on math; is that intentional?
Anyway, good to see it on HN again. Anything that pushes more pattern matching research is a plus for me =). Mainstream languages barely started to adopt first-order pattern matching!
I started to implement a computer algebra system as a killer application of customizable pattern matching of Egison.
It allows users to customize the pattern-method for mathematical expressions from more primitive level than the other computer algebra systems.
I doubt I have time to learn this or use it but it would be cool to see a list of examples of things that are much more concise and elegant in this language so that it expands my thinking. Is there a list of examples with comparisons to traditional languages?
Here is a link for a new paper that discusses the integration of tensor index notation including the support for differential forms.
Mandatory XKCD: https://xkcd.com/297/