aboutsummaryrefslogtreecommitdiff
path: root/Haskell-book/14/qc
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
committerEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
commit98329e0a3dd4f78b5d815ac3896272ec70904901 (patch)
tree80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/14/qc
parent3624c712d72d246f21d4e710cec7c11e052e0326 (diff)
downloadbook-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/14/qc')
-rw-r--r--Haskell-book/14/qc/qc.cabal32
-rw-r--r--Haskell-book/14/qc/src/UsingQuickCheck.hs58
-rw-r--r--Haskell-book/14/qc/stack.yaml66
-rw-r--r--Haskell-book/14/qc/tests/Idempotence.hs30
-rw-r--r--Haskell-book/14/qc/tests/UsingQuickCheckTest.hs128
5 files changed, 314 insertions, 0 deletions
diff --git a/Haskell-book/14/qc/qc.cabal b/Haskell-book/14/qc/qc.cabal
new file mode 100644
index 0000000..f0b5b3a
--- /dev/null
+++ b/Haskell-book/14/qc/qc.cabal
@@ -0,0 +1,32 @@
+name: qc
+version: 0.1.0.0
+author: Eugen Wissner
+maintainer: belka@caraus.de
+category: Math
+build-type: Simple
+cabal-version: >= 1.10
+
+library
+ hs-source-dirs: src
+ build-depends: base >= 4.7 && < 5
+ , QuickCheck
+ exposed-modules: UsingQuickCheck
+ ghc-options: -Wall
+ default-language: Haskell2010
+
+test-suite tests
+ type: exitcode-stdio-1.0
+ main-is: UsingQuickCheckTest.hs
+ hs-source-dirs: tests
+ ghc-options: -Wall
+ build-depends: base >= 4.7 && < 5
+ , QuickCheck
+ , qc
+
+test-suite idempotence
+ type: exitcode-stdio-1.0
+ main-is: Idempotence.hs
+ hs-source-dirs: tests
+ ghc-options: -Wall
+ build-depends: base >= 4.7 && < 5
+ , QuickCheck \ No newline at end of file
diff --git a/Haskell-book/14/qc/src/UsingQuickCheck.hs b/Haskell-book/14/qc/src/UsingQuickCheck.hs
new file mode 100644
index 0000000..f0fa27f
--- /dev/null
+++ b/Haskell-book/14/qc/src/UsingQuickCheck.hs
@@ -0,0 +1,58 @@
+module UsingQuickCheck where
+
+import Test.QuickCheck
+
+--
+-- 1
+--
+half :: (Eq a, Fractional a) => a -> a
+half x = x / 2
+
+halfIdentity :: (Eq a, Fractional a) => a -> a
+halfIdentity = (*2) . half
+
+--
+-- 2
+--
+-- for any list you apply sort to
+-- this property should hold
+listOrdered :: (Ord a) => [a] -> Bool
+listOrdered xs =
+ snd $ foldr go (Nothing, True) xs
+ where go _ status@(_, False) = status
+ go y (Nothing, t) = (Just y, t)
+ go y (Just x, _) = (Just y, x >= y)
+
+--
+-- 3
+--
+plusAssociative :: (Ord a, Integral a) => a -> a -> a -> Bool
+plusAssociative x y z = x + (y + z) == (x + y) + z
+
+plusCommutative :: (Ord a, Integral a) => a -> a -> Bool
+plusCommutative x y = x + y == y + x
+
+--
+-- 4
+--
+mulAssociative :: (Ord a, Integral a) => a -> a -> a -> Bool
+mulAssociative x y z = x * (y * z) == (x * y) * z
+
+mulCommutative :: (Ord a, Integral a) => a -> a -> Bool
+mulCommutative x y = x * y == y * x
+
+data Fool = Fulse
+ | Frue
+ deriving (Eq, Show)
+
+data Fool' = Fulse' -- 2/3
+ | Frue' -- 1/3
+ deriving (Eq, Show)
+
+instance Arbitrary Fool where
+ arbitrary = oneof [ return Fulse
+ , return Frue ]
+
+instance Arbitrary Fool' where
+ arbitrary = frequency [ (3, return Fulse')
+ , (1, return Frue')] \ No newline at end of file
diff --git a/Haskell-book/14/qc/stack.yaml b/Haskell-book/14/qc/stack.yaml
new file mode 100644
index 0000000..22e3463
--- /dev/null
+++ b/Haskell-book/14/qc/stack.yaml
@@ -0,0 +1,66 @@
+# This file was automatically generated by 'stack init'
+#
+# Some commonly used options have been documented as comments in this file.
+# For advanced use and comprehensive documentation of the format, please see:
+# https://docs.haskellstack.org/en/stable/yaml_configuration/
+
+# Resolver to choose a 'specific' stackage snapshot or a compiler version.
+# A snapshot resolver dictates the compiler version and the set of packages
+# to be used for project dependencies. For example:
+#
+# resolver: lts-3.5
+# resolver: nightly-2015-09-21
+# resolver: ghc-7.10.2
+# resolver: ghcjs-0.1.0_ghc-7.10.2
+# resolver:
+# name: custom-snapshot
+# location: "./custom-snapshot.yaml"
+resolver: lts-9.17
+
+# User packages to be built.
+# Various formats can be used as shown in the example below.
+#
+# packages:
+# - some-directory
+# - https://example.com/foo/bar/baz-0.0.2.tar.gz
+# - location:
+# git: https://github.com/commercialhaskell/stack.git
+# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
+# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
+# extra-dep: true
+# subdirs:
+# - auto-update
+# - wai
+#
+# A package marked 'extra-dep: true' will only be built if demanded by a
+# non-dependency (i.e. a user package), and its test suites and benchmarks
+# will not be run. This is useful for tweaking upstream packages.
+packages:
+- .
+# Dependency packages to be pulled from upstream that are not in the resolver
+# (e.g., acme-missiles-0.3)
+# extra-deps: []
+
+# Override default flag values for local packages and extra-deps
+# flags: {}
+
+# Extra package databases containing global packages
+# extra-package-dbs: []
+
+# Control whether we use the GHC we find on the path
+# system-ghc: true
+#
+# Require a specific version of stack, using version ranges
+# require-stack-version: -any # Default
+# require-stack-version: ">=1.6"
+#
+# Override the architecture used by stack, especially useful on Windows
+# arch: i386
+# arch: x86_64
+#
+# Extra directories used by stack for building
+# extra-include-dirs: [/path/to/dir]
+# extra-lib-dirs: [/path/to/dir]
+#
+# Allow a newer minor version of GHC than the snapshot specifies
+# compiler-check: newer-minor \ No newline at end of file
diff --git a/Haskell-book/14/qc/tests/Idempotence.hs b/Haskell-book/14/qc/tests/Idempotence.hs
new file mode 100644
index 0000000..4b484d0
--- /dev/null
+++ b/Haskell-book/14/qc/tests/Idempotence.hs
@@ -0,0 +1,30 @@
+module Main where
+
+import Data.Char
+import Data.List
+import Test.QuickCheck
+
+capitalizeWord :: String -> String
+capitalizeWord [] = []
+capitalizeWord (x:xs) = toUpper x : xs
+
+twice :: (a -> a) -> (a -> a)
+twice y = y . y
+
+fourTimes :: (a -> a) -> (a -> a)
+fourTimes = twice . twice
+
+f :: String -> Bool
+f x =
+ (capitalizeWord x == twice capitalizeWord x)
+ && (capitalizeWord x == fourTimes capitalizeWord x)
+
+f' :: Ord a => [a] -> Bool
+f' x =
+ (sort x == twice sort x)
+ && (sort x == fourTimes sort x)
+
+main :: IO ()
+main = do
+ quickCheck f
+ quickCheck (f' :: String -> Bool) \ No newline at end of file
diff --git a/Haskell-book/14/qc/tests/UsingQuickCheckTest.hs b/Haskell-book/14/qc/tests/UsingQuickCheckTest.hs
new file mode 100644
index 0000000..da5ddb5
--- /dev/null
+++ b/Haskell-book/14/qc/tests/UsingQuickCheckTest.hs
@@ -0,0 +1,128 @@
+module Main where
+
+import Data.List (sort)
+import UsingQuickCheck
+import Test.QuickCheck
+
+prop_half :: (Eq a, Fractional a) => a -> Bool
+prop_half x = (halfIdentity x) == x
+
+associativeGen :: (Integer -> Integer -> Integer -> Bool) -> Gen Bool
+associativeGen f = do
+ x <- (arbitrary :: Gen Integer)
+ y <- (arbitrary :: Gen Integer)
+ z <- (arbitrary :: Gen Integer)
+ elements [f x y z]
+
+commutativeGen :: (Integer -> Integer -> Bool) -> Gen Bool
+commutativeGen f = do
+ x <- (arbitrary :: Gen Integer)
+ y <- (arbitrary :: Gen Integer)
+ elements [f x y]
+
+assocNotNegGen :: (Int -> Int -> Int -> Bool) -> Gen Bool
+assocNotNegGen f = do
+ x <- choose (1 :: Int, 100)
+ y <- choose (1 :: Int, 100)
+ z <- choose (1 :: Int, 100)
+ elements [f x y z]
+
+commutNotNegGen :: (Int -> Int -> Bool) -> Gen Bool
+commutNotNegGen f = do
+ x <- choose (1 :: Int, 100)
+ y <- choose (1 :: Int, 100)
+ elements [f x y]
+
+
+prop_quotRem :: Property
+prop_quotRem =
+ forAll (prop_quotRem') (\(x ,y) -> (quot x y) * y + (rem x y) == x)
+ where prop_quotRem' = do
+ x <- choose (1 :: Int, 10000)
+ y <- choose (1 :: Int, 10000)
+ return (x, y)
+
+prop_divMod :: Property
+prop_divMod =
+ forAll (prop_divMod') (\(x ,y) -> (div x y) * y + (mod x y) == x)
+ where prop_divMod' = do
+ x <- choose (1 :: Int, 10000)
+ y <- choose (1 :: Int, 10000)
+ return (x, y)
+
+prop_reverse :: Property
+prop_reverse =
+ forAll prop_reverse' (\xs -> (reverse . reverse) xs == id xs)
+ where prop_reverse' = do
+ x <- (arbitrary :: Gen [Integer])
+ return x
+
+prop_dollar :: Property
+prop_dollar =
+ forAll prop_dollar' (\x -> x)
+ where prop_dollar' = do
+ x <- (arbitrary :: Gen Integer)
+ return ((id $ x) == (id x))
+
+prop_point :: Property
+prop_point =
+ forAll prop_point' (\x -> x)
+ where prop_point' = do
+ x <- (arbitrary :: Gen Integer)
+ let pointFunc = negate . id
+ let appliedFunc = \y -> negate (id y)
+ return (pointFunc x == appliedFunc x)
+
+prop_foldr1 :: Property
+prop_foldr1 =
+ forAll prop_foldr1' (\x -> x)
+ where prop_foldr1' = do
+ x <- (arbitrary :: Gen [Integer])
+ y <- (arbitrary :: Gen [Integer])
+ return ((foldr (:) x y) == (x ++ y))
+
+prop_foldr2 :: Property
+prop_foldr2 =
+ forAll prop_foldr2' (\x -> x)
+ where prop_foldr2' = do
+ x <- (arbitrary :: Gen [[Integer]])
+ return ((foldr (++) [] x) == (concat x))
+
+prop_length :: Property
+prop_length =
+ forAll prop_length' (\x -> x)
+ where prop_length' = do
+ n <- (arbitrary :: Gen Int)
+ xs <- (arbitrary :: Gen [Integer])
+ return ((length (take n xs)) == n)
+
+prop_readShow :: Property
+prop_readShow =
+ forAll prop_readShow' (\x -> x)
+ where prop_readShow' = do
+ x <- (arbitrary :: Gen Integer)
+ return ((read (show x)) == x)
+
+main :: IO ()
+main = do
+ quickCheck (prop_half :: Double -> Bool)
+ quickCheck $ (listOrdered :: [Int] -> Bool) . sort
+
+ quickCheck $ associativeGen plusAssociative
+ quickCheck $ commutativeGen plusCommutative
+ quickCheck $ associativeGen mulAssociative
+ quickCheck $ commutativeGen mulCommutative
+
+ quickCheck prop_quotRem
+ quickCheck prop_divMod
+
+ quickCheck $ assocNotNegGen (\x y z -> x ^ (y ^ z) == (x ^ y) ^ z)
+ quickCheck $ commutNotNegGen (\x y -> x ^ y == y ^ x)
+
+ quickCheck prop_reverse
+ quickCheck prop_dollar
+ quickCheck prop_point
+ quickCheck prop_foldr1
+ quickCheck prop_foldr2
+ quickCheck prop_length
+ quickCheck prop_readShow \ No newline at end of file