Cephei.QL is a library that wraps the QuantLib C++ library with classes that provide CTS interfaces that can be used from F# Cephei.QL is not a simple wrapper that stops at plumbing for P/Invoke type C functions that replicate Excel addin function.. Cephei.QL provides a high level interface that looks like a class library developed in a .NET language
- Almost every QuantLib class has a peer .NET class, that replicates the interface functions
- The interface definitions are provided by pure CIL interfaces that can be intercepted or remoted (e.g. for integration with Excel Services). Where the underlying class is concrete or used as an array input (e.g. CashFlows) into another function, the factory interface defines the implementation functions.
- The raw quantlib .lib is linked directly with the C++/CLI wrapper code into a mixed mode assembly that includes the interface implementation, factory and collection functions.
- To provide seamless functional programing library with full IntelliSense, all the Factory functions are bundled together in a Cephei.Fun F# module.
As far as possible the library is thread-safe and leak-safe:
- Non-const functions spin-lock their parameters in the wrapper to prevent co-mutation (true FP functions cause no locks).
- Inspection functions that return pointers to an object’s owned values are hell for managed environments when the garbage collector deletes the owning object and inspection object randomly steps off a deleted pointer and GPFs. Cephei.QL addresses this problem by holding owner references for all inspection functions.
- The easy way to meet the need to present a collection with a CIL IList<> for managed code and a std::vector<> for native code is to spend a lot of time copying object.. Cephei.QL avoids this by copying only once and updating both arrays as needed.
- QuantLib follows quite a quaint pattern of having a global Evaluation-Date that is read by functions that need them (so IInstrument.NPV does not take a date parameter!).. which is Ok for single-theaded programs, but a pain for modern multi-threaded programs. Cephei.QL addresses this by turning on QuantLib session with a Session Object (that conveniently has a functional function With to inject the date (e.g. session.With(instrument, date).NPV)
To make the library more like a proper function programing library a few obvious changes have been made:
- Void functions (e.g. void Index::AddFixing()) have been changed to return “this” so that they can be used in functional programs (e.g. let ix = Fun.Index.Create(…).AddFixing(..) :> IIndex) and chained.
- Default parameter values are a common but not portable paradigm, that have been replaced with F# option syntax, so defaults are passed as None.
Although there is half a million lines of code in the library, more than 98% of the code is generated (by Cephei.Gen) so casual errors should have been avoided and/or most errors have already been trapped with the current testing.
The full open-source (MSRL) code is available from CodePlex
The open-source assembly is available from Visual Studio NuGet manager or directly from NuGet