aboutsummaryrefslogtreecommitdiffstats
path: root/src/VeriFuzz/Config.hs
diff options
context:
space:
mode:
authorYann Herklotz Grave <git@yannherklotzgrave.com>2019-03-04 16:50:00 +0000
committerYann Herklotz Grave <git@yannherklotzgrave.com>2019-03-04 16:50:00 +0000
commit8454ad46e1022f50fcc0eaf717d2806500e2d7fa (patch)
treecba2a6c211f673c845337bb9cb467fb00ea1eec3 /src/VeriFuzz/Config.hs
parent8a4fa6bbacfd613904f895f66389ff5a4a11bbff (diff)
downloadverismith-8454ad46e1022f50fcc0eaf717d2806500e2d7fa.tar.gz
verismith-8454ad46e1022f50fcc0eaf717d2806500e2d7fa.zip
Add toml configuration support for probabilities
Diffstat (limited to 'src/VeriFuzz/Config.hs')
-rw-r--r--src/VeriFuzz/Config.hs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/VeriFuzz/Config.hs b/src/VeriFuzz/Config.hs
new file mode 100644
index 0000000..a51743e
--- /dev/null
+++ b/src/VeriFuzz/Config.hs
@@ -0,0 +1,57 @@
+{-|
+Module : VeriFuzz.Config
+Description : Configuration file format and parser.
+Copyright : (c) 2019, Yann Herklotz Grave
+License : GPL-3
+Maintainer : ymherklotz [at] gmail [dot] com
+Stability : experimental
+Portability : POSIX
+
+Configuration file format and parser.
+-}
+
+{-# LANGUAGE TemplateHaskell #-}
+
+module VeriFuzz.Config
+ ( Config(..)
+ , Probability(..)
+ , probAssign
+ , probAlways
+ , configProbability
+ , parseConfigFile
+ )
+where
+
+import Control.Applicative (Alternative)
+import Control.Lens hiding ((.=))
+import Data.Maybe (fromMaybe)
+import Toml (TomlCodec, (.=))
+import qualified Toml
+
+data Probability = Probability { _probAssign :: {-# UNPACK #-} !Int
+ , _probAlways :: {-# UNPACK #-} !Int
+ }
+ deriving (Eq, Show)
+
+makeLenses ''Probability
+
+newtype Config = Config { _configProbability :: Probability }
+ deriving (Eq, Show)
+
+makeLenses ''Config
+
+defaultValue
+ :: (Alternative r, Applicative w) =>
+ b -> Toml.Codec r w a b -> Toml.Codec r w a b
+defaultValue x = Toml.dimap Just (fromMaybe x) . Toml.dioptional
+
+probCodec :: TomlCodec Probability
+probCodec = Probability
+ <$> Toml.int "assign" .= _probAssign
+ <*> defaultValue 1 (Toml.int "always") .= _probAlways
+
+configCodec :: TomlCodec Config
+configCodec = Toml.dimap _configProbability Config $ Toml.table probCodec "probability"
+
+parseConfigFile :: FilePath -> IO Config
+parseConfigFile = Toml.decodeFile configCodec