为什么ghci不使用相对路径



如果我有一个这样结构的项目:

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/anything),并尝试: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。

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

您也可以从ghcmod.vim 中向ghc-mod传递-i<dir>标志

如果要提供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

最新更新