Reputation: 289
I'm attempting writing a program which takes in any generic statement, evaluates it, and shows each step as it is evaluated.
For example, if we have a sequence of assignment statements, it should run as:
Evaluate Assignment Statement 1
Show Evaluation Result 1
Evaluate Assignment Statement 2
Show Evaluation Result 2
Right now, it shows only the final result. The code is
> evalS_maybe :: Statement -> Store -> Maybe Store
> evalS_maybe w@(While e s1) s = evalS_maybe (If e
> (Sequence s1 (While e s1))
> Skip)
> s
> evalS_maybe Skip s = Just s
> evalS_maybe (Sequence s1 s2) s = do
> sq <-evalS_maybe s1 s
> evalS_maybe s2 sq
> evalS_maybe (Assign x e ) s = do
> ag <-evalE_maybe e s
> return ( Map.insert x ag s )
> evalS_maybe (If e s1 s2) s = do
> b2 <- evalE_maybe e s
> case b2 of
> BoolVal True -> evalS_maybe s1 s
> BoolVal False -> evalS_maybe s2 s
> _ -> return s
Edit:
The rest of the data types used are:
> type Variable = String
> data Statement =
> Assign Variable Expression
> | If Expression Statement Statement
> | While Expression Statement
> | Sequence Statement Statement
> | Skip
> deriving (Show)
> data Expression =
> Var Variable
> | Val Value
> | Op Bop Expression Expression
> deriving (Show)
> data Bop =
> Plus
> | Minus
> | Times
> | Divide
> | Gt
> | Ge
> | Lt
> | Le
> deriving (Show)
> data Value =
> IntVal Int
> | BoolVal Bool
> deriving (Show)
> type Store = Map Variable Value
Can anybody please help? Thanks in advance!
Upvotes: 1
Views: 377
Reputation: 38893
If you only want to see the traces, you can just use trace
from Debug.Trace
to "cheat" referential transparency and output intermediate information.
The other approach is to wrap a WriterT
around your Maybe
, and use tell
to emit intermediate information. This actually will give back some values you can inspect in Haskell.
Note that this gives back your trace only if your evaluation succeeds. To always give back a trace, you wrap MaybeT
over Writer
instead.
Upvotes: 1