~safinaskar/check-cfg-ambiguity

85afe581c66e4cba2405a12a387c562f00a97a5f — Askar Safin 1 year, 5 months ago add2252
Optimization
1 files changed, 6 insertions(+), 9 deletions(-)

M CheckCFGAmbiguity.hs
M CheckCFGAmbiguity.hs => CheckCFGAmbiguity.hs +6 -9
@@ 35,6 35,7 @@ import Data.Foldable(for_)
import Data.Maybe(fromJust, catMaybes)
import Data.List(find)
import Data.Functor((<&>))
import Control.Monad(when)

data TerminalOrNonterminal t n = T t | N n deriving (Eq, Ord, Show)



@@ 124,20 125,16 @@ lowLevelTestAmbiguity (Grammar g) start count = case Data.Map.member start g of 
          case after of {
            [] -> return ();
            (N nn):rest -> for_ (fromJust $ Data.Map.lookup nn g) $ \prod -> do {
              let { newWord = before ++ prod ++ rest };
              do {
                allWordsV <- readSTRef allWords;
                if Data.Set.member newWord allWordsV
                  then writeSTRef collision True
                  else return ();
                writeSTRef allWords $ Data.Set.insert newWord allWordsV;
              };
              currWordsV2 <- readSTRef currWords;
              writeSTRef currWords $ newWord:currWordsV2;
              writeSTRef currWords $ (before ++ prod ++ rest):currWordsV2;
            };
            _ -> error "Impossible";
          };
        };
        currWordsV2 <- readSTRef currWords;
        allWordsV <- readSTRef allWords;
        writeSTRef allWords (Data.Set.union allWordsV (Data.Set.fromList currWordsV2));
        when (Data.Set.size (Data.Set.union allWordsV (Data.Set.fromList currWordsV2)) /= (Data.Set.size allWordsV + length currWordsV2)) $ writeSTRef collision True;
      };
      iV <- readSTRef i;
      writeSTRef i (iV + 1);