为什么ghci不使用相对路径?

时间:2015-06-28 02:36:02

标签: haskell ghci ghc-mod

如果我的项目结构如下:

project/
  src/
    Foo.hs
    Bar.hs

使用文件Foo.hs:

module Foo where  

foo :: String
foo = "foo"

和Bar.hs:

module Bar where

import Foo 

bar :: String
bar = foo ++ "bar"

如果我当前的目录是src,我输入ghci并运行:l Bar.hs,我会得到预期的输出:

[1 of 2] Compiling Foo              ( Foo.hs, interpreted )
[2 of 2] Compiling Bar              ( Bar.hs, interpreted )
Ok, modules loaded: Bar, Foo.

但是,如果我升级到project目录(我喜欢留下并运行vim / ghci /等),并尝试:l src/Bar.hs,我得到:

src/Bar.hs:3:8:
    Could not find module ‘Foo’
    Use -v to see a list of the files searched for.
Failed, modules loaded: none.

为什么ghc不会在与Bar相同的目录中搜索Foo?我可以这样做吗?我可以将更改传播到ghc-mod然后传播到ghcmod.vim吗?因为当我在vim中运行语法检查程序或ghc-mod类型检查程序时,我发现无法找到模块的错误。

我正在运行ghc 7.10.1。

1 个答案:

答案 0 :(得分:5)

您正在寻找的旗帜是-i<dir>

% ghci --help
Usage:

    ghci [command-line-options-and-input-files]

...

In addition, ghci accepts most of the command-line options that plain
GHC does.  Some of the options that are commonly used are:

    -i<dir>         Search for imported modules in the directory <dir>.

例如

% ls src
Bar.hs Foo.hs
% ghci -isrc
GHCi, version 7.8.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
λ :l Foo
[1 of 1] Compiling Foo              ( src/Foo.hs, interpreted )
Ok, modules loaded: Foo.
λ :l Bar
[1 of 2] Compiling Foo              ( src/Foo.hs, interpreted )
[2 of 2] Compiling Bar              ( src/Bar.hs, interpreted )
Ok, modules loaded: Foo, Bar.

您还可以pass ghc-mod the -i<dir> flag from within ghcmod.vim

  

如果您想提供GHC选项,请设置g:ghcmod_ghc_options

let g:ghcmod_ghc_options = ['-idir1', '-idir2']
     

此外,还有缓冲区本地版本b:ghcmod_ghc_options

autocmd BufRead,BufNewFile ~/.xmonad/* call s:add_xmonad_path()
function! s:add_xmonad_path()
  if !exists('b:ghcmod_ghc_options')
    let b:ghcmod_ghc_options = []
  endif
  call add(b:ghcmod_ghc_options, '-i' . expand('~/.xmonad/lib'))
endfunction