| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
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
- newtype ContFlow (xs :: [Type]) r = ContFlow (ContTuple xs r -> r)
- type family ContTuple (xs :: [Type]) r where ...
- (>:>) :: MultiCont a => a -> ContTuple (MultiContTypes a) r -> r
- (>-:>) :: (MultiCont a, MultiContTypes a ~ '[b]) => a -> (b -> r) -> r
- (>%:>) :: (MultiCont a, ReorderTuple ts (ContTuple (MultiContTypes a) r)) => a -> ts -> r
- (>::>) :: forall (xs :: [Type]) r. ContFlow xs r -> ContTuple xs r -> r
- (>:-:>) :: ContFlow '[a] r -> (a -> r) -> r
- (>:%:>) :: forall ts (xs :: [Type]) r. ReorderTuple ts (ContTuple xs r) => ContFlow xs r -> ts -> r
- type family ToMultiCont (xs :: [Type]) r :: [Type] where ...
- class MultiCont a where
- type MultiContTypes a :: [Type]
- toCont :: a -> ContFlow (MultiContTypes a) r
- toContM :: Monad m => m a -> ContFlow (MultiContTypes a) (m r)
Documentation
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
| ContVariant xs => MultiCont (V xs) Source # | |||||
Defined in Data.Variant Associated Types
| |||||
| (Functor (VariantF xs), ContVariant (ApplyAll (EADT xs) xs)) => MultiCont (EADT xs) Source # | MultiCont instance
| ||||
Defined in Data.Variant.EADT Associated Types
| |||||
| ContVariant (ApplyAll e xs) => MultiCont (VariantF xs e) Source # | |||||
Defined in Data.Variant.VariantF Associated Types
| |||||