計算機科学のブログ

Haskellを使用するための準備 実行可能ファイル

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のLESSON 1(Haskellを使用するための準備)、1.6(練習問題)Q1-2の解答を求めてみる。

email/package.yaml

name:                email
version:             0.1.0.0
github:              "githubuser/email"
license:             BSD3
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2022 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/email#readme>

dependencies:
- base >= 4.7 && < 5
- text

library:
  source-dirs: src

executables:
  email-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - email


tests:
  email-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - email

default-extensions: OverloadedStrings

コード

email/app/Main.hs

module Main where

import Data.Text.IO as TIO
import Lib (createEmail)

main :: IO ()
main = do
  --   print "Who is the email for?"
  --   recipient <- getLine
  --   print "What is the Title?"
  --   title <- getLine
  --   print "Who is the Author?"
  --   author <- getLine
  --   print $ createEmail recipient title author
  TIO.putStrLn "Who is the email for?"
  recipient <- TIO.getLine
  TIO.putStrLn "What is the Title?"
  title <- TIO.getLine
  TIO.putStrLn "Who is the Author?"
  author <- TIO.getLine
  TIO.putStrLn $ createEmail recipient title author

コード

email/src/Lib.hs

module Lib
  ( createEmail,
  )
where

import Data.Text as T

-- toPart :: String -> String
toPart :: T.Text -> T.Text
toPart recipient =
  mconcat
    [ "Dear ",
      recipient,
      ",\n"
    ]

-- bodyPart :: String -> String
bodyPart :: T.Text -> T.Text
bodyPart bookTitle =
  mconcat
    [ "Thanks for buying ",
      bookTitle,
      ".\n"
    ]

-- fromPart :: String -> String
fromPart :: T.Text -> T.Text
fromPart author =
  mconcat
    [ "Thanks,\n",
      author
    ]

-- createEmail :: String -> String -> String -> String
createEmail :: T.Text -> T.Text -> T.Text -> T.Text
createEmail recipient bookTitle author =
  mconcat
    [ toPart recipient,
      bodyPart bookTitle,
      fromPart author
    ]

入出力結果(Terminal, Zsh)

% stack new email
Downloading template "new-template" to create project "email" in email/ ...
                                                                                
The following parameters were needed by the template but not provided: author-name
You can provide them in /Users/…/.stack/config.yaml, like this:
templates:
  params:
    author-name: value
Or you can pass each one as parameters like this:
stack new email new-template -p "author-name:value"

                                                                                
The following parameters were needed by the template but not provided: author-email, author-name, category, copyright, github-username
You can provide them in /Users/…/.stack/config.yaml, like this:
templates:
  params:
    author-email: value
    author-name: value
    category: value
    copyright: value
    github-username: value
Or you can pass each one as parameters like this:
stack new email new-template -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value"

Looking for .cabal or package.yaml files to use to init the project.            
Using cabal packages:                                                           
- email/                                                                        

Selecting the best among 21 snapshots...                                        

* Matches https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19/4.yaml
                                                                                
