THEff-0.1.1.0: TH implementation of effects.

Safe HaskellNone
LanguageHaskell2010
Extensions
  • FlexibleContexts
  • KindSignatures
  • RankNTypes
  • ExplicitForAll

Control.THEff.Validator

Contents

Synopsis

Overview

{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}

import Control.THEff
import Control.THEff.Validator

mkEff "MyVldtr"   ''Validator   ''Float     ''NoEff

test1 :: Int -> Either Float Float -- return Left if out of range
test1 i = runMyVldtr (validator (<30)) $
                    chk $ pi ^ i

test2 :: Int -> Float -- If value is out of range, it is set on the border of the range.
test2 i = withRange runMyVldtr 0 30 $ chk $ pi ^ i
>>> test1 2
Right 9.869605
>>> test1 3
Left 31.006279
>>> test2 2
9.869605
>>> test2 3
30.0

Types and functions used in mkEff

data Validator' v e Source

Actually, the effect type - v - Type - the parameter of the effect. - e - mkEff generated type.

data Validator m e o v a Source

Type implements link in the chain of effects. Constructors must be named {EffectName}{Outer|WriterAction|WriterResult} and have a specified types of fields. - m - Or Monad (if use the Lift) or phantom type - stub (if used NoEff). - o - Type of outer effect. - a - The result of mkEff generated runEEEE... function.

type ValidatorArgT v r = v -> Either v v Source

Type of fourth argument of runEffValidator and first argument of runEEEE.

type ValidatorResT r v = Either v r Source

Result type of runEEEE.

effValidator :: EffClass Validator' v e => Validator' v r -> Eff e r Source

This function is used in the mkEff generated runEEEE functions and typically in effect action functions. Calling the effect action.

runEffValidator Source

Arguments

:: forall (t :: * -> *) (u :: (* -> *) -> * -> *) (m :: * -> *) (m1 :: * -> *) (o :: (* -> *) -> * -> *). Monad m 
=> (u t r -> (r -> m (ValidatorResT z v)) -> m (ValidatorResT z v))

The outer effect function

-> (Validator m1 e o w a -> r)

The chain of effects link wrapper.

-> (r -> Validator t r u v z)

The chain of effects link unwrapper.

-> ValidatorArgT v z

The argument of effect. A value validator function.

-> Eff r a 
-> m (ValidatorResT z v) 

The main function of the effect implementing. This function is used in the mkEff generated runEEEE functions.

Functions that use this effect

chk :: EffClass Validator' v e => v -> Eff e v Source

Check the conditions specified by the first argument of runEEEE

Functions for substitution in the first argument runEEEEE....

validator Source

Arguments

:: Ord v 
=> (v -> Bool)

predicate

-> v

value

-> Either v v 

validator returns Right v if the predicate returns True and Left v else.

range Source

Arguments

:: Ord v 
=> v

The lower limit of the range.

-> v

The upper limit of the range

-> v

The value

-> Either v v 

If the value is outside this range, range sets the value of the range. Always returns Right.

Helper functions

right :: Either a b -> b Source

right ~(Right v) = v

withRange Source

Arguments

:: Ord v 
=> ((v -> Either v v) -> e -> Either l r)

Validator effect runEEEE function

-> v

The lower limit of the range.

-> v

The upper limit of the range.

-> e

Eff (MyValidator m ...) ...

-> r 

If the runEEEE return value is out of range, it is set on the border of the range.