Tainted: Tainted type, and associated operations

[ bsd3, control, data, library, monads, trans ] [ Propose Tags ]

Tainted type, and associated operations


[Skip to Readme]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.1, 0.1.0, 0.1.0.1, 0.1.0.2
Dependencies base (>=4 && <5), mtl (>=2.0 && <2.3) [details]
License BSD-3-Clause
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 repo head: git clone git://github.com/RossMeikleham/Tainted.git
Uploaded by RossMeikleham at 2015-05-27T07:24:04Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 3235 total (13 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for Tainted-0.1.0.1

[back to package description]

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.