Skip to content
Snippets Groups Projects
Commit ed078088 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel
Browse files

random-dfa: Implement coalgebra output format

parent a5badbb0
No related branches found
No related tags found
No related merge requests found
module CoalgOutput () where
module CoalgOutput (coalgB) where
import Data.Foldable ( fold )
import Data.List ( intersperse )
import qualified Data.Text.Lazy.Builder as Build
import qualified Data.Text.Lazy.Builder.Int as Build
import Lens.Micro.Platform
import Type
coalgB :: DFA -> Build.Builder
coalgB dfa = functorB dfa <> "\n\n" <> transitionsB dfa
functorB :: DFA -> Build.Builder
functorB dfa = "{True,False}x(X^" <> Build.decimal (dfa ^. letters) <> ")"
transitionsB :: DFA -> Build.Builder
transitionsB dfa = foldMap (forStateB dfa) [0 .. dfa ^. states - 1]
forStateB :: DFA -> Int -> Build.Builder
forStateB dfa n =
"s"
<> Build.decimal n
<> ": ("
<> Build.fromString (show (dfa ^?! isFinal . ix n))
<> ", {"
<> fold
(intersperse
", "
(zipWith successorB [0 ..] (dfa ^.. transitions . ix n . each))
)
<> "})\n"
successorB :: Int -> Int -> Build.Builder
successorB label target = Build.decimal label <> ": s" <> Build.decimal target
......@@ -9,7 +9,7 @@ import Lens.Micro.Platform
import Type
dotB :: DFA -> Build.Builder
dotB dfa = "digraph {\n" <> nodesB dfa <> transitionsB dfa <> "}"
dotB dfa = "digraph {\n" <> nodesB dfa <> transitionsB dfa <> "}\n"
nodesB :: DFA -> Build.Builder
......
......@@ -14,6 +14,7 @@ import Lens.Micro.Platform
import Type
import DotOutput
import CoalgOutput
-- | Generate a random DFA
......@@ -32,9 +33,17 @@ randomDFA s a = do
}
data OutputFormat = Dot | MA
instance Show OutputFormat where
show Dot = "dot"
show MA = "ma"
data Options = Options
{ _optStates :: Int
, _optLetters :: Int
, _optOutputFormat :: OutputFormat
}
makeLensesWith abbreviatedFields ''Options
......@@ -50,6 +59,23 @@ options =
OptParse.auto
(OptParse.metavar "M" <> OptParse.help "Size of the alphabet")
)
<*> (OptParse.option
readFormat
( OptParse.long "output-format"
<> OptParse.metavar "FORMAT"
<> OptParse.value MA
<> OptParse.help
"Syntax used for the output. Posible values are 'ma' or 'dot'."
<> OptParse.showDefault
)
)
readFormat :: OptParse.ReadM OutputFormat
readFormat = OptParse.maybeReader $ \case
"ma" -> Just MA
"dot" -> Just Dot
_ -> Nothing
main :: IO ()
......@@ -62,7 +88,11 @@ main = do
)
)
let builder = case opts^.outputFormat of
MA -> coalgB
Dot -> dotB
dfa <- randomDFA (opts ^. states) (opts ^. letters)
TL.putStrLn (Build.toLazyText (dotB dfa))
TL.putStr (Build.toLazyText (builder dfa))
return ()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment