{-# LANGUAGE CPP #-}

#if __GLASGOW_HASKELL__ >= 902
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
#endif

-- | This is the code for the main engine.  This captures the posix
-- subexpressions.  There is also a non-capturing engine, and a
-- testing engine.
--
-- It is polymorphic over the internal Uncons type class, and
-- specialized to produce the needed variants.

module Text.Regex.TDFA.NewDFA.Engine_FA(execMatch) where

import Data.Array.Base(unsafeRead,unsafeWrite,STUArray(..))

import GHC.Arr(STArray(..))
import GHC.ST(ST(..))
import GHC.Exts(MutableByteArray#,RealWorld,Int#,sizeofMutableByteArray#,unsafeCoerce#,State#)

import Prelude hiding ((!!))
import Control.Monad(when,unless,forM,forM_,liftM2,foldM)
import Data.Array.MArray(MArray(..))
import Data.Array.Unsafe(unsafeFreeze)
import Data.Array.IArray(Array,bounds,assocs,Ix(range))
import qualified Data.IntMap.CharMap2 as CMap(findWithDefault)
import Data.IntMap(IntMap)
import qualified Data.IntMap as IMap(null,toList,lookup,insert)
import Data.Maybe(catMaybes)
import Data.Monoid as Mon(Monoid(..))
import Data.IntSet(IntSet)
import qualified Data.IntSet as ISet(toAscList,null)
import Data.Array.IArray((!))
import Data.List(sortBy,groupBy)
import Data.STRef(STRef,newSTRef,readSTRef,writeSTRef)
import qualified Control.Monad.ST.Strict as S(ST,runST)
import Data.Sequence(Seq,ViewL(..),viewl)
import qualified Data.Sequence as Seq(null)
import qualified Data.ByteString.Char8 as SBS(ByteString)
import qualified Data.ByteString.Lazy.Char8 as LBS(ByteString)
import Foreign.Ptr(Ptr)

import Text.Regex.Base(MatchArray,MatchOffset,MatchLength)
import Text.Regex.TDFA.Common hiding (indent)
import Text.Regex.TDFA.NewDFA.Uncons(Uncons(uncons))
import Text.Regex.TDFA.NewDFA.MakeTest(test_singleline,test_multiline)

--import Debug.Trace

-- trace :: String -> a -> a
-- trace _ a = a

err :: String -> a
err :: forall a. [Char] -> a
err [Char]
s = forall a. [Char] -> [Char] -> a
common_error [Char]
"Text.Regex.TDFA.NewDFA.Engine_FA"  [Char]
s

{-# INLINE (!!) #-}
(!!) :: (MArray a e (S.ST s),Ix i) => a i e -> Int -> S.ST s e
!! :: forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
(!!) = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Position -> m e
unsafeRead
{-# INLINE set #-}
set :: (MArray a e (S.ST s),Ix i) => a i e -> Int -> e -> S.ST s ()
set :: forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> Position -> e -> m ()
unsafeWrite

noSource :: ((Index, Instructions),STUArray s Tag Position,OrbitLog)
noSource :: forall s.
((Position, Instructions), STUArray s Position Position, OrbitLog)
noSource = ((-Position
1,forall a. [Char] -> a
err [Char]
"noSource"),forall a. [Char] -> a
err [Char]
"noSource",forall a. [Char] -> a
err [Char]
"noSource")

{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> ([] Char) -> [MatchArray] #-}
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> (Seq Char) -> [MatchArray] #-}
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> SBS.ByteString -> [MatchArray] #-}
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> LBS.ByteString -> [MatchArray] #-}
execMatch :: forall text. Uncons text => Regex -> Position -> Char -> text -> [MatchArray]
execMatch :: forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
execMatch (Regex { regex_dfa :: Regex -> DFA
regex_dfa =  DFA {d_id :: DFA -> SetIndex
d_id=SetIndex
didIn,d_dt :: DFA -> DT
d_dt=DT
dtIn}
                 , regex_init :: Regex -> Position
regex_init = Position
startState
                 , regex_b_index :: Regex -> (Position, Position)
regex_b_index = (Position, Position)
b_index
                 , regex_b_tags :: Regex -> (Position, Position)
regex_b_tags = (Position, Position)
b_tags_all
                 , regex_tags :: Regex -> Array Position OP
regex_tags = Array Position OP
aTags
                 , regex_groups :: Regex -> Array Position [GroupInfo]
regex_groups = Array Position [GroupInfo]
aGroups
                 , regex_compOptions :: Regex -> CompOption
regex_compOptions = CompOption { multiline :: CompOption -> Bool
multiline = Bool
newline } } )
          Position
offsetIn Char
prevIn text
inputIn = forall a. (forall s. ST s a) -> a
S.runST forall s. ST s [MatchArray]
goNext where

  b_tags :: (Tag,Tag)
  !b_tags :: (Position, Position)
b_tags = (Position, Position)
b_tags_all

  orbitTags :: [Tag]
  !orbitTags :: [Position]
orbitTags = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter ((OP
Orbitforall a. Eq a => a -> a -> Bool
==)forall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> [(i, e)]
assocs forall a b. (a -> b) -> a -> b
$ Array Position OP
aTags

  test :: WhichTest -> Index -> Char -> text -> Bool
  !test :: WhichTest -> Position -> Char -> text -> Bool
test = forall text.
Uncons text =>
Bool -> WhichTest -> Position -> Char -> text -> Bool
mkTest Bool
newline

  comp :: C s
  comp :: forall s. C s
comp = {-# SCC "matchHere.comp" #-} forall s. Array Position OP -> C s
ditzyComp'3 Array Position OP
aTags

  goNext :: forall s. ST s [MatchArray]
  goNext :: forall s. ST s [MatchArray]
goNext = {-# SCC "goNext" #-} do
    (SScratch MScratch s
s1In MScratch s
s2In (MQ s
winQ,BlankScratch s
blank,STArray
  s
  Position
  ((Position, Instructions), STUArray s Position Position, OrbitLog)
which)) <- forall s.
(Position, Position) -> (Position, Position) -> ST s (SScratch s)
newScratch (Position, Position)
b_index (Position, Position)
b_tags
    forall s.
(Position, Position)
-> BlankScratch s -> Position -> MScratch s -> Position -> ST s ()
spawnAt (Position, Position)
b_tags BlankScratch s
blank Position
startState MScratch s
s1In Position
offsetIn
    let next :: MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
dt Position
offset Char
prev text
input = {-# SCC "goNext.next" #-}
          case DT
dt of
            Testing' {dt_test :: DT -> WhichTest
dt_test=WhichTest
wt,dt_a :: DT -> DT
dt_a=DT
a,dt_b :: DT -> DT
dt_b=DT
b} ->
              if WhichTest -> Position -> Char -> text -> Bool
test WhichTest
wt Position
offset Char
prev text
input
                then MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
a Position
offset Char
prev text
input
                else MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1 MScratch s
s2 SetIndex
did DT
b Position
offset Char
prev text
input
            Simple' {dt_win :: DT -> IntMap Instructions
dt_win=IntMap Instructions
w,dt_trans :: DT -> CharMap Transition
dt_trans=CharMap Transition
t,dt_other :: DT -> Transition
dt_other=Transition
o} -> do
              forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall a. IntMap a -> Bool
IMap.null IntMap Instructions
w) forall a b. (a -> b) -> a -> b
$
                MScratch s -> IntMap Instructions -> Position -> ST s ()
processWinner MScratch s
s1 IntMap Instructions
w Position
offset
              case forall a. Uncons a => a -> Maybe (Char, a)
uncons text
input of
                Maybe (Char, text)
Nothing -> ST s [MatchArray]
finalizeWinner
                Just (Char
c,text
input') ->
                  case forall a. a -> Char -> CharMap a -> a
CMap.findWithDefault Transition
o Char
c CharMap Transition
t of
                    Transition {trans_single :: Transition -> DFA
trans_single=DFA {d_id :: DFA -> SetIndex
d_id=SetIndex
did',d_dt :: DFA -> DT
d_dt=DT
dt'},trans_how :: Transition -> DTrans
trans_how=DTrans
dtrans}
                      | SetIndex -> Bool
ISet.null SetIndex
did' -> ST s [MatchArray]
finalizeWinner
                      | Bool
otherwise -> MScratch s
-> MScratch s
-> SetIndex
-> SetIndex
-> DT
-> DTrans
-> Position
-> Char
-> text
-> ST s [MatchArray]
findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Position
offset Char
c text
input'

-- compressOrbits gets all the current Tag-0 start information from
-- the NFA states; then it loops through all the Orbit tags with
-- compressOrbit.
--
-- compressOrbit on such a Tag loops through all the NFS states'
-- m_orbit record, discarding ones that are Nothing and discarding
-- ones that are too new to care about (after the cutoff value).
--
-- compressOrbit then groups the Orbits records by the Tag-0 start
-- position and the basePos position.  Entries in different groups
-- will never be comparable in the future so they can be processed
-- separately.  Groups could probably be even more finely
-- distinguished, as a further optimization, but the justification will
-- be tricky.
--
-- Current Tag-0 values are at most offset and all newly spawned
-- groups will have Tag-0 of at least (succ offset) so the current
-- groups are closed to those spawned in the future.  The basePos may
-- be as large as offset and may be overwritten later with values of
-- offset or larger (and this will also involve deleting the Orbits
-- record).  Thus there could be a future collision between a current
-- group with basePos==offset and an updated record that acquires
-- basePos==offset.  By excluding groups with basePos before the
-- current offset the collision between existing and future records
-- is avoided.
--
-- An entry in a group can only collide with that group's
-- descendants. compressOrbit sends each group to the compressGroup
-- command.
--
-- compressGroup on a single record checks whether it's Seq can be
-- cleared and if so it will clear it (and set ordinal to Nothing but
-- this this not particularly important).
--
-- compressGroup on many records sorts and groups the members and zips
-- the groups with their new ordinal value.  The comparison is based
-- on the old ordinal value, then the inOrbit value, and then the (Seq
-- Position) data.
--
-- The old ordinals of the group will all be Nothing or all be Just,
-- but this condition is neither checked nor violations detected.
-- This comparison is justified because once records get different
-- ordinals assigned they will never change places.
--
-- The inOrbit Bool is only different if one of them has set the stop
-- position to at most (succ offset).  They will obly be compared if
-- the other one leaves, an its stop position will be at least offset.
-- The previous sentence is justified by inspection of the "assemble"
-- function in the TDFA module: there is no (PostUpdate
-- LeaveOrbitTask) so the largest possible value for the stop Tag is
-- (pred offset). Thus the record with inOrbit==False would beat (be
-- GT than) the record with inOrbit==True.
--
-- The Seq comparison is safe because the largest existing Position
-- value is (pred offset) and the smallest future Position value is
-- offset.  The previous sentence is justified by inspection of the
-- "assemble" function in the TDFA module: there is no (PostUpdate
-- EnterOrbitTags) so the largest possible value in the Seq is (pred
-- offset).
--
-- The updated Orbits get the new ordinal value and an empty (Seq
-- Position).

        compressOrbits :: MScratch s -> IntSet -> Position -> ST s ()
        compressOrbits :: MScratch s -> SetIndex -> Position -> ST s ()
compressOrbits MScratch s
s1 SetIndex
did Position
offset = do
          let getStart :: Position -> ST s (Position, Position)
getStart Position
state = do Position
start <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err [Char]
"compressOrbit,1") (forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state
                                  forall (m :: * -> *) a. Monad m => a -> m a
return (Position
state,Position
start)
              cutoff :: Position
cutoff = Position
offset forall a. Num a => a -> a -> a
- Position
50 -- Require: cutoff <= offset, MAGIC TUNABLE CONSTANT 50
          [(Position, Position)]
ss <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {s}.
(MArray (STUArray s) Position (ST s),
 MArray
   (STArray s) (Maybe (STUArray s Position Position)) (ST s)) =>
Position -> ST s (Position, Position)
getStart (SetIndex -> [Position]
ISet.toAscList SetIndex
did)
          let compressOrbit :: Position -> ST s ()
compressOrbit Position
tag = do
                [Maybe ((Position, Position), Orbits)]
mos <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Position, Position)]
ss ( \ p :: (Position, Position)
p@(Position
state,Position
_start) -> do
                                  Maybe Orbits
mo <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
tag) (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state)
                                  case Maybe Orbits
mo of
                                    Just Orbits
orbits | Orbits -> Position
basePos Orbits
orbits forall a. Ord a => a -> a -> Bool
< Position
cutoff -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just ((Position, Position)
p,Orbits
orbits))
                                                | Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
                                    Maybe Orbits
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing )
                let compressGroup :: [((Position, b), Orbits)] -> ST s ()
compressGroup [((Position
state,b
_),Orbits
orbit)] | forall a. Seq a -> Bool
Seq.null (Orbits -> Seq Position
getOrbits Orbits
orbit) = forall (m :: * -> *) a. Monad m => a -> m a
return ()
                                                      | Bool
otherwise =
                      forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1) Position
state
                      forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Position -> a -> IntMap a -> IntMap a
IMap.insert Position
tag forall a b. (a -> b) -> a -> b
$! (Orbits
orbit { ordinal :: Maybe Position
ordinal = forall a. Maybe a
Nothing, getOrbits :: Seq Position
getOrbits = forall a. Monoid a => a
mempty}))
                      forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state

                    compressGroup [((Position, b), Orbits)]
gs = do
                      let sortPos :: (a, Orbits) -> (a, Orbits) -> Ordering
sortPos (a
_,Orbits
b1) (a
_,Orbits
b2) = forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Maybe Position
ordinal Orbits
b1) (Orbits -> Maybe Position
ordinal Orbits
b2) forall a. Monoid a => a -> a -> a
`mappend`
                                                  forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Bool
inOrbit Orbits
b2) (Orbits -> Bool
inOrbit Orbits
b1) forall a. Monoid a => a -> a -> a
`mappend`
                                                  ViewL Position -> ViewL Position -> Ordering
comparePos (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
b1)) (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
b2))
                          groupPos :: (a, Orbits) -> (a, Orbits) -> Bool
groupPos (a
_,Orbits
b1) (a
_,Orbits
b2) = Orbits -> Maybe Position
ordinal Orbits
b1 forall a. Eq a => a -> a -> Bool
== Orbits -> Maybe Position
ordinal Orbits
b2 Bool -> Bool -> Bool
&& Orbits -> Seq Position
getOrbits Orbits
b1 forall a. Eq a => a -> a -> Bool
== Orbits -> Seq Position
getOrbits Orbits
b2
                          gs' :: [(Position, [((Position, b), Orbits)])]
gs' = forall a b. [a] -> [b] -> [(a, b)]
zip [(Position
1::Int)..] (forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy forall {a} {a}. (a, Orbits) -> (a, Orbits) -> Bool
groupPos forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy forall {a} {a}. (a, Orbits) -> (a, Orbits) -> Ordering
sortPos forall a b. (a -> b) -> a -> b
$ [((Position, b), Orbits)]
gs)
                      forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Position, [((Position, b), Orbits)])]
gs' forall a b. (a -> b) -> a -> b
$ \ (!Position
n,[((Position, b), Orbits)]
eqs) -> do
                        forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [((Position, b), Orbits)]
