2

我在 f# 中开始使用微积分工具箱。学习 f# 与最终在我想到的其他一些项目中使用有用的东西一样重要。基本和不完整的代码是

namespace BradGoneSurfing.Symbolics

open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
open Microsoft.FSharp.Quotations.DerivedPatterns
open System
open Microsoft.FSharp.Reflection
open Microsoft.FSharp.Quotations

open FSharpx.Linq.QuotationEvaluation
open Microsoft.FSharp.Linq.RuntimeHelpers

module Calculus =

    let rec der_impl param quotation =

        let (|X|_|) input = if input = param then Some(X) else None

        match quotation with

        | SpecificCall <@ (*) @> (_,types,l::r::[]) -> 
            let dl = der_impl param l
            let dr = der_impl param r
            <@@ (%%dl:double) * (%%r:double) + (%%l:double) * (%%dr:double) @@>

        | SpecificCall <@ Math.Sin @> (_,_types, arg::_) ->
            let di = der_impl param arg
            <@@ Math.Cos( (%%arg:double) ) @@> 

        | ExprShape.ShapeVar v -> 
            match v with 
            | X -> <@@ 1.0 @@>
            | _ -> (Expr.Var v)

        | ExprShape.ShapeLambda (v,expr) -> Expr.Lambda (v,der_impl param expr)
        | ExprShape.ShapeCombination (o, exprs) -> ExprShape.RebuildShapeCombination (o,List.map (fun e -> der_impl param e ) exprs)

    let rec der expr =
        match expr with
        | Lambda(param, body) ->
            Expr.Lambda(param, (der_impl param body))
        | _ -> failwith "oops"

我有一个 NUnit / FSUnit 测试证明我的第一段代码

namespace BradGoneSurfing.Symbolics.Test

open FsUnit
open NUnit.Framework
open Microsoft.FSharp.Quotations
open BradGoneSurfing.Symbolics.Calculus
open FSharpx.Linq.QuotationEvaluation
open System
open Microsoft.FSharp.Linq.RuntimeHelpers

[<TestFixture>]
type ``This is a test for symbolic derivatives`` ()=

    [<Test>] 
    member x.``Derivative should work`` ()=

        let e = <@ (fun (y:double) -> y * y) @>
        let d = der e

        let x = <@ fun (y:double) -> 1.0 * y + y * 1.0 @>
        d |> should equal x   

测试类型的作品,但没有。结果说

Expected:
Lambda (y,
        Call (None, op_Addition,
              [Call (None, op_Multiply, [Value (1.0), y]),
               Call (None, op_Multiply, [y, Value (1.0)])]))

But Was:               
Lambda (y,
        Call (None, op_Addition,
              [Call (None, op_Multiply, [Value (1.0), y]),
               Call (None, op_Multiply, [y, Value (1.0)])]))

现在在我看来,这两个是相同的,但似乎不是。我猜我已经与 Expr 与 Expr<'t> 进行了某种混淆,但我不确定。一些实现代码经过反复试验才能编译。

有什么想法可能是这里的微妙错误吗?

更新解决方案

@Jack 是正确的, Var 实现了引用相等,并且很难使用带有代码引用的标准相等检查。出于测试目的,比较字符串“足够正确”。为了使这个可口,我为 FsUnit/NUnit 创建了一个自定义匹配器,如下所示

type EqualsAsString (e:obj)= 
    inherit NUnit.Framework.Constraints.Constraint()

    let expected = e

    override x.Matches(actual:obj)=
        x.actual <- actual
        x.actual.ToString() = expected.ToString()

    override x.WriteDescriptionTo(writer)=
        writer.WriteExpectedValue(expected)

    override x.WriteActualValueTo(writer)=
        writer.WriteActualValue(x.actual)

和一个 FSUnit 包装器

let equalExpr (x:Expr<'t>) = new EqualsAsString(x)

所以我可以

d |> should equalExpr <@ fun y -> y * y @>

它会按预期工作。

4

1 回答 1

4

与 F# 引用一起使用的Var类型不支持结构相等。事实上,它也没有实现IEquatable<'T>——它只提供了对Equals检查引用相等性的方法的覆盖:

https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/quotations.fs#L122

So, it looks like the reason your test is failing is because the y variables in your quotations aren't recognized as 'equal'. The first solution that comes to mind would be to create a custom implementation of IEqualityComparer<'T> which does handle the variable equality in the way you want, then use it to check the expected value against the generated value.

于 2013-03-10T20:39:56.223 回答