Search code examples
f#quotations

F# quotation evaluation issue


I'm getting an issue with the F# powerpack quotation evaluation.

open Microsoft.FSharp.Linq.QuotationEvaluation

let print x = System.Console.WriteLine(sprintf "%A" x)

type record = { x:int; y:int }
let val1 = { x = 1; y = 1; }
let val2 = { x = 1; y = 1; }
let result = val1 = val2
print result

let quote = <@ let value1 = { x = 1; y = 1; }
               let value2 = { x = 1; y = 1; }
               let result2 = value1 = value2
               result2 @>

print (quote.EvalUntyped())

The first result is true as you would expect. The second is false. Is this a bug, or am I missing something?


Solution

  • This looks like a bug to me. Someone from the F# team will probably give a clear answer on this :-). In the meantime, here is a simple workaround that you can use - The problem seems to be with the compilation of the = operator. You can define your own operator (or a function) and call this operator from the quoted code:

    let (><) a b = a = b
    let quote = 
     <@ let value1 = { x = 1; y = 1; } 
        let value2 = { x = 1; y = 1; } 
        let result2 = value1 >< value2
        result2 @>      
    print (quote.EvalUntyped()) 
    

    Instead of generating a wrong call to the standard = operator, this will generate code that calls your custom operator (which then runs the comparison as a standard, correctly compiled F# code), so this gives the expected result.