Haskell - コードの整理とプロジェクトのビルド - QuickCheckを使ったプロパティテスト - 対応する型の範囲を広げる, quickcheck-instancesパッケージ
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT6(コードの整理とプロジェクトのビルド)、LESSON 36(QuickCHeckを使ったプロパティテスト)、36.5(練習問題)、 Q36-1の解答を求めてみる。
軽量マークアップ言語
palindrome-testing/package.yaml
name: palindrome-testing
version: 0.1.0.0
github: "githubuser/palindrome-testing"
license: BSD-3-Clause
author: "Author name here"
maintainer: "example@example.com"
copyright: "2025 Author name here"
extra-source-files:
- README.md
- CHANGELOG.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/palindrome-testing#readme>
dependencies:
- base >= 4.7 && < 5
- text
- QuickCheck
- quickcheck-instances
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
default-extensions:
- OverloadedStrings
library:
source-dirs: src
executables:
palindrome-testing-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- palindrome-testing
tests:
palindrome-testing-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- palindrome-testing
コード
palindrome-testing/src/Lib.hs
module Lib (isPalindrome, preProcess) where
import qualified Data.Text as T
import Data.Char (isSpace,isPunctuation)
stripWhiteSpace :: T.Text -> T.Text
stripWhiteSpace = T.filter (not . isSpace)
stripPuctuation :: T.Text -> T.Text
stripPuctuation = T.filter (not . isPunctuation)
preProcess :: T.Text -> T.Text
preProcess = stripWhiteSpace . stripPuctuation . T.toLower
isPalindrome :: T.Text -> Bool
isPalindrome text = cleanText == T.reverse cleanText
where cleanText = preProcess text
palindrome-testing/test/Spec.hs
import Lib
import Data.Char(isSpace,isPunctuation)
import Test.QuickCheck
import Test.QuickCheck.Instances()
import qualified Data.Text as T
prop_whitespaceInvariant :: T.Text -> Bool
prop_whitespaceInvariant text =
let noSpacText = T.filter (not . isSpace) text
in preProcess text == preProcess noSpacText
prop_punctuationInvariant :: T.Text -> Bool
prop_punctuationInvariant text =
let noPuncText = T.filter (not . isPunctuation) text
in preProcess text == preProcess noPuncText
prop_upperInvariant :: T.Text -> Bool
prop_upperInvariant text =
let upper = T.toUpper text
in preProcess text == preProcess upper
prop_lowerInvariant :: T.Text -> Bool
prop_lowerInvariant text =
let lower = T.toLower text
in preProcess text == preProcess lower
prop_reverseInvarient :: T.Text -> Bool
prop_reverseInvarient text = isPalindrome text == isPalindrome (T.reverse text)
main :: IO ()
main = do
quickCheck prop_whitespaceInvariant
quickCheck prop_punctuationInvariant
quickCheck prop_upperInvariant
quickCheck prop_lowerInvariant
quickCheck prop_reverseInvarient
putStrLn "done!"
入出力結果(Terminal, Zsh)
% stack test
palindrome-testing> build (lib + exe + test) with ghc-9.8.4
Preprocessing library for palindrome-testing-0.1.0.0..
Building library for palindrome-testing-0.1.0.0..
[1 of 2] Compiling Lib [Source file changed]
Preprocessing executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Building executable 'palindrome-testing-exe' for palindrome-testing-0.1.0.0..
Preprocessing test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
Building test suite 'palindrome-testing-test' for palindrome-testing-0.1.0.0..
palindrome-testing> copy/register
Installing library in /Users/.../palindrome-testing/.stack-work/install/aarch64-osx/e7436b3217cf53d6b9beeebd2bdc5dd8dcab6f8595b8bde259631442ce745f25/9.8.4/lib/aarch64-osx-ghc-9.8.4/palindrome-testing-0.1.0.0-Jc7lOYEDWk94e4Cs0gDnxC
Installing executable palindrome-testing-exe in /Users/.../palindrome-testing/.stack-work/install/aarch64-osx/e7436b3217cf53d6b9beeebd2bdc5dd8dcab6f8595b8bde259631442ce745f25/9.8.4/bin
Registering library for palindrome-testing-0.1.0.0..
palindrome-testing> test (suite: palindrome-testing-test)
+++ OK, passed 100 tests.
+++ OK, passed 100 tests.
+++ OK, passed 100 tests.
+++ OK, passed 100 tests.
+++ OK, passed 100 tests.
done!
palindrome-testing> Test suite palindrome-testing-test passed
Completed 2 action(s).
%