control-dsl-0.2.1.3: An alternative to monads for control flow DSLs

Safe HaskellSafe
LanguageHaskell2010

Control.Dsl.Return

Synopsis

Documentation

data Return r' r a where Source #

Constructors

Return :: r' -> Return r' r Void 
Instances
PolyCont (Return r) r Void Source # 
Instance details

Defined in Control.Dsl.Return

Methods

runPolyCont :: Return r r' Void -> (Void -> r) -> r Source #

Applicative m => PolyCont (Return r) (m r) Void Source # 
Instance details

Defined in Control.Dsl.Return

Methods

runPolyCont :: Return r r' Void -> (Void -> m r) -> m r Source #

PolyCont (Return r) (Cont r' r) Void Source # 
Instance details

Defined in Control.Dsl.Cont

Methods

runPolyCont :: Return r r'0 Void -> (Void -> Cont r' r) -> Cont r' r Source #

PolyCont (Return r) (State s r) Void Source # 
Instance details

Defined in Control.Dsl.State.State

Methods

runPolyCont :: Return r r' Void -> (Void -> State s r) -> State s r Source #

return :: PolyCont (Return r') r Void => r' -> r Source #

Lift r to the answer type, similar to return.

This return function aims to be used as the last statement of a do block.

When return is present in a nested do block for when or unless, if the r' is not (), it will create a Cont that performs early return, skipping the rest statements of the outer do notation.

Examples

Expand
>>> :set -XTypeOperators
>>> :set -XRebindableSyntax
>>> import Prelude hiding ((>>), (>>=), return, fail)
>>> import Control.Dsl
>>> import Control.Dsl.Return
>>> import Control.Dsl.Yield
>>> import Control.Dsl.Cont
>>> import Control.Dsl.Empty
>>> :{
earlyGenerator :: Bool -> Cont [String] Integer
earlyGenerator earlyReturn = do
  Yield "inside earlyGenerator"
  when earlyReturn $ do
    Yield "early return"
    return 1
  Yield "normal return"
  return 0
:}
>>> :{
earlyGeneratorTest :: [String]
earlyGeneratorTest = do
  Yield "before earlyGenerator"
  i <- earlyGenerator True
  Yield "after earlyGenerator"
  Yield $ "the return value of earlyGenerator is " ++ show i
  empty
:}
>>> earlyGeneratorTest
["before earlyGenerator","inside earlyGenerator","early return","after earlyGenerator","the return value of earlyGenerator is 1"]

fail :: PolyCont (Return IOError) r Void => String -> r Source #

Lift an IOError to the answer type, similar to fail.

This fail function aims to be used as the last statement of a do block.