module GLL.Combinators.Visit.FUNGLL where

import GLL.Types.Grammar
import GLL.Types.BSR
import GLL.Types.DataSets
import GLL.Types.Input

import qualified Data.IntMap as IM
import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.IntSet as IS
import Data.Text (pack)

type Command t  = State t (ContF t) -> State t (ContF t)
data ContF t    = ContF (Input t -> Descr t -> Command t)

type Parse_Symb t   = (Symbol t, Input t -> Slot t -> Int -> Int -> ContF t -> Command t)
type Parse_Choice t = Input t -> Nt -> Int -> ContF t -> Command t 
type Parse_Seq t    = Input t -> Nt -> [Symbol t] -> Int -> ContF t -> Command t
type Parse_Alt t    = Parse_Seq t

parser_for :: (Parseable t) => Nt -> Parse_Symb t -> Input t -> ParseResult t
parser_for :: forall t.
Parseable t =>
Nt -> Parse_Symb t -> Input t -> ParseResult t
parser_for Nt
x Parse_Symb t
p Input t
inp = forall t c. Parseable t => Input t -> State t c -> ParseResult t
resultFromState Input t
inp (forall t.
Nt
-> Parse_Symb t
-> Input t
-> Int
-> State t (ContF t)
-> State t (ContF t)
run_parse Nt
x Parse_Symb t
p Input t
inp Int
0 forall t c. Ord t => State t c
emptyState)

run_parse :: Nt -> Parse_Symb t -> Input t -> Int -> 
                                State t (ContF t) -> State t (ContF t)
run_parse :: forall t.
Nt
-> Parse_Symb t
-> Input t
-> Int
-> State t (ContF t)
-> State t (ContF t)
run_parse Nt
x p :: Parse_Symb t
p@(Symbol t
y,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
pf) Input t
inp Int
l = Input t -> Slot t -> Int -> Int -> ContF t -> Command t
pf Input t
inp (forall t. Nt -> [Symbol t] -> [Symbol t] -> Slot t
Slot Nt
x [Symbol t
y] []) Int
l Int
l forall t. ContF t
counter_cont 

counter_cont :: ContF t
counter_cont :: forall t. ContF t
counter_cont = forall t. (Input t -> Descr t -> Command t) -> ContF t
ContF forall {p} {a} {b} {t} {c}.
p -> (a, b, Int) -> State t c -> State t c
cf
  where cf :: p -> (a, b, Int) -> State t c -> State t c
cf p
_ (a
_,b
_,Int
r) State t c
s = State t c
s { successes :: IntMap Int
successes = forall a. (Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
IM.alter Maybe Int -> Maybe Int
updater Int
r (forall t c. State t c -> IntMap Int
successes State t c
s) } 
          where updater :: Maybe Int -> Maybe Int
updater = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. a -> Maybe a
Just Int
1) (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int
1forall a. Num a => a -> a -> a
+))

parse_nterm :: (Ord t) => Nt -> [Parse_Seq t] -> Parse_Symb t
parse_nterm :: forall t. Ord t => Nt -> [Parse_Seq t] -> Parse_Symb t
parse_nterm Nt
n = forall t. Ord t => Nt -> Parse_Choice t -> Parse_Symb t
nterm Nt
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall t. Parse_Choice t -> Parse_Seq t -> Parse_Choice t
altOp forall t. Parse_Choice t
altStart 

parse_term :: Parseable t => t -> Parse_Symb t
parse_term :: forall t. Parseable t => t -> Parse_Symb t
parse_term = forall t. Parseable t => t -> Parse_Symb t
term

parse_apply :: Ord t => Parse_Symb t -> Parse_Seq t
parse_apply :: forall t. Ord t => Parse_Symb t -> Parse_Seq t
parse_apply = forall t. Ord t => Parse_Seq t -> Parse_Symb t -> Parse_Seq t
seqOp forall t. Ord t => Parse_Seq t
seqStart 

parse_seq :: Ord t => Parse_Seq t -> Parse_Symb t -> Parse_Seq t
parse_seq :: forall t. Ord t => Parse_Seq t -> Parse_Symb t -> Parse_Seq t
parse_seq = forall t. Ord t => Parse_Seq t -> Parse_Symb t -> Parse_Seq t
seqOp

