pure-borrow
Safe HaskellNone
LanguageGHC2021

Control.Monad.Borrow.Pure.BO

Description

This module provides all the safe API of BO monad, including the advanced, low-level combinators that are not meant to be used by most users. For the conceptual overview, please refer to Control.Monad.Borrow.Pure, which is the prelude of this package.

Synopsis

Core BO monad

data BO (α :: Lifetime) a Source #

Computation returning a that can be performed only during the lifetime α. Internally it is a linear ST monad.

Instances

Instances details
(α >= β, a <: b) => (BO α a :: Type) <: (BO β b :: Type) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

subtype :: SubtypeWitness (BO α a) (BO β b) Source #

Applicative (BO α) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

pure :: a %1 -> BO α a #

(<*>) :: BO α (a %1 -> b) %1 -> BO α a %1 -> BO α b #

liftA2 :: (a %1 -> b %1 -> c) %1 -> BO α a %1 -> BO α b %1 -> BO α c #

Functor (BO α) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

fmap :: (a %1 -> b) %1 -> BO α a %1 -> BO α b #

Monad (BO α) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

(>>=) :: BO α a %1 -> (a %1 -> BO α b) %1 -> BO α b #

(>>) :: BO α () %1 -> BO α a %1 -> BO α a #

Applicative (BO α) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

pure :: a -> BO α a #

(<*>) :: BO α (a %1 -> b) %1 -> BO α a %1 -> BO α b #

liftA2 :: (a %1 -> b %1 -> c) -> BO α a %1 -> BO α b %1 -> BO α c #

Functor (BO α) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

fmap :: (a %1 -> b) -> BO α a %1 -> BO α b #

Monoid w => Monoid (BO α w) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

mempty :: BO α w #

Semigroup w => Semigroup (BO α w) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

(<>) :: BO α w %1 -> BO α w %1 -> BO α w #

execBO :: forall (α :: Lifetime) a. BO α a %1 -> Now α %1 -> (Now α, a) Source #

runBO :: Linearly %1 -> (forall (α :: Lifetime). BO α (After α a)) %1 -> a Source #

Runs a BO computation and returns the result of postprocessing After the lifetime has ended.

See also: runBOLend and runBO_.

runBOLend :: Linearly %1 -> (forall (α :: Lifetime). BO α (Lend α a)) %1 -> a Source #

A variant of runBO that returns the original rsource retained by the Lender

runBO_ :: Linearly %1 -> (forall (α :: Lifetime). BO α a) %1 -> a Source #

A variant of runBO that returns the direct value of BO computation.

sexecBO :: forall (α :: Lifetime) (β :: Lifetime) a. BO/\ β) a %1 -> Now α %1 -> BO β (Now α, a) Source #

See also scope.

scope_ :: forall (α :: Lifetime) (β :: Lifetime) a. Now α %1 -> BO/\ β) a %1 -> BO β (Now α, a) Source #

Flipped version of sexecBO.

srunBO :: forall (β :: Lifetime) a. (forall (α :: Lifetime). BO/\ β) (After α a)) %1 -> BO β a Source #

Runs a BO computation within the ephemeral sublifetime and returns the result.

srunBO_ :: forall (β :: Lifetime) a. (forall (α :: Lifetime). BO/\ β) a) %1 -> BO β a Source #

A variant of srunBO that returns the direct value of BO computation.

askLinearly :: forall (α :: Lifetime). BO α Linearly Source #

asksLinearly :: forall r (α :: Lifetime). (Linearly %1 -> r) %1 -> BO α r Source #

asksLinearlyM :: forall (α :: Lifetime) r. (Linearly %1 -> BO α r) %1 -> BO α r Source #

evaluateBO :: forall a (α :: Lifetime). a %1 -> BO α a Source #

In-place modification with mutable borrows

modifyBO :: a %1 -> Linearly %1 -> (forall (α :: Lifetime). Mut α a %1 -> BO α r) %1 -> (r, a) Source #

Modifies linear resources in-place, together with results.

modifyBO_ :: a %1 -> Linearly %1 -> (forall (α :: Lifetime). Mut α a %1 -> BO α ()) %1 -> a Source #

Modifies linear resources in-place, without results.

modifyLinearOnlyBO :: LinearOnly a => a %1 -> (forall (α :: Lifetime). Mut α a %1 -> BO α r) %1 -> (r, a) Source #

Modifies linear resources in-place, together with results.

modifyLinearOnlyBO_ :: LinearOnly a => a %1 -> (forall (α :: Lifetime). Mut α a %1 -> BO α ()) %1 -> a Source #

