```{-# LANGUAGE RecordWildCards #-}

import Data.Map
import Data.Monoid
-- import Data.Bounds

import Data.LinFunc
import Data.LinearProgram.Types
import Data.LinearProgram.Spec

-- | A 'State' monad used for the construction of a linear program.
type LPM v c = State (LP v c)

-- | Constructs a linear programming problem, returning any
-- desired return value.
runLPM :: (Ord v, Module r c) => LPM v c a -> (a, LP v c)
runLPM m = runState m (LP Max zero [] mempty mempty)

-- | Constructs a linear programming problem.
execLPM :: (Ord v, Module r c) => LPM v c a -> LP v c
execLPM = snd . runLPM

-- | Sets the optimization direction of the linear program:
-- maximization or minimization.
setDirection :: Direction -> LPM v c ()
setDirection dir = modify (\ lp -> lp{direction = dir})

equal, leq, geq :: (Ord v, Module r c) => LinFunc v c -> LinFunc v c -> LPM v c ()
equal f g = equalTo (f ^-^ g) zero
leq f g = leqTo (f ^-^ g) zero
geq = flip leq

equal', leq', geq' :: (Ord v, Module r c) => String -> LinFunc v c -> LinFunc v c -> LPM v c ()
equal' lab f g = equalTo' lab (f ^-^ g) zero
leq' lab f g = leqTo' lab (f ^-^ g) zero
geq' = flip . leq'

equalTo, leqTo, geqTo :: LinFunc v c -> c -> LPM v c ()
equalTo f v = constrain f (Equ v)
leqTo f v = constrain f (UBound v)
geqTo f v = constrain f (LBound v)

equalTo', leqTo', geqTo' :: String -> LinFunc v c -> c -> LPM v c ()
equalTo' lab f v = constrain' lab f (Equ v)
leqTo' lab f v = constrain' lab f (UBound v)
geqTo' lab f v = constrain' lab f (LBound v)

varEq, varLeq, varGeq :: (Ord v, Ord c) => v -> c -> LPM v c ()
varEq v c = setVarBounds v (Equ c)
varLeq v c = setVarBounds v (UBound c)
varGeq v c = setVarBounds v (LBound c)

varBds :: (Ord v, Ord c) => v -> c -> c -> LPM v c ()
varBds v l u = setVarBounds v (Bound l u)

constrain :: LinFunc v c -> Bounds c -> LPM v c ()
constrain f bds = modify addConstr where
= lp{constraints = Constr Nothing f bds:constraints}

constrain' :: String -> LinFunc v c -> Bounds c -> LPM v c ()
constrain' lab f bds = modify addConstr where
= lp{constraints = Constr (Just lab) f bds:constraints}

setObjective :: LinFunc v c -> LPM v c ()
setObjective obj = modify setObj where
setObj lp = lp{objective = obj}

addObjective :: (Ord v, Module r c) => LinFunc v c -> LPM v c ()