contstuff-0.2.2: Easy to use CPS-based monads

Stability experimental Ertugrul Soeylemez

Control.ContStuff

Description

This module implements a number of monad transformers using a CPS approach internally.

Synopsis

newtype Id a Source

The identity monad. This monad represents values themselves, i.e. computations without effects.

Constructors

 Id FieldsgetId :: a

Instances

## Identity transformer

newtype IdT m a Source

The identity monad transformer. This monad transformer represents computations themselves without further side effects. Unlike most other monad transformers in this module it is not implemented in terms of continuation passing style.

Constructors

 IdT FieldsgetIdT :: m a

Instances

 Transformer IdT Runnable IdT r m r Monad m => Monad (IdT m) Functor m => Functor (IdT m) MonadFix m => MonadFix (IdT m) Applicative m => Applicative (IdT m) Alternative m => Alternative (IdT m) (LiftBase m a, Monad m) => LiftBase (IdT m) a

type Cont r a = ContT r Id aSource

Pure CPS monad derived from ContT.

runCont :: (a -> r) -> Cont r a -> rSource

Run a pure CPS computation.

evalCont :: Cont r r -> rSource

Evaluate a pure CPS computation to its final result.

modifyCont :: (r -> r) -> Cont r ()Source

Modify the result of a CPS computation along the way.

newtype ContT r m a Source

The continuation passing style monad transformer. This monad transformer models the most basic form of CPS.

Constructors

 ContT FieldsgetContT :: (a -> m r) -> m r

Instances

 Transformer (ContT r) Runnable (ContT r) r m a Monad (ContT r m) Functor (ContT r m) Applicative (ContT r m) (Alternative m, Monad m) => Alternative (ContT r m) (Monad m, Stateful m) => Stateful (ContT r m) CallCC (ContT r m) Applicative m => Abortable (ContT r m) (Functor m, Monoid w) => Writable (ContT (r, w) m) w Alternative m => Writable (ContT r m) r (LiftBase m a, Monad m) => LiftBase (ContT r m) a

runContT :: (a -> m r) -> ContT r m a -> m rSource

Run a CPS-style computation given the supplied final continuation.

evalContT :: Applicative m => ContT r m r -> m rSource

Evaluate a CPS-style computation to its final result.

modifyContT :: Functor m => (r -> r) -> ContT r m ()Source

Transform the final result along the way.

### Choice/nondeterminism

newtype ChoiceT r i m a Source

The choice monad transformer, which models, as the most common interpretation, nondeterminism. Internally a list of choices is represented as a CPS-based left-fold function.

Constructors

 ChoiceT FieldsgetChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> m r

Instances

 Transformer (ChoiceT r i) Monad (ChoiceT r i m) Functor (ChoiceT r i m) Applicative (ChoiceT r i m) Alternative (ChoiceT r i m) (LiftBase m a, Monad m) => LiftBase (ChoiceT r i m) a

runChoiceT :: (i -> a -> (i -> m r) -> m r) -> i -> (i -> m r) -> ChoiceT r i m a -> m rSource

Run a choice computation.

findFirst :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)Source

Find the first solution.

findAll :: (Alternative f, Applicative m) => ChoiceT (f a) (f a) m a -> m (f a)Source

Find all solutions.

listChoiceT :: Applicative m => ChoiceT [a] [a] m a -> m [a]Source

Get list of solutions (faster than `findAll`, but returns solutions in reversed order).

listA :: Alternative f => [a] -> f aSource

Turn a list into a computation with alternatives.

### State

type State r s a = StateT r s Id aSource

Pure state monad derived from StateT.

runState :: s -> (s -> a -> r) -> State r s a -> rSource

Run a stateful computation.

evalState :: s -> State r s r -> rSource

Run a stateful computation returning its result.

execState :: s -> State s s a -> sSource

Run a stateful computation returning its result.

newtype StateT r s m a Source

Constructors

 StateT FieldsgetStateT :: s -> (s -> a -> m r) -> m r

Instances

 Transformer (StateT r s) Runnable (StateT r s) r m a Monad (StateT r s m) Functor (StateT r s m) Applicative (StateT r s m) Alternative m => Alternative (StateT r s m) Stateful (StateT r s m) CallCC (StateT r s m) Applicative m => Abortable (StateT r s m) (Functor m, Monoid w) => Writable (StateT (r, w) s m) w Alternative m => Writable (StateT r s m) r (LiftBase m a, Monad m) => LiftBase (StateT r s m) a

runStateT :: s -> (s -> a -> m r) -> StateT r s m a -> m rSource

Run a state transformer.

evalStateT :: Applicative m => s -> StateT r s m r -> m rSource

Run a state transformer returning its result.

execStateT :: Applicative m => s -> StateT s s m a -> m sSource

Run a state transformer returning its final state.

type OldWriter r w a = ContT (r, w) Id aSource

runOldWriter :: Monoid w => OldWriter r w r -> (r, w)Source

evalOldWriter :: Monoid w => OldWriter r w r -> rSource

Run a traditional writer computation and return its result.

execOldWriter :: Monoid w => OldWriter r w r -> wSource

Run a traditional writer computation and return its log.

type OldWriterT r w m a = ContT (r, w) m aSource

runOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m (r, w)Source

evalOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m rSource

Run a traditional writer transformer and return its result.

execOldWriterT :: (Applicative m, Monoid w) => OldWriterT r w m r -> m wSource

Run a traditional writer transformer and return its log.

type WriterT = ContTSource

The writer monad transformer. Supports logging effects.

runWriterT :: Alternative m => WriterT r m a -> m rSource

Run a writer transformer.

# Effect classes

class Abortable m whereSource

Associated Types

type Result m Source

Methods

abort :: Result m -> m aSource

Instances

 Applicative m => Abortable (ContT r m) Applicative m => Abortable (StateT r s m)

class CallCC m whereSource

Methods

callCC :: ((a -> m b) -> m a) -> m aSource

Call with current continuation.

Instances

 CallCC (ContT r m) CallCC (StateT r s m)

data Label m a Source

labelCC :: (Applicative m, CallCC m) => a -> m (a, Label m a)Source

Capture the current continuation for later use.

goto :: Applicative m => Label m a -> a -> m bSource

class LiftBase m a whereSource

Associated Types

type Base m a Source

Methods

base :: Base m a -> m aSource

Instances

 LiftBase [] a LiftBase IO a LiftBase Maybe a LiftBase Id a LiftBase ((->) r) a LiftBase (ST s) a (LiftBase m a, Monad m) => LiftBase (IdT m) a (LiftBase m a, Monad m) => LiftBase (ContT r m) a (LiftBase m a, Monad m) => LiftBase (StateT r s m) a (LiftBase m a, Monad m) => LiftBase (ChoiceT r i m) a

io :: (LiftBase m a, Base m a ~ IO a) => Base m a -> m aSource

Handy alias for lifting IO computations.

class Runnable t r m a whereSource

Every monad transformer `t` that supports transforming `t m a` to `m a` can be an instance of this class.

Associated Types

type Argument t r m a Source

Methods

runT :: Argument t r m a -> t m a -> m rSource

Instances

 Runnable IdT r m r Runnable (ContT r) r m a Runnable (StateT r s) r m a

class Stateful m whereSource

Associated Types

type StateOf m Source

Methods

get :: m (StateOf m)Source

Get the current state.

put :: StateOf m -> m ()Source

Set the current state and force it.

putLazy :: StateOf m -> m ()Source

Set the current state, but don't force it.

Instances

 (Monad m, Stateful m) => Stateful (ContT r m) Stateful (StateT r s m)

getField :: (Functor m, Stateful m) => (StateOf m -> a) -> m aSource

Get a certain field.

modify :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()Source

Apply a function to the current state.

modifyField :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()Source

Get a field and modify the state.

modifyFieldLazy :: (Monad m, Stateful m) => (StateOf m -> a) -> (a -> StateOf m) -> m ()Source

Get a field and modify the state. Lazy version.

modifyLazy :: (Monad m, Stateful m) => (StateOf m -> StateOf m) -> m ()Source

Apply a function to the current state. Lazy version.

class Transformer t whereSource

The monad transformer class. Lifting computations one level down the monad stack, or stated differently promoting a computation of the underlying monad to the transformer.

Methods

lift :: Monad m => m a -> t m aSource

Promote a monadic computation to the transformer.

Instances

 Transformer IdT Transformer (ContT r) Transformer (StateT r s) Transformer (ChoiceT r i)

class Writable m w whereSource
{"ft_lang_label":"__label__en","ft_lang_prob":0.6831623,"math_prob":0.84121966,"size":8955,"snap":"2021-04-2021-17","text_gpt3_token_len":2755,"char_repetition_ratio":0.18657133,"word_repetition_ratio":0.41153634,"special_character_ratio":0.32294807,"punctuation_ratio":0.13763066,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9958352,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-18T21:33:07Z\",\"WARC-Record-ID\":\"<urn:uuid:c17c32f4-42e0-4927-8456-80fad979b44f>\",\"Content-Length\":\"63181\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:491f07ad-703f-47f2-b7ee-3c40f24f00f7>\",\"WARC-Concurrent-To\":\"<urn:uuid:3d14dfb3-0895-472e-b445-402a9f23aaba>\",\"WARC-IP-Address\":\"151.101.200.68\",\"WARC-Target-URI\":\"https://hackage.haskell.org/package/contstuff-0.2.2/docs/Control-ContStuff.html\",\"WARC-Payload-Digest\":\"sha1:3NRI52YARRCHKYASUTPEMGFFUS5XAQZI\",\"WARC-Block-Digest\":\"sha1:7YRL47TQXDGU6DOKBSNDYDTO7TL6MZJC\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703515235.25_warc_CC-MAIN-20210118185230-20210118215230-00523.warc.gz\"}"}