Modifies linear resources in-place, together with results.

Parallel computation

parBO :: forall (α :: Lifetime) a b. BO α a %1 -> BO α b %1 -> BO α (a, b) Source #

Run two computations in parallel, returning their results as a tuple.

Borrowing

data Alias (ak :: AliasKind) (α :: Lifetime) a Source #

Alias of kind ak to a resource of type a.

Instances

Instances details
Reborrowable Mut Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.Experimental.Reborrowable

Methods

locally' :: forall (α :: Lifetime) a (α' :: Lifetime) r. Mut α a %1 -> (forall (β :: Lifetime). Mut/\ α) a %1 -> BO/\ α') (After β r)) %1 -> BO α' (r, Mut α a) Source #

Reborrowable Share Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.Experimental.Reborrowable

Methods

locally' :: forall (α :: Lifetime) a (α' :: Lifetime) r. Share α a %1 -> (forall (β :: Lifetime). Share/\ α) a %1 -> BO/\ α') (After β r)) %1 -> BO α' (r, Share α a) Source #

(α <= β, a <: b) => (Lend α a :: Type) <: (Lend β b :: Type) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

subtype :: SubtypeWitness (Lend α a) (Lend β b) Source #

(α >= β, a <: b, b <: a) => (Mut α a :: Type) <: (Mut β b :: Type) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

subtype :: SubtypeWitness (Mut α a) (Mut β b) Source #

(α >= β, a <: b) => (Share α a :: Type) <: (Share β b :: Type) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

subtype :: SubtypeWitness (Share α a) (Share β b) Source #

bk ~ 'Mut => LinearOnly (Borrow bk α a :: Type) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Consumable (Borrow bk α a) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

consume :: Borrow bk α a %1 -> () #

k ~ 'Borrow 'Share => Dupable (Alias k α a) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

dupR :: Alias k α a %1 -> Replicator (Alias k α a) #

dup2 :: Alias k α a %1 -> (Alias k α a, Alias k α a) #

k ~ 'Borrow 'Share => Movable (Alias k α a) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

move :: Alias k α a %1 -> Ur (Alias k α a) #

Affine (Borrow bk α a) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

aff :: Borrow bk α a %1 -> Aff (Borrow bk α a) Source #

data AliasKind Source #

Alias kind.

data BorrowKind Source #

Borrower kind.

type Borrow (bk :: BorrowKind) = Alias ('Borrow bk) Source #

Borrower of kind bk that is active during the lifetime α.

type Mut = Borrow 'Mut Source #

Mutable borrower, which is affine and can update the data.

type Share = Borrow 'Share Source #

Shared borrower, which is unrestricted but usually can only read from the data.

type Lend = Alias 'Lend Source #

Lender, which can retrieve the lifetime at the lifetime α.

coerceShare :: forall b (α :: Lifetime) a. Coercible a b => Share α a %1 -> Share α b Source #

shareCoercion :: forall a b (α :: Lifetime). Coercible a b => Coercion (Share α a) (Share α b) Source #

borrowM :: forall a (α :: Lifetime). a %1 -> BO α (Mut α a, Lend α a) Source #

Borrow a resource linearly for the same lifetime as the ambient BO computation. Returns the pair of the mutable borrow to the resource, and Lender to be invoked later to reclaim the resource at the End of the lifetime.

See also borrowLinearlyM.

If you want to borrow a resource indepdendent of the ambient lifetime, you can use borrow instead.

borrowLinearlyM :: forall a (α :: Lifetime). (Linearly %1 -> a) %1 -> BO α (Mut α a, Lend α a) Source #

A variant of borrowM that does linear allocation first.

sharing' :: forall (α :: Lifetime) a (α' :: Lifetime) r. Mut α a %1 -> (forall (β :: Lifetime). Share/\ α) a -> BO/\ α') (After β r)) %1 -> BO α' (r, Mut α a) Source #

Executes an operation on Shared borrow in sub lifetime. You may need -XImpredicativeTypes extension to use this function.

See also: sharing and sharing_. For Mutable borrows, see reborrowing'.

sharing :: forall (α :: Lifetime) (α' :: Lifetime) a r. Mut α a %1 -> (forall (β :: Lifetime). Share/\ α) a -> BO/\ α') r) %1 -> BO α' (r, Mut α a) Source #

A variant of sharing' that returns the direct value of the computation on the shared borrow. There is also a flipped infix version (<$~).

See also: sharing_. For Mutable borrows, see reborrowing.

(<$~) :: forall (α :: Lifetime) a (α' :: Lifetime) r. (forall (β :: Lifetime). Share/\ α) a -> BO/\ α') r) %1 -> Mut α a %1 -> BO α' (r, Mut α a) infix 4 Source #

Flipped infix version of sharing, smoewhat analgous to (<$>) and (<%~) in lens package.

sharing_ :: forall (α :: Lifetime) (α' :: Lifetime) a r. Consumable r => Mut α a %1 -> (forall (β :: Lifetime). Share/\ α) a -> BO/\ α') r) %1 -> BO α' (Mut α a) Source #

A variant of sharing' that discards the final result of the computation. There is also a flipped infix version (<$=).

See also: sharing. For Mutable borrows, see reborrowing_.

(<$=) :: forall (α :: Lifetime) a (α' :: Lifetime). (forall (β :: Lifetime). Share/\ α) a -> BO/\ α') ()) %1 -> Mut α a %1 -> BO α' (Mut α a) Source #

Flipped infix version of sharing_, smoewhat analgous to (<$>) and (<%=) in lens package.

reborrowing' :: forall (α :: Lifetime) a (α' :: Lifetime) r. Mut α a %1 -> (forall (β :: Lifetime). Mut/\ α) a %1 -> BO/\ α') (After β r)) %1 -> BO α' (r, Mut α a) Source #

Executes an operation on Mutable borrow in sub lifetime. You may need -XImpredicativeTypes extension to use this function.

See also: reborrowing, and reborrowing_. For Shared borrows, see sharing, sharing', and sharing_.

reborrowing :: forall (α :: Lifetime) a (α' :: Lifetime) r. Mut α a %1 -> (forall (β :: Lifetime). Mut/\ α) a %1 -> BO/\ α') r) %1 -> BO α' (r, Mut α a) Source #

A variant of reborrowing' that returns the direct value of the operation on the reborrowed mutable borrow. There is also a flipped infix version (<%~).

See also: reborrowing_ and sharing.

(<%~) :: forall (α :: Lifetime) a (α' :: Lifetime) r. (forall (β :: Lifetime). Mut/\ α) a %1 -> BO/\ α') r) %1 -> Mut α a %1 -> BO α' (r, Mut α a) infix 4 Source #

Flipped infix version of reborrowing, smoewhat analgous to (<$>) and (<%~) in lens package.

reborrowing_ :: forall r (α :: Lifetime) a (α' :: Lifetime). Consumable r => Mut α a %1 -> (forall (β :: Lifetime). Mut/\ α) a %1 -> BO/\ α') r) %1 -> BO α' (Mut α a) Source #

A variant of reborrowing' that discards the final result of the computation. There is also a flipped infix version (<%=).

See also: reborrowing and sharing_.

(<%=) :: forall (α :: Lifetime) a (α' :: Lifetime). (forall (β :: Lifetime). Mut/\ α) a %1 -> BO/\ α') ()) %1 -> Mut α a %1 -> BO α' (Mut α a) infix 4 Source #

Flipped infix version of reborrowing_, smoewhat analgous to (<$>) and (<%=) in lens package.

share :: forall (k :: BorrowKind) (α :: Lifetime) a. Borrow k α a %1 -> Ur (Share α a) Source #

Shares a mutable borrow, invalidating the original one.

reclaim' :: forall (α :: Lifetime) a. Lend α a %1 -> After α a Source #

Reclaims a borrowed resource at the End of lifetime @α'.

reclaim :: forall (α :: Lifetime) a. End α => Lend α a %1 -> a Source #

Reclaims a borrowed resource at the End of lifetime @α'.

pureAfter :: forall (α :: Lifetime) a. (End α => a) %1 -> BO α (After α a) Source #

reborrow :: forall (β :: Lifetime) (α :: Lifetime) a. α >= β => Mut α a %1 -> (Mut β a, Lend β (Mut α a)) Source #

Reborrow a mutable borrow into a sublifetime.

joinMut :: forall (bk :: BorrowKind) (α :: Lifetime) (β :: Lifetime) a. Borrow bk α (Mut β a) %1 -> Borrow bk (α /\ β) a Source #

Collapse a borrower to a mutable borrower.

joinLend :: forall (α :: Lifetime) a. Lend α (Lend α a) %1 -> Lend α a Source #

Lower-level operators

borrow :: forall (α :: Lifetime) a. a %1 -> Linearly %1 -> (Mut α a, Lend α a) Source #

Borrow a resource linearly and obtain the mutable borrow to it and Lend witness to 'reclaim the resource to lend at the End of the lifetime.

For typical usage, you should use borrowM to avoid type ambiguity.

borrowLinearOnly :: forall (α :: Lifetime) a. LinearOnly a => a %1 -> (Mut α a, Lend α a) Source #

A variant of borrow that obtains Linearly viar LinearOnly.

Splitting aliases

class DistributesAlias (f :: Type -> Type) Source #

Distribute an alias over a functor.

Instances

Instances details
DistributesAlias First Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (First x) %1 -> First (Alias ak α x)

DistributesAlias Last Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Last x) %1 -> Last (Alias ak α x)

DistributesAlias Max Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Max x) %1 -> Max (Alias ak α x)

DistributesAlias Min Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Min x) %1 -> Min (Alias ak α x)

DistributesAlias Identity Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Identity x) %1 -> Identity (Alias ak α x)

DistributesAlias First Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (First x) %1 -> First (Alias ak α x)

DistributesAlias Last Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Last x) %1 -> Last (Alias ak α x)

DistributesAlias Down Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Down x) %1 -> Down (Alias ak α x)

DistributesAlias Dual Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Dual x) %1 -> Dual (Alias ak α x)

DistributesAlias Par1 Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Par1 x) %1 -> Par1 (Alias ak α x)

DistributesAlias Maybe Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Maybe x) %1 -> Maybe (Alias ak α x)

DistributesAlias Solo Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Solo x) %1 -> Solo (Alias ak α x)

DistributesAlias [] Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α [x] %1 -> [Alias ak α x]

Unsatisfiable ('Text "Use splitEither directly!") => DistributesAlias (Either e) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Either e x) %1 -> Either e (Alias ak α x)

Unsatisfiable ('Text "Use splitPair instead!") => DistributesAlias ((,) a) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (a, x) %1 -> (a, Alias ak α x)

GenericDistributesAlias f => DistributesAlias (Generically1 f) Source # 
Instance details

Defined in Control.Monad.Borrow.Pure.BO.Internal

Methods

split_ :: forall (ak :: AliasKind) (α :: Lifetime) x. Alias ak α (Generically1 f x) %1 -> Generically1 f (Alias ak α x)

split :: forall f x (ak :: AliasKind) (α :: Lifetime). DistributesAlias f => Alias ak α (f x) %1 -> f (Alias ak α x) Source #

type GenericDistributesAlias (f :: Type -> Type) = (Generic1 f, GDistributeAlias (Rep1 f)) Source #

genericSplit :: forall f x (ak :: AliasKind) (α :: Lifetime). GenericDistributesAlias f => Alias ak α (f x) %1 -> f (Alias ak α x) Source #

splitPair :: forall (ak :: AliasKind) (α :: Lifetime) a b. Alias ak α (a, b) %1 -> (Alias ak α a, Alias ak α b) Source #

splitEither :: forall (ak :: AliasKind) (α :: Lifetime) a b. Alias ak α (Either a b) %1 -> Either (Alias ak α a) (Alias ak α b) Source #

Misc Utilities

Manual lifetime reassociation

assocRBO :: forall (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. BO ((α /\ β) /\ γ) a %1 -> BO/\/\ γ)) a Source #

assocLBO :: forall (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. BO/\/\ γ)) a %1 -> BO ((α /\ β) /\ γ) a Source #

assocBOEq :: forall (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. BO ((α /\ β) /\ γ) a :~: BO/\/\ γ)) a Source #

assocBorrowL :: forall (bk :: BorrowKind) (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. Borrow bk (α /\/\ γ)) a %1 -> Borrow bk ((α /\ β) /\ γ) a Source #

assocBorrowR :: forall (bk :: BorrowKind) (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. Borrow bk ((α /\ β) /\ γ) a %1 -> Borrow bk (α /\/\ γ)) a Source #

assocBorrowEq :: forall (bk :: BorrowKind) (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. Borrow bk ((α /\ β) /\ γ) a :~: Borrow bk (α /\/\ γ)) a Source #

assocLendL :: forall (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. Lend/\/\ γ)) a %1 -> Lend ((α /\ β) /\ γ) a Source #

assocLendR :: forall (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. Lend ((α /\ β) /\ γ) a %1 -> Lend/\/\ γ)) a Source #

assocLendEq :: forall (α :: Lifetime) (β :: Lifetime) (γ :: Lifetime) a. Lend ((α /\ β) /\ γ) a :~: Lend/\/\ γ)) a Source #

Re-exports