nterm :: (Ord t) => Nt -> Parse_Choice t -> Parse_Symb t
nterm :: forall t. Ord t => Nt -> Parse_Choice t -> Parse_Symb t
nterm Nt
n Parse_Choice t
p = (forall t. Nt -> Symbol t
Nt Nt
n, Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser)
  where parser :: Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser Input t
inp Slot t
g Int
l Int
k ContF t
c State t (ContF t)
s 
          | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
rs   = Parse_Choice t
p Input t
inp Nt
n Int
k forall t. ContF t
cont_for State t (ContF t)
s'
          | Bool
otherwise = forall t. [Command t] -> Command t
compAll [ forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c (forall t. Int -> Input t -> Input t
removePrefix Int
len Input t
inp) (Slot t
g,Int
l,Int
r) 
                                | Int
r <- [Int]
rs, let len :: Int
len = Int
r forall a. Num a => a -> a -> a
- Int
k ] State t (ContF t)
s'
          where s' :: State t (ContF t)
s' = State t (ContF t)
s { grel :: GRel t (ContF t)
grel = forall t c.
Ord t =>
Comm t -> (Slot t, Int, c) -> GRel t c -> GRel t c
addCont (Nt
n,Int
k) (Slot t
g,Int
l,ContF t
c) (forall t c. State t c -> GRel t c
grel State t (ContF t)
s) } 
                rs :: [Int]
rs = forall t. Comm t -> PRel t -> [Int]
extents (Nt
n,Int
k) (forall t c. State t c -> PRel t
prel State t (ContF t)
s) 

        cont_for :: ContF t
cont_for = forall t. (Input t -> Descr t -> Command t) -> ContF t
ContF forall {t} {a}.
Input t -> (a, Int, Int) -> State t (ContF t) -> State t (ContF t)
cf 
         where cf :: Input t -> (a, Int, Int) -> State t (ContF t) -> State t (ContF t)
cf Input t
inp (a
_,Int
k,Int
r) State t (ContF t)
s = 
                forall t. [Command t] -> Command t
compAll [ forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c Input t
inp (Slot t
g,Int
l',Int
r) 
                        | (Slot t
g,Int
l',ContF t
c) <- forall t c. Comm t -> GRel t c -> [(Slot t, Int, c)]
conts (Nt
n,Int
k) (forall t c. State t c -> GRel t c
grel State t (ContF t)
s) ] State t (ContF t)
s'
                where s' :: State t (ContF t)
s' = State t (ContF t)
s { prel :: PRel t
prel = forall t. Comm t -> Int -> PRel t -> PRel t
addExtent (Nt
n,Int
k) Int
r (forall t c. State t c -> PRel t
prel State t (ContF t)
s) }

term :: Parseable t => t -> Parse_Symb t
term :: forall t. Parseable t => t -> Parse_Symb t
term t
t = (forall t. t -> Symbol t
Term t
t, forall a b. (a, b) -> b
snd (forall t. Parseable t => Nt -> (t -> Bool) -> Parse_Symb t
predicate (String -> Nt
pack (forall a. Show a => a -> String
show t
t)) (forall a. Parseable a => a -> a -> Bool
matches t
t))) 

seqStart :: Ord t => Parse_Seq t
seqStart :: forall t. Ord t => Parse_Seq t
seqStart Input t
inp Nt
x [Symbol t]
beta Int
l ContF t
c = forall t. Ord t => Input t -> BSR t -> ContF t -> Command t
continue Input t
inp (forall t. Nt -> [Symbol t] -> [Symbol t] -> Slot t
Slot Nt
x [] [Symbol t]
beta, Int
l, Int
l, Int
l) ContF t
c

seqOp :: Ord t => Parse_Seq t -> Parse_Symb t -> Parse_Seq t
seqOp :: forall t. Ord t => Parse_Seq t -> Parse_Symb t -> Parse_Seq t
seqOp Parse_Seq t
p (Symbol t
s,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
q) Input t
inp Nt
x [Symbol t]
beta Int
l ContF t
c0 = Parse_Seq t
p Input t
inp Nt
x (Symbol t
sforall a. a -> [a] -> [a]
:[Symbol t]
beta) Int
l ContF t
c1
  where c1 :: ContF t
