Commit a5badbb0 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel
Browse files

random-dfa: Implement CLI options

parent 2e5cb9dd
......@@ -250,6 +250,9 @@ executable random-dfa
default-language: Haskell2010
default-extensions: OverloadedStrings
, LambdaCase
, MultiParamTypeClasses
, FlexibleInstances
, FunctionalDependencies
build-depends: base
, vector
, microlens
......@@ -257,3 +260,4 @@ executable random-dfa
, microlens-platform
, random
, text
, optparse-applicative
{-# LANGUAGE TemplateHaskell #-}
module Main (main) where
import System.Environment
import Control.Applicative
import qualified Data.Text.Lazy.IO as TL
import qualified Data.Text.Lazy.Builder as Build
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import System.Random
import qualified Options.Applicative as OptParse
import Lens.Micro.TH
import Lens.Micro.Platform
import Type
import DotOutput
-- | Generate a random DFA
randomDFA
:: Int -- ^ Number of states
......@@ -20,20 +25,44 @@ randomDFA s a = do
finals <- VU.replicateM s randomIO
matrix <- V.replicateM s (VU.replicateM a (randomRIO (0, s - 1)))
return DFA
{ _states = s
, _letters = a
, _isFinal = finals
, _transitions = matrix
{ dfaStates = s
, dfaLetters = a
, dfaIsFinal = finals
, dfaTransitions = matrix
}
data Options = Options
{ _optStates :: Int
, _optLetters :: Int
}
makeLensesWith abbreviatedFields ''Options
options :: OptParse.Parser Options
options =
Options
<$> (OptParse.argument
OptParse.auto
(OptParse.metavar "N" <> OptParse.help "Number of states")
)
<*> (OptParse.argument
OptParse.auto
(OptParse.metavar "M" <> OptParse.help "Size of the alphabet")
)
main :: IO ()
main = do
[tNumStates, tSizeAlph] <- getArgs
let numStates = read tNumStates
sizeAlph = read tSizeAlph
opts <- OptParse.execParser
(OptParse.info
(options <**> OptParse.helper)
( OptParse.fullDesc
<> OptParse.progDesc "Generate a random determinstic finite automaton"
)
)
dfa <- randomDFA numStates sizeAlph
dfa <- randomDFA (opts ^. states) (opts ^. letters)
TL.putStrLn (Build.toLazyText (dotB dfa))
return ()
{-# LANGUAGE TemplateHaskell #-}
module Type (DFA(..), states, letters, transitions, isFinal) where
module Type
( DFA(..)
, HasStates(..)
, HasLetters(..)
, HasTransitions(..)
, HasIsFinal(..)
) where
import Data.Vector ( Vector )
import qualified Data.Vector.Unboxed as VU
import Lens.Micro.TH
data DFA = DFA
{ _states :: Int
, _letters :: Int
, _transitions :: Vector (VU.Vector Int)
, _isFinal :: VU.Vector Bool
{ dfaStates :: Int
, dfaLetters :: Int
, dfaTransitions :: Vector (VU.Vector Int)
, dfaIsFinal :: VU.Vector Bool
}
makeLenses ''DFA
makeLensesWith abbreviatedFields ''DFA
Supports Markdown
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