eqs forall a b. (a -> b) -> a -> b
$ \ ((Position
state,b
_),Orbits
orbit) ->
                          forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1) Position
state
                           forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Position -> a -> IntMap a -> IntMap a
IMap.insert Position
tag forall a b. (a -> b) -> a -> b
$! (Orbits
orbit { ordinal :: Maybe Position
ordinal = forall a. a -> Maybe a
Just Position
n, getOrbits :: Seq Position
getOrbits = forall a. Monoid a => a
mempty }))
                            forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
state
                let sorter :: ((a, a), Orbits) -> ((a, a), Orbits) -> Ordering
sorter ((a
_,a
a1),Orbits
b1) ((a
_,a
a2),Orbits
b2) = forall a. Ord a => a -> a -> Ordering
compare a
a1 a
a2 forall a. Monoid a => a -> a -> a
`mappend` forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Position
basePos Orbits
b1) (Orbits -> Position
basePos Orbits
b2)
                    grouper :: ((a, a), Orbits) -> ((a, a), Orbits) -> Bool
grouper ((a
_,a
a1),Orbits
b1) ((a
_,a
a2),Orbits
b2) = a
a1forall a. Eq a => a -> a -> Bool
==a
a2 Bool -> Bool -> Bool
&& Orbits -> Position
basePos Orbits
b1 forall a. Eq a => a -> a -> Bool
== Orbits -> Position
basePos Orbits
b2
                    orbitGroups :: [[((Position, Position), Orbits)]]
orbitGroups = forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy forall {a} {a} {a}.
Eq a =>
((a, a), Orbits) -> ((a, a), Orbits) -> Bool
grouper forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy forall {a} {a} {a}.
Ord a =>
((a, a), Orbits) -> ((a, a), Orbits) -> Ordering
sorter forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [Maybe a] -> [a]
catMaybes forall a b. (a -> b) -> a -> b
$ [Maybe ((Position, Position), Orbits)]
mos
                forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {s} {b}.
MArray (STArray s) OrbitLog (ST s) =>
[((Position, b), Orbits)] -> ST s ()
compressGroup [[((Position, Position), Orbits)]]
orbitGroups
          forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {s}.
MArray (STArray s) OrbitLog (ST s) =>
Position -> ST s ()
compressOrbit [Position]
orbitTags

-- findTrans has to (part 1) decide, for each destination, "which" of
-- zero or more source NFA states will be the chosen source.  Then it
-- has to (part 2) perform the transition or spawn.  It keeps track of
-- the starting index while doing so, and compares the earliest start
-- with the stored winners.  (part 3) If some winners are ready to be
-- released then the future continuation of the search is placed in
-- "storeNext".  If no winners are ready to be released then the
-- computation continues immediately.

        findTrans
          :: MScratch s
          -> MScratch s
          -> IntSet
          -> SetIndex
          -> DT
          -> DTrans
          -> Index
          -> Char
          -> text
          -> ST s [MatchArray]
        findTrans :: MScratch s
-> MScratch s
-> SetIndex
-> SetIndex
-> DT
-> DTrans
-> Position
-> Char
-> text
-> ST s [MatchArray]
findTrans MScratch s
s1 MScratch s
s2 SetIndex
did SetIndex
did' DT
dt' DTrans
dtrans Position
offset Char
prev' text
input' =  {-# SCC "goNext.findTrans" #-} do
          -- findTrans part 0
          -- MAGIC TUNABLE CONSTANT 100 (and 100-1). TODO: (offset .&. 127 == 127) instead?
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Position]
orbitTags) Bool -> Bool -> Bool
&& (Position
offset forall a. Integral a => a -> a -> a
`rem` Position
100 forall a. Eq a => a -> a -> Bool
== Position
99)) (MScratch s -> SetIndex -> Position -> ST s ()
compressOrbits MScratch s
s1 SetIndex
did Position
offset)
          -- findTrans part 1
          let findTransTo :: (Position, IntMap (a, Instructions)) -> ST s ()
findTransTo (Position
destIndex,IntMap (a, Instructions)
sources) | forall a. IntMap a -> Bool
IMap.null IntMap (a, Instructions)
sources =
                forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray
  s
  Position
  ((Position, Instructions), STUArray s Position Position, OrbitLog)
which Position
destIndex forall s.
((Position, Instructions), STUArray s Position Position, OrbitLog)
noSource
                                              | Bool
otherwise = do
                let prep :: (Position, (a, Instructions))
-> ST
     s
     ((Position, Instructions), STUArray s Position Position, OrbitLog)
prep (Position
sourceIndex,(a
_dopa,Instructions
instructions)) = {-# SCC "goNext.findTrans.prep" #-} do
                      STUArray s Position Position
pos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err forall a b. (a -> b) -> a -> b
$ [Char]
"findTrans,1 : "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show (Position
sourceIndex,Position
destIndex,SetIndex
did')) forall (m :: * -> *) a. Monad m => a -> m a
return
                               forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
                      OrbitLog
orbit <- forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
                      let orbit' :: OrbitLog
orbit' = forall b a. b -> (a -> b) -> Maybe a -> b
maybe OrbitLog
orbit (\ Position -> OrbitLog -> OrbitLog
f -> Position -> OrbitLog -> OrbitLog
f Position
offset OrbitLog
orbit) (Instructions -> Maybe (Position -> OrbitLog -> OrbitLog)
newOrbits Instructions
instructions)
                      forall (m :: * -> *) a. Monad m => a -> m a
return ((Position
sourceIndex,Instructions
instructions),STUArray s Position Position
pos,OrbitLog
orbit')
                    challenge :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
    OrbitLog)
-> ST
     s
     ((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position
_si1,Instructions
ins1),STUArray s Position Position
_p1,OrbitLog
_o1) x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position
_si2,Instructions
ins2),STUArray s Position Position
_p2,OrbitLog
_o2) = {-# SCC "goNext.findTrans.challenge" #-} do
                      Ordering
check <- forall s. C s
comp Position
offset ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 (Instructions -> [(Position, Action)]
newPos Instructions
ins1) ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 (Instructions -> [(Position, Action)]
newPos Instructions
ins2)
                      if Ordering
checkforall a. Eq a => a -> a -> Bool
==Ordering
LT then forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 else forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1
                [((Position, Instructions), STUArray s Position Position,
  OrbitLog)]
first_rest <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {s} {a}.
(MArray (STArray s) (Maybe (STUArray s Position Position)) (ST s),
 MArray (STArray s) OrbitLog (ST s)) =>
(Position, (a, Instructions))
-> ST
     s
     ((Position, Instructions), STUArray s Position Position, OrbitLog)
prep (forall a. IntMap a -> [(Position, a)]
IMap.toList IntMap (a, Instructions)
sources)
                let ((Position, Instructions), STUArray s Position Position, OrbitLog)
first:[((Position, Instructions), STUArray s Position Position,
  OrbitLog)]
rest = [((Position, Instructions), STUArray s Position Position,
  OrbitLog)]
first_rest
                forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray
  s
  Position
  ((Position, Instructions), STUArray s Position Position, OrbitLog)
which Position
destIndex forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {s}.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
    OrbitLog)