c1 = forall t. (Input t -> Descr t -> Command t) -> ContF t
ContF Input t -> (Slot t, Int, Int) -> Command t
c1f
         where c1f :: Input t -> (Slot t, Int, Int) -> Command t
c1f Input t
inp ((Slot Nt
_ [Symbol t]
alpha [Symbol t]
_),Int
l,Int
k) = Input t -> Slot t -> Int -> Int -> ContF t -> Command t
q Input t
inp (forall t. Nt -> [Symbol t] -> [Symbol t] -> Slot t
Slot Nt
x ([Symbol t]
alphaforall a. [a] -> [a] -> [a]
++[Symbol t
s]) [Symbol t]
beta) Int
l Int
k ContF t
c2
                where c2 :: ContF t
c2 = forall t. (Input t -> Descr t -> Command t) -> ContF t
ContF Input t -> (Slot t, Int, Int) -> Command t
c2f
                       where c2f :: Input t -> (Slot t, Int, Int) -> Command t
c2f Input t
inp (Slot t
g,Int
l,Int
r) = forall t. Ord t => Input t -> BSR t -> ContF t -> Command t
continue Input t
inp (Slot t
g,Int
l,Int
k,Int
r) ContF t
c0

continue :: (Ord t) => Input t -> BSR t -> ContF t -> Command t
continue :: forall t. Ord t => Input t -> BSR t -> ContF t -> Command t
continue Input t
inp bsr :: BSR t
bsr@(g :: Slot t
g@(Slot Nt
x [Symbol t]
alpha [Symbol t]
beta),Int
l,Int
k,Int
r) ContF t
c State t (ContF t)
s 
  | forall t. Ord t => Descr t -> USet t -> Bool
hasDescr (Slot t, Int, Int)
descr (forall t c. State t c -> USet t
uset State t (ContF t)
s) = State t (ContF t)
s'
  | Bool
otherwise               = forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c Input t
inp (Slot t, Int, Int)
descr State t (ContF t)
s''
  where descr :: (Slot t, Int, Int)
descr = (Slot t
g,Int
l,Int
r)
        s' :: State t (ContF t)
s'  | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Symbol t]
alpha) Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Symbol t]
beta = State t (ContF t)
s { bsrs :: BSRs t
bsrs = forall t. Ord t => BSR t -> BSRs t -> BSRs t
addBSR BSR t
bsr (forall t c. State t c -> BSRs t
bsrs State t (ContF t)
s) }
            | Bool
otherwise                     = State t (ContF t)
s
        s'' :: State t (ContF t)
s'' = State t (ContF t)
s' { uset :: USet t
uset = forall t. Ord t => Descr t -> USet t -> USet t
addDescr (Slot t, Int, Int)
descr (forall t c. State t c -> USet t
uset State t (ContF t)
s') }

altStart :: Parse_Choice t
altStart :: forall t. Parse_Choice t
altStart Input t
inp Nt
n Int
l ContF t
c State t (ContF t)
s = State t (ContF t)
s

altOp :: Parse_Choice t -> Parse_Seq t -> Parse_Choice t
altOp :: forall t. Parse_Choice t -> Parse_Seq t -> Parse_Choice t
altOp Parse_Choice t
p Parse_Seq t
q Input t
inp Nt
n Int
l ContF t
c = Parse_Choice t
p Input t
inp Nt
n Int
l ContF t
c forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parse_Seq t
q Input t
inp Nt
n [] Int
l ContF t
c
{- MUCH SLOWER ?
altOp p q inp n l c s = 
  let s1 = p inp n l counter_cont s
      s2 = q inp n [] l counter_cont s1
  in compAll [ applyCF c (error "cont_for assert", l, r) 
             | r <- IS.toList (IS.union (IM.keysSet (successes s1)) 
                                        (IM.keysSet (successes s2))) ] s2
-}

compAll :: [Command t] -> Command t
compAll :: forall t. [Command t] -> Command t
compAll = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) forall a. a -> a
id

