22

Here's what can be done in C# -

var two = 2;
System.Linq.Expressions.Expression<System.Func<int, int>> expr = x => x * two;
expr.Compile().Invoke(4); // returns 8

I wish to do the precise equivalent in F#. Here's what I tried, but did not compile -

let two = 2
let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>>
expr.Compile().Invoke(4) // desired to return 8

Perhaps predictably, compilation fails on line 2 with the following error -

"This function takes too many arguments, or is used in a context where a function is not expected."
let expr = (fun x -> x * two) : System.Linq.Expressions.Expression<System.Func<int, int>>
            ^^^^^^^^^^^^^^^^
Bryan Edds
  • 1,696
  • 12
  • 28

1 Answers1

26

I'm not sure why you want to avoid using F# quotations - under the cover, they are pretty much the same thing as C# expression trees and if you want to create an expression tree in F#, the compiler will be using quotations under the cover in any case...

Anyway, you can do this without writing explicit <@ .. @> because the compiler can automatically quote a function when it is passed as an argument to a method. So you can do:

type Expr = 
  static member Quote(e:Expression<System.Func<int, int>>) = e

let two = 2
let expr = Expr.Quote(fun x -> x * two) 
expr.Compile().Invoke(4) // desired to return 8

EDIT: However, this really compiles to an F# quotation wrapped in a call that converts it to C# expression tree. So, in the end, you'll get the same thing as if you wrote:

open Microsoft.FSharp.Linq.RuntimeHelpers

let two = 2
let expr = 
  <@ System.Func<_, _>(fun x -> x * two) @>
  |> LeafExpressionConverter.QuotationToExpression 
  |> unbox<Expression<Func<int, int>>>
expr.Compile().Invoke(4) // desired to return 8
ildjarn
  • 62,044
  • 9
  • 127
  • 211
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • Thank you for your answer! It's nice, though it seems a tiny, tiny bit work-aroundy :) However, to answer your question - I wanted to avoid code quotations because, AFAIK, they do not allow capturing variables from the lexical environment (such as the `two` variable). Please correct me however if that is wrong, though! – Bryan Edds Apr 18 '14 at 02:53
  • 1
    It behaves the same in both cases. Local `let` bound variables are not captured by the quotation - it will include just the value of the variable. Public variables exposed by a module will be captured as a reference (to a property getter). – Tomas Petricek Apr 18 '14 at 02:57
  • Follow up to your edit - so will this not work predictably if the expression tree itself mutates references pulled from its surrounding context? Some of the lambdas I intend to use this with could be mutating the wider program state. – Bryan Edds Apr 18 '14 at 02:58
  • Well, this is not allowed for `mutable` variables - for example the following gives an error `let mutable two = 2 in <@ two <- 10 @>` – Tomas Petricek Apr 18 '14 at 02:59
  • But if you are using heap-allocated reference cell (using `ref`) then it should be predictable. e.g. `let two = ref 2 in <@ two := 10 @>` should be fine. – Tomas Petricek Apr 18 '14 at 03:00
  • That would be a reasonable limitation. As long as it can mutate captured references in a predictable way, it will work perfectly! – Bryan Edds Apr 18 '14 at 03:01
  • I have several very important follow-up questions. 1) How do I invoke the code quotation? 2) Will it be invoked in a manner less efficient than if it weren't a code quotation? 3) will it require run-time support for reflection / code-emitting functionality (which is often absent from embedded platforms I am targeting). – Bryan Edds Apr 18 '14 at 03:33
  • 2
    1) F# quotations cannot be invoked directly - so you'll have to convert it to C# expression tree and invoke that. 2) It will be slower than ordinary F#, because the conversion to expression tree adds some overhead. I do not know how much exactly - have not done any measurements. 3) Not sure - depends on whether C# expression eval is available there. – Tomas Petricek Apr 18 '14 at 03:48
  • Sorry, I just now moved that follow-up question here - http://stackoverflow.com/questions/23147064/f-code-quotation-invocation-performance-and-run-time-requirements . Please feel free to move your answer as well :) – Bryan Edds Apr 18 '14 at 03:50
  • For anyone who's interested in doing this, in the latest F# instead of `... |> LeafExpressionConverter.QuotationToExpression |> unbox>>` you can just use `... |> LeafExpressionConverter.QuotationToLambdaExpression` instead. – Jono Job Oct 16 '19 at 23:01