Skip to content

Commit a9cbeed

Browse files
committed
Update the code from playing around with QuickCheck from ch14
1 parent f75ebc9 commit a9cbeed

File tree

7 files changed

+66
-119
lines changed

7 files changed

+66
-119
lines changed

ch14/qc/Examples.hs

Lines changed: 0 additions & 70 deletions
This file was deleted.

ch14/qc/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright Dwayne Crooks (c) 2016
1+
Copyright Dwayne Crooks (c) 2017
22

33
All rights reserved.
44

ch14/qc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Kicking around QuickCheck

ch14/qc/qc.cabal

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
name: qc
22
version: 0.1.0.0
3-
synopsis: Simple project template from stack
4-
description: Please see README.md
53
homepage: https://github.com/dwayne/qc#readme
64
license: BSD3
75
license-file: LICENSE
86
author: Dwayne Crooks
97
maintainer: me@dwaynecrooks.com
10-
copyright: 2016 Dwayne Crooks
8+
copyright: 2017 Dwayne Crooks
119
category: Text
1210
build-type: Simple
1311
cabal-version: >=1.10
12+
extra-source-files: README.md
1413

15-
library
16-
hs-source-dirs: .
17-
exposed-modules: CoArbitrary
18-
, Examples
19-
, First
20-
ghc-options: -Wall -fwarn-tabs
21-
default-language: Haskell2010
22-
build-depends: base >= 4.7 && < 5
23-
, hspec
24-
, QuickCheck
14+
executable qc
15+
hs-source-dirs: src
16+
main-is: Main.hs
17+
default-language: Haskell2010
18+
build-depends: base >= 4.7 && < 5
19+
, QuickCheck

ch14/qc/CoArbitrary.hs renamed to ch14/qc/src/CoArbitrary.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ module CoArbitrary where
55
import GHC.Generics
66
import Test.QuickCheck
77

8-
data Bool' = True' | False' deriving (Generic)
8+
9+
data Bool' = True' | False' deriving Generic
10+
911

1012
instance CoArbitrary Bool'
1113

12-
-- coarbitrary :: CoArbitrary a => a -> Gen b -> Gen b
1314

1415
trueGen :: Gen Int
1516
trueGen = coarbitrary True' arbitrary
1617

18+
1719
falseGen :: Gen Int
1820
falseGen = coarbitrary False' arbitrary

ch14/qc/First.hs renamed to ch14/qc/src/Main.hs

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,76 @@
1-
module First where
2-
3-
-- 14.6 Kicking around QuickCheck
1+
module Main where
42

3+
import CoArbitrary
54
import Test.QuickCheck
5+
import Test.QuickCheck.Gen (oneof)
6+
7+
8+
main :: IO ()
9+
main = do
10+
sample trivialGen
11+
12+
13+
-- Trivial
614

715
data Trivial = Trivial deriving (Eq, Show)
816

17+
918
trivialGen :: Gen Trivial
1019
trivialGen = return Trivial
1120

21+
1222
instance Arbitrary Trivial where
1323
arbitrary = trivialGen
1424

25+
26+
-- Identity
27+
1528
data Identity a = Identity a deriving (Eq, Show)
1629

30+
1731
identityGen :: Arbitrary a => Gen (Identity a)
18-
identityGen = arbitrary >>= return . Identity
19-
-- identityGen = do
20-
-- a <- arbitrary
21-
-- return (Identity a)
32+
identityGen = do
33+
a <- arbitrary
34+
return (Identity a)
35+
2236

2337
instance Arbitrary a => Arbitrary (Identity a) where
2438
arbitrary = identityGen
2539

40+
2641
identityGenInt :: Gen (Identity Int)
2742
identityGenInt = identityGen
2843

44+
45+
-- Pair
46+
2947
data Pair a b = Pair a b deriving (Eq, Show)
3048

49+
3150
pairGen :: (Arbitrary a, Arbitrary b) => Gen (Pair a b)
3251
pairGen = do
3352
a <- arbitrary
3453
b <- arbitrary
3554
return (Pair a b)
3655

56+
3757
instance (Arbitrary a, Arbitrary b) => Arbitrary (Pair a b) where
3858
arbitrary = pairGen
3959

