MyNixOS website logo
Description

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, 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.

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.

Metadata

Version

0.1.0.2

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows