FSharp.DependentTypesProvider


DependentTypes

Dependent types are types that depend on inhabitants of other types. As an example, consider a type that defines all the strings less than a particular length. This type would depend on the specified length, which is itself an instance of another type (e.g. int).

This library allows us to encode those rules about what constitutes a valid subset of a primitive type into a specific type in your domain. Using this library, you can create such a constrained string (i.e. BoundedString), as well as constrained numeric types.

The DependentTypes library can be installed from NuGet:
PM> Install-Package FSharp.DependentTypes

Example

A simple example of constraining a string and a numeric type:

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
#r "DependentTypes.dll"
open FSharp.DependentTyping

open FSharp.DependentTypes.Strings
type ProductDescription = BoundedString<10, 2000>

open FSharp.DependentTypes.Numbers
type VolumeLevel = BoundedByte<11> // lower bound defaults to 0

Samples & documentation

  • Tutorial contains a further explanation of this sample library.

  • TODO a fully worked domain model

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into documentation.

The library is available under MIT license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

namespace Microsoft.FSharp
type ProductDescription = obj

Full name: Index.ProductDescription
type VolumeLevel = obj

Full name: Index.VolumeLevel
F# Project
Fork me on GitHub