What is the most modern way to compile code quotes in F #

I create a symbolic derivative mechanism. for example

let f = <@ fun x:double -> x * x @> let df = der f 

and the resulting expression will be

 <@ 2 * x @> 

Actual equations can be arbitrarily complex.

Generating derivatives is not too hard using recursive pattern matching and transformations, but in the end I want to use the generated equations in dense numerical cycles, as if I had a hand written them. This is a numerical computational code, so faster is always better (if possible)

I looked at the FSharpX quote compiler, but it looks more like an interpreter than a compiler.

+7
source share
1 answer

I have not tested this, but the code that translates F # quotes into LINQ expressions (and compiles them) has now moved from F # PowerPack to the F # Core library, so I believe this is the most modern version:

 open Microsoft.FSharp.Linq.RuntimeHelpers LeafExpressionConverter.EvaluateQuotation <@ 1 + 2 @> 

and use it for lambdas

 let d=LeafExpressionConverter.EvaluateQuotation <@ fun y -> y+1.0 @> :?> ( double -> double ) Console.WriteLine(d 10) 

exits

 11 

Check out the listing at the end to convert `` obj '' to the lambda of the correct type.

+7
source

All Articles