如何在 GHC-8.2.2 和 Cabal-2.0.0.1 中使用 Text.Parsec

How to use Text.Parsec in GHC-8.2.2 and Cabal-2.0.0.1

据我所知,Text.ParserCombinators.ParsecText.Parsec

取代

这里是我的环境

4.9.73-1-MANJARO

The Glorious Glasgow Haskell Compilation System, version 8.2.2

cabal-install version 2.0.0.1
compiled using version 2.0.1.0 of the Cabal library 

以下源码是我的main.hs

module Main where
import System.Environment
import Text.Parsec

main :: IO ()
main = do
     args <- getArgs
     putStrLn (readExpr (args !! 0))

然后我编译它

$ ghc -package parsec -o main main.hs

出现如下错误信息

[1 of 1] Compiling Main             ( main.hs, main.o )

main.hs:3:1: error:
    Could not find module ‘Text.Parsec’
    There are files missing in the ‘parsec-3.1.11’ package,
    try running 'ghc-pkg check'.
    Use -v to see a list of the files searched for.
  |
3 | import Text.Parsec
  | ^^^^^^^^^^^^^^^^^^
rm: cannot remove '*.hi': No such file or directory
./run.sh: line 11: ./TestProj: No such file or directory

我确定我已经安装了 parsec。所以我想问一下我做错了什么?

$ cabal install parsec
Resolving dependencies...
All the requested packages are already installed:
parsec-3.1.11
Use --reinstall if you want to reinstall anyway.

Manjaro 可能继承了 Arch 的 Haskell 问题。

这是怎么回事

Arch 安装动态库,但 ghc 默认静态链接。这也是错误信息 "There are files missing in the ... package" 的内容,表明包存在但不包含 ghc 正在查找的内容。

如果我尝试在启用 -v 详细标志的情况下进行编译,错误消息将扩展为:

ghc -v main.hs

(...)
Locations searched:
  Text/Parsec.hs
  Text/Parsec.lhs
  Text/Parsec.hsig
  Text/Parsec.lhsig
  /usr/lib/ghc-8.2.2/site-local/parsec-3.1.11/Text/Parsec.hi
(...)

尤其要查看上次报告的位置,这可能与您的系统不同;如果我的猜测是正确的,ghc 正在寻找静态接口文件 .hi,如消息所示,但只有动态接口文件 .dyn_hi.

修复

使用 -dynamic 标志编译。

 ghc -dynamic main.hs

如果可行,请阅读此内容以修复设置:

https://wiki.archlinux.org/index.php/Haskell

您将不得不在静态链接和动态链接之间做出选择;我实际上不明白这里的权衡。