applyCF :: ContF t -> Input t -> Descr t -> Command t
applyCF (ContF Input t -> Descr t -> Command t
cf) Input t
inp Descr t
a = Input t -> Descr t -> Command t
cf Input t
inp Descr t
a

{- EXTENSIONS -}

parse_lexical :: Nt -> RawParser t -> Parse_Symb t
parse_lexical :: forall t. Nt -> RawParser t -> Parse_Symb t
parse_lexical Nt
n RawParser t
scanner = (forall t. Nt -> Symbol t
Nt Nt
n, Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser)
  where parser :: Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser Input t
inp Slot t
g Int
l Int
k ContF t
c State t (ContF t)
s = 
          forall t. [Command t] -> Command t
compAll [ forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c (forall t. Int -> Input t -> Input t
removePrefix Int
len Input t
inp) (Slot t
g, Int
l, Int
k forall a. Num a => a -> a -> a
+ Int
len) 
                  | [t]
prefix <- forall t. RawParser t -> Input t -> [[t]]
apply_scanner RawParser t
scanner Input t
inp 
                  , let len :: Int
len = forall (t :: * -> *) a. Foldable t => t a -> Int
length [t]
prefix ] State t (ContF t)
s

{- EXPERIMENTAL -}

andNot :: (Show t) => Parse_Symb t -> Parse_Symb t -> Parse_Symb t
andNot :: forall t. Show t => Parse_Symb t -> Parse_Symb t -> Parse_Symb t
andNot (Symbol t
lnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
p) (Symbol t
rnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
q) = (forall t. Nt -> Symbol t
Nt Nt
lhs_symb,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
parser)
  where lhs_symb :: Nt
lhs_symb = String -> Nt
pack (String
"__andNot(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Symbol t
lnt forall a. [a] -> [a] -> [a]
++String
","forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Symbol t
rnt forall a. [a] -> [a] -> [a]
++ String
")")
        parser :: Input t -> Slot t -> Int -> Int -> ContF t -> Command t
parser Input t
inp Slot t
g Int
l Int
k ContF t
c State t (ContF t)
s = forall t. [Command t] -> Command t
compAll [ forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c (forall t. Int -> Input t -> Input t
removePrefix Int
len Input t
inp) (Slot t
g, Int
l, Int
r) 
                                       | Int
r <- [Int]
rs, let len :: Int
len = Int
r forall a. Num a => a -> a -> a
- Int
k ] 
                                       State t (ContF t)
s2{successes :: IntMap Int
successes = forall t c. State t c -> IntMap Int
successes State t (ContF t)
s}
          where s1 :: State t (ContF t)
s1 = forall t.
Nt
-> Parse_Symb t
-> Input t
-> Int
-> State t (ContF t)
-> State t (ContF t)
run_parse Nt
lhs_symb (Symbol t
lnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
p) Input t
inp Int
k State t (ContF t)
s{successes :: IntMap Int
successes = forall a. IntMap a
IM.empty}
                s2 :: State t (ContF t)
s2 = forall t.
Nt
-> Parse_Symb t
-> Input t
-> Int
-> State t (ContF t)
-> State t (ContF t)
run_parse Nt
lhs_symb (Symbol t
rnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
q) Input t
inp Int
k State t (ContF t)
s1{successes :: IntMap Int
successes = forall a. IntMap a
IM.empty}
                rs :: [Int]
rs = IntSet -> [Int]
IS.toList forall a b. (a -> b) -> a -> b
$ IntSet -> IntSet -> IntSet
IS.difference (forall a. IntMap a -> IntSet
IM.keysSet (forall t c. State t c -> IntMap Int
successes State t (ContF t)
s1))
                                               (forall a. IntMap a -> IntSet
IM.keysSet (forall t c. State t c -> IntMap Int
successes State t (ContF t)
s2))


ands :: (Show t) => [Parse_Symb t] -> Parse_Symb t
ands :: forall t. Show t => [Parse_Symb t] -> Parse_Symb t
ands = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall t. Show t => Parse_Symb t -> Parse_Symb t -> Parse_Symb t
andOp forall t. Parse_Symb t
andStart

