Loading src/random-wta/Generator.hs +8 −9 Original line number Diff line number Diff line Loading @@ -29,13 +29,13 @@ runGenerator config action = runReaderT action config genMonoidValue :: Generator m m genMonoidValue = asks ((monoid . spec) &&& differentValues) >>= \case (Powerset, Nothing) -> liftIO $ randomIO (Powerset, Nothing) -> liftIO randomIO (Powerset, Just 1) -> return True (Powerset, Just 2) -> liftIO $ randomIO (Powerset, _) -> error $ "differentValues >2 not supported for powerset" (OrWord, Nothing) -> liftIO $ randomIO (Powerset, Just 2) -> liftIO randomIO (Powerset, _) -> error "differentValues >2 not supported for powerset" (OrWord, Nothing) -> liftIO randomIO (OrWord, Just x) -> liftIO $ randomRIO (1, fromIntegral x) (MaxInt, Nothing) -> liftIO $ randomIO (MaxInt, Nothing) -> liftIO randomIO (MaxInt, Just x) -> liftIO $ randomRIO (1, x) genStates :: Generator m (Vector m) Loading @@ -49,8 +49,7 @@ aritySummand arity = do return $ V.length (V.filter (/= 0) (V.take arity arities)) decideZero :: Generator m Bool decideZero = do asks zeroFreq >>= liftIO . decide decideZero = asks zeroFreq >>= liftIO . decide -- Generates Nothing, when it decides that a zero value would be in order genTransition :: Int -> Int -> [State] -> Generator m (Maybe (Transition m)) Loading @@ -67,7 +66,7 @@ genTransition arity symbol succs = decideZero >>= \case genForSymbol :: Int -> Int -> Generator m (Vector (Transition m)) genForSymbol arity symbol = do states <- asks (numStates . spec) fmap (V.fromList . catMaybes) $ traverse V.fromList . catMaybes <$> traverse (genTransition arity symbol) (replicateM arity (coerce [0 .. states - 1])) Loading @@ -79,7 +78,7 @@ genForArity arity = do genStateTransitions :: Generator m (Vector (Transition m)) genStateTransitions = do arities <- asks (numSymbols . spec) fold <$> (traverse genForArity (V.findIndices (/= 0) arities)) fold <$> traverse genForArity (V.findIndices (/= 0) arities) genTransitions :: Generator m (Vector (Vector (Transition m))) genTransitions = do Loading src/random-wta/Main.hs +3 −6 Original line number Diff line number Diff line {-# LANGUAGE RankNTypes #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE ExistentialQuantification #-} Loading Loading @@ -58,7 +57,7 @@ readSymbols :: Options.ReadM SymbolSpec readSymbols = Options.maybeReader (Mega.parseMaybe parser) where parser :: Mega.Parsec Void String (Vector Int) parser = V.fromList <$> Mega.decimal `Mega.sepBy` (Mega.string ",") parser = V.fromList <$> Mega.decimal `Mega.sepBy` Mega.string "," readCount :: String -> Either String Int readCount input = case readDec input of Loading Loading @@ -128,7 +127,7 @@ computeProbability spec (OutDegree d) = main :: IO () main = do let optSpec = Options.info (parseOpts Options.<**> Options.helper) (Options.fullDesc) Options.info (parseOpts Options.<**> Options.helper) Options.fullDesc opts <- Options.execParser optSpec case optMonoid opts of Loading @@ -138,9 +137,7 @@ main = do exitFailure _ -> return () case optRandomState opts of Nothing -> return () Just x -> setStdGen x mapM_ setStdGen (optRandomState opts) withSpec opts $ \spec -> do randGen <- getStdGen Loading src/random-wta/Output.hs +5 −7 Original line number Diff line number Diff line Loading @@ -14,12 +14,12 @@ import Data.Maybe import Types wtaFunctor :: (WTASpec m) -> Builder wtaFunctor :: WTASpec m -> Builder wtaFunctor wta = monoidForStates (monoid wta) <> " × " <> withMonoidForWeights (monoid wta) (polynomial (numSymbols wta)) monoidForStates :: (MonoidType m) -> Builder monoidForStates :: MonoidType m -> Builder monoidForStates MaxInt = "Z" monoidForStates OrWord = "N" monoidForStates Powerset = "2" Loading @@ -44,9 +44,7 @@ buildValue :: ValueUse -> MonoidType m -> m -> Builder buildValue _ MaxInt i = Build.decimal i buildValue Weight OrWord w = "0x" <> Build.hexadecimal w buildValue StateVal OrWord w = Build.decimal w buildValue _ Powerset b = case b of False -> "0" True -> "1" buildValue _ Powerset b = if b then "1" else "0" buildTransition :: MonoidType m -> Transition m -> Builder buildTransition mon trans = Loading @@ -59,7 +57,7 @@ buildTransition mon trans = where buildSuccs succs = if V.null succs then Build.decimal (symbol trans) else "(" <> Build.decimal (symbol trans) <> ", " <> (sepList ", " buildStateName (successors trans)) <> ")" else "(" <> Build.decimal (symbol trans) <> ", " <> sepList ", " buildStateName (successors trans) <> ")" buildWeight :: MonoidType m -> m -> Builder buildWeight Powerset _ = "" Loading Loading @@ -88,7 +86,7 @@ buildStates wta = foldMap where indices = [0 .. V.length (stateValue wta) - 1] buildWTA :: WTA m -> Builder buildWTA a = (wtaFunctor (spec a)) <> "\n" <> buildStates a buildWTA a = wtaFunctor (spec a) <> "\n" <> buildStates a -- helpers Loading Loading
src/random-wta/Generator.hs +8 −9 Original line number Diff line number Diff line Loading @@ -29,13 +29,13 @@ runGenerator config action = runReaderT action config genMonoidValue :: Generator m m genMonoidValue = asks ((monoid . spec) &&& differentValues) >>= \case (Powerset, Nothing) -> liftIO $ randomIO (Powerset, Nothing) -> liftIO randomIO (Powerset, Just 1) -> return True (Powerset, Just 2) -> liftIO $ randomIO (Powerset, _) -> error $ "differentValues >2 not supported for powerset" (OrWord, Nothing) -> liftIO $ randomIO (Powerset, Just 2) -> liftIO randomIO (Powerset, _) -> error "differentValues >2 not supported for powerset" (OrWord, Nothing) -> liftIO randomIO (OrWord, Just x) -> liftIO $ randomRIO (1, fromIntegral x) (MaxInt, Nothing) -> liftIO $ randomIO (MaxInt, Nothing) -> liftIO randomIO (MaxInt, Just x) -> liftIO $ randomRIO (1, x) genStates :: Generator m (Vector m) Loading @@ -49,8 +49,7 @@ aritySummand arity = do return $ V.length (V.filter (/= 0) (V.take arity arities)) decideZero :: Generator m Bool decideZero = do asks zeroFreq >>= liftIO . decide decideZero = asks zeroFreq >>= liftIO . decide -- Generates Nothing, when it decides that a zero value would be in order genTransition :: Int -> Int -> [State] -> Generator m (Maybe (Transition m)) Loading @@ -67,7 +66,7 @@ genTransition arity symbol succs = decideZero >>= \case genForSymbol :: Int -> Int -> Generator m (Vector (Transition m)) genForSymbol arity symbol = do states <- asks (numStates . spec) fmap (V.fromList . catMaybes) $ traverse V.fromList . catMaybes <$> traverse (genTransition arity symbol) (replicateM arity (coerce [0 .. states - 1])) Loading @@ -79,7 +78,7 @@ genForArity arity = do genStateTransitions :: Generator m (Vector (Transition m)) genStateTransitions = do arities <- asks (numSymbols . spec) fold <$> (traverse genForArity (V.findIndices (/= 0) arities)) fold <$> traverse genForArity (V.findIndices (/= 0) arities) genTransitions :: Generator m (Vector (Vector (Transition m))) genTransitions = do Loading
src/random-wta/Main.hs +3 −6 Original line number Diff line number Diff line {-# LANGUAGE RankNTypes #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE ExistentialQuantification #-} Loading Loading @@ -58,7 +57,7 @@ readSymbols :: Options.ReadM SymbolSpec readSymbols = Options.maybeReader (Mega.parseMaybe parser) where parser :: Mega.Parsec Void String (Vector Int) parser = V.fromList <$> Mega.decimal `Mega.sepBy` (Mega.string ",") parser = V.fromList <$> Mega.decimal `Mega.sepBy` Mega.string "," readCount :: String -> Either String Int readCount input = case readDec input of Loading Loading @@ -128,7 +127,7 @@ computeProbability spec (OutDegree d) = main :: IO () main = do let optSpec = Options.info (parseOpts Options.<**> Options.helper) (Options.fullDesc) Options.info (parseOpts Options.<**> Options.helper) Options.fullDesc opts <- Options.execParser optSpec case optMonoid opts of Loading @@ -138,9 +137,7 @@ main = do exitFailure _ -> return () case optRandomState opts of Nothing -> return () Just x -> setStdGen x mapM_ setStdGen (optRandomState opts) withSpec opts $ \spec -> do randGen <- getStdGen Loading
src/random-wta/Output.hs +5 −7 Original line number Diff line number Diff line Loading @@ -14,12 +14,12 @@ import Data.Maybe import Types wtaFunctor :: (WTASpec m) -> Builder wtaFunctor :: WTASpec m -> Builder wtaFunctor wta = monoidForStates (monoid wta) <> " × " <> withMonoidForWeights (monoid wta) (polynomial (numSymbols wta)) monoidForStates :: (MonoidType m) -> Builder monoidForStates :: MonoidType m -> Builder monoidForStates MaxInt = "Z" monoidForStates OrWord = "N" monoidForStates Powerset = "2" Loading @@ -44,9 +44,7 @@ buildValue :: ValueUse -> MonoidType m -> m -> Builder buildValue _ MaxInt i = Build.decimal i buildValue Weight OrWord w = "0x" <> Build.hexadecimal w buildValue StateVal OrWord w = Build.decimal w buildValue _ Powerset b = case b of False -> "0" True -> "1" buildValue _ Powerset b = if b then "1" else "0" buildTransition :: MonoidType m -> Transition m -> Builder buildTransition mon trans = Loading @@ -59,7 +57,7 @@ buildTransition mon trans = where buildSuccs succs = if V.null succs then Build.decimal (symbol trans) else "(" <> Build.decimal (symbol trans) <> ", " <> (sepList ", " buildStateName (successors trans)) <> ")" else "(" <> Build.decimal (symbol trans) <> ", " <> sepList ", " buildStateName (successors trans) <> ")" buildWeight :: MonoidType m -> m -> Builder buildWeight Powerset _ = "" Loading Loading @@ -88,7 +86,7 @@ buildStates wta = foldMap where indices = [0 .. V.length (stateValue wta) - 1] buildWTA :: WTA m -> Builder buildWTA a = (wtaFunctor (spec a)) <> "\n" <> buildStates a buildWTA a = wtaFunctor (spec a) <> "\n" <> buildStates a -- helpers Loading