[Ur] css_style and 'Anonymous function remains at code generation'

Adam Chlipala adamc at csail.mit.edu
Sun Oct 6 20:12:41 EDT 2013


On 10/04/2013 03:26 PM, Sergey Mironov wrote:
>> Rather, in Ur/Web, no first-class
>> functions are allowed to exist in server-side code at runtime.  The compiler
>> can't find a way to optimize your code to hide the first-class functions of
>> type [css_style ->  css_style], since there is currently no optimization to
>> simplify calls to recursive functions (e.g., [foldr]) statically.
>>      
> Thanks, that is clear. Is that 'mkcss' approach the only way to
> synthesize css styles? I'm going to store heights/widths of images in
> the database, that is why I need the way to tweak some styles
> dynamically. I already have the code which does it using javascript
> FFI but that method is not what I'd like to see in the end.
>    

The basic approach you used in your code seems reasonable.  I just 
suggest making it less higher-order, as I suggested in the quoted text 
below.

>> Two effective refactorings that come to mind are:
>> 1. Fold over a record instead of a list.
>> 2. Fold over a list of "(n, v) pairs" instead of a list of functions,
>> calling [mkcss] within the first argument to [List.foldr].
>>      
> Could you please recommend some reading covering the translation of
> functional languages into imperative ones? My knowledge on this topic
> is too small, I'd like to learn how we deal with first-class functions
> when building C code.
>    

I wouldn't want to claim that much about the Ur/Web compilation strategy 
is particularly standard, and I really do want to rework it considerably 
at some point, to improve predictability.

Fundamentally, the compiler needs to eliminate all first-class functions 
at compile time, for server-side code.  You can go a long way toward 
understanding how it does that just by imagining that it applies 
standard algebraic laws, like this one:
     (fn x => e1) e2 = e1 with e2 substituted for x
A similar rule is used to inline the definitions of sufficiently short, 
non-recursive functions.  Also, the compiler will _specialize_ functions 
like [List.foldr] to particular arguments of function type.  I think 
these ingredients are sufficient to explain why my suggestion "2." above 
would help.



More information about the Ur mailing list