Haskell可以编译代码,还是只能使用’cabal repl –build-depends fmt’?

huangapple go评论59阅读模式
英文:

can haskell compile code or just 'cabal repl --build-depends fmt'?

问题

You can use the following translated parts:

在Haskell中,我可以使用以下代码导入Fmt模块:

C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>cabal repl --build-depends fmt

但是,我无法使用以下代码导入Fmt模块:

C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>ghc vocab3.hs

关于使用GHC编译还是使用Cabal运行代码的选择,你可以考虑以下原因:

  • 使用GHC编译:编译器通常执行更多的优化,生成更快的可执行文件,适用于发布最终产品。但是,你需要确保所有依赖项都在GHC的可搜索路径中,否则会出现找不到模块的错误。
  • 使用Cabal运行:Cabal是Haskell的构建工具和包管理器,它可以自动解决依赖关系并提供更方便的开发环境。你可以使用cabal repl来交互式运行代码,也可以使用cabal build构建可执行文件。这对于开发和测试代码非常方便,尤其是处理依赖关系时。

你可以根据你的需求和开发流程来选择适合你的方法。

英文:

i can use this to import Fmt in haskell

C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>cabal repl --build-depends fmt
Resolving dependencies...
Build profile: -w ghc-9.6.1 -O1
In order, the following will be built (use -v for more details):
...
...
...
Warning: No exposed modules
GHCi, version 9.6.1: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from C:\\Users\\Administrator\\AppData\\Local\\Temp\\cabal-repl.-5812\\setcwd.ghci
ghci> import Fmt
ghci>

but i cant

C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>ghc vocab3.hs
[1 of 2] Compiling Main             ( vocab3.hs, vocab3.o )

vocab3.hs:12:1: error:
    Could not find module ‘Fmt’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
12 | import Fmt
   | ^^^^^^^^^^

C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>
C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>
C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>
C:\Users\Administrator\Downloads\hid-examples-master\hid-examples-master\ch01>ghc vocab3.hs --build-depends fmt
ghc-9.6.1.exe: unrecognised flag: --build-depends

Usage: For basic information, try the `--help' option.

can i use ghc to compile ? i like to compile code cause i think compiler is better than interpreter
or just use cabal to run code ?
can you explain why ?

答案1

得分: 1

任何“真正的”开发都应该始终在一个(Cabal)包内进行。在包内使用 cabal build 将以正确的选项运行编译器以找到您指定的任何库。一旦使用 cabal 构建了一个程序,它就是自己的可执行文件,无需再需要 cabal。只是在你运行它之前,cabal install,可执行文件深藏在目录树中,因此 cabal run 是找到并运行程序的最简单方式。您可以使用 find dist-newstyle -type f -perm -u+x 来找到实际的编译可执行文件。

使用 cabal 的原因是在 Haskell 生态系统中全局安装包(仅按名称标识)是不切实际的。一周之内,安装一个可执行文件可能会拉取某些库以特定版本。下周,这些库可能会更新,不同的可执行文件可能需要新版本。通过包管理器/构建系统来调解对包的访问是为了避免陷入混乱的必要之举。这与 Python 有 Pip 和 venv,Java 有 Maven 的原因相同。

话虽如此,如果您真的想直接使用 GHC,而不会搞出一团糟,方法如下:

# 假设您有一些代码在 vocab03.hs 中
$ cabal install --lib fmt --package-env=./my-env # cabal 将下载并编译 fmt(如果需要),然后写出一个文件,可用于指示 GHC 找到构建结果
$ ghc -package-env ./my-env -outputdir build vocab03.hs # 直接编译调用(!!)编译器
# 任何“混乱”都将包含在文件 ./my-env 中(即将来对 ./my-env 的 cabal install 可能会由于冲突的包版本而失败)
# 实际上,多次 cabal install 到同一环境文件总是在我上次检查时都是错误的
# 但无论如何,您总是可以删除该文件,然后使用新的包列表再次运行 cabal-install

也就是说,使用 cabal 构建 fmt 并写出一个文件以定位构建结果,然后使用 ghc 编译您自己的代码在该构建之上。

英文:

Any "real" development should always be done inside a (Cabal) package. cabal build inside a package will run the compiler with the correct options to find any libraries you specify. Once a program is built with cabal, it is its own executable that runs without needing cabal anymore. It's just that, until you cabal install it, the executable is buried very deep in a directory tree, so cabal run is the easiest way to find the program to run it. You can issue find dist-newstyle -type f -perm -u+x to find the actual compiled executable for yourself.

The reason to use cabal is that it is impractical to install packages globally (keyed only by name) in the Haskell ecosystem. One week, installing an executable may pull in certain libraries at certain versions. The next week, the libraries may update, and a different executable might want the new versions. Mediating access to packages through a package manager/build system is necessary to avoid getting into messes. This is the same reason Python has Pip and venvs and Java has Maven.

That said, if you really want to use GHC directly, the way to do it without making an irreparable mess is

<!-- language: sh -->

# assuming you have some code in vocab03.hs
$ cabal install --lib fmt --package-env=./my-env # cabal will download and compile fmt (if necessary), then write out a file that can be used to point GHC at the build product
$ ghc -package-env ./my-env -outputdir build vocab03.hs # direct(!!) compiler invocation
# any &quot;messes&quot; will be contained inside the file ./my-env (i.e. a future cabal install into ./my-env may fail due to conflicting package versions)
# actually, multiple cabal installs into the same environment file was always broken last I checked
# but either way you can always just delete the file and cabal-install again with a new list of packages

That is, use cabal to build fmt and write out a file locating the build result, then use ghc to compile your own code on top of that build.

huangapple
  • 本文由 发表于 2023年6月1日 02:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定