当我执行Shape
或使用Main
时,Main
模块在stack build
模块内的:r
模块所在的目录中获取时出错} stack ghci
。
/home/vamsi/Do/learn-haskell/my-project/src/Main.hs:5:1: error:
Failed to load interface for ‘Shapes’
Use -v to see a list of the files searched for.
Failed, modules loaded: none.
在模块中添加-v
后运行:
[debug] Run process: ghc --interactive -i -odir=/home/vamsi/Do/learn-haskell/my-project/.stack-work/odir -hidir=/home/vamsi/Do/learn-haskell/my-project/.stack-work/odir -hide-all-packages -i/home/vamsi/Do/learn-haskell/my-project/src -i/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/autogen -i/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build -i/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/my-project/my-project-tmp -stubdir=/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build -package-id=base-4.9.1.0 -package-id=time-1.6.0.1 -package-id=aeson-1.0.2.1-CYngHPh1vosBJovY7C9thn -package-id=rethinkdb-2.2.0.9-Av4kLRfBxQS6TPGimEKUNt -optP-include -optP/tmp/vamsi/ghci11211/cabal_macros.h -ghci-script=/tmp/vamsi/ghci11211/ghci-script
@(Stack/Exec.hs:79:10)
2017-04-02 10:46:28.136659: [debug] Creating process: /home/vamsi/.stack/programs/x86_64-linux/ghc-8.0.2/bin/ghc --interactive -i -odir=/home/vamsi/Do/learn-haskell/my-project/.stack-work/odir -hidir=/home/vamsi/Do/learn-haskell/my-project/.stack-work/odir -hide-all-packages -i/home/vamsi/Do/learn-haskell/my-project/src -i/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/autogen -i/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build -i/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/my-project/my-project-tmp -stubdir=/home/vamsi/Do/learn-haskell/my-project/.stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build -package-id=base-4.9.1.0 -package-id=time-1.6.0.1 -package-id=aeson-1.0.2.1-CYngHPh1vosBJovY7C9thn -package-id=rethinkdb-2.2.0.9-Av4kLRfBxQS6TPGimEKUNt -optP-include -optP/tmp/vamsi/ghci11211/cabal_macros.h -ghci-script=/tmp/vamsi/ghci11211/ghci-script
我根据教程here创建了一个Shape
模块:
module Shapes(
Point(..),
Shape(..),
surface,
nudge,
baseCircle,
baseRectangle,
circle,
rectangle
) where
-- deriving Show automagically makes the type part of the Show typeclass
-- if there is only one value constructor for a type use the same name as the type to represent the value constructor
-- to export all value constructor for a type constructor use (..) after the type constructor's name like `Shape(..)`
-- to allow only the type constructor to be exported just write `Shape` instead.
-- if no type constructor is exported, use the auxiliary functions to create the types instead.
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point deriving (Show)
surface :: Shape -> Float
surface (Circle _ r) = pi * r ^ 2
surface (Rectangle (Point x1 y1) (Point x2 y2)) = abs (x2 -x1) * abs (y2 - y1)
nudge:: Shape -> Float -> Float -> Shape
nudge (Circle (Point x y) r) a b = Circle (Point (x + a) (y + b)) r
nudge (Rectangle (Point x1 y1) (Point x2 y2)) a b = Rectangle (Point (x1 + a) (y1 + b)) (Point (x2 + a) (y2 + b))
-- create auxiliary functions to avoid dealing with types directly
-- partially apply the value constructor and alias it as base circle
-- the function still needs a radius parameter
baseCircle :: Float -> Shape
baseCircle = Circle (Point 0 0)
baseRectangle :: Float -> Float -> Shape
baseRectangle w h = Rectangle (Point 0 0) (Point w h)
circle :: Float -> Float -> Float -> Shape
circle x y r = nudge (baseCircle r) x y
rectangle :: Float -> Float -> Float -> Float -> Shape
rectangle startX startY width height = nudge (baseRectangle width height) startX startY
我试图在main
中使用它:
module Main where
import System.IO (readFile)
import Data.Time (getCurrentTime)
import Data.Aeson (encode)
import Shapes (circle)
greet :: String -> String
greet xs = "Hello " ++ xs ++ "!"
printTime = do
time <- getCurrentTime
print time
printConfig = do
contents <- readFile "stack.yaml"
putStrLn contents
numbers:: [Int]
numbers = [1, 2, 3, 4, 5]
main :: IO ()
main = do
name <- getLine
putStrLn $ greet name
printConfig
printTime
print $ encode numbers
my-project.cabal
文件如下所示:
name: my-project
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/githubuser/my-project#readme
license: BSD3
license-file: LICENSE
author: Author name here
maintainer: example@example.com
copyright: 2017 Author name here
category: Web
build-type: Simple
cabal-version: >=1.10
extra-source-files: README.md
executable my-project
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5,
time,
aeson,
rethinkdb
更新
建议exposed-modules
文件中的build-depends
和*.cabal
:
exposed-modules: Shapes
build-depends: base >= 4.7 && < 5,
time,
aeson,
rethinkdb