andOp :: (Show t) => Parse_Symb t -> Parse_Symb t -> Parse_Symb t
andOp :: forall t. Show t => Parse_Symb t -> Parse_Symb t -> Parse_Symb t
andOp (Symbol t
lnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
p) (Symbol t
rnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
q) = (forall t. Nt -> Symbol t
Nt Nt
lhs_symb,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
parser)
  where lhs_symb :: Nt
lhs_symb = String -> Nt
pack (String
"__and(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Symbol t
lnt forall a. [a] -> [a] -> [a]
++String
","forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Symbol t
rnt forall a. [a] -> [a] -> [a]
++ String
")")
        parser :: Input t -> Slot t -> Int -> Int -> ContF t -> Command t
parser Input t
inp Slot t
g Int
l Int
k ContF t
c State t (ContF t)
s = forall t. [Command t] -> Command t
compAll [ forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c (forall t. Int -> Input t -> Input t
removePrefix Int
len Input t
inp) (Slot t
g, Int
l, Int
r) 
                                       | Int
r <- [Int]
rs, let len :: Int
len = Int
r forall a. Num a => a -> a -> a
- Int
k ] State t (ContF t)
s2
          where s1 :: State t (ContF t)
s1 = forall t.
Nt
-> Parse_Symb t
-> Input t
-> Int
-> State t (ContF t)
-> State t (ContF t)
run_parse Nt
lhs_symb (Symbol t
lnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
p) Input t
inp Int
k State t (ContF t)
s 
                s2 :: State t (ContF t)
s2 = forall t.
Nt
-> Parse_Symb t
-> Input t
-> Int
-> State t (ContF t)
-> State t (ContF t)
run_parse Nt
lhs_symb (Symbol t
rnt,Input t -> Slot t -> Int -> Int -> ContF t -> Command t
q) Input t
inp Int
k State t (ContF t)
s1
                rs :: [Int]
rs = IntSet -> [Int]
IS.toList forall a b. (a -> b) -> a -> b
$ IntSet -> IntSet -> IntSet
IS.intersection (forall a. IntMap a -> IntSet
IM.keysSet (forall t c. State t c -> IntMap Int
successes State t (ContF t)
s1))
                                                 (forall a. IntMap a -> IntSet
IM.keysSet (forall t c. State t c -> IntMap Int
successes State t (ContF t)
s2))

andStart :: Parse_Symb t
andStart :: forall t. Parse_Symb t
andStart = (forall t. Nt -> Symbol t
Nt (String -> Nt
pack String
"__and_unit"), forall {t}.
Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser)
  where parser :: Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser Input t
inp Slot t
g Int
l Int
k ContF t
c State t (ContF t)
s = forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c Input t
inp (Slot t
g, Int
l, Int
k) State t (ContF t)
s

predicate :: Parseable t => Nt -> (t -> Bool) -> Parse_Symb t
predicate :: forall t. Parseable t => Nt -> (t -> Bool) -> Parse_Symb t
predicate Nt
nt t -> Bool
p = (forall t. Nt -> Symbol t
Nt Nt
nt, Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser)
  where parser :: Input t
-> Slot t
-> Int
-> Int
-> ContF t
-> State t (ContF t)
-> State t (ContF t)
parser Input t
inp Slot t
g Int
l Int
k ContF t
c State t (ContF t)
s =
          forall t. [Command t] -> Command t
compAll [ forall {t}. ContF t -> Input t -> Descr t -> Command t
applyCF ContF t
c (forall t. Int -> Input t -> Input t
removePrefix Int
len Input t
inp) (Slot t
g, Int
l, Int
k forall a. Num a => a -> a -> a
+ Int
len)   
                  | [t]
prefix <- forall t. RawParser t -> Input t -> [[t]]
apply_scanner (forall t. (t -> Bool) -> RawParser t
scanner_from_predicate t -> Bool
p) Input t
inp
                  , let len :: Int
len = forall (t :: * -> *) a. Foldable t => t a -> Int
length [t]
prefix ] State t (ContF t)
s

