module Language.Lexer.Tlex.Machine.State (
    StateNum,
    initialStateNum,

    StateSet,
    emptySet,
    singletonSet,
    listToSet,
    setToList,
    nullSet,
    insertSet,
    intersectSet,
    diffSet,
    unionSet,
    lengthSet,
    memberSet,

    StateMap,
    emptyMap,
    insertOrUpdateMap,
    insertMap,
    lookupMap,
    assocsMap,

    StateArray,
    totalStateMapToArray,
    mapArrayWithIx,
    indexArray,
    arrayAssocs,

    StateGraph,
    stateArrayToGraph,
    liftGraphOp,
    indexGraph,
) where

import           Language.Lexer.Tlex.Prelude

import qualified Data.Array                  as Array
import qualified Data.Graph                  as Graph
import qualified Data.Hashable               as Hashable
import qualified Data.IntMap.Strict          as IntMap
import qualified Data.IntSet                 as IntSet


newtype StateNum = StateNum Int
    deriving (StateNum -> StateNum -> Bool
(StateNum -> StateNum -> Bool)
-> (StateNum -> StateNum -> Bool) -> Eq StateNum
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StateNum -> StateNum -> Bool
$c/= :: StateNum -> StateNum -> Bool
== :: StateNum -> StateNum -> Bool
$c== :: StateNum -> StateNum -> Bool
Eq, Eq StateNum
Eq StateNum
-> (StateNum -> StateNum -> Ordering)
-> (StateNum -> StateNum -> Bool)
-> (StateNum -> StateNum -> Bool)
-> (StateNum -> StateNum -> Bool)
-> (StateNum -> StateNum -> Bool)
-> (StateNum -> StateNum -> StateNum)
-> (StateNum -> StateNum -> StateNum)
-> Ord StateNum
StateNum -> StateNum -> Bool
StateNum -> StateNum -> Ordering
StateNum -> StateNum -> StateNum
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: StateNum -> StateNum -> StateNum
$cmin :: StateNum -> StateNum -> StateNum
max :: StateNum -> StateNum -> StateNum
$cmax :: StateNum -> StateNum -> StateNum
>= :: StateNum -> StateNum -> Bool
$c>= :: StateNum -> StateNum -> Bool
> :: StateNum -> StateNum -> Bool
$c> :: StateNum -> StateNum -> Bool
<= :: StateNum -> StateNum -> Bool
$c<= :: StateNum -> StateNum -> Bool
< :: StateNum -> StateNum -> Bool
$c< :: StateNum -> StateNum -> Bool
compare :: StateNum -> StateNum -> Ordering
$ccompare :: StateNum -> StateNum -> Ordering
$cp1Ord :: Eq StateNum
Ord, Int -> StateNum -> ShowS
[StateNum] -> ShowS
StateNum -> String
(Int -> StateNum -> ShowS)
-> (StateNum -> String) -> ([StateNum] -> ShowS) -> Show StateNum
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StateNum] -> ShowS
$cshowList :: [StateNum] -> ShowS
show :: StateNum -> String
$cshow :: StateNum -> String
showsPrec :: Int -> StateNum -> ShowS
$cshowsPrec :: Int -> StateNum -> ShowS
Show, Ord StateNum
Ord StateNum
-> ((StateNum, StateNum) -> [StateNum])
-> ((StateNum, StateNum) -> StateNum -> Int)
-> ((StateNum, StateNum) -> StateNum -> Int)
-> ((StateNum, StateNum) -> StateNum -> Bool)
-> ((StateNum, StateNum) -> Int)
-> ((StateNum, StateNum) -> Int)
-> Ix StateNum
(StateNum, StateNum) -> Int
(StateNum, StateNum) -> [StateNum]
(StateNum, StateNum) -> StateNum -> Bool
(StateNum, StateNum) -> StateNum -> Int
forall a.
Ord a
-> ((a, a) -> [a])
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Bool)
-> ((a, a) -> Int)
-> ((a, a) -> Int)
-> Ix a
unsafeRangeSize :: (StateNum, StateNum) -> Int
$cunsafeRangeSize :: (StateNum, StateNum) -> Int
rangeSize :: (StateNum, StateNum) -> Int
$crangeSize :: (StateNum, StateNum) -> Int
inRange :: (StateNum, StateNum) -> StateNum -> Bool
$cinRange :: (StateNum, StateNum) -> StateNum -> Bool
unsafeIndex :: (StateNum, StateNum) -> StateNum -> Int
$cunsafeIndex :: (StateNum, StateNum) -> StateNum -> Int
index :: (StateNum, StateNum) -> StateNum -> Int
$cindex :: (StateNum, StateNum) -> StateNum -> Int
range :: (StateNum, StateNum) -> [StateNum]
$crange :: (StateNum, StateNum) -> [StateNum]
$cp1Ix :: Ord StateNum
Ix)
    deriving (Int -> StateNum -> Int
StateNum -> Int
(Int -> StateNum -> Int) -> (StateNum -> Int) -> Hashable StateNum
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: StateNum -> Int
$chash :: StateNum -> Int
hashWithSalt :: Int -> StateNum -> Int
$chashWithSalt :: Int -> StateNum -> Int
Hashable.Hashable, Int -> StateNum
StateNum -> Int
StateNum -> [StateNum]
StateNum -> StateNum
StateNum -> StateNum -> [StateNum]
StateNum -> StateNum -> StateNum -> [StateNum]
(StateNum -> StateNum)
-> (StateNum -> StateNum)
-> (Int -> StateNum)
-> (StateNum -> Int)
-> (StateNum -> [StateNum])
-> (StateNum -> StateNum -> [StateNum])
-> (StateNum -> StateNum -> [StateNum])
-> (StateNum -> StateNum -> StateNum -> [StateNum])
-> Enum StateNum
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: StateNum -> StateNum -> StateNum -> [StateNum]
$cenumFromThenTo :: StateNum -> StateNum -> StateNum -> [StateNum]
enumFromTo :: StateNum -> StateNum -> [StateNum]
$cenumFromTo :: StateNum -> StateNum -> [StateNum]
enumFromThen :: StateNum -> StateNum -> [StateNum]
$cenumFromThen :: StateNum -> StateNum -> [StateNum]
enumFrom :: StateNum -> [StateNum]
$cenumFrom :: StateNum -> [StateNum]
fromEnum :: StateNum -> Int
$cfromEnum :: StateNum -> Int
toEnum :: Int -> StateNum
$ctoEnum :: Int -> StateNum
pred :: StateNum -> StateNum
$cpred :: StateNum -> StateNum
succ :: StateNum -> StateNum
$csucc :: StateNum -> StateNum
Enum) via Int

initialStateNum :: StateNum
initialStateNum :: StateNum
initialStateNum = Int -> StateNum
StateNum Int
0


newtype StateSet = StateSet IntSet.IntSet
    deriving (StateSet -> StateSet -> Bool
(StateSet -> StateSet -> Bool)
-> (StateSet -> StateSet -> Bool) -> Eq StateSet
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StateSet -> StateSet -> Bool
$c/= :: StateSet -> StateSet -> Bool
== :: StateSet -> StateSet -> Bool
$c== :: StateSet -> StateSet -> Bool
Eq, Int -> StateSet -> ShowS
[StateSet] -> ShowS
StateSet -> String
(Int -> StateSet -> ShowS)
-> (StateSet -> String) -> ([StateSet] -> ShowS) -> Show StateSet
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StateSet] -> ShowS
$cshowList :: [StateSet] -> ShowS
show :: StateSet -> String
$cshow :: StateSet -> String
showsPrec :: Int -> StateSet -> ShowS
$cshowsPrec :: Int -> StateSet -> ShowS
Show)

instance Hashable.Hashable StateSet where
    hashWithSalt :: Int -> StateSet -> Int
hashWithSalt Int
s (StateSet IntSet
x) = Int -> [Int] -> Int
forall a. Hashable a => Int -> a -> Int
Hashable.hashWithSalt Int
s do IntSet -> [Int]
IntSet.toAscList IntSet
x

