如何在GHCi中导入Haskell模块? [英] How can I import a Haskell module in GHCi?

查看:256
本文介绍了如何在GHCi中导入Haskell模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从《学好Haskell,造福人类》这本书中教自己Haskell.我已经读到了第7章(模块)的最后一节,其中介绍了如何创建自己的模块.我复制并粘贴了本节开头的书中给出的Geometry模块.正如书中所建议的那样,该文件的名称为Geometry.hs,该文件位于ghci的bin目录中,这是我以前能够使用:l成功加载另一个.hs文件的位置.

I am trying to teach myself Haskell from the book Learn You A Haskell for Great Good. I got up to the last section of chapter 7 (Modules), where it tells how to create your own module. I did a copy and paste of the Geometry module given in the book at the beginning of the section. The name of the file is Geometry.hs, as the book suggested, and the file is in the bin directory for ghci, which is where I previously was able to successfully do a load using :l for another .hs file.

当我在GHCi中键入以下命令时

When I type the following command in GHCi

import Geometry

我收到以下错误:

找不到模块'Geometry'当前不是模块 程序或任何已知软件包中

Could not find module 'Geometry' It is not a module in the current program or in any known package

我必须做的事情显然是错误的,但我不知道是什么.

I must be doing something that is obviously wrong, but I can't figure out what it is.

推荐答案

在GHCi中使用import ModuleName时,它的工作方式(大部分)与import Data.List的工作方式相同:GHC检查本地软件包数据库中的模块,加载它,并将其(导出的)内容放入范围.

When you use import ModuleName in GHCi, it works (mostly) in the same way import Data.List works: GHC checks your local package database for the module, loads it, and brings its (exported) contents into scope.

但是,Geometry不是随ghc-pkg安装的软件包的模块.因此,GHC根本不知道模块Geometry存在.它也不是交互式变体GHCi.

However, Geometry isn't a module of a package installed with ghc-pkg. Therefore, GHC doesn't know that a module Geometry exists at all. Neither does it interactive variant GHCi.

但是,如果您:l有一个程序,情况就会改变. GHC将考虑使用的模块:

But if you :load a program, things change. GHC will take its used modules into account:

-- Foo.hs
module Foo where

foo :: IO ()
foo = putStrLn "Hello from foo!"

-- Main.hs
module Main where
import Foo (foo)

main :: IO ()
main = foo

$ cd /path/to/your/files
$ ghci
GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> import Foo

<no location info>:
    Could not find module ‘Foo’
    It is not a module in the current program, or in any known package.

Prelude> :l Main.hs
[1 of 2] Compiling Foo              ( Foo.hs, interpreted )
[2 of 2] Compiling Main             ( Main.hs, interpreted )
Ok, modules loaded: Main, Foo.
*Main> :l Main.hs
*Main> foo
Hello from foo!
*Main> import Foo
*Main Foo> -- module now loaded

如您所见,导入<​​c7>首先失败.但是,在实际加载使用Foo的程序之后,便可以在GHCi中使用import Foo.

As you can see, importing Foo first failed. However, after we've actually loaded the program that uses Foo, we were able to use import Foo in GHCi.

因此,如果要在GHCi中使用import,请确保GHC可以找到模块,方法是将其包含在包装器中或安装它.如果只想加载模块本身,请使用:l oad.

So if you want to use import in GHCi, make sure that GHC can find your module, either by including it in a wrapper or installing it. If you just want to load the module itself, use :load.

这篇关于如何在GHCi中导入Haskell模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