delta-store-1.0.0.0: Facilities for storing a Haskell value, using delta types.
Copyright© 2022-2023 IOHK 2023-2025 Cardano Foundation
LicenseApache-2.0
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Delta.Update

Description

Update represents a computation which produces a delta and a result.

Similar to the State monad, but involves a Delta type.

Useful for composing updates to a DBVar, via onDBVar.

Note: This module is preliminary.

Synopsis

Update

Type

data Update da r Source #

A computation which inspects a value a ~ Base da and produces a delta da and a result of type r.

Related to the State monad: The type Update (Replace s) r is essentially equivalent to State s r.

Instances

Instances details
Functor (Update da) Source #

Map results.

Instance details

Defined in Data.Delta.Update

Methods

fmap :: (a -> b) -> Update da a -> Update da b #

(<$) :: a -> Update da b -> Update da a #

View

runUpdate :: a ~ Base da => Update da r -> a -> (Maybe da, r) Source #

Run the Update computation.

applyUpdate :: (Delta da, a ~ Base da) => Update da r -> a -> (a, r) Source #

Semantics.

onDBVar :: (Monad m, Delta da) => DBVar m da -> Update da r -> m r Source #

Apply an Update to a DBVar.

Combinators

nop :: Update da () Source #

No operation.

Use the Functor instance, specifically (<$) to add results other than ().

update :: a ~ Base da => (a -> da) -> Update da () Source #

Compute a delta.

updateWithResult :: a ~ Base da => (a -> (da, r)) -> Update da r Source #

Compute a delta with result.

Helpers

updateWithError :: a ~ Base da => (a -> Either e da) -> Update da (Either e ()) Source #

Compute a delta or fail.

updateWithResultAndError :: a ~ Base da => (a -> Either e (da, r)) -> Update da (Either e r) Source #

Compute a delta with result or fail.

updateMany :: Update da r -> Update [da] r Source #

Lift an update for a single delta to a list of deltas.

updateField Source #

Arguments

:: (a ~ Base da, b ~ Base db) 
=> (b -> a)

View field.

-> (da -> db)

Lift delta to

-> Update da r 
-> Update db r 

Helper function for lifting the Update from a record field to the record.

Example:

data Pair a b = Pair a b
first :: Pair a b -> a

data DeltaPair da db
    = UpdateFirst da
    | UpdateSecond db

updateField first UpdateFirst
    :: (a -> Update da r)
    -> (Pair a b -> Update (DeltaPair da db) r)