[Added some exercises based on arithmetic properties. Nils Anders Danielsson **20050901103350 Suggested by Andreas Farre. ] { hunk ./.boring 10 +^MoreProperties\.hs$ addfile ./ArithmeticProperties.hs hunk ./ArithmeticProperties.hs 1 +import QuickCheck + +prop_addCommutative :: Integer -> Integer -> Bool +prop_addCommutative x y = x + y == y + x + +prop_mulCommutative :: Integer -> Integer -> Bool +prop_mulCommutative x y = x * y == y * x + +prop_addAssociative :: Integer -> Integer -> Integer -> Bool +prop_addAssociative x y z = x + (y + z) == (x + y) + z + +prop_mulAssociative :: Integer -> Integer -> Integer -> Bool +prop_mulAssociative x y z = x * (y * z) == (x * y) * z + +prop_mulLeftDistributive :: Integer -> Integer -> Integer -> Bool +prop_mulLeftDistributive x y z = x * (y + z) == x * y + x * z hunk ./IntroAssignment.ws 8 +import MoreProperties hunk ./IntroAssignment.ws 26 + moreProperties + hunk ./Makefile 1 -SOURCE = ArbitraryRatio.hs Commands.ws Intro.ws Pythagoras.ws \ -Temperature.hs Calculator.ws Functions.ws IntroAssignment.ws \ -MoreFunctions.ws Question.hs TypeClasses.ws +SOURCE = ArbitraryRatio.hs Commands.ws Intro.ws MoreProperties.ws \ +Pythagoras.ws Temperature.hs Calculator.ws Functions.ws \ +IntroAssignment.ws MoreFunctions.ws Question.hs TypeClasses.ws addfile ./MoreProperties.ws hunk ./MoreProperties.ws 1 +module MoreProperties where + +import Question + +commutativeUrl = "http://en.wikipedia.org/wiki/Commutative" +associativeUrl = "http://en.wikipedia.org/wiki/Associative" +distributiveUrl = "http://en.wikipedia.org/wiki/Distributive" + +moreProperties :: WithHTML x CGI () +moreProperties = do + +

More properties

+ + p <#>Let's now practice writing some properties. Recall that a + property is a function that should always return True. + + ul $ do + + <#>Write a property stating that addition of Integers is + >commutative (i.e. x + y = y + + x). Test it. .?> do + + fixed $ unlines + [ "prop_addCommutative :: Integer -> Integer -> Bool" + , "prop_addCommutative x y = x + y == y + x" + ] + + prompts "Main>" + [ ("quickCheck prop_addCommutative", "OK, passed 100 tests.") ] + + <#>Write a property stating that multiplication of Integers is + commutative. .?> do + + fixed $ unlines + [ "prop_mulCommutative :: Integer -> Integer -> Bool" + , "prop_mulCommutative x y = x * y == y * x" + ] + + <#>Write a property stating that addition of Integers is + >associative. .?> do + + fixed $ unlines + [ "prop_addAssociative :: Integer -> Integer -> Integer -> Bool" + , "prop_addAssociative x y z = x + (y + z) == (x + y) + z" + ] + + <#>Write a property stating that multiplication of Integers is + associative. .?> do + + fixed $ unlines + [ "prop_mulAssociative :: Integer -> Integer -> Integer -> Bool" + , "prop_mulAssociative x y z = x * (y * z) == (x * y) * z" + ] + + <#>Write a property stating that multiplication >distributes from the left over addition + (for Integers). .?> do + + fixed $ unlines + [ "prop_mulLeftDistributive :: Integer -> Integer -> Integer -> Bool" + , "prop_mulLeftDistributive x y z = x * (y + z) == x * y + x * z" + ] }