Commit eb15e479 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢

Convert mdp parser from doubles to rationals

parent 0033c4c0
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-} {-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
module Main (main) where module Main (main) where
...@@ -169,7 +170,7 @@ main = do ...@@ -169,7 +170,7 @@ main = do
Valmari -> valmariMdpB Valmari -> valmariMdpB
Valmari2 -> valmariMdp2B Valmari2 -> valmariMdp2B
Copar -> mdpB Copar -> mdpB
in convert opts mdpP (builder initPartition) in convert opts (alien (fromRational @Double) $ mdpP) (builder initPartition)
(inType, outType) -> (inType, outType) ->
let mcType = let mcType =
case inType of case inType of
...@@ -181,3 +182,7 @@ main = do ...@@ -181,3 +182,7 @@ main = do
Valmari2 -> error "valmari2 only implemented for MDPs" Valmari2 -> error "valmari2 only implemented for MDPs"
Copar -> markovChainB Copar -> markovChainB
in convert opts (markovChainP mcType) (builder initPartition) in convert opts (markovChainP mcType) (builder initPartition)
alien :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
alien = ((<$>) . (<$>))
...@@ -31,21 +31,21 @@ import qualified Mdp.Mcrl2 as Mcrl2 ...@@ -31,21 +31,21 @@ import qualified Mdp.Mcrl2 as Mcrl2
-- Parser -- Parser
---------------------------------------------------------------------- ----------------------------------------------------------------------
mdpP :: Parser (Mdp Double) mdpP :: Parser (Mdp Rational)
mdpP = Mdp <$> decimalP <*> decimalP <*> transitionsP mdpP = Mdp <$> decimalP <*> decimalP <*> transitionsP
transitionsP :: Parser (Vector (Transition Double)) transitionsP :: Parser (Vector (Transition Rational))
transitionsP = do transitionsP = do
numTrans <- decimalP numTrans <- decimalP
V.replicateM numTrans transitionP V.replicateM numTrans transitionP
transitionP :: Parser (Transition Double) transitionP :: Parser (Transition Rational)
transitionP = transitionP =
Transition Transition
<$> decimalP <$> decimalP
<*> decimalP <*> decimalP
<*> decimalP <*> decimalP
<*> doubleP <*> rationalP
<*> optional nameP <*> optional nameP
---------------------------------------------------------------------- ----------------------------------------------------------------------
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveFunctor #-}
module Mdp.Types module Mdp.Types
( Mdp(Mdp) ( Mdp(Mdp)
...@@ -33,7 +34,7 @@ data Mdp p = Mdp ...@@ -33,7 +34,7 @@ data Mdp p = Mdp
{ mdpNumStates :: Int { mdpNumStates :: Int
, mdpNumChoices :: Int , mdpNumChoices :: Int
, mdpTransitions :: Vector (Transition p) , mdpTransitions :: Vector (Transition p)
} deriving (Show) } deriving (Show, Functor)
-- TODO Support optional action label -- TODO Support optional action label
data Transition p = Transition data Transition p = Transition
...@@ -42,7 +43,7 @@ data Transition p = Transition ...@@ -42,7 +43,7 @@ data Transition p = Transition
, transitionTarget :: Int , transitionTarget :: Int
, transitionProbability :: p , transitionProbability :: p
, transitionAction :: Maybe Text , transitionAction :: Maybe Text
} deriving (Show) } deriving (Show, Functor)
makeFields ''Mdp makeFields ''Mdp
makeFields ''Transition makeFields ''Transition
...@@ -5,6 +5,7 @@ module Parser ...@@ -5,6 +5,7 @@ module Parser
, lexeme , lexeme
, decimalP , decimalP
, doubleP , doubleP
, rationalP
, nameP , nameP
, symbolP , symbolP
) where ) where
...@@ -29,6 +30,10 @@ decimalP = lexeme L.decimal ...@@ -29,6 +30,10 @@ decimalP = lexeme L.decimal
doubleP :: Parser Double doubleP :: Parser Double
doubleP = lexeme (try L.float <|> (fromIntegral @Int <$> L.decimal)) doubleP = lexeme (try L.float <|> (fromIntegral @Int <$> L.decimal))
-- | Parse a decimal into a ratio
rationalP :: Parser Rational
rationalP = toRational <$> lexeme L.scientific
isName1 :: Char -> Bool isName1 :: Char -> Bool
isName1 c = isLetter c || c == '_' isName1 c = isLetter c || c == '_'
......
...@@ -11,7 +11,6 @@ module StatesFile ...@@ -11,7 +11,6 @@ module StatesFile
import Control.Monad import Control.Monad
import Data.Bifunctor (first) import Data.Bifunctor (first)
import Data.Semigroup
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
......
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