Commit 0e62f678 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

wta: Fix whitespace

parent dcb87ef9
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
module IndexedTransition (IndexedTransition(..), maxIndex, fromIndex) where module IndexedTransition (IndexedTransition(..), maxIndex, fromIndex) where
import Data.Vector (Vector) import Data.Vector ( Vector )
import qualified Data.Vector as V import qualified Data.Vector as V
import Data.Maybe import Data.Maybe
import Data.Tuple import Data.Tuple
import Types import Types
newtype IndexedTransition = Index { fromIndexd :: Integer } newtype IndexedTransition = Index { fromIndexd :: Integer }
deriving newtype (Num, Eq, Ord) deriving newtype (Num, Eq, Ord)
...@@ -17,14 +17,14 @@ newtype IndexedTransition = Index { fromIndexd :: Integer } ...@@ -17,14 +17,14 @@ newtype IndexedTransition = Index { fromIndexd :: Integer }
maxIndex :: WTASpec m -> IndexedTransition maxIndex :: WTASpec m -> IndexedTransition
maxIndex spec = maxIndex spec =
let n = numStates spec let n = numStates spec
(t, _) = transitionsPerState spec (t, _) = transitionsPerState spec
in Index (fromIntegral n * t) in Index (fromIntegral n * t)
fromIndex :: WTASpec m -> IndexedTransition -> (State, Transition ()) fromIndex :: WTASpec m -> IndexedTransition -> (State, Transition ())
fromIndex spec (Index i) = fromIndex spec (Index i) =
let n = numStates spec let n = numStates spec
(t, symbolSums) = transitionsPerState spec (t , symbolSums ) = transitionsPerState spec
(state, stateTransition) = i `divMod` t (state, stateTransition) = i `divMod` t
...@@ -35,31 +35,33 @@ fromIndex spec (Index i) = ...@@ -35,31 +35,33 @@ fromIndex spec (Index i) =
arityTransition = stateTransition - (symbolSums V.! arity) arityTransition = stateTransition - (symbolSums V.! arity)
symbolBounds :: Vector Int = V.cons (numSymbols spec V.! arity) (V.replicate arity n) symbolBounds :: Vector Int =
V.cons (numSymbols spec V.! arity) (V.replicate arity n)
symbolDigits = decodeFromInt symbolBounds arityTransition symbolDigits = decodeFromInt symbolBounds arityTransition
symbol = V.head symbolDigits symbol = V.head symbolDigits
successors = V.tail symbolDigits successors = V.tail symbolDigits
trans = Transition trans = Transition { weight = ()
{ weight = () , summand = aritySummand spec arity
, summand = aritySummand spec arity , symbol = symbol
, symbol = symbol , successors = V.map State successors
, successors = V.map State successors }
} in (State (fromIntegral state), trans)
in (State (fromIntegral state), trans)
index :: WTASpec m1 -> Int -> Transition m2 -> IndexedTransition index :: WTASpec m1 -> Int -> Transition m2 -> IndexedTransition
index spec state trans = index spec state trans =
let (t, symbolSums) = transitionsPerState spec let (t, symbolSums) = transitionsPerState spec
arity :: Int = summandArity spec (summand trans) arity :: Int = summandArity spec (summand trans)
symbolBounds :: Vector Int = V.cons (numSymbols spec V.! arity) (V.replicate arity (numStates spec)) symbolBounds :: Vector Int = V.cons
arityIdx :: Integer = encodeAsInt symbolBounds (V.cons (symbol trans) (V.map fromState $ successors trans)) (numSymbols spec V.! arity)
(V.replicate arity (numStates spec))
arityIdx :: Integer = encodeAsInt
symbolBounds
(V.cons (symbol trans) (V.map fromState $ successors trans))
stateLocal :: Integer = symbolSums V.! arity + arityIdx stateLocal :: Integer = symbolSums V.! arity + arityIdx
in Index $ (fromIntegral state) * t + stateLocal
in Index $ (fromIntegral state) * t + stateLocal
-- Helpers -- Helpers
...@@ -69,22 +71,22 @@ summandArity spec summand = V.findIndices (/= 0) (numSymbols spec) V.! summand ...@@ -69,22 +71,22 @@ summandArity spec summand = V.findIndices (/= 0) (numSymbols spec) V.! summand
aritySummand :: WTASpec m -> Int -> Int aritySummand :: WTASpec m -> Int -> Int
aritySummand spec arity = aritySummand spec arity =
let arities = numSymbols spec let arities = numSymbols spec
in V.length (V.filter (/= 0) (V.take arity arities)) in V.length (V.filter (/= 0) (V.take arity arities))
transitionsPerState :: WTASpec m -> (Integer, Vector Integer) transitionsPerState :: WTASpec m -> (Integer, Vector Integer)
transitionsPerState spec = transitionsPerState spec =
let n = numStates spec let n = numStates spec
tPerSymbol = (V.imap (\i syms -> fromIntegral $ syms * n ^ i) (numSymbols spec)) tPerSymbol =
(V.imap (\i syms -> fromIntegral $ syms * n ^ i) (numSymbols spec))
runningTotal = V.scanl' (+) 0 tPerSymbol runningTotal = V.scanl' (+) 0 tPerSymbol
in (V.last runningTotal, runningTotal) in (V.last runningTotal, runningTotal)
encodeAsInt :: Vector Int -> Vector Int -> Integer encodeAsInt :: Vector Int -> Vector Int -> Integer
encodeAsInt maxBounds digits = encodeAsInt maxBounds digits =
let factors = V.prescanr' (*) 1 (fmap fromIntegral maxBounds) let factors = V.prescanr' (*) 1 (fmap fromIntegral maxBounds)
in sum (V.zipWith (*) factors (fmap fromIntegral digits)) in sum (V.zipWith (*) factors (fmap fromIntegral digits))
decodeFromInt :: Vector Int -> Integer -> Vector Int decodeFromInt :: Vector Int -> Integer -> Vector Int
decodeFromInt maxBounds encoded = decodeFromInt maxBounds encoded = V.map (fromIntegral . fst)
V.map (fromIntegral . fst) $ V.postscanr' doDigit (0, encoded) (fmap fromIntegral maxBounds) $ V.postscanr' doDigit (0, encoded) (fmap fromIntegral maxBounds)
where doDigit bound (_, current) = (swap $ current `divMod` bound) where doDigit bound (_, current) = (swap $ current `divMod` bound)
...@@ -118,11 +118,9 @@ parseZeroFreq = ...@@ -118,11 +118,9 @@ parseZeroFreq =
) )
<|> (NumTransitions <$> Options.option <|> (NumTransitions <$> Options.option
Options.auto Options.auto
( Options.long "transitions" (Options.long "transitions" <> Options.metavar "NUM" <> Options.help
<> Options.metavar "NUM" ("Number of transitions to generate. They will be distributed randomly over states."
<> Options.help )
("Number of transitions to generate. They will be distributed randomly over states."
)
) )
) )
...@@ -158,7 +156,7 @@ main = do ...@@ -158,7 +156,7 @@ main = do
randGen <- getStdGen randGen <- getStdGen
-- let zeroFreq = computeProbability spec (optEdgeConfig opts) -- let zeroFreq = computeProbability spec (optEdgeConfig opts)
-- hPutStrLn stderr $ "p hacking: " ++ show zeroFreq -- hPutStrLn stderr $ "p hacking: " ++ show zeroFreq
wta <- runGenerator wta <- runGenerator
(GeneratorConfig spec (optEdgeConfig opts) (optDifferentValues opts)) (GeneratorConfig spec (optEdgeConfig opts) (optDifferentValues opts))
genWTA genWTA
putStrLn $ "# Random state for this automaton: '" <> show randGen <> "'" putStrLn $ "# Random state for this automaton: '" <> show randGen <> "'"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment