variant-1.0.2: Variant and EADT
Safe HaskellNone
LanguageHaskell2010

Data.Variant.ContFlow

Description

Continuation-based control-flow

This module provides safe pattern matching on V values using multi-continuations. Instead of pattern matching with the V pattern (which the compiler cannot check for completeness), we can provide a function per constructor as in a pattern-match.

Safe pattern matching with ordered continuations (>:>)

With multi-continuations we can transform a variant V [A,B,C] into a function whose type is (A -> r, B -> r, C -> r) -> r. Hence the compiler will ensure that we provide the correct number of alternatives in the continuation tuple.

Applying a multi-continuation to a Variant is done with >:>:

import Data.Variant.ContFlow

printV :: V [String,Int,Float] -> IO ()
printV v = v >:>
   ( \s -> putStrLn ("Found string: " ++ s)
   , \i -> putStrLn ("Found int: " ++ show i)
   , \f -> putStrLn ("Found float: " ++ show f)
   )

Safe pattern matching with unordered continuations (>%:>)

By using the >%:> operator instead of >:>, we can provide continuations in any order as long as an alternative for each constructor is provided.

The types must be unambiguous as the Variant constructor types cannot be used to infer the continuation types (as is done with >:>). Hence the type ascriptions in the following example:

printU :: V [String,Int,Float] -> IO ()
printU v = v >%:>
   ( \f -> putStrLn ("Found float: " ++ show (f :: Float))
   , \s -> putStrLn ("Found string: " ++ s)
   , \i -> putStrLn ("Found int: " ++ show (i :: Int))
   )
Synopsis

Documentation

newtype ContFlow (xs :: [Type]) r Source #

A continuation based control-flow

Constructors

ContFlow (ContTuple xs r -> r) 

type family ContTuple (xs :: [Type]) r where ... Source #

Convert a list of types into the actual data type representing the continuations.

Equations

ContTuple xs r = Tuple (ToMultiCont xs r) 

(>:>) :: MultiCont a => a -> ContTuple (MultiContTypes a) r -> r infixl 0 Source #

Bind a multi-continuable type to a tuple of continuations

(>-:>) :: (MultiCont a, MultiContTypes a ~ '[b]) => a -> (b -> r) -> r infixl 0 Source #

Bind a single-continuable type to a 1-tuple of continuations

(>%:>) :: (MultiCont a, ReorderTuple ts (ContTuple (MultiContTypes a) r)) => a -> ts -> r infixl 0 Source #

Bind a multi-continuable type to a tuple of continuations and reorder fields if necessary

(>::>) :: forall (xs :: [Type]) r. ContFlow xs r -> ContTuple xs r -> r infixl 0 Source #

Bind a flow to a tuple of continuations

(>:-:>) :: ContFlow '[a] r -> (a -> r) -> r infixl 0 Source #

Bind a flow to a 1-tuple of continuations

(>:%:>) :: forall ts (xs :: [Type]) r. ReorderTuple ts (ContTuple xs r) => ContFlow xs r -> ts -> r infixl 0 Source #

Bind a flow to a tuple of continuations and reorder fields if necessary

type family ToMultiCont (xs :: [Type]) r :: [Type] where ... Source #

Equations

ToMultiCont ('[] :: [Type]) r = '[] :: [Type] 
ToMultiCont (x ': xs) r = (x -> r) ': ToMultiCont xs r 

class MultiCont a where Source #

A multi-continuable type

Associated Types

type MultiContTypes a :: [Type] Source #

Methods

toCont :: a -> ContFlow (MultiContTypes a) r Source #

Convert a data into a multi-continuation

toContM :: Monad m => m a -> ContFlow (MultiContTypes a) (m r) Source #

Convert a data into a multi-continuation (monadic)

Instances

Instances details
ContVariant xs => MultiCont (V xs) Source # 
Instance details

Defined in Data.Variant

Associated Types

type MultiContTypes (V xs) 
Instance details

Defined in Data.Variant

type MultiContTypes (V xs) = xs

Methods

toCont :: V xs -> ContFlow (MultiContTypes (V xs)) r Source #

toContM :: Monad m => m (V xs) -> ContFlow (MultiContTypes (V xs)) (m r) Source #

(Functor (VariantF xs), ContVariant (ApplyAll (EADT xs) xs)) => MultiCont (EADT xs) Source #

MultiCont instance

>>> let f x = toCont x >::> (const "[]", \(ConsF u us) -> u ++ ":" ++ f us)
>>> f a
"Hello:World:[]"
Instance details

Defined in Data.Variant.EADT

Associated Types

type MultiContTypes (EADT xs) 
Instance details

Defined in Data.Variant.EADT

type MultiContTypes (EADT xs) = ApplyAll (EADT xs) xs

Methods

toCont :: EADT xs -> ContFlow (MultiContTypes (EADT xs)) r Source #

toContM :: Monad m => m (EADT xs) -> ContFlow (MultiContTypes (EADT xs)) (m r) Source #

ContVariant (ApplyAll e xs) => MultiCont (VariantF xs e) Source # 
Instance details

Defined in Data.Variant.VariantF

Associated Types

type MultiContTypes (VariantF xs e) 
Instance details

Defined in Data.Variant.VariantF

type MultiContTypes (VariantF xs e) = ApplyAll e xs

Methods

toCont :: VariantF xs e -> ContFlow (MultiContTypes (VariantF xs e)) r Source #

toContM :: Monad m => m (VariantF xs e) -> ContFlow (MultiContTypes (VariantF xs e)) (m r) Source #