emptySet :: StateSet
emptySet :: StateSet
emptySet = IntSet -> StateSet
StateSet IntSet
IntSet.empty

singletonSet :: StateNum -> StateSet
singletonSet :: StateNum -> StateSet
singletonSet (StateNum Int
s) = IntSet -> StateSet
StateSet do Int -> IntSet
IntSet.singleton Int
s

insertSet :: StateNum -> StateSet -> StateSet
insertSet :: StateNum -> StateSet -> StateSet
insertSet (StateNum Int
s) (StateSet IntSet
ss) = IntSet -> StateSet
StateSet do Int -> IntSet -> IntSet
IntSet.insert Int
s IntSet
ss

listToSet :: [StateNum] -> StateSet
listToSet :: [StateNum] -> StateSet
listToSet [StateNum]
ss = IntSet -> StateSet
StateSet do [Int] -> IntSet
IntSet.fromList do [StateNum] -> [Int]
coerce [StateNum]
ss

setToList :: StateSet -> [StateNum]
setToList :: StateSet -> [StateNum]
setToList (StateSet IntSet
ss) = [Int] -> [StateNum]
coerce do IntSet -> [Int]
IntSet.toList IntSet
ss

nullSet :: StateSet -> Bool
nullSet :: StateSet -> Bool
nullSet (StateSet IntSet
ss) = IntSet -> Bool
IntSet.null IntSet
ss

intersectSet :: StateSet -> StateSet -> StateSet
intersectSet :: StateSet -> StateSet -> StateSet
intersectSet (StateSet IntSet
ss1) (StateSet IntSet
ss2) = IntSet -> StateSet
StateSet do IntSet -> IntSet -> IntSet
IntSet.intersection IntSet
ss1 IntSet
ss2

diffSet :: StateSet -> StateSet -> StateSet
diffSet :: StateSet -> StateSet -> StateSet
diffSet (StateSet IntSet
ss1) (StateSet IntSet
ss2) = IntSet -> StateSet
StateSet do IntSet -> IntSet -> IntSet
IntSet.difference IntSet
ss1 IntSet
ss2

unionSet :: StateSet -> StateSet -> StateSet
unionSet :: StateSet -> StateSet -> StateSet
unionSet (StateSet IntSet
ss1) (StateSet IntSet
ss2) = IntSet -> StateSet
StateSet do IntSet -> IntSet -> IntSet
IntSet.union IntSet
ss1 IntSet
ss2

lengthSet :: StateSet -> Int
lengthSet :: StateSet -> Int
lengthSet (StateSet IntSet
ss) = IntSet -> Int
IntSet.size IntSet
ss

memberSet :: StateNum -> StateSet -> Bool
memberSet :: StateNum -> StateSet -> Bool
memberSet (StateNum Int
s) (StateSet IntSet
ss) = Int -> IntSet -> Bool
IntSet.member Int
s IntSet
ss


