haskell - 无法在 Haskell 中导入模块

标签 haskell visual-studio-code haskell-stack

您好,我正在尝试将 Haskell 与 vscode 一起使用,如果我尝试在另一个模块中使用模块,我会收到以下错误:

Failed to load interface for **Client**
Use -v to see a list of the files searched for.

第一个模块:

module Main where 

import qualified Client as Cl

main ::IO ()
main = do
name<- getLine
sp

第二个模块:

    module Client 
    (sp)where

    sp::IO()
    sp=print "SecondModule"

经过一番阅读后,我尝试更新堆栈并安装构建工具,此时我收到以下错误:

   $ stack install gtk2hs-buildtools
    ←[0mprimitive-0.6.3.0: using precompiled package←[0m
    ←[0mCabal-2.0.1.1: using precompiled package←[0m
    ←[0mmtl-2.2.1: using precompiled package←[0m
    ←[0mrandom-1.1: using precompiled package←[0m
    ←[0mtext-1.2.2.2: using precompiled package←[0m
    ←[0mvector-0.12.0.1: using precompiled package←[0m
    ←[0mtf-random-0.5: configure←[0m
    ←[0mhashable-1.2.6.1: using precompiled package←[0m
    ←[0mhappy-1.19.9: download←[0m
    ←[0mtf-random-0.5: build←[0m
    ←[0mhashtables-1.2.2.1: download←[0m
    ←[0mhashtables-1.2.2.1: configure←[0m
    ←[0mhashtables-1.2.2.1: build←[0m
    ←[0mhappy-1.19.9: configure←[0m
    ←[0mtf-random-0.5: copy/register←[0m
    ←[0mQuickCheck-2.10.1: download←[0m
    ←[0mhappy-1.19.9: build←[0m
    ←[0mQuickCheck-2.10.1: configure←[0m
    ←[0mQuickCheck-2.10.1: build←[0m
    ←[0mhashtables-1.2.2.1: copy/register←[0m
    ←[0mQuickCheck-2.10.1: copy/register←[0m
    ←[0malex-3.2.3: download←[0m
    ←[0malex-3.2.3: configure←[0m
    ←[0malex-3.2.3: build←[0m
    ←[0mhappy-1.19.9: copy/register←[0m
    ←[0malex-3.2.3: copy/register←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: download←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: configure←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: build←[0m
    ←[0mgtk2hs-buildtools-0.13.3.1: copy/register←[0m
    Completed 13 action(s).
    removeDirectoryRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:removeContentsRecursive:removePathRecursive:DeleteFile "C:\\Users\\aita\\AppData\\Local\\Temp\\stack7744\\gtk2hs-buildtools-0.13.3.1\\.stack-work\\dist\\5c8418a7\\build\\gtk2hsC2hs\\gtk2hsC2hs.exe": permission denied (The process cannot access the file because it is being used by another process.)

我很小心,不让任何其他进程使用此文件夹,但我不明白为什么会出现错误。

这是我的stack.yaml 文件:

# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/

# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
# resolver: ghcjs-0.1.0_ghc-7.10.2
# resolver:
#  name: custom-snapshot
#  location: "./custom-snapshot.yaml"
resolver: lts-10.7

# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# - location:
#    git: https://github.com/commercialhaskell/stack.git
#    commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#   extra-dep: true
#  subdirs:
#  - auto-update
#  - wai
#
# A package marked 'extra-dep: true' will only be built if demanded by a
# non-dependency (i.e. a user package), and its test suites and benchmarks
# will not be run. This is useful for tweaking upstream packages.
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver
# (e.g., acme-missiles-0.3)
# extra-deps: []

# Override default flag values for local packages and extra-deps
# flags: {}

# Extra package databases containing global packages
# extra-package-dbs: []

# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=1.6"
#
# Override the architecture used by stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
Main
Client

cabal 文件。

name:                Hworld
version:             0.1.0.0
-- synopsis:
-- description:
homepage:            https://github.com/githubuser/Hworld#readme
license:             BSD3
license-file:        LICENSE
author:              Author name here
maintainer:          example@example.com
copyright:           2018 Author name here
category:            Web
build-type:          Simple
cabal-version:       >=1.10
extra-source-files:  README.md

executable Hworld
  hs-source-dirs:      src
  main-is:             Main.hs
  default-language:    Haskell2010
  build-depends:       base >= 4.7 && < 5

最佳答案

首先,stack install有点用词不当。不像 npm installbower install ,它不会下载任何内容或“添加”任何内容到您的项目中。下stack ,所有包都会(潜在地,逻辑上)下载并安装。 stack将在第一次使用时构建一个包,但无需明确询问 stack下载它。

什么stack install实际上,它会构建包,如果包包含任何可执行文件,它会将它们放在 PATH 上的文件系统上的特殊位置。 ,这样现在您就可以运行这些可执行文件了。了解如何始终使用 stack exec package 运行可执行文件无需安装它们,使用 stack install通常不鼓励这样做,除非您确实确定有必要。

要将包添加到您的项目,您需要将其包含在 .cabal 中文件在 build-depends部分或在您的package.yaml中文件在 dependencies部分(无论您使用的是哪一个)。 Here is an example of a package.yaml ,和here's an example of a .cabal file .

但是您想要做的似乎是引用您自己的模块,而不是外部依赖项。为此,stack install与事实相差甚远。

要引用您自己的模块,您需要两件事:(1) 正确的文件系统结构和 (2) 将它们添加到 .cabalpackage.yaml .

文件系统结构:在 Haskell 中(或者更准确地说,在 GHC 中),模块必须位于文件系统上与其分层名称相匹配的位置。例如,模块 Main将位于./Main.hs ,和模块Client将位于./Client.hs ,或者说模块 Client.Internals将位于./Client/Internals.hs 。有点烦人,是的。但这就是 Haskell 的方式。

Cabal 文件:包/库中的所有模块必须列在 .cabal 中文件,位于 exposed-modules 下或下other-modules 。只需将它们添加到那里,编译器就会看到它们。

关于haskell - 无法在 Haskell 中导入模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49119117/

相关文章:

php - 是否可以使用Live-server for PHP并在保存时自动重载?

proxy - vscode中的代理问题

haskell - 堆栈运行单一基准测试

haskell-stack - 构建 :haddock-arguments option of stack. yaml 的用法

字符串到 Int 列表

haskell - 我该如何处理 "Occurs check: cannot construct the infinite type: a0 = [a0]"?

haskell - 光线追踪并找到交点处表面的法向量

Azure函数: test locally in VS Code

haskell - 在 Haskell 中,为什么我需要在实例声明中指定类型类?

haskell - 如何检查堆栈安装了哪个版本的软件包?