C # library overloads ^ operator. How to use ** instead?

The Symbolism library overloads arithmetic operators. Although it is written in C #, I can use it from 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) 

output:

 3 + 6 * x + 13 * y + z 

However, it also overloads ^ for authority. This, of course, works poorly with F #.

As a step towards a workaround, I exported a group of methods for permissions:

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

output:

 x ^ 3 

How can I overload ** instead of the Aux.Pow group of methods?

I can do something like this:

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

And this works for MathObject values:

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

But Aux.Pow also overloaded for 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; } 

Any suggestions are welcome!

+6
source share
2 answers

You can use the trick described here as follows:

 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 ( ** ) ab = (?<-) MathObjectOverloads ab let two = Integer(2) let three = Integer(3) two ** three two ** 3 2 ** three 

Unlike the linked answer, we must use the operator (? <-), because it is the only operator that can take 3 arguments instead of 2, and we need to overload both the left and right side ^ operator

+10
source

Here is the same answer, but without operators. It works only in F # 3.0, and you can use any number of parameters.

 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 ( ** ) ab = i3(MathObjectOverloads, a, b) 
+5
source

All Articles