Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides some tools to encode multiple versions
of a data model in a single data-type parametrized by version number.
The addition or removal of a field can be expressed through the
Since
and Until
type families.
Example:
data Rec v = Rec { foo :: Int -- this field exists in all versions , bar :: Since V2 v Bool -- this field has been introduced in V2 , baz :: Until V2 v Double -- this field has been removed in V3 }
Besides reducing the number of data declarations, this approach also has other advantages:
- It makes migrations declarative and self-documenting.
- It allows for less verbose version-upcasting functions, since the fields that have a non-parametric type do not need to be copied.
- It is a foundation on which other useful abstractions can be built.
Please note that some classes may require a separate standalone deriving clause for each version of a data-type or some kind of inductive deriving mechanism.
- newtype V = V Nat
- type family GetV (v :: V) :: Nat where ...
- versionNumber :: forall a v. KnownNat (GetV v) => a v -> Natural
- type family Since (s :: V) (v :: V) a :: * where ...
- type family Until (u :: V) (v :: V) a :: * where ...
- type NA = Maybe Bare
- na :: NA
- type V1 = V 1
- type V2 = V 2
- type V3 = V 3
- type V4 = V 4
- type V5 = V 5
- type V6 = V 6
- type V7 = V 7
- type V8 = V 8
- type V9 = V 9
- type V10 = V 10
- type V11 = V 11
- type V12 = V 12
- type V13 = V 13
- type V14 = V 14
- type V15 = V 15
- type V16 = V 16
- type V17 = V 17
- type V18 = V 18
- type V19 = V 19
- type V20 = V 20
Documentation
versionNumber :: forall a v. KnownNat (GetV v) => a v -> Natural Source #
Get the version number of a versioned value
type family Since (s :: V) (v :: V) a :: * where ... Source #
This allows us to express that a field is only present since a given version. The first parameter is the version in which the field has been introduced, the second parameter is the actual version of the data-type.
type family Until (u :: V) (v :: V) a :: * where ... Source #
This allows us to express that a field is only present until a given version. The first parameter is the last version in which the field is present, the second parameter is the actual version of the data-type.