-- | 
-- The "ParseResult" datatype contains some information about a parse:
--
--  * Whether the parse was successful
--  * The number of descriptors that have been processed
--  * The number of BSR elements 
data ParseResult t = ParseResult{ forall t. ParseResult t -> BSRs t
bsrs_result               :: BSRs t
                                , forall t. ParseResult t -> Bool
res_success               :: Bool
                                , forall t. ParseResult t -> IntMap Int
res_successes             :: IM.IntMap Int
                                , forall t. ParseResult t -> Int
nr_descriptors            :: Int
                                , forall t. ParseResult t -> Int
nr_bsrs                   :: Int
                                , forall t. ParseResult t -> String
error_message             :: String
                                }

matchedUpTo :: IM.IntMap Int -> Int -> Bool
matchedUpTo :: IntMap Int -> Int -> Bool
matchedUpTo IntMap Int
res Int
r = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (forall a b. a -> b -> a
const Bool
True) (forall a. Int -> IntMap a -> Maybe a
IM.lookup Int
r IntMap Int
res)

resultFromState :: Parseable t => Input t -> State t c -> ParseResult t
resultFromState :: forall t c. Parseable t => Input t -> State t c -> ParseResult t
resultFromState Input t
inp (State USet t
uset GRel t c
_ PRel t
_ BSRs t
pMap IntMap Int
cs) =
    let usize :: Int
usize       = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum  [ forall a. Set a -> Int
S.size Set (Slot t)
s   | (Int
l, IntMap (Set (Slot t))
r2s) <- forall a. IntMap a -> [(Int, a)]
IM.assocs USet t
uset
                                        , (Int
r,Set (Slot t)
s) <- forall a. IntMap a -> [(Int, a)]
IM.assocs IntMap (Set (Slot t))
r2s ]
        p_nodes :: Int
p_nodes     = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ IntSet -> Int
IS.size IntSet
ks  | (Int
l, IntMap (IntMap (Map (Prod t) IntSet))
r2j) <- forall a. IntMap a -> [(Int, a)]
IM.assocs BSRs t
pMap
                                        , (Int
r, IntMap (Map (Prod t) IntSet)
j2s) <- forall a. IntMap a -> [(Int, a)]
IM.assocs IntMap (IntMap (Map (Prod t) IntSet))
r2j
                                        , (Int
j, Map (Prod t) IntSet
s2k) <- forall a. IntMap a -> [(Int, a)]
IM.assocs IntMap (Map (Prod t) IntSet)
j2s
                                        , (Prod t
s, IntSet
ks)  <- forall k a. Map k a -> [(k, a)]
M.assocs Map (Prod t) IntSet
s2k ]
        succs :: Int
succs = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 forall a. a -> a
id (forall a. Int -> IntMap a -> Maybe a
IM.lookup (forall t. Input t -> Int
inputLength Input t
inp) IntMap Int
cs)
    in forall t.
BSRs t
-> Bool -> IntMap Int -> Int -> Int -> String -> ParseResult t
ParseResult BSRs t
pMap (Int
succs forall a. Ord a => a -> a -> Bool
> Int
0) IntMap Int
cs Int
usize Int
p_nodes String
"no errors to report" 

instance Show (ParseResult t) where
    show :: ParseResult t -> String
show ParseResult t
res | forall t. ParseResult t -> Bool
res_success ParseResult t
res = String
result_string
             | Bool
otherwise       = String
result_string forall a. [a] -> [a] -> [a]
++ String
"\n" forall a. [a] -> [a] -> [a]
++ forall t. ParseResult t -> String
error_message ParseResult t
res
     where result_string :: String
result_string = [String] -> String
unlines forall a b. (a -> b) -> a -> b
$
                [   String
"Success             "  forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall t. ParseResult t -> Bool
res_success ParseResult t
res)
                ,   String
"#Success            "  forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall t. ParseResult t -> IntMap Int
res_successes ParseResult t
res)
                ,   String
"Descriptors:        "  forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall t. ParseResult t -> Int
nr_descriptors ParseResult t
res)
                ,   String
"BSRs:               "  forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall t. ParseResult t -> Int
nr_bsrs ParseResult t
res)
                ]