{-# OPTIONS_GHC -Wall -fno-warn-orphans #-} ---------------------------------------------------------------------- -- | -- Module : StateAdd -- Copyright : (c) Conal Elliott 2010 -- License : BSD3 -- -- Maintainer : conal@conal.net -- Stability : experimental -- -- Experiments in addition. Formulated via the State monad -- See ---------------------------------------------------------------------- module StateAdd where import Control.Applicative (Applicative(..)) import Control.Monad.State import Control.Compose (result) import qualified AddingMachines as AM type Carrier = State Bool -- Add two values and a carry, resulting in a value and a carry. -- Component of carry-based adding machines. type Adder a = a -> a -> Carrier a addBase :: (Ord a, Num a) => a -> Adder a addBase = (result.result.result) State AM.addBase adds :: Adder a -> Adder [a] adds = zipWithM {-------------------------------------------------------------------- Misc --------------------------------------------------------------------} -- I hope this instance will get added to mtl someday. instance Applicative (State s) where { pure = return ; (<*>) = ap } {-------------------------------------------------------------------- Testing --------------------------------------------------------------------} add10 :: Adder Int add10 = addBase 10 t1 :: (Int,Bool) t1 = runState (add10 3 1) False -- (4,False) t2 :: ([Int],Bool) t2 = runState (adds add10 [3,5,7,8] [1,6,4,1]) False -- ([4,1,2,0],True)