60+
4061
pairGenIntString :: Gen (Pair Int String)
4162
pairGenIntString = pairGen
4263

43-
data Sum a b = First a | Second b deriving (Eq, Show)
4464

45-
-- equal Odds
65+
-- Sum
66+
67+
data Sum a b
68+
= First a
69+
| Second b
70+
deriving (Eq, Show)
71+
72+
73+
-- equal odds for each
4674
sumGenEqual :: (Arbitrary a, Arbitrary b) => Gen (Sum a b)
4775
sumGenEqual = do
4876
a <- arbitrary
@@ -51,29 +79,20 @@ sumGenEqual = do
5179
, return $ Second b
5280
]
5381

54-
-- instance (Arbitrary a, Arbitrary b) => Arbitrary (Sum a b) where
55-
-- arbitrary = sumGenEqual
5682

5783
sumGenCharInt :: Gen (Sum Char Int)
5884
sumGenCharInt = sumGenEqual
5985

60-
-- from the QuickCheck library
61-
-- instance Arbitrary a => Arbitrary (Maybe a) where
62-
-- arbitrary = frequency [ (1, return Nothing)
63-
-- , (3, liftM Just arbitrary)
64-
-- ]
6586

6687
sumGenFirstPls :: (Arbitrary a, Arbitrary b) => Gen (Sum a b)
6788
sumGenFirstPls = do
6889
a <- arbitrary
6990
b <- arbitrary
70-
frequency [ (10, return $ First a)
71-
, (1, return $ Second b)
72-
]
91+
frequency
92+
[ (10, return $ First a)
93+
, (1, return $ Second b)
94+
]
95+
7396

7497
sumGenCharIntFirst :: Gen (Sum Char Int)
7598
sumGenCharIntFirst = sumGenFirstPls
76-
77-
main :: IO ()
78-
main = do
79-
sample trivialGen

ch14/qc/stack.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# This file was automatically generated by 'stack init'
2-
#
2+
#
33
# Some commonly used options have been documented as comments in this file.
44
# For advanced use and comprehensive documentation of the format, please see:
55
# http://docs.haskellstack.org/en/stable/yaml_configuration/
66

77
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
88
# A snapshot resolver dictates the compiler version and the set of packages
99
# to be used for project dependencies. For example:
10-
#
10+
#
1111
# resolver: lts-3.5
1212
# resolver: nightly-2015-09-21
1313
# resolver: ghc-7.10.2
1414
# resolver: ghcjs-0.1.0_ghc-7.10.2
1515
# resolver:
1616
# name: custom-snapshot
1717
# location: "./custom-snapshot.yaml"
18-
resolver: lts-5.17
18+
resolver: lts-8.2
1919

2020
# User packages to be built.
2121
# Various formats can be used as shown in the example below.
22-
#
22+
#
2323
# packages:
2424
# - some-directory
2525
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
@@ -31,7 +31,7 @@ resolver: lts-5.17
3131
# subdirs:
3232
# - auto-update
3333
# - wai
34-
#
34+
#
3535
# A package marked 'extra-dep: true' will only be built if demanded by a
3636
# non-dependency (i.e. a user package), and its test suites and benchmarks
3737
# will not be run. This is useful for tweaking upstream packages.
@@ -49,18 +49,18 @@ extra-package-dbs: []
4949

5050
# Control whether we use the GHC we find on the path
5151
# system-ghc: true
52-
#
52+
#
5353
# Require a specific version of stack, using version ranges
5454
# require-stack-version: -any # Default
55-
# require-stack-version: ">=1.1"
56-
#
55+
# require-stack-version: ">=1.3"
56+
#
5757
# Override the architecture used by stack, especially useful on Windows
5858
# arch: i386
5959
# arch: x86_64
60-
#
60+
#
6161
# Extra directories used by stack for building
6262
# extra-include-dirs: [/path/to/dir]
6363
# extra-lib-dirs: [/path/to/dir]
64-
#
64+
#
6565
# Allow a newer minor version of GHC than the snapshot specifies
6666
# compiler-check: newer-minor

0 commit comments

Comments
 (0)