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

Why does ghci not use relative paths?

如果我的项目结构如下:

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/whatever),然后尝试 :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 类型检查器时,我收到关于找不到 module 的错误

我是 运行ning ghc 7.10.1.

您要查找的标志是 -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

If you'd like to give GHC options, set g:ghcmod_ghc_options.

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

Also, there's buffer-local version 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