newtype StateMap a = StateMap (IntMap.IntMap a)
    deriving (StateMap a -> StateMap a -> Bool
(StateMap a -> StateMap a -> Bool)
-> (StateMap a -> StateMap a -> Bool) -> Eq (StateMap a)
forall a. Eq a => StateMap a -> StateMap a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StateMap a -> StateMap a -> Bool
$c/= :: forall a. Eq a => StateMap a -> StateMap a -> Bool
== :: StateMap a -> StateMap a -> Bool
$c== :: forall a. Eq a => StateMap a -> StateMap a -> Bool
Eq, Int -> StateMap a -> ShowS
[StateMap a] -> ShowS
StateMap a -> String
(Int -> StateMap a -> ShowS)
-> (StateMap a -> String)
-> ([StateMap a] -> ShowS)
-> Show (StateMap a)
forall a. Show a => Int -> StateMap a -> ShowS
forall a. Show a => [StateMap a] -> ShowS
forall a. Show a => StateMap a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StateMap a] -> ShowS
$cshowList :: forall a. Show a => [StateMap a] -> ShowS
show :: StateMap a -> String
$cshow :: forall a. Show a => StateMap a -> String
showsPrec :: Int -> StateMap a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> StateMap a -> ShowS
Show, a -> StateMap b -> StateMap a
(a -> b) -> StateMap a -> StateMap b
(forall a b. (a -> b) -> StateMap a -> StateMap b)
-> (forall a b. a -> StateMap b -> StateMap a) -> Functor StateMap
forall a b. a -> StateMap b -> StateMap a
forall a b. (a -> b) -> StateMap a -> StateMap b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> StateMap b -> StateMap a
$c<$ :: forall a b. a -> StateMap b -> StateMap a
fmap :: (a -> b) -> StateMap a -> StateMap b
$cfmap :: forall a b. (a -> b) -> StateMap a -> StateMap b
Functor)

emptyMap :: StateMap a
emptyMap :: StateMap a
emptyMap = IntMap a -> StateMap a
forall a. IntMap a -> StateMap a
StateMap IntMap a
forall a. IntMap a
IntMap.empty

insertMap :: StateNum -> a -> StateMap a -> StateMap a
insertMap :: StateNum -> a -> StateMap a -> StateMap a
insertMap (StateNum Int
k) a
x (StateMap IntMap a
m) = IntMap a -> StateMap a
forall a. IntMap a -> StateMap a
StateMap do Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
k a
x IntMap a
m

insertOrUpdateMap :: StateNum -> a -> (a -> a) -> StateMap a -> StateMap a
insertOrUpdateMap :: StateNum -> a -> (a -> a) -> StateMap a -> StateMap a
insertOrUpdateMap (StateNum Int
k) ~a
dx ~a -> a
uf (StateMap IntMap a
m) = IntMap a -> StateMap a
forall a. IntMap a -> StateMap a
StateMap case Int -> IntMap a -> Maybe a
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
k IntMap a
m of
    Maybe a
Nothing -> Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
k a
dx IntMap a
m
    Just a
x  -> Int -> a -> IntMap a -> IntMap a
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
k (a -> a
uf a
x) IntMap a
m

lookupMap :: StateNum -> StateMap a -> Maybe a
lookupMap :: StateNum -> StateMap a -> Maybe a
lookupMap (StateNum Int
sn) (StateMap IntMap a
m) = Int -> IntMap a -> Maybe a
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
sn IntMap a
m

assocsMap :: StateMap a -> [(StateNum, a)]
assocsMap :: StateMap a -> [(StateNum, a)]
assocsMap (StateMap IntMap a
m) = [(Int, a)] -> [(StateNum, a)]
coerce do IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
IntMap.assocs IntMap a
m


