module Data.State
(
State(..)
,Final(..)
,terminal
,isError
) where
import Control.Applicative
import Control.Monad
data State a =
Q a
| QE deriving(Show, Eq, Ord)
instance Functor State where
fmap _ QE = QE
fmap f (Q q) = Q $ f q
instance Applicative State where
pure = Q
QE <*> _ = QE
(Q f) <*> q = fmap f q
instance Monad State where
return = pure
QE >>= _ = QE
(Q q) >>= f = f q
instance (Enum a) => Enum (State a) where
toEnum n = if n<0 then QE else Q (toEnum n)
fromEnum QE = 1
fromEnum (Q a) = fromEnum a
instance (Bounded a)=> Bounded (State a) where
minBound = Q minBound
maxBound = QE
instance Monoid a => Monoid (State a) where
mempty = QE
QE `mappend` m = m
m `mappend` QE = m
(Q a) `mappend` (Q b) = Q (a `mappend` b)
instance Foldable State where
foldr _ z QE = z
foldr f z (Q x) = f x z
foldl _ z QE = z
foldl f z (Q x) = f z x
type Final a = [State a]
terminal :: (Eq a) => Final a -> State a -> Bool
terminal qs q = elem q qs
isError::(Eq a) => State a -> Bool
isError q = q==QE