The Tainted package

[Tags:bsd3, library]

A Tainted type contains either a clean or dirty value. Values which are clean stay clean as long as an operation performed on them results in a clean value. If combined with a dirty value, the value becomes tainted as dirty and remains that way through further operations. This package contains implementations of the Tainted Monad as well as TaintedT (the Tainted Monad Transformer), as well as examples of using each of them.


[Skip to Readme]

Properties

Versions 0.0.1, 0.1.0, 0.1.0.1, 0.1.0.2
Dependencies base (==4.*), mtl (>=2.0 && <2.3) [details]
License BSD3
Copyright Copyright (C) 2015 Ross Meikleham
Author RossMeikleham
Maintainer rossmeikleham@hotmail.co.uk
Category Data, Monads, Control, Trans
Home page https://github.com/RossMeikleham/Tainted
Source repository head: git clone git://github.com/RossMeikleham/Tainted.git
Uploaded Wed May 27 08:07:21 UTC 2015 by RossMeikleham
Distributions NixOS:0.1.0.2
Downloads 549 total (10 in the last 30 days)
Votes
0 []
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees

Readme for Tainted

Readme for Tainted-0.1.0.2

Tainted

travis hackage

Tainted type, and associated operations

A Tainted type contains either a clean or dirty value. Values which are clean stay clean as long as an operation performed on them results in a clean value. If combined with a dirty value, this taints the value causing it to become dirty, and any further operation keeps it dirty. This is similar to the Maybe monad except once the dirty has been reached, calculations can still be performed on the value it contains.

One use case is evaluating whether expressions are pure from multiple sources combining impure and pure values. This can be useful for type checking to enforce purity in certain areas. As soon as an impure part of an expression is reached it taints the entire expression as impure.

A simple example given here is a expression evaluator which is given values from different sources which are marked as pure or impure.

module TaintExample where

import Data.Tainted
import Control.Monad

data Expr = 
      Number (Tainted Int)
    | Add Expr Expr
    
    deriving Show

pure1   = Number (Clean 3)
pure2   = Number (Clean 7)
impure1 = Number (Dirty 5)

expr1 = Add pure1 pure2
expr2 = Add impure1 pure1
expr3 = Add pure1 (Add impure1 pure2) 

--Evaluate expression as much as Possible
evalExpr :: Expr -> Expr
evalExpr (Number n) = Number n
evalExpr (Add e1 e2) = 
    case (evalExpr e1, evalExpr e2) of
        (Number i, Number j) -> Number $ liftM2 (+) i j
        (x, y) -> Add x y

reducedExpr1 = evalExpr expr1
reducedExpr2 = evalExpr expr2
reducedExpr3 = evalExpr expr3

Evaluating expr1:

Number (Clean 10)

Adding 2 clean values 7 and 3 gives a clean value, clean values haven't become tainted

Evaluating expr2:

Number (Dirty 8)

Adding a clean value 3 and dirty value 5 taints the expression as dirty so the expression evaluates to dirty value of 8

Evaluating expr3:

Number (Dirty 15)

This shows the propogation of dirty states, as the inner expression evaluates to a dirty value, then added with a clean value still gives a dirty value.