Can pure expression be reduced/inlined and optimized at compile time?

Hi,

First thank you all for making such a great language,
I’m going through the book again and I find the experience very polished :slight_smile:
In particular, setting up psc-ide-mode in emacs was quite simple and super useful to manage import.

So my question is, would it be possible for the compiler to optimize pure expression in their final form?
For example, could this expression "Hello " <> "World!" be compiled as "Hello World!" (instead of "Hello " + "World!") ?

It seems like this isn’t possible at the moment, and I was wondering if purescript being pure could do that easily.

2 Likes

PureScript can theoretically implement that (and might do so at some point). But almost every JavaScript minifier will do that for you. And if you don’t use a minifier, any half-good JavaScript runtime will optimize most obvious stuff for you. So, are simple optimizations like that even necessary?

However, if we’re talking about code size instead of performance here, then the optimizations that you’re presumably thinking about might be worthwhile if the most common JS minfiers can’t do these optimizations, due to not wanting to break code or otherwise.

Note: In case you’re thinking about performance @tristanC, there are many optimizations that PureScript can implement using the plethora of type information and guaranteed purity that the compiler possesses that may not be possible using JIT compilers.

2 Likes

Thank you for the prompt feedback, I’ll look into using a minifier.

Also perhaps my example was too simple, I was initially looking at the output of foldl append mempty [ "Hello ", "World!" ].

1 Like

The compiler does not currently have a robust, general optimization framework other than the handful of specific inlining rules it currently performs (function composition/application, operations on primitive types, Effect binds, aka magic-do).

2 Likes

As it currently stands, an optimization like that will probably not be implemented into the compiler in the near future. I think I read somewhere that performance will get more attention after 1.0 is released.

2 Likes

Evaluation at compile time might also be a good way to eliminate unsafePartial in some situations.

You can use fold instead of foldl append mempty.