newtype StateArray a = StateArray (Array.Array Int a)
    deriving (StateArray a -> StateArray a -> Bool
(StateArray a -> StateArray a -> Bool)
-> (StateArray a -> StateArray a -> Bool) -> Eq (StateArray a)
forall a. Eq a => StateArray a -> StateArray a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StateArray a -> StateArray a -> Bool
$c/= :: forall a. Eq a => StateArray a -> StateArray a -> Bool
== :: StateArray a -> StateArray a -> Bool
$c== :: forall a. Eq a => StateArray a -> StateArray a -> Bool
Eq, Int -> StateArray a -> ShowS
[StateArray a] -> ShowS
StateArray a -> String
(Int -> StateArray a -> ShowS)
-> (StateArray a -> String)
-> ([StateArray a] -> ShowS)
-> Show (StateArray a)
forall a. Show a => Int -> StateArray a -> ShowS
forall a. Show a => [StateArray a] -> ShowS
forall a. Show a => StateArray a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StateArray a] -> ShowS
$cshowList :: forall a. Show a => [StateArray a] -> ShowS
show :: StateArray a -> String
$cshow :: forall a. Show a => StateArray a -> String
showsPrec :: Int -> StateArray a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> StateArray a -> ShowS
Show, a -> StateArray b -> StateArray a
(a -> b) -> StateArray a -> StateArray b
(forall a b. (a -> b) -> StateArray a -> StateArray b)
-> (forall a b. a -> StateArray b -> StateArray a)
-> Functor StateArray
forall a b. a -> StateArray b -> StateArray a
forall a b. (a -> b) -> StateArray a -> StateArray b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> StateArray b -> StateArray a
$c<$ :: forall a b. a -> StateArray b -> StateArray a
fmap :: (a -> b) -> StateArray a -> StateArray b
$cfmap :: forall a b. (a -> b) -> StateArray a -> StateArray b
Functor, StateArray a -> Bool
(a -> m) -> StateArray a -> m
(a -> b -> b) -> b -> StateArray a -> b
(forall m. Monoid m => StateArray m -> m)
-> (forall m a. Monoid m => (a -> m) -> StateArray a -> m)
-> (forall m a. Monoid m => (a -> m) -> StateArray a -> m)
-> (forall a b. (a -> b -> b) -> b -> StateArray a -> b)
-> (forall a b. (a -> b -> b) -> b -> StateArray a -> b)
-> (forall b a. (b -> a -> b) -> b -> StateArray a -> b)
-> (forall b a. (b -> a -> b) -> b -> StateArray a -> b)
-> (forall a. (a -> a -> a) -> StateArray a -> a)
-> (forall a. (a -> a -> a) -> StateArray a -> a)
-> (forall a. StateArray a -> [a])
-> (forall a. StateArray a -> Bool)
-> (forall a. StateArray a -> Int)
-> (forall a. Eq a => a -> StateArray a -> Bool)
-> (forall a. Ord a => StateArray a -> a)
-> (forall a. Ord a => StateArray a -> a)
-> (forall a. Num a => StateArray a -> a)
-> (forall a. Num a => StateArray a -> a)
-> Foldable StateArray
forall a. Eq a => a -> StateArray a -> Bool
forall a. Num a => StateArray a -> a
forall a. Ord a => StateArray a -> a
forall m. Monoid m => StateArray m -> m
forall a. StateArray a -> Bool
forall a. StateArray a -> Int
forall a. StateArray a -> [a]
forall a. (a -> a -> a) -> StateArray a -> a
forall m a. Monoid m => (a -> m) -> StateArray a -> m
forall b a. (b -> a -> b) -> b -> StateArray a -> b
forall a b. (a -> b -> b) -> b -> StateArray a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: StateArray a -> a
$cproduct :: forall a. Num a => StateArray a -> a
sum :: StateArray a -> a
$csum :: forall a. Num a => StateArray a -> a
minimum :: StateArray a -> a
$cminimum :: forall a. Ord a => StateArray a -> a
maximum :: StateArray a -> a
$cmaximum :: forall a. Ord a => StateArray a -> a
elem :: a -> StateArray a -> Bool
$celem :: forall a. Eq a => a -> StateArray a -> Bool
length :: StateArray a -> Int
$clength :: forall a. StateArray a -> Int
null :: StateArray a -> Bool
$cnull :: forall a. StateArray a -> Bool
toList :: StateArray a -> [a]
$ctoList :: forall a. StateArray a -> [a]
foldl1 :: (a -> a -> a) -> StateArray a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> StateArray a -> a
foldr1 :: (a -> a -> a) -> StateArray a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> StateArray a -> a
foldl' :: (b -> a -> b) -> b -> StateArray a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> StateArray a -> b
foldl :: (b -> a -> b) -> b -> StateArray a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> StateArray a -> b
foldr' :: (a -> b -> b) -> b -> StateArray a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> StateArray a -> b
foldr :: (a -> b -> b) -> b -> StateArray a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> StateArray a -> b
foldMap' :: (a -> m) -> StateArray a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> StateArray a -> m
foldMap :: (a -> m) -> StateArray a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> StateArray a -> m
fold :: StateArray m -> m
$cfold :: forall m. Monoid m => StateArray m -> m
Foldable)

