Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
module fstropical
type MinPlus =
struct
val value: float
new(x: float) = { value = x }
override this.ToString() =
if this.value = System.Double.PositiveInfinity then "" else string this.value
static member Zero = new MinPlus(System.Double.PositiveInfinity)
static member One = new MinPlus(0.0)
static member (+) (x: MinPlus, y: MinPlus) = new MinPlus(min x.value y.value)
static member (*) (x: MinPlus, y: MinPlus) = new MinPlus(x.value + y.value)
static member op_Explicit (x: float) = new MinPlus(x)
end
type Overloads = Add | Multiply with
member this.exec (x: MinPlus) (y: MinPlus) =
(match this with Add->(+) | Multiply->( * )) x y
static member (?<-) (x: MinPlus, op: Overloads, y: MinPlus) =
op.exec x y
static member (?<-) (x: MinPlus, op: Overloads, y: float) =
op.exec x (MinPlus y)
static member (?<-) (x: MinPlus, op: Overloads, y: int) =
op.exec x (MinPlus (float y))
static member (?<-) (x: float, op: Overloads, y: MinPlus) =
op.exec (MinPlus x) y
static member (?<-) (x: float, op: Overloads, y: float) =
op.exec (MinPlus x) (MinPlus y)
static member (?<-) (x: float, op: Overloads, y: int) =
op.exec (MinPlus x) (MinPlus (float y))
static member (?<-) (x: int, op: Overloads, y: MinPlus) =
op.exec (MinPlus (float x)) y
static member (?<-) (x: int, op: Overloads, y: float) =
op.exec (MinPlus (float x)) (MinPlus y)
static member (?<-) (x: int, op: Overloads, y: int) =
op.exec (MinPlus (float x)) (MinPlus (float y))
let inline (@+) x y = x ? (Overloads.Add) <- y
let inline (@*) x y = x ? (Overloads.Multiply) <- y
open System
open fstropical
[<EntryPoint>]
let main argv =
let inline (+) x y = x @+ y // main関数内で+演算子を上書き
let inline (*) x y = x @* y // main関数内で+演算子を上書き
let inf = Double.PositiveInfinity // 略記
printfn "1 + 1 = %A" (1 + 1) // 1
printfn "2 + 3 = %A" (2 + 3) // 2
printfn "2 * 3 = %A" (2 * 3) // 5
printfn "2 * 0 = %A" (2 * 0) // 2
printfn "3 + ∞ = %A" (3.0 + inf) // 3
printfn "3 * ∞ = %A" (3.0 * inf) // ∞
printfn "(2 * (3 + ∞)) + (∞ + 8) = %A" ((2 * (3 + inf)) + (inf + 8)) // 5
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment