F# Quotations Evaluator


The F# Quotations Evaluator library can be installed from NuGet:
PM> Install-Package FSharp.Quotations.Evaluator


This component is an F# quotations evaluator, implemented by compiling to LINQ expression trees.

For simple expression-based scenarios you may be able to simply use the method `Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.EvaluateQuotationfromFSharp.Core.dll.

However that component has restrictions in the quotations accepted - for example, statements such as while-loops are not accepted. This component accepts more quotations (including those involving statements), though some restrictions remain. Please help by contributing additional functionality to lift remaining restrictions.

Performance of generated code is generally OK though not as good as F# compiled code, expecially for recursive functions.
However it is still good enough for many purposes of dynamic code generation. Contributions to improve performance are welcome.

The component requires .NET 4.x. It is not usable on mobile devices or portable profiles, where code generation APIs are not available.


This example demonstrates using a function defined in this sample library.

#r "FSharp.Quotations.Evaluator.dll"
open FSharp.Quotations.Evaluator

QuotationEvaluator.Evaluate <@ 1 + 1 @>

let addPlusOne = QuotationEvaluator.Compile <@ fun x y -> x + y + 1 @> 

let nine = addPlusOne 3 5  // gives 9

Samples & documentation

The library comes with comprehensible documentation. It can include a tutorials automatically generated from *.fsx files in the content folder. The API reference is automatically generated from Markdown comments in the library implementation.

  • Tutorial contains a further explanation of this sample library.

  • API Reference contains automatically generated documentation for all types, modules and functions in the library. This includes additional brief samples on using most of the functions.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding new public API, please also consider adding samples that can be turned into a documentation. You might also want to read library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

namespace FSharp
namespace FSharp.Quotations
namespace FSharp.Quotations.Evaluator
type QuotationEvaluator
static member Compile : Expr<'T> -> 'T
static member CompileUntyped : Expr -> obj
static member Evaluate : Expr<'T> -> 'T
static member EvaluateUntyped : Expr -> obj
static member private EvaluateUntypedUsingQueryApproximations : Expr -> obj
static member ToLinqExpression : Expr -> Expression

Full name: FSharp.Quotations.Evaluator.QuotationEvaluator
static member QuotationEvaluator.Evaluate : Quotations.Expr<'T> -> 'T
val addPlusOne : (int -> int -> int)

Full name: Index.addPlusOne
val x : int
val y : int
val nine : int

Full name: Index.nine
F# Project
Fork me on GitHub