7 people like it.

Where does the Celsius come from?

An exploration of the power of Computation Expressions, Type Extensions, Extension Methods, and Method Overloading. May or may not be a monad or a monoid - we're not sure, nor do we care to check.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
open FSharp.Data.UnitSystems.SI.UnitSymbols

type float<[<Measure>] 'm> with
    member a.Bind(f,x) = f x * a
    member a.Return(x) = a * sqrt x
    member a.Yield(x) = a / x
    member a.Combine(b:float<'m1>, c:float<'m2>) = a * b * c
    member a.Quote(x) = System.Runtime.Hosting.ApplicationActivator()
    member a.Delay(f) = f() * a
    member a.Run(f) = Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.EvaluateQuotation f
 
[<System.Runtime.CompilerServices.Extension>]
type A =
  [<System.Runtime.CompilerServices.Extension>]
  static member Yield(_:float<S>, b:float<Pa>) = b * 1.0<C>   

// Mystery question: Where does the Celsius come from?
let x = 
    3.14<S> {
        yield 2.71<Pa> 
        yield 2.71<S> 
    }
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Data
namespace Microsoft.FSharp.Data.UnitSystems
namespace Microsoft.FSharp.Data.UnitSystems.SI
namespace Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
Multiple items
type MeasureAttribute =
  inherit Attribute
  new : unit -> MeasureAttribute

Full name: Microsoft.FSharp.Core.MeasureAttribute

--------------------
new : unit -> MeasureAttribute
val a : float<'Measure>
member float.Bind : f:('a -> float<'u>) * x:'a -> float<'Measure 'u>

Full name: Script.Bind
val f : ('a -> float<'u>)
val x : 'a
member float.Return : x:float -> float<'Measure>

Full name: Script.Return
val x : float
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
member float.Yield : x:float<'u> -> float<'Measure/'u>

Full name: Script.Yield
val x : float<'u>
member float.Combine : b:float<'m1> * c:float<'m2> -> float<'Measure 'm1 'm2>

Full name: Script.Combine
val b : float<'m1>
val c : float<'m2>
member float.Quote : x:'a -> System.Runtime.Hosting.ApplicationActivator

Full name: Script.Quote
namespace System
namespace System.Runtime
namespace System.Runtime.Hosting
Multiple items
type ApplicationActivator =
  new : unit -> ApplicationActivator
  member CreateInstance : activationContext:ActivationContext -> ObjectHandle + 1 overload

Full name: System.Runtime.Hosting.ApplicationActivator

--------------------
System.Runtime.Hosting.ApplicationActivator() : unit
member float.Delay : f:(unit -> float<'u>) -> float<'Measure 'u>

Full name: Script.Delay
val f : (unit -> float<'u>)
member float.Run : f:Quotations.Expr -> obj

Full name: Script.Run
val f : Quotations.Expr
namespace Microsoft
namespace Microsoft.FSharp.Linq
namespace Microsoft.FSharp.Linq.RuntimeHelpers
module LeafExpressionConverter

from Microsoft.FSharp.Linq.RuntimeHelpers
val EvaluateQuotation : Quotations.Expr -> obj

Full name: Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.EvaluateQuotation
namespace System.Runtime.CompilerServices
Multiple items
type ExtensionAttribute =
  inherit Attribute
  new : unit -> ExtensionAttribute

Full name: System.Runtime.CompilerServices.ExtensionAttribute

--------------------
System.Runtime.CompilerServices.ExtensionAttribute() : unit
type A =
  static member Yield : float<S> * b:float<Pa> -> float<C Pa>

Full name: Script.A
static member A.Yield : float<S> * b:float<Pa> -> float<C Pa>

Full name: Script.A.Yield
[<Measure>]
type S = Data.UnitSystems.SI.UnitNames.siemens

Full name: Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols.S
val b : float<Pa>
[<Measure>]
type Pa = Data.UnitSystems.SI.UnitNames.pascal

Full name: Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols.Pa
[<Measure>]
type C = Data.UnitSystems.SI.UnitNames.coulomb

Full name: Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols.C
val x : obj

Full name: Script.x

More information

Link:http://fssnip.net/7UV
Posted:5 years ago
Author:Tomas Petricek, Krzysztof Cieślak, John Azariah, Phillip Carter
Tags: computation expressions , monad , monoid , quotation , quotations , units of measure , nonsense