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