0

As a school project i am going to create a graphic calculator. The following code comes from two files, Expr.hs and ExprQC.hs.

From Expr.hs

 readExpr :: String -> Maybe Expr
 readExpr s = 
   case expr modS of
    Just (a,"") -> Just a
    _           -> Nothing
   where
     modS = filter (/= ' ') s

From ExprQC.hs

module ExprQC where
import Test.QuickCheck

import Expr

prop_ShowReadExpr :: Expr -> Bool
prop_ShowReadExpr expr =
  readExpr (show expr) == Just (assoc expr)

assoc :: Expr -> Expr
assoc (Add (Add a b) c) = assoc (Add a (Add b c))
assoc (Add a b)         = Add (assoc a) (assoc b)
assoc (Mul (Mul a b) c) = assoc (Mul a (Mul b c))
assoc (Mul a b)         = Mul (assoc a) (assoc b)
assoc (Sin a)           = Sin (assoc a)
assoc (Cos a)           = Cos (assoc a)
assoc a                 = a

I get the error on GHCi

Prelude> :l ExprQC.hs
[1 of 2] Compiling Expr             ( Expr.hs, interpreted )
[2 of 2] Compiling ExprQC           ( ExprQC.hs, interpreted )
Ok, modules loaded: ExprQC, Expr.
*ExprQC> quickCheck prop_ShowReadExpr 
 *** Failed! Falsifiable (after 1 test):  
Num 0.0

How can I fix this?

Edit: This is what GHCi returns after inputting the respective expression.

*ExprQC> readExpr (show (Num 0.0))
Nothing

And

*ExprQC> Just (assoc (Num 0.0))
Just (Num 0.0)

Thank you.

e_phed
  • 21
  • 3
  • 1
    just try in GHCi and see it yourself: `readExpr (show (Num 0.0))` and `Just (assoc (Num 0.0))` ... are those the same/equal? (I cannot tell as I don't see your `Show` decl.) – Random Dev Nov 04 '15 at 11:56
  • 1
    Also, try `===` in `prop_ShowReadExpr` instead (and `prop_ShowReadExpr :: Expr -> Property`). This will show how both values actually differ. Still without `expr` and `show` it's not clear where the actual error is. – Zeta Nov 04 '15 at 12:12
  • Maybe there's a `showExpr` function which you should use instead of `show`? Just guessing ... – chi Nov 04 '15 at 12:37
  • 1
    It seems like your `readExpr` function is failing for `Num 0.0` – user2407038 Nov 04 '15 at 15:36

0 Answers0