为什么我不能导入这个Haskell模块? [英] Why can't I import this Haskell module?
问题描述
我把它放在〜/ Desktop / Shapes.hs
中:
模块形状
(Shape(Rectangle)
)其中
data Shape = Circle |矩形派生(显示)
然后我做到这一点:
cd〜/桌面
ghci
ghci> :m +形状
<无位置信息>:
找不到模块`形状'
它不是当前程序或任何已知包中的模块。
ghci>导入形状
<无位置信息>:
找不到模块`形状'
它不是当前程序或任何已知软件包中的模块。
为什么我得到这个错误?
我也试着先用 ghc -c Shapes.hs
编译。它仍然不起作用。
我在OS X 10.9.2上安装了来自haskell.org的Haskell Platform 2013.2.0.0 for Mac OS X,64位小牛。我也跟着他们的 ghc-clang-wrapper
指示。
更新
有人建议先做:l Shapes.hs
。问题是:l Shapes.hs
加载了整个Shapes文件,这意味着我可以访问 Circle code>值构造函数,即使我没有导出它。查看我之前的问题:为什么我可以使用这个私人值构造函数?我只想加载模块。这可能吗?
您需要加载 Shapes.hs
首先由:l Shapes.hs
。
-
因为您的
Shapes
尚未加载,因此:m Shapes
不起作用。 Because your
Shapes
have not been loaded, so:m Shapes
will not work.Because your
Shapes
does not exist in a compiled package thatghci
can find, soimport Shapes
will not work.
因为你的
Shapes
不存在于 ghci
可以找到的编译包中,所以 import Shapes
不起作用。 在:load
模块之后,可以使用:module 或 import
只能导入这些符号。例如,在之后:load Shapes.hs
和:module Shapes
, Rectangle $
请参阅:
href =http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html#ghci-scope =nofollow>提示符的范围有多大?
:module和:加载
I put this in ~/Desktop/Shapes.hs
:
module Shapes
( Shape(Rectangle)
) where
data Shape = Circle | Rectangle deriving (Show)
I then do this:
cd ~/Desktop
ghci
ghci> :m +Shapes
<no location info>:
Could not find module `Shapes'
It is not a module in the current program, or in any known package.
ghci> import Shapes
<no location info>:
Could not find module `Shapes'
It is not a module in the current program, or in any known package.
Why do I get this error?
I also tried compiling first with ghc -c Shapes.hs
. It still doesn't work.
I installed the "Haskell Platform 2013.2.0.0 for Mac OS X, 64 bit" from haskell.org on my OS X 10.9.2 Mavericks. I also followed their ghc-clang-wrapper
instructions.
Update:
Someone suggested doing :l Shapes.hs
first. The problem is that :l Shapes.hs
loads the entire Shapes file, meaning I have access to the Circle
value constructor even if I did not export it. See my earlier question: Why can I use this "private" value constructor? I want to load only the module. Is this possible?
解决方案 You need to load your Shapes.hs
first by :l Shapes.hs
.
If you only want the exported symbols in scope, after :load
a module, you could use :module
or import
to only import those symbols. For example, after :load Shapes.hs
and :module Shapes
, Rectangle
will in scope but Circle
will not.
See:
What's really in scope at the prompt?
:module and :load
这篇关于为什么我不能导入这个Haskell模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!