151 points · by ehatti · 4y ago · 40 comments
``` {-# RULES
"map/map" forall f g xs. map f (map g xs) = map (f.g) xs
"map/append" forall f xs ys. map f (xs ++ ys) = map f xs ++ map f ys
#-}
```Depending on the order of rules application, the quality of optimization can differ a lot. This fragility makes it harder to understand the impact of even a minor code change on the performance. It is also very hard to test that a particular optimization is applied, and to track down why if it's not.
"Stream Fusion: From Lists to Streams to Nothing at All." Duncan Coutts, Roman Leshchinskiy, Don Stewart. ICFP 2007.
Note that Rust doesn't need to do this because of the way `iter_into()` and mapping works. List fusion boils down to removing interior `.collect().iter_into()`.
There is something to be said for designing APIs so that you don't need such rewrite rules, though, of course, the need comes up eventually anyways, so rewrite rules end up being kinda necessary.
It looks like this method is aimed at doing user pattern-matching on expressions in the first stage, kind of integrating Haskell-style rewrite rules in the main user language (rather than bolting them on the side in comments).
Hobbes looks so totally frickin' awesome, I'm so going to play with this tonight.
Incredible work!
What would be really cool for this language is to hook into jupyterlab via xeus. It's not even that hard to do - I did it for my own (far inferior) toy language.
You need two ingredients: (1) it must be possible to define a macro and function simultaneously for the same symbol. (2) the regular macro expander must detect fixed points in macro expansion and stop expanding. You want (2) anyway; if a macro returns the original form, so that its output is eq to its input, it make no sense to call it again.
This is the TXR Lisp interactive listener of TXR 275.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR Lisp has no protected class members; C++ refugees may face discrimination.
1> (sqrt 5)
2.23606797749979
2> (fboundp 'sqrt)
t
3> (mboundp 'sqrt)
nil
4> (defmacro sqrt (n :form f)
(if (constantp n) (sqrt (eval n)) f))
** expr-4:1: warning: defmacro: defining sqrt, which is also a built-in defun
sqrt
5> (sqrt 5)
2.23606797749979
6> (macroexpand '(sqrt 5))
2.23606797749979
7> (macroexpand '(sqrt x))
(sqrt x)
8> (mboundp 'sqrt)
t
9> (mmakunbound 'sqrt)
sqrt
10> (mboundp 'sqrt)
nil
11> (macroexpand '(sqrt 5))
(sqrt 5)
Unlike a compiler macro, sqrt will always expand, guaranteed.User-defined optimizations want to be tree rewrite patterns. We can impose that onto macros (or compiler macros).
In the TXR world, this can be done with a parameter macro, and one is provided for that, namely :match.
12> (defmacro sqrt (:match :form f)
(((@(constantp @exp)) (eval exp)))
(((* @exp @exp)) exp)
(((expt @exp 2)) exp)
((@else) f))
** expr-23:1: warning: defmacro: defining sqrt, which is also a built-in defun
sqrt
13> (macroexpand '(sqrt (* (+ x y) (+ x y))))
(+ x y)
14> (macroexpand '(sqrt (* (+ x y) (+ x z))))
(sqrt (* (+ x y) (+ x z)))
15> (macroexpand '(sqrt (* 5.5 5.5)))
5.5
16> (macroexpand '(sqrt (expt (sin x) 2)))
(sin x)
17> (macroexpand '(sqrt (expt (sin x) 3)))
(sqrt (expt (sin x) 3))
Parameter macros like :match are bound in the keyword namespace. When these bound keywords occur at the head of a parameter list, their macro gets invoked, receiving the parameter list and the function body, rewriting both of them together.If a function call form (op ...) is the result of the expansion of a macro form (op ...) then after its argument forms are recursively expanded, if those recursive expansions make any difference, the resulting form should again be tried as a macro form.
This is because transformations on the arguments of the function call may turn the call into something that is interesting to the macro again.
With this, I can do the following now:
This is the TXR Lisp interactive listener of TXR 275.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR contains many small parts, unsuitable for children under 12 months.
1> (defmacro sqrt (:match :form f)
(((* @exp @exp)) exp) ;; (sqrt (* x x)) -> x
(@else f))
** expr-1:1: warning: defmacro: defining sqrt, which is also a built-in defun
sqrt
2> (defmacro expt (:match :form f)
((@exp 2) ^(* ,exp ,exp)) ;; (expt x 2) -> (* x x)
(@else f))
** expr-2:1: warning: defmacro: defining expt, which is also a built-in defun
expt
3> (expand '(sqrt (expt x 2)))
x
See? (sqrt (expt x 2)) reduced down to x, even though the sqrt macro has no such optimization case.First (sqrt (expt x 2)) is tried as a macro. That immediately hits a fixed point by returning the form. The expander makes a note that expansion took place (even if a no-op). Then the expander expands it as a function call, expanding the argument expression, turning the form into (sqrt (* x x)). That expansion having made a difference, together with the fact that the form came from a macro, now means that the expander tries the form again as a macro: (sqrt (* x x)) is expanded again, and now there is a "hit".
(For simplicity, these examples don't care about multiple evaluation; and it is a given that algebraic changes are risky in the face of floating-point. It's a good way to illustrate the technique.)
CL compiler macros could do something like this. I don't see the requirement in the HyperSpec. The requirement could be that when a compiler macro declines expansion, then after the resulting function form is recursively processed for more macros, the compiler macro should be tried again, and this process should repeat until both conditions are met: the macro declines, and the function form no longer contains any arguments that are macros.
Of course, a compiler macro can capture an &environment and do its own expanding via macroexpand. In this example, that would work. What I implemented is subtly different: a complete expansion is performed on the function. You would need macroexpand-all to simulate the feature in the macro itself. Not having to do that at all, having things Just Work, is pretty nice.
Peridot is "novel" in that it introduces user-level compiler optimizations into a language that does have compile-time typing by default. But that's kind of like Chevrolet making a plug-in hybrid version of the C8 Corvette (which they recently announced they are going to do). Neither the C8 nor plug-in hybrids are new. A plug-in hybrid C8 will be new, and when it happens it will be a Big Deal to a niche market: the intersection of C8 fans and plug-in-hybrid fans. But it won't change the world.
(For the record, I happen to be a member of the niche market to which a plug-in hybrid C8 will appeal, so I am very excited about it. So I get that some people may be excited about Peridot. I just think it's important to keep things in perspective.)