-> ST
     s
     ((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge ((Position, Instructions), STUArray s Position Position, OrbitLog)
first [((Position, Instructions), STUArray s Position Position,
  OrbitLog)]
rest
          let dl :: [(Position, IntMap (DoPa, Instructions))]
dl = forall a. IntMap a -> [(Position, a)]
IMap.toList DTrans
dtrans
          forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {a}. (Position, IntMap (a, Instructions)) -> ST s ()
findTransTo [(Position, IntMap (DoPa, Instructions))]
dl
          -- findTrans part 2
          let performTransTo :: (Position, b) -> ST s ()
performTransTo (Position
destIndex,b
_sources) = {-# SCC "goNext.findTrans.performTransTo" #-} do
                x :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x@((Position
sourceIndex,Instructions
_instructions),STUArray s Position Position
_pos,OrbitLog
_orbit') <- STArray
  s
  Position
  ((Position, Instructions), STUArray s Position Position, OrbitLog)
which forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
destIndex
                forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Position
sourceIndex forall a. Eq a => a -> a -> Bool
== (-Position
1)) forall a b. (a -> b) -> a -> b
$
                  (forall s.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> Position -> MScratch s -> Position -> ST s ()
updateCopy ((Position, Instructions), STUArray s Position Position, OrbitLog)
x Position
offset MScratch s
s2 Position
destIndex)
          forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {b}. (Position, b) -> ST s ()
performTransTo [(Position, IntMap (DoPa, Instructions))]
dl
          -- findTrans part 3
          let offset' :: Position
offset' = forall a. Enum a => a -> a
succ Position
offset in seq :: forall a b. a -> b -> b
seq Position
offset' forall a b. (a -> b) -> a -> b
$ MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s2 MScratch s
s1 SetIndex
did' DT
dt' Position
offset' Char
prev' text
input'

-- The "newWinnerThenProceed" can find both a new non-empty winner and
-- a new empty winner.  A new non-empty winner can cause some of the
-- NFA states that comprise the DFA state to be eliminated, and if the
-- startState is eliminated then it must then be respawned.  And
-- imperative flag setting and resetting style is used.
--
-- A non-empty winner from the startState might obscure a potential
-- empty winner (form the startState at the current offset).  This
-- winEmpty possibility is also checked for. (unit test pattern ".*")
-- (further test "(.+|.+.)*" on "aa\n")

        {-# INLINE processWinner #-}
        processWinner :: MScratch s -> IntMap Instructions -> Position -> ST s ()
        processWinner :: MScratch s -> IntMap Instructions -> Position -> ST s ()
processWinner MScratch s
s1 IntMap Instructions
w Position
offset = {-# SCC "goNext.newWinnerThenProceed" #-} do
          let prep :: (Position, Instructions)
-> ST
     s
     (Position,
      ((Position, Instructions), STUArray s Position Position, OrbitLog))
prep x :: (Position, Instructions)
x@(Position
sourceIndex,Instructions
instructions) = {-# SCC "goNext.newWinnerThenProceed.prep" #-} do
                STUArray s Position Position
pos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. [Char] -> a
err [Char]
"newWinnerThenProceed,1") forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
                Position
startPos <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
                OrbitLog
orbit <- forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
sourceIndex
                let orbit' :: OrbitLog
orbit' = forall b a. b -> (a -> b) -> Maybe a -> b
maybe OrbitLog
orbit (\ Position -> OrbitLog -> OrbitLog
f -> Position -> OrbitLog -> OrbitLog
f Position
offset OrbitLog
orbit) (Instructions -> Maybe (Position -> OrbitLog -> OrbitLog)
newOrbits Instructions
instructions)
                forall (m :: * -> *) a. Monad m => a -> m a
return (Position
startPos,((Position, Instructions)
x,STUArray s Position Position
pos,OrbitLog
orbit'))
              challenge :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
    OrbitLog)
-> ST
     s
     ((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position
_si1,Instructions
ins1),STUArray s Position Position
_p1,OrbitLog
_o1) x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position
_si2,Instructions
ins2),STUArray s Position Position
_p2,OrbitLog
_o2) = {-# SCC "goNext.newWinnerThenProceed.challenge" #-} do
                Ordering
check <- forall s. C s
comp Position
offset ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 (Instructions -> [(Position, Action)]
newPos Instructions
ins1) ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 (Instructions -> [(Position, Action)]
newPos Instructions
ins2)
                if Ordering
checkforall a. Eq a => a -> a -> Bool
==Ordering
LT then forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 else forall (m :: * -> *) a. Monad m => a -> m a
return ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1
          [(Position,
  ((Position, Instructions), STUArray s Position Position,
   OrbitLog))]
prep'd <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {s}.
(MArray (STUArray s) Position (ST s),
 MArray (STArray s) (Maybe (STUArray s Position Position)) (ST s),
 MArray (STArray s) OrbitLog (ST s)) =>
(Position, Instructions)
-> ST
     s
     (Position,
      ((Position, Instructions), STUArray s Position Position, OrbitLog))
prep (forall a. IntMap a -> [(Position, a)]
IMap.toList IntMap Instructions
w)
          case forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(Position,
  ((Position, Instructions), STUArray s Position Position,
   OrbitLog))]
prep'd of
            [] -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
            (((Position, Instructions), STUArray s Position Position, OrbitLog)
first:[((Position, Instructions), STUArray s Position Position,
  OrbitLog)]
rest) -> forall a c.
Position
-> ((a, Instructions), STUArray s Position Position, c) -> ST s ()
newWinner Position
offset forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM forall {s}.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> ((Position, Instructions), STUArray s Position Position,
    OrbitLog)
-> ST
     s
     ((Position, Instructions), STUArray s Position Position, OrbitLog)
challenge ((Position, Instructions), STUArray s Position Position, OrbitLog)
first [((Position, Instructions), STUArray s Position Position,
  OrbitLog)]
rest

        newWinner :: Position -> ((a, Instructions), STUArray s Tag Position, c) -> ST s ()
        newWinner :: forall a c.
Position
-> ((a, Instructions), STUArray s Position Position, c) -> ST s ()
newWinner Position
preTag ((a
_sourceIndex,Instructions
winInstructions),STUArray s Position Position
oldPos,c
_newOrbit) = {-# SCC "goNext.newWinner" #-} do
          STUArray s Position Position
newerPos <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
          forall i s e.
(Show i, Ix i, MArray (STUArray s) e (ST s)) =>
STUArray s i e -> STUArray s i e -> ST s ()
copySTU STUArray s Position Position
oldPos STUArray s Position Position
newerPos
          forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
newerPos (Instructions -> [(Position, Action)]
newPos Instructions
winInstructions)
          forall s. WScratch s -> MQ s -> ST s ()
putMQ (forall s. STUArray s Position Position -> WScratch s
WScratch STUArray s Position Position
newerPos) MQ s
winQ

        finalizeWinner :: ST s [MatchArray]
        finalizeWinner :: ST s [MatchArray]
finalizeWinner = do
          Maybe (WScratch s)
mWinner <- forall s a. STRef s a -> ST s a
readSTRef (forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin MQ s
winQ)
          case Maybe (WScratch s)
mWinner of
            Maybe (WScratch s)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return []
            Just WScratch s
winner -> forall s. MQ s -> ST s ()
resetMQ MQ s
winQ forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall s.
Array Position [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Position [GroupInfo]
aGroups) [WScratch s
winner]

    -- goNext then ends with the next statement
    MScratch s
-> MScratch s
-> SetIndex
-> DT
-> Position
-> Char
-> text
-> ST s [MatchArray]
next MScratch s
s1In MScratch s
s2In SetIndex
didIn DT
dtIn Position
offsetIn Char
prevIn text
inputIn

{-# INLINE doActions #-}
doActions :: Position -> STUArray s Tag Position -> [(Tag, Action)] -> ST s ()
doActions :: forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
pos [(Position, Action)]
ins = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ forall {s}.
MArray (STUArray s) Position (ST s) =>
(Position, Action) -> ST s ()
doAction [(Position, Action)]
ins where
  postTag :: Position
postTag = forall a. Enum a => a -> a
succ Position
preTag
  doAction :: (Position, Action) -> ST s ()
doAction (Position
tag,Action
SetPre) = forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
tag Position
preTag
  doAction (Position
tag,Action
SetPost) = forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
tag Position
postTag
  doAction (Position
tag,SetVal Position
v) = forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
tag Position
v

----

{-# INLINE mkTest #-}
mkTest :: Uncons text => Bool -> WhichTest -> Index -> Char -> text -> Bool
mkTest :: forall text.
Uncons text =>
Bool -> WhichTest -> Position -> Char -> text -> Bool
mkTest Bool
isMultiline = if Bool
isMultiline then forall text.
Uncons text =>
WhichTest -> Position -> Char -> text -> Bool
test_multiline else forall text.
Uncons text =>
WhichTest -> Position -> Char -> text -> Bool
test_singleline

----

{- MUTABLE WINNER QUEUE -}

newtype MQ s = MQ { forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin :: STRef s (Maybe (WScratch s)) }

newMQ :: S.ST s (MQ s)
newMQ :: forall s. ST s (MQ s)
newMQ = do
  STRef s (Maybe (WScratch s))
mWin <- forall a s. a -> ST s (STRef s a)
newSTRef forall a. Maybe a
Nothing
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall s. STRef s (Maybe (WScratch s)) -> MQ s
MQ STRef s (Maybe (WScratch s))
mWin)

resetMQ :: MQ s -> S.ST s ()
resetMQ :: forall s. MQ s -> ST s ()
resetMQ (MQ {mq_mWin :: forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin=STRef s (Maybe (WScratch s))
mWin}) = do
  forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (Maybe (WScratch s))
mWin forall a. Maybe a
Nothing

putMQ :: WScratch s -> MQ s -> S.ST s ()
putMQ :: forall s. WScratch s -> MQ s -> ST s ()
putMQ WScratch s
ws (MQ {mq_mWin :: forall s. MQ s -> STRef s (Maybe (WScratch s))
mq_mWin=STRef s (Maybe (WScratch s))
mWin}) = do
  forall s a. STRef s a -> a -> ST s ()
writeSTRef STRef s (Maybe (WScratch s))
mWin (forall a. a -> Maybe a
Just WScratch s
ws)

{- MUTABLE SCRATCH DATA STRUCTURES -}

data SScratch s = SScratch { forall s. SScratch s -> MScratch s
_s_1 :: !(MScratch s)
                           , forall s. SScratch s -> MScratch s
_s_2 :: !(MScratch s)
                           , forall s.
SScratch s
-> (MQ s, BlankScratch s,
    STArray
      s
      Position
      ((Position, Instructions), STUArray s Position Position, OrbitLog))
_s_rest :: !( MQ s
                                        , BlankScratch s
                                        , STArray s Index ((Index,Instructions),STUArray s Tag Position,OrbitLog)
                                        )
                           }
data MScratch s = MScratch { forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos :: !(STArray s Index (Maybe (STUArray s Tag Position)))
                           , forall s. MScratch s -> STArray s Position OrbitLog
m_orbit :: !(STArray s Index OrbitLog)
                           }
newtype BlankScratch s = BlankScratch { forall s. BlankScratch s -> STUArray s Position Position
_blank_pos :: (STUArray s Tag Position)
                                      }
newtype WScratch s = WScratch { forall s. WScratch s -> STUArray s Position Position
w_pos :: (STUArray s Tag Position)
                              }

{- DEBUGGING HELPERS -}

{-
indent :: String -> String
indent xs = ' ':' ':xs

showMS :: MScratch s -> Index -> ST s String
showMS s i = do
  ma <- m_pos s !! i
  mc <- m_orbit s !! i
  a <- case ma of
        Nothing -> return "No pos"
        Just pos -> fmap show (getAssocs pos)
  let c = show mc
  return $ unlines [ "MScratch, index = "++show i
                   , indent a
                   , indent c]

showWS :: WScratch s -> ST s String
showWS (WScratch pos) = do
  a <- getAssocs pos
  return $ unlines [ "WScratch"
                   , indent (show a)]
-}
{- CREATING INITIAL MUTABLE SCRATCH DATA STRUCTURES -}

{-# INLINE newA #-}
newA :: (MArray (STUArray s) e (ST s)) => (Tag,Tag) -> e -> S.ST s (STUArray s Tag e)
newA :: forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> e -> ST s (STUArray s Position e)
newA (Position, Position)
b_tags e
initial = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_tags e
initial

{-# INLINE newA_ #-}
newA_ :: (MArray (STUArray s) e (ST s)) => (Tag,Tag) -> S.ST s (STUArray s Tag e)
newA_ :: forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> m (a i e)
newArray_ (Position, Position)
b_tags

newScratch :: (Index,Index) -> (Tag,Tag) -> S.ST s (SScratch s)
newScratch :: forall s.
(Position, Position) -> (Position, Position) -> ST s (SScratch s)
newScratch (Position, Position)
b_index (Position, Position)
b_tags = do
  MScratch s
s1 <- forall s. (Position, Position) -> ST s (MScratch s)
newMScratch (Position, Position)
b_index
  MScratch s
s2 <- forall s. (Position, Position) -> ST s (MScratch s)
newMScratch (Position, Position)
b_index
  MQ s
winQ <- forall s. ST s (MQ s)
newMQ
  BlankScratch s
blank <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s. STUArray s Position Position -> BlankScratch s
BlankScratch (forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> e -> ST s (STUArray s Position e)
newA (Position, Position)
b_tags (-Position
1))
  STArray
  s
  Position
  ((Position, Instructions), STUArray s Position Position, OrbitLog)
which <- (forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_index ((-Position
1,forall a. [Char] -> a
err [Char]
"newScratch which 1"),forall a. [Char] -> a
err [Char]
"newScratch which 2",forall a. [Char] -> a
err [Char]
"newScratch which 3"))
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall s.
MScratch s
-> MScratch s
-> (MQ s, BlankScratch s,
    STArray
      s
      Position
      ((Position, Instructions), STUArray s Position Position, OrbitLog))
-> SScratch s
SScratch MScratch s
s1 MScratch s
s2 (MQ s
winQ,BlankScratch s
blank,STArray
  s
  Position
  ((Position, Instructions), STUArray s Position Position, OrbitLog)
which))

newMScratch :: (Index,Index) -> S.ST s (MScratch s)
newMScratch :: forall s. (Position, Position) -> ST s (MScratch s)
newMScratch (Position, Position)
b_index = do
  STArray s Position (Maybe (STUArray s Position Position))
pos's <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_index forall a. Maybe a
Nothing
  STArray s Position OrbitLog
orbit's <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position, Position)
b_index forall a. Monoid a => a
Mon.mempty
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall s.
STArray s Position (Maybe (STUArray s Position Position))
-> STArray s Position OrbitLog -> MScratch s
MScratch STArray s Position (Maybe (STUArray s Position Position))
pos's STArray s Position OrbitLog
orbit's)

{- COMPOSE A FUNCTION CLOSURE TO COMPARE TAG VALUES -}

newtype F s = F ([F s] -> C s)
type C s = Position
        -> ((Int, Instructions), STUArray s Tag Position, IntMap Orbits)
        -> [(Int, Action)]
        -> ((Int, Instructions), STUArray s Tag Position, IntMap Orbits)
        -> [(Int, Action)]
        -> ST s Ordering

{-# INLINE orderOf #-}
orderOf :: Action -> Action -> Ordering
orderOf :: Action -> Action -> Ordering
orderOf Action
post1 Action
post2 =
  case (Action
post1,Action
post2) of
    (Action
SetPre,Action
SetPre) -> Ordering
EQ
    (Action
SetPost,Action
SetPost) -> Ordering
EQ
    (Action
SetPre,Action
SetPost) -> Ordering
LT
    (Action
SetPost,Action
SetPre) -> Ordering
GT
    (SetVal Position
v1,SetVal Position
v2) -> forall a. Ord a => a -> a -> Ordering
compare Position
v1 Position
v2
    (Action, Action)
_ -> forall a. [Char] -> a
err forall a b. (a -> b) -> a -> b
$ [Char]
"bestTrans.compareWith.choose sees incomparable "forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> [Char]
show (Action
post1,Action
post2)

ditzyComp'3 :: forall s. Array Tag OP -> C s
ditzyComp'3 :: forall s. Array Position OP -> C s
ditzyComp'3 Array Position OP
aTagOP = C s
comp0 where
  (F [F s] -> C s
comp1:[F s]
compsRest) = Position -> [F s]
allcomps Position
1

  comp0 :: C s
  comp0 :: C s
comp0 Position
preTag x1 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1@((Position, Instructions)
_state1,STUArray s Position Position
pos1,OrbitLog
_orbit1') [(Position, Action)]
np1 x2 :: ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2@((Position, Instructions)
_state2,STUArray s Position Position
pos2,OrbitLog
_orbit2') [(Position, Action)]
np2 = do
    Ordering
c <- forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Ord a => a -> a -> Ordering
compare (STUArray s Position Position
pos2forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!!Position
0) (STUArray s Position Position
pos1forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!!Position
0) -- reversed since Minimize
    case Ordering
c of
      Ordering
EQ -> [F s] -> C s
comp1 [F s]
compsRest Position
preTag ((Position, Instructions), STUArray s Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s Position Position, OrbitLog)
x2 [(Position, Action)]
np2
      Ordering
answer -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
answer

  allcomps :: Tag -> [F s]
  allcomps :: Position -> [F s]
allcomps Position
tag | Position
tag forall a. Ord a => a -> a -> Bool
> Position
top = [forall s. ([F s] -> C s) -> F s
F (\ [F s]
_ Position
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s Position Position, OrbitLog)
_ [(Position, Action)]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ)]
               | Bool
otherwise =
    case Array Position OP
aTagOP forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! Position
tag of
      OP
Orbit -> forall s. ([F s] -> C s) -> F s
F (forall s1.
Position
-> [F s1]
-> Position
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ST s1 Ordering
challenge_Orb Position
tag) forall a. a -> [a] -> [a]
: Position -> [F s]
allcomps (forall a. Enum a => a -> a
succ Position
tag)
      OP
Maximize -> forall s. ([F s] -> C s) -> F s
F (forall s1.
Position
-> [F s1]
-> Position
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ST s1 Ordering
challenge_Max Position
tag) forall a. a -> [a] -> [a]
: Position -> [F s]
allcomps (forall a. Enum a => a -> a
succ Position
tag)
      OP
Ignore -> forall s. ([F s] -> C s) -> F s
F (forall s1.
Position
-> [F s1]
-> Position
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ST s1 Ordering
challenge_Ignore Position
tag) forall a. a -> [a] -> [a]
: Position -> [F s]
allcomps (forall a. Enum a => a -> a
succ Position
tag)
      OP
Minimize -> forall a. [Char] -> a
err [Char]
"allcomps Minimize"
   where top :: Position
top = forall a b. (a, b) -> b
snd (forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds Array Position OP
aTagOP)

  challenge_Ignore
    :: Int
    -> [F s1]
    -> Position
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ST s1 Ordering
  challenge_Ignore :: forall s1.
Position
-> [F s1]
-> Position
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ST s1 Ordering
challenge_Ignore !Position
tag (F [F s1] -> C s1
next:[F s1]
comps) Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2 =
    case [(Position, Action)]
np1 of
      ((Position
t1,Action
_):[(Position, Action)]
rest1) | Position
t1forall a. Eq a => a -> a -> Bool
==Position
tag ->
        case [(Position, Action)]
np2 of
          ((Position
t2,Action
_):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag -> [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
          [(Position, Action)]
_ -> [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2
      [(Position, Action)]
_ -> do
        case [(Position, Action)]
np2 of
          ((Position
t2,Action
_):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag -> [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
          [(Position, Action)]
_ ->  [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2
  challenge_Ignore Position
_ [] Position
_ ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
_ [(Position, Action)]
_ = forall a. [Char] -> a
err [Char]
"impossible 2347867"

  challenge_Max
    :: Int
    -> [F s1]
    -> Position
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ST s1 Ordering
  challenge_Max :: forall s1.
Position
-> [F s1]
-> Position
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ST s1 Ordering
challenge_Max !Position
tag (F [F s1] -> C s1
next:[F s1]
comps) Position
preTag x1 :: ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1@((Position, Instructions)
_state1,STUArray s1 Position Position
pos1,OrbitLog
_orbit1') [(Position, Action)]
np1 x2 :: ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2@((Position, Instructions)
_state2,STUArray s1 Position Position
pos2,OrbitLog
_orbit2') [(Position, Action)]
np2 =
    case [(Position, Action)]
np1 of
      ((Position
t1,Action
b1):[(Position, Action)]
rest1) | Position
t1forall a. Eq a => a -> a -> Bool
==Position
tag ->
        case [(Position, Action)]
np2 of
          ((Position
t2,Action
b2):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag ->
            if Action
b1forall a. Eq a => a -> a -> Bool
==Action
b2 then [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
              else forall (m :: * -> *) a. Monad m => a -> m a
return (Action -> Action -> Ordering
orderOf Action
b1 Action
b2)
          [(Position, Action)]
_ -> do
            Position
p2 <- STUArray s1 Position Position
pos2 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
tag
            let p1 :: Position
p1 = case Action
b1 of Action
SetPre -> Position
preTag
                                Action
SetPost -> forall a. Enum a => a -> a
succ Position
preTag
                                SetVal Position
v -> Position
v
            if Position
p1forall a. Eq a => a -> a -> Bool
==Position
p2 then [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
rest1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2
              else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2)
      [(Position, Action)]
_ -> do
        Position
p1 <- STUArray s1 Position Position
pos1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
tag
        case [(Position, Action)]
np2 of
          ((Position
t2,Action
b2):[(Position, Action)]
rest2) | Position
t2forall a. Eq a => a -> a -> Bool
==Position
tag -> do
            let p2 :: Position
p2 = case Action
b2 of Action
SetPre -> Position
preTag
                                Action
SetPost -> forall a. Enum a => a -> a
succ Position
preTag
                                SetVal Position
v -> Position
v
            if Position
p1forall a. Eq a => a -> a -> Bool
==Position
p2 then [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
rest2
              else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2)
          [(Position, Action)]
_ -> do
            Position
p2 <- STUArray s1 Position Position
pos2 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
tag
            if Position
p1forall a. Eq a => a -> a -> Bool
==Position
p2 then [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2
              else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2)
  challenge_Max Position
_ [] Position
_ ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
_ [(Position, Action)]
_ = forall a. [Char] -> a
err [Char]
"impossible 9384324"

  challenge_Orb
    :: Int
    -> [F s1]
    -> Position
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ((Int, Instructions), STUArray s1 Tag Position, IntMap Orbits)
    -> [(Int, Action)]
    -> ST s1 Ordering
  challenge_Orb :: forall s1.
Position
-> [F s1]
-> Position
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ((Position, Instructions), STUArray s1 Position Position,
    OrbitLog)
-> [(Position, Action)]
-> ST s1 Ordering
challenge_Orb !Position
tag (F [F s1] -> C s1
next:[F s1]
comps) Position
preTag x1 :: ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1@((Position, Instructions)
_state1,STUArray s1 Position Position
_pos1,OrbitLog
orbit1') [(Position, Action)]
np1 x2 :: ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2@((Position, Instructions)
_state2,STUArray s1 Position Position
_pos2,OrbitLog
orbit2') [(Position, Action)]
np2 =
    let s1 :: Maybe Orbits
s1 = forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
tag OrbitLog
orbit1'
        s2 :: Maybe Orbits
s2 = forall a. Position -> IntMap a -> Maybe a
IMap.lookup Position
tag OrbitLog
orbit2'
    in case (Maybe Orbits
s1,Maybe Orbits
s2) of
         (Maybe Orbits
Nothing,Maybe Orbits
Nothing) -> [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2
         (Just Orbits
o1,Just Orbits
o2) | Orbits -> Bool
inOrbit Orbits
o1 forall a. Eq a => a -> a -> Bool
== Orbits -> Bool
inOrbit Orbits
o2 ->
            case forall a. Ord a => a -> a -> Ordering
compare (Orbits -> Maybe Position
ordinal Orbits
o1) (Orbits -> Maybe Position
ordinal Orbits
o2) forall a. Monoid a => a -> a -> a
`mappend`
                 ViewL Position -> ViewL Position -> Ordering
comparePos (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
o1)) (forall a. Seq a -> ViewL a
viewl (Orbits -> Seq Position
getOrbits Orbits
o2)) of
              Ordering
EQ -> [F s1] -> C s1
next [F s1]
comps Position
preTag ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x1 [(Position, Action)]
np1 ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
x2 [(Position, Action)]
np2
              Ordering
answer -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
answer
         (Maybe Orbits, Maybe Orbits)
_ -> forall a. [Char] -> a
err forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines [ [Char]
"challenge_Orb is too stupid to handle mismatched orbit data :"
                           , forall a. Show a => a -> [Char]
show(Position
tag,Position
preTag,[(Position, Action)]
np1,[(Position, Action)]
np2)
                           , forall a. Show a => a -> [Char]
show Maybe Orbits
s1
                           , forall a. Show a => a -> [Char]
show Maybe Orbits
s2
                           ]
  challenge_Orb Position
_ [] Position
_ ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
_ [(Position, Action)]
_ ((Position, Instructions), STUArray s1 Position Position, OrbitLog)
_ [(Position, Action)]
_ = forall a. [Char] -> a
err [Char]
"impossible 0298347"

comparePos :: (ViewL Position) -> (ViewL Position) -> Ordering
comparePos :: ViewL Position -> ViewL Position -> Ordering
comparePos ViewL Position
EmptyL ViewL Position
EmptyL = Ordering
EQ
comparePos ViewL Position
EmptyL ViewL Position
_      = Ordering
GT
comparePos ViewL Position
_      ViewL Position
EmptyL = Ordering
LT
comparePos (Position
p1 :< Seq Position
ps1) (Position
p2 :< Seq Position
ps2) =
  forall a. Ord a => a -> a -> Ordering
compare Position
p1 Position
p2 forall a. Monoid a => a -> a -> a
`mappend` ViewL Position -> ViewL Position -> Ordering
comparePos (forall a. Seq a -> ViewL a
viewl Seq Position
ps1) (forall a. Seq a -> ViewL a
viewl Seq Position
ps2)

{- CONVERT WINNERS TO MATCHARRAY -}

tagsToGroupsST :: forall s. Array GroupIndex [GroupInfo] -> WScratch s -> S.ST s MatchArray
tagsToGroupsST :: forall s.
Array Position [GroupInfo] -> WScratch s -> ST s MatchArray
tagsToGroupsST Array Position [GroupInfo]
aGroups (WScratch {w_pos :: forall s. WScratch s -> STUArray s Position Position
w_pos=STUArray s Position Position
pos})= do
  let b_max :: Position
b_max = forall a b. (a, b) -> b
snd (forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds (Array Position [GroupInfo]
aGroups))
  STArray s Position (Position, Position)
ma <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Position
0,Position
b_max) (-Position
1,Position
0) :: ST s (STArray s Int (MatchOffset,MatchLength))
  Position
startPos0 <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
0
  Position
stopPos0 <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
1
  forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray s Position (Position, Position)
ma Position
0 (Position
startPos0,Position
stopPos0forall a. Num a => a -> a -> a
-Position
startPos0)
  let act :: Position -> [GroupInfo] -> ST s ()
act Position
_this_index [] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
      act Position
this_index ((GroupInfo Position
_ Position
parent Position
start Position
stop Position
flagtag):[GroupInfo]
gs) = do
        Position
flagVal <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
flagtag
        if (-Position
1) forall a. Eq a => a -> a -> Bool
== Position
flagVal then Position -> [GroupInfo] -> ST s ()
act Position
this_index [GroupInfo]
gs
          else do
        Position
startPos <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
start
        Position
stopPos <- STUArray s Position Position
pos forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
stop
        (Position
startParent,Position
lengthParent) <- STArray s Position (Position, Position)
ma forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
parent
        let ok :: Bool
ok = (Position
0 forall a. Ord a => a -> a -> Bool
<= Position
startParent Bool -> Bool -> Bool
&&
                  Position
0 forall a. Ord a => a -> a -> Bool
<= Position
lengthParent Bool -> Bool -> Bool
&&
                  Position
startParent forall a. Ord a => a -> a -> Bool
<= Position
startPos Bool -> Bool -> Bool
&&
                  Position
stopPos forall a. Ord a => a -> a -> Bool
<= Position
startPos forall a. Num a => a -> a -> a
+ Position
lengthParent)
        if Bool -> Bool
not Bool
ok then Position -> [GroupInfo] -> ST s ()
act Position
this_index [GroupInfo]
gs
          else forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STArray s Position (Position, Position)
ma Position
this_index (Position
startPos,Position
stopPosforall a. Num a => a -> a -> a
-Position
startPos)
  forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (forall a. Ix a => (a, a) -> [a]
range (Position
1,Position
b_max)) forall a b. (a -> b) -> a -> b
$ (\Position
i -> forall {s}.
(MArray (STUArray s) Position (ST s),
 MArray (STArray s) (Position, Position) (ST s)) =>
Position -> [GroupInfo] -> ST s ()
act Position
i (Array Position [GroupInfo]
aGroupsforall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!Position
i))
  forall i (a :: * -> * -> *) e (m :: * -> *) (b :: * -> * -> *).
(Ix i, MArray a e m, IArray b e) =>
a i e -> m (b i e)
unsafeFreeze STArray s Position (Position, Position)
ma

{- MUTABLE TAGGED TRANSITION (returning Tag-0 value) -}

{-# INLINE spawnAt #-}
-- Reset the entry at "Index", or allocate such an entry.
-- set tag 0 to the "Position"
spawnAt :: (Tag,Tag) -> BlankScratch s -> Index -> MScratch s -> Position -> S.ST s ()
spawnAt :: forall s.
(Position, Position)
-> BlankScratch s -> Position -> MScratch s -> Position -> ST s ()
spawnAt (Position, Position)
b_tags (BlankScratch STUArray s Position Position
blankPos) Position
i MScratch s
s1 Position
thisPos = do
  Maybe (STUArray s Position Position)
oldPos <- forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
i
  STUArray s Position Position
pos <- case Maybe (STUArray s Position Position)
oldPos of
           Maybe (STUArray s Position Position)
Nothing -> do
             STUArray s Position Position
pos' <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
             forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s1) Position
i (forall a. a -> Maybe a
Just STUArray s Position Position
pos')
             forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Position Position
pos'
           Just STUArray s Position Position
pos -> forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Position Position
pos
  forall i s e.
(Show i, Ix i, MArray (STUArray s) e (ST s)) =>
STUArray s i e -> STUArray s i e -> ST s ()
copySTU STUArray s Position Position
blankPos STUArray s Position Position
pos
  forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s1) Position
i forall a b. (a -> b) -> a -> b
$! forall a. Monoid a => a
mempty
  forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set STUArray s Position Position
pos Position
0 Position
thisPos

{-# INLINE updateCopy #-}
updateCopy :: ((Index, Instructions), STUArray s Tag Position, OrbitLog)
           -> Index
           -> MScratch s
           -> Int
           -> ST s ()
updateCopy :: forall s.
((Position, Instructions), STUArray s Position Position, OrbitLog)
-> Position -> MScratch s -> Position -> ST s ()
updateCopy ((Position
_i1,Instructions
instructions),STUArray s Position Position
oldPos,OrbitLog
newOrbit) Position
preTag MScratch s
s2 Position
i2 = do
  (Position, Position)
b_tags <- forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> m (i, i)
getBounds STUArray s Position Position
oldPos
  STUArray s Position Position
newerPos <- forall b a. b -> (a -> b) -> Maybe a -> b
maybe (do
    STUArray s Position Position
a <- forall s e.
MArray (STUArray s) e (ST s) =>
(Position, Position) -> ST s (STUArray s Position e)
newA_ (Position, Position)
b_tags
    forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s2) Position
i2 (forall a. a -> Maybe a
Just STUArray s Position Position
a)
    forall (m :: * -> *) a. Monad m => a -> m a
return STUArray s Position Position
a) forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall s.
MScratch s
-> STArray s Position (Maybe (STUArray s Position Position))
m_pos MScratch s
s2 forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> ST s e
!! Position
i2
  forall i s e.
(Show i, Ix i, MArray (STUArray s) e (ST s)) =>
STUArray s i e -> STUArray s i e -> ST s ()
copySTU STUArray s Position Position
oldPos STUArray s Position Position
newerPos
  forall s.
Position
-> STUArray s Position Position -> [(Position, Action)] -> ST s ()
doActions Position
preTag STUArray s Position Position
newerPos (Instructions -> [(Position, Action)]
newPos Instructions
instructions)
  forall (a :: * -> * -> *) e s i.
(MArray a e (ST s), Ix i) =>
a i e -> Position -> e -> ST s ()
set (forall s. MScratch s -> STArray s Position OrbitLog
m_orbit MScratch s
s2) Position
i2 forall a b. (a -> b) -> a -> b
$! OrbitLog
newOrbit

{- USING memcpy TO COPY STUARRAY DATA -}

-- #ifdef __GLASGOW_HASKELL__
foreign import ccall unsafe "memcpy"
    memcpyIO :: MutableByteArray# RealWorld -> MutableByteArray# RealWorld -> Int# -> IO (Ptr a)

memcpyST :: MutableByteArray# s -> MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #)
memcpyST :: forall s a.
MutableByteArray# s
-> MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #)
memcpyST = unsafeCoerce# :: forall a b. a -> b
unsafeCoerce# forall a.
MutableByteArray# RealWorld
-> MutableByteArray# RealWorld -> Int# -> IO (Ptr a)
memcpyIO

{-
Prelude Data.Array.Base> :i STUArray
data STUArray s i e
  = STUArray !i !i !Int (GHC.Prim.MutableByteArray# s)
  -- Defined in Data.Array.Base
-}
-- This has been updated for ghc 6.8.3 and still works with ghc 6.10.1
{-# INLINE copySTU #-}
copySTU :: (Show i,Ix i,MArray (STUArray s) e (S.ST s)) => STUArray s i e -> STUArray s i e -> S.ST s () -- (STUArray s i e)
copySTU :: forall i s e.
(Show i, Ix i, MArray (STUArray s) e (ST s)) =>
STUArray s i e -> STUArray s i e -> ST s ()
copySTU _source :: STUArray s i e
_source@(STUArray i
_ i
_ Position
_ MutableByteArray# s
msource) _destination :: STUArray s i e
_destination@(STUArray i
_ i
_ Position
_ MutableByteArray# s
mdest) =
-- do b1 <- getBounds s1
--  b2 <- getBounds s2
--  when (b1/=b2) (error ("\n\nWTF copySTU: "++show (b1,b2)))
  forall s a. STRep s a -> ST s a
ST forall a b. (a -> b) -> a -> b
$ \State# s
s1# ->
    case forall d. MutableByteArray# d -> Int#
sizeofMutableByteArray# MutableByteArray# s
msource        of { Int#
n# ->
    case forall s a.
MutableByteArray# s
-> MutableByteArray# s -> Int# -> State# s -> (# State# s, Ptr a #)
memcpyST MutableByteArray# s
mdest MutableByteArray# s
msource Int#
n# State# s
s1# of { (# State# s
s2#, Ptr Any
_ #) ->
    (# State# s
s2#, () #) }}
{-
-- #else /* !__GLASGOW_HASKELL__ */

copySTU :: (MArray (STUArray s) e (S.ST s))=> STUArray s Tag e -> STUArray s Tag e -> S.ST s (STUArray s i e)
copySTU source destination = do
  b@(start,stop) <- getBounds source
  b' <- getBounds destination
  -- traceCopy ("> copySTArray "++show b) $ do
  when (b/=b') (fail $ "Text.Regex.TDFA.RunMutState copySTUArray bounds mismatch"++show (b,b'))
  forM_ (range b) $ \index ->
    set destination index =<< source !! index
  return destination
-- #endif /* !__GLASGOW_HASKELL__ */
-}