totalStateMapToArray :: StateNum -> StateMap a -> StateArray a
totalStateMapToArray :: StateNum -> StateMap a -> StateArray a
totalStateMapToArray (StateNum Int
boundState) (StateMap IntMap a
m) = Array Int a -> StateArray a
forall a. Array Int a -> StateArray a
StateArray
    do (Int, Int) -> [(Int, a)] -> Array Int a
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
Array.array (Int
0, Int -> Int
forall a. Enum a => a -> a
pred Int
boundState) do IntMap a -> [(Int, a)]
forall a. IntMap a -> [(Int, a)]
IntMap.toAscList IntMap a
m

mapArrayWithIx :: (StateNum -> a -> a) -> StateArray a -> StateArray a
mapArrayWithIx :: (StateNum -> a -> a) -> StateArray a -> StateArray a
mapArrayWithIx StateNum -> a -> a
f (StateArray Array Int a
arr) = Array Int a -> StateArray a
forall a. Array Int a -> StateArray a
StateArray
    do (Int, Int) -> [a] -> Array Int a
forall i e. Ix i => (i, i) -> [e] -> Array i e
Array.listArray
        do Array Int a -> (Int, Int)
forall i e. Array i e -> (i, i)
Array.bounds Array Int a
arr
        do [ StateNum -> a -> a
f (Int -> StateNum
StateNum Int
i) a
x | (Int
i, a
x) <- Array Int a -> [(Int, a)]
forall i e. Ix i => Array i e -> [(i, e)]
Array.assocs Array Int a
arr ]

indexArray :: StateArray a -> StateNum -> a
indexArray :: StateArray a -> StateNum -> a
indexArray (StateArray Array Int a
arr) (StateNum Int
i) = Array Int a
arr Array Int a -> Int -> a
forall i e. Ix i => Array i e -> i -> e
Array.! Int
i

arrayAssocs :: StateArray a -> [(StateNum, a)]
arrayAssocs :: StateArray a -> [(StateNum, a)]
arrayAssocs (StateArray Array Int a
arr) = [(Int, a)] -> [(StateNum, a)]
coerce do Array Int a -> [(Int, a)]
forall i e. Ix i => Array i e -> [(i, e)]
Array.assocs Array Int a
arr


newtype StateGraph = StateGraph Graph.Graph

stateArrayToGraph :: StateArray [StateNum] -> StateGraph
stateArrayToGraph :: StateArray [StateNum] -> StateGraph
stateArrayToGraph (StateArray Array Int [StateNum]
m) = Graph -> StateGraph
StateGraph do Array Int [StateNum] -> Graph
coerce Array Int [StateNum]
m

liftGraphOp :: (Graph.Graph -> Graph.Graph) -> StateGraph -> StateGraph
liftGraphOp :: (Graph -> Graph) -> StateGraph -> StateGraph
liftGraphOp Graph -> Graph
f StateGraph
x = (Graph -> Graph) -> StateGraph -> StateGraph
coerce Graph -> Graph
f StateGraph
x

indexGraph :: StateGraph -> StateNum -> [StateNum]
indexGraph :: StateGraph -> StateNum -> [StateNum]
indexGraph (StateGraph Graph
x) (StateNum Int
i) = [Int] -> [StateNum]
coerce do Graph
x Graph -> Int -> [Int]
forall i e. Ix i => Array i e -> i -> e
Array.! Int
i