計算機科学のブログ

コードの整理とプロジェクトのビルド Monad型クラス QuickCheckを使ったプロパティテスト さまざまな種類のテスト 手動でのテストとstackからのGHCiの呼び出し

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT6(コードの整理とプロジェクトのビルド)、LESSON36(QuickCheckを使ったプロパティテスト)、36.2(さまざまな種類のテスト)、手動でのテストとstackからのGHCiの呼び出し、クイックチェック 37-2の解答を求めてみる。

コード

src/Main.hs

module Main where

import Lib ()

main :: IO ()
main = putStrLn "Hello World!"

コード

src/Lib.hs

module Lib
  ( isPalindrome,
  )
where

isPalindrome :: String -> Bool
isPalindrome text = text == reverse text

入出力結果(Terminal, Zsh)

% stack ghci
Using main module: 1. Package `palindrome-testing' component palindrome-testing:exe:palindrome-testing-exe with main-is file: /Users/…/palindrome-testing/app/Main.hs
palindrome-testing> initial-build-steps (lib + exe)
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: palindrome-testing

* * * * * * * *

Warning: Multiple files use the same module name:
         * Paths_palindrome_testing found at the following paths
           * /Users/…/palindrome-testing/.stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/autogen/Paths_palindrome_testing.hs (palindrome-testing:lib)
           * /Users/…/palindrome-testing/.stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/palindrome-testing-exe/autogen/Paths_palindrome_testing.hs (palindrome-testing:exe:palindrome-testing-exe)
* * * * * * * *

GHCi, version 8.10.7: https://www.haskell.org/ghc/  :? for help
macro 'doc' overwrites builtin command.  Use ':def!' to overwrite.
(0.00 secs, 0 bytes)
(0.00 secs, 0 bytes)
Loaded GHCi configuration from /Users/…/.ghc/ghci.conf
[1 of 3] Compiling Lib              ( /Users/…/palindrome-testing/src/Lib.hs, interpreted )
[2 of 3] Compiling Main             ( /Users/…/palindrome-testing/app/Main.hs, interpreted )
[3 of 3] Compiling Paths_palindrome_testing ( /Users/…/palindrome-testing/.stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/autogen/Paths_palindrome_testing.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /private/var/folders/2y/nfx9b0d9267fxn7rhzybw2xh0000gn/T/haskell-stack-ghci/959789a9/ghci-script
*Main Lib Paths_palindrome_testing
λ> isPalindrome "sam I mas"
True
it :: Bool
(0.02 secs, 447,944 bytes)
*Main Lib Paths_palindrome_testing
λ> :quit
Leaving GHCi.
%