1

I am supposed to fill in something for undefined to get the program compiled and test it. I don't really know what symdiff here is supposed to do, so I don't know what I can fill in for undefined. Can someone give me a hint, what I can insert into undefined?

By the way, when I want to compile the code with ghci 7.6.3 I get an error: Could not find module 'Test.SmallCheck.Series' how can I fix that?

Here is the code:

{-# language FlexibleInstances #-}
{-# language MultiParamTypeClasses #-}
{-# language NoMonomorphismRestriction #-}

module Blueprint where

import Test.SmallCheck
import Test.SmallCheck.Series

data N = Z | S N deriving (Show , Eq)

symdiff :: N -> N -> N
symdiff x y = undefined



-- for testing in ghci:  smallCheck 10 spec1


spec1 = \ (x,y) -> symdiff x y == symdiff y x
spec2 = \ (x,y) -> symdiff x (plus x y) == y


plus :: N -> N -> N
plus x y = case x of
    Z -> y
    S x' -> S (plus x' y)

test :: Bool
test = and
   [ null $ failures 10 1000 $ spec1
   , null $ failures 10 1000 $ spec2   
   ]

instance Monad m => Serial m N where series = cons0 Z \/ cons1 S

-- | first f failures from t testcases for property p
failures f t p = take f
               $ filter ( \ x -> not $ p x ) 
               $ take t
               $ do d <- [ 0 .. ] ; list d series

thanks, that helped a lot! what about this:

symdiff :: N -> N -> N
symdiff x y = case x of
    Z -> y
    S x' -> case y of
        Z -> x
        S y' -> ???

Are these lines correct (except the line with ???, I am thinking about it at already)

This works for the last line: S y' -> symdiff x' y'

Peter
  • 1,679
  • 2
  • 31
  • 60

1 Answers1

6

The hints are in the smallcheck tests:

spec1 = \ (x,y) -> symdiff x y == symdiff y x
spec2 = \ (x,y) -> symdiff x (plus x y) == y

This means that symdiff needs to satisfy the two equations

symdiff x y == symdiff y x  -- shouldn't matter what order (symmetric?)
symdiff x (plus x y) == y   -- if they differ by y, that's the answer (difference)

That means that symdiff has to find the difference between the two arguments.

You need to think of Z as zero and S x as the successor of x, i.e. x+1.

The good news is that there's only two possibilities for N, so a maximum of four equations for symdiff:

symdiff Z Z =
symdiff Z (S y) = 
symdiff (S x) Z = 
symdiff (S x) (S y) =

Think about how you would find the difference between
0 and 0
0 and 1+y
1+x and 0
1+x and 1+y

to help you.

(You can simplify into fewer cases, but start with these four.)

Have a think about it now, and if that's not enough hinting after a good think, comment.

AndrewC
  • 32,300
  • 7
  • 79
  • 115