1

我正在尝试编写一个程序,该程序接受任何通用语句,对其进行评估,并在评估时显示每个步骤。

例如,如果我们有一系列赋值语句,它应该运行为:

  • 评估作业声明 1

  • 显示评估结果1

  • 评估赋值语句 2

  • 显示评估结果 2

现在,它只显示最终结果。代码是

> 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

编辑:

其余使用的数据类型是:

> 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

有人可以帮忙吗?提前致谢!

4

1 回答 1

1

如果您只想查看痕迹,您可以使用tracefromDebug.Trace来“欺骗”引用透明度并输出中间信息。

另一种方法是将 a 包裹WriterT在您的Maybe, 并用于tell发出中间信息。这实际上会返回一些您可以在 Haskell 中检查的值。

请注意,仅当您的评估成功时才会返回您的跟踪信息。为了总是回馈一个踪迹,你改为换行MaybeTWriter

于 2013-02-06T15:18:17.947 回答