| Copyright | © 2021-2023 IOHK 2024 Cardano Foundation |
|---|---|
| License | Apache-2.0 |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Data.Delta.Embedding
Contents
Description
Embeddings of delta types.
Synopsis
- data Embedding da db
- data Embedding' da db where
- Embedding' :: (Delta da, Delta db, a ~ Base da, b ~ Base db) => {..} -> Embedding' da db
- mkEmbedding :: Embedding' da db -> Embedding da db
- fromEmbedding :: (Delta da, Delta db) => Embedding da db -> Embedding' da db
- pair :: Embedding da1 db1 -> Embedding da2 db2 -> Embedding (da1, da2) (db1, db2)
- liftUpdates :: Delta da => Embedding da db -> [da] -> Base da -> (Base db, [db])
- replaceFromApply :: (Delta da, a ~ Base da) => Embedding' da (Replace a)
- inject :: Embedding da db -> Base da -> Machine da db
- project :: Embedding da db -> Base db -> Either SomeException (Base da, Machine da db)
Documentation
An Embedding da db embeds one type and its delta type da
into another type and its delta type db.
For reasons of efficiency, Embedding is an abstract type.
It is constructed using the Embedding' type, which has
three components.
writeembeds values from the typea =into the typeBasedab =.Basedbloadattempts to retrieve the value of typeafrom the typeb, but does not necessarily succeed.updatemaps a delta typedato a delta typedb. For this mapping, both the value of typeaand a corresponding value of typebare provided; the delta typesdaanddbare relative to these values. In the definition ofupdate, we can assume thatRight a = load b.
The embedding of one type into the other is characterized by the following properties:
The embedding is not necessarily surjective: The type
bmay contain many values that do not correspond to a value of typea. Hence,loadhas anEitherresult. (See Note [EitherSomeException] for the choice of exception type.) However, retrieving a written value always succeeds, we haveload . write = Right
The embedding is redundant: The type
bmay contain multiple values that correspond to one and the samea. This is why theupdatefunction expects the typebas argument, so that the right deltas can be computed. Put differently, we often havewrite a ≠ b where Right a = load b
The embedding of a delta commutes with
apply. We haveRight (apply da a) = load (apply (update a b da) b) where Right a = load bHowever, since the embedding is redundant, we often have
apply (update a (write a) da) (write a) ≠ write (apply da a)
Embedding with efficient composition o.
To construct an embedding, use mkEmbedding.
data Embedding' da db where Source #
Specification of an embedding of a type a with delta types da
into the type b with delta type db.
See the discussion of Embedding
for a more detailed description.
Constructors
| Embedding' | |
mkEmbedding :: Embedding' da db -> Embedding da db Source #
Construct Embedding with efficient composition
fromEmbedding :: (Delta da, Delta db) => Embedding da db -> Embedding' da db Source #
pair :: Embedding da1 db1 -> Embedding da2 db2 -> Embedding (da1, da2) (db1, db2) Source #
A pair of Embeddings gives an embedding of pairs.
Arguments
| :: Delta da | |
| => Embedding da db | |
| -> [da] | List of deltas to apply.
The deltas are applied right-to-left; the |
| -> Base da | Base value to apply the deltas to. |
| -> (Base db, [db]) | (Final base value, updates that were applied ( |
Lift a sequence of updates through an Embedding.
(b, dbs) = liftUpdates (mkEmbedding embedding') das a implies load embedding' b = Right (apply das a) b = apply dbs (write embedding' a)
replaceFromApply :: (Delta da, a ~ Base da) => Embedding' da (Replace a) Source #