6

Symbolism库重载了算术运算符。虽然它是用 C# 编写的,但我可以从 F# 中使用它:

open Symbolism

let x = new Symbol("x")
let y = new Symbol("y")
let z = new Symbol("z")

printfn "%A" (2*x + 3 + 4*x + 5*y + z + 8*y)

输出:

3 + 6 * x + 13 * y + z

但是,它也使权力超载^。这当然不适用于 F#。

作为解决方法的一步,我为权力导出了一个方法组:

printfn "%A" (Aux.Pow(x, 2) * x)

输出:

x ^ 3

如何重载**以使用Aux.Pow方法组?

我可以做这样的事情:

let ( ** ) (a: MathObject) (b: MathObject) = Aux.Pow(a, b)

这确实适用于MathObject价值观:

> x ** y * x;;
val it : MathObject = x ^ (1 + y)

但是Aux.Pow也超载int

    public static MathObject Pow(MathObject a, MathObject b)
    { return new Power(a, b).Simplify(); }

    public static MathObject Pow(MathObject a, int b)
    { return a ^ new Integer(b); }

    public static MathObject Pow(int a, MathObject b)
    { return new Integer(a) ^ b; }

欢迎任何建议!

4

2 回答 2

10

您可以像这样使用此处描述的技巧:

open Symbolism

type MathObjectOverloads =
    | MathObjectOverloads 
    static member (?<-) (MathObjectOverloads, a: #MathObject, b: int) = MathObject.op_ExclusiveOr(a, b)
    static member (?<-) (MathObjectOverloads, a: #MathObject, b: #MathObject) = MathObject.op_ExclusiveOr(a, b)
    static member (?<-) (MathObjectOverloads, a: System.Int32, b: #MathObject) = MathObject.op_ExclusiveOr(a, b)

let inline ( ** ) a b = (?<-) MathObjectOverloads a b

let two = Integer(2)
let three = Integer(3)

two ** three

two ** 3

2 ** three

与链接的答案不同,我们必须使用 (?<-) 运算符,因为它是唯一可以接受 3 个参数而不是 2 个参数的运算符,并且我们需要在 ^ 运算符的左侧和右侧重载

于 2013-03-11T16:30:53.277 回答
5

这是相同的答案,但没有运算符。它仅适用于 F# 3.0,您可以使用任意数量的参数。

let inline i3 (a:^a,b:^b,c:^c) = ((^a or ^b or ^c) : (static member threeParams: ^a* ^b* ^c -> _) (a,b,c))

open Symbolism

type MathObjectOverloads =
    | MathObjectOverloads 
    static member threeParams (MathObjectOverloads, a: #MathObject , b: int        ) = MathObject.op_ExclusiveOr(a, b)
    static member threeParams (MathObjectOverloads, a: #MathObject , b: #MathObject) = MathObject.op_ExclusiveOr(a, b)
    static member threeParams (MathObjectOverloads, a: System.Int32, b: #MathObject) = MathObject.op_ExclusiveOr(a, b)

let inline ( ** ) a b = i3(MathObjectOverloads, a, b)
于 2013-03-13T18:50:04.843 回答