Selected resolver: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19/4.yaml
Initialising configuration using resolver: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/19/4.yaml
Total number of user packages considered: 1                                     
Writing configuration to file: email/stack.yaml                                 
All done.                                                                       
/Users/…/.stack/templates/new-template.hsfiles:    6.06 KiB downloaded...
…@MacBook lesson1 % cd email 
% stack setup
stack will use a sandboxed GHC it installed
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec
% stack build
Building all executables for `email' once. After a successful build of all of them, only specified executables will be rebuilt.
email> configure (lib + exe)
Configuring email-0.1.0.0...
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_email
Preprocessing executable 'email-exe' for email-0.1.0.0..
Building executable 'email-exe' for email-0.1.0.0..
[1 of 2] Compiling Main
[2 of 2] Compiling Paths_email
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/email-exe/email-exe ...
email> copy/register
Installing library in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/lib/x86_64-osx-ghc-9.0.2/email-0.1.0.0-3I4M0k9NFYZI5igWgZ1YPp
Installing executable email-exe in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/bin
Registering library for email-0.1.0.0..
% code .
% stack build
email-0.1.0.0: unregistering (local file changes: app/Main.hs src/Lib.hs)
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
[2 of 2] Compiling Lib
Preprocessing executable 'email-exe' for email-0.1.0.0..
Building executable 'email-exe' for email-0.1.0.0..
[2 of 2] Compiling Main
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/email-exe/email-exe ...
email> copy/register
Installing library in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/lib/x86_64-osx-ghc-9.0.2/email-0.1.0.0-3I4M0k9NFYZI5igWgZ1YPp
Installing executable email-exe in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/bin
Registering library for email-0.1.0.0..
% stack exec email-exe
"Who is the email for?"
Happy Reader
"What is the Title?"
Learn Haskell
"Who is the Author?"
Will Kurt
"DearHappy Reader,\nThanks for buyingLearn Haskell.\nThanks,\nWill Kurt"
% stack build         
email-0.1.0.0: unregistering (local file changes: src/Lib.hs)
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
[2 of 2] Compiling Lib
Preprocessing executable 'email-exe' for email-0.1.0.0..
Building executable 'email-exe' for email-0.1.0.0..
[1 of 2] Compiling Main [Lib changed]
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/email-exe/email-exe ...
email> copy/register
Installing library in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/lib/x86_64-osx-ghc-9.0.2/email-0.1.0.0-3I4M0k9NFYZI5igWgZ1YPp
Installing executable email-exe in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/bin
Registering library for email-0.1.0.0..
% stack exec email-exe
"Who is the email for?"
Happy Reader
"What is the Title?"
Learn Haskell
"Who is the Author?"
Will Kurt
"Dear Happy Reader,\nThanks for buying Learn Haskell.\nThanks,\nWill Kurt"
% cat ../../
% stack build
email-0.1.0.0: unregistering (dependencies changed)
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
[1 of 2] Compiling Lib

/Users/…/email/src/Lib.hs:6:1: error:
    Could not load module ‘Data.Text’
    It is a member of the hidden package ‘text-1.2.5.0’.
    Perhaps you need to add ‘text’ to the build-depends in your .cabal file.
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
6 | import Data.Text as T
  | ^^^^^^^^^^^^^^^^^^^^^

--  While building package email-0.1.0.0 (scroll up to its section to see the error) using:
      /Users/…/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.4.1.0_ghc-9.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.4.1.0 build lib:email exe:email-exe --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
% stack build
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
[1 of 2] Compiling Lib

/Users/…/email/src/Lib.hs:6:1: error:
    Could not load module ‘Data.Text’
    It is a member of the hidden package ‘text-1.2.5.0’.
    Perhaps you need to add ‘text’ to the build-depends in your .cabal file.
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
6 | import Data.Text as T
  | ^^^^^^^^^^^^^^^^^^^^^

--  While building package email-0.1.0.0 (scroll up to its section to see the error) using:
      /Users/…/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.4.1.0_ghc-9.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.4.1.0 build lib:email exe:email-exe --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
% stack build
email> configure (lib + exe)
Configuring email-0.1.0.0...
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_email
Preprocessing executable 'email-exe' for email-0.1.0.0..
Building executable 'email-exe' for email-0.1.0.0..
[1 of 2] Compiling Main

/Users/…/email/app/Main.hs:9:3: error:
    Not in scope: ‘TIO.putStrLn’
    Perhaps you meant one of these:
      ‘T.IO.putStrLn’ (imported from Data.Text.IO),
      ‘T.IO.putStr’ (imported from Data.Text.IO),
      ‘T.IO.hPutStrLn’ (imported from Data.Text.IO)
    No module named ‘TIO’ is imported.
  |
9 |   TIO.putStrLn "Who is the email for?"
  |   ^^^^^^^^^^^^

/Users/…/email/app/Main.hs:10:16: error:
    Ambiguous occurrence ‘getLine’
    It could refer to
       either ‘Prelude.getLine’,
              imported from ‘Prelude’ at app/Main.hs:1:8-11
              (and originally defined in ‘System.IO’)
           or ‘T.IO.getLine’,
              imported from ‘Data.Text.IO’ at app/Main.hs:3:1-27
   |
10 |   recipient <- getLine
   |                ^^^^^^^

/Users/…/email/app/Main.hs:12:3: error:
    Not in scope: ‘TIO.putStrLn’
    Perhaps you meant one of these:
      ‘T.IO.putStrLn’ (imported from Data.Text.IO),
      ‘T.IO.putStr’ (imported from Data.Text.IO),
      ‘T.IO.hPutStrLn’ (imported from Data.Text.IO)
    No module named ‘TIO’ is imported.
   |
12 |   TIO.putStrLn "What is the Title?"
   |   ^^^^^^^^^^^^

/Users/…/email/app/Main.hs:13:12: error:
    Ambiguous occurrence ‘getLine’
    It could refer to
       either ‘Prelude.getLine’,
              imported from ‘Prelude’ at app/Main.hs:1:8-11
              (and originally defined in ‘System.IO’)
           or ‘T.IO.getLine’,
              imported from ‘Data.Text.IO’ at app/Main.hs:3:1-27
   |
13 |   title <- getLine
   |            ^^^^^^^

/Users/…/email/app/Main.hs:15:3: error:
    Not in scope: ‘TIO.putStrLn’
    Perhaps you meant one of these:
      ‘T.IO.putStrLn’ (imported from Data.Text.IO),
      ‘T.IO.putStr’ (imported from Data.Text.IO),
      ‘T.IO.hPutStrLn’ (imported from Data.Text.IO)
    No module named ‘TIO’ is imported.
   |
15 |   TIO.putStrLn "Who is the Author?"
   |   ^^^^^^^^^^^^

/Users/…/email/app/Main.hs:16:13: error:
    Ambiguous occurrence ‘getLine’
    It could refer to
       either ‘Prelude.getLine’,
              imported from ‘Prelude’ at app/Main.hs:1:8-11
              (and originally defined in ‘System.IO’)
           or ‘T.IO.getLine’,
              imported from ‘Data.Text.IO’ at app/Main.hs:3:1-27
   |
16 |   author <- getLine
   |             ^^^^^^^

/Users/…/email/app/Main.hs:18:3: error:
    Not in scope: ‘TIO.putStrLn’
    Perhaps you meant one of these:
      ‘T.IO.putStrLn’ (imported from Data.Text.IO),
      ‘T.IO.putStr’ (imported from Data.Text.IO),
      ‘T.IO.hPutStrLn’ (imported from Data.Text.IO)
    No module named ‘TIO’ is imported.
   |
18 |   TIO.putStrLn $ createEmail recipient title author
   |   ^^^^^^^^^^^^

--  While building package email-0.1.0.0 (scroll up to its section to see the error) using:
      /Users/…/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.4.1.0_ghc-9.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.4.1.0 build lib:email exe:email-exe --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
% stack build\
> 
% stack build 
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
Preprocessing executable 'email-exe' for email-0.1.0.0..
Building executable 'email-exe' for email-0.1.0.0..
[1 of 2] Compiling Main

/Users/…/email/app/Main.hs:10:16: error:
    Ambiguous occurrence ‘getLine’
    It could refer to
       either ‘Prelude.getLine’,
              imported from ‘Prelude’ at app/Main.hs:1:8-11
              (and originally defined in ‘System.IO’)
           or ‘TIO.getLine’,
              imported from ‘Data.Text.IO’ at app/Main.hs:3:1-26
   |
10 |   recipient <- getLine
   |                ^^^^^^^

/Users/…/email/app/Main.hs:13:12: error:
    Ambiguous occurrence ‘getLine’
    It could refer to
       either ‘Prelude.getLine’,
              imported from ‘Prelude’ at app/Main.hs:1:8-11
              (and originally defined in ‘System.IO’)
           or ‘TIO.getLine’,
              imported from ‘Data.Text.IO’ at app/Main.hs:3:1-26
   |
13 |   title <- getLine
   |            ^^^^^^^

/Users/…/email/app/Main.hs:16:13: error:
    Ambiguous occurrence ‘getLine’
    It could refer to
       either ‘Prelude.getLine’,
              imported from ‘Prelude’ at app/Main.hs:1:8-11
              (and originally defined in ‘System.IO’)
           or ‘TIO.getLine’,
              imported from ‘Data.Text.IO’ at app/Main.hs:3:1-26
   |
16 |   author <- getLine
   |             ^^^^^^^

--  While building package email-0.1.0.0 (scroll up to its section to see the error) using:
      /Users/…/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_3.4.1.0_ghc-9.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-3.4.1.0 build lib:email exe:email-exe --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
% stack build
email> build (lib + exe)
Preprocessing library for email-0.1.0.0..
Building library for email-0.1.0.0..
Preprocessing executable 'email-exe' for email-0.1.0.0..
Building executable 'email-exe' for email-0.1.0.0..
[1 of 2] Compiling Main
[2 of 2] Compiling Paths_email
Linking .stack-work/dist/x86_64-osx/Cabal-3.4.1.0/build/email-exe/email-exe ...
email> copy/register
Installing library in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/lib/x86_64-osx-ghc-9.0.2/email-0.1.0.0-1ivPkMLe8fB2gd3w3G8FAT
Installing executable email-exe in /Users/…/email/.stack-work/install/x86_64-osx/d2294ed6a5c94682cf7e69e26cdfea24180c06d32bfe03638b87987d7ac393d8/9.0.2/bin
Registering library for email-0.1.0.0..
% stack exec email-exe
Who is the email for?
Happy Reader
What is the Title?
Learn Haskell
Who is the Author?
Will Kurt
Dear Happy Reader,
Thanks for buying Learn Haskell.
Thanks,
Will Kurt
%