fork download
  1. type MpCalc<'a> = { add: 'a -> 'a -> 'a; min: 'a -> 'a -> 'a; }
  2.  
  3. type Mp<'a> =
  4. struct
  5. val value: 'a
  6. val calc: MpCalc<'a>
  7. new(v, c) = { value = v; calc = c }
  8. static member (+) (x: Mp<'a>, y: Mp<'a>) =
  9. new Mp<_>(x.calc.min x.value y.value, x.calc)
  10. static member (*) (x: Mp<'a>, y: Mp<'a>) =
  11. new Mp<_>(x.calc.add x.value y.value, x.calc)
  12. end
  13.  
  14. let inline toMp v = new Mp<_>(v, { add = (+); min = Operators.min; })
  15.  
  16. [<EntryPoint>]
  17. let main argv =
  18. printfn "1 + 1 = %A" ((toMp 1) + (toMp 1)).value
  19. printfn "2 + 3 = %A" ((toMp 2) + (toMp 3)).value
  20. printfn "2 * 3 = %A" ((toMp 2) * (toMp 3)).value
  21. printfn "2 * 0 = %A" ((toMp 2) * (toMp 0)).value
  22. printfn "3.0 + infinity = %A" ((toMp 3.0) + (toMp Operators.infinity)).value
  23. printfn "3.0 * infinity = %A" ((toMp 3.0) * (toMp Operators.infinity)).value
  24. 0
  25.  
Success #stdin #stdout 0.2s 25312KB
stdin
Standard input is empty
stdout
1 + 1 = 1
2 + 3 = 2
2 * 3 = 5
2 * 0 = 2
3.0 + infinity = 3.0
3.0 * infinity = infinity