You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从父目录导入Haskell模块?附具体目录结构场景

Hey there! Let's tackle this Haskell module import issue with your directory structure. First, let's recap your setup to make sure I'm on the same page:

root
├── scripts
│   └── script1.hs
└── source
    ├── librarymodule.hs
    └── libraryconfig.txt

You need script1.hs to use functions from librarymodule.hs, which also relies on libraryconfig.txt in the same source directory. Here are a few solid solutions depending on whether you want a quick fix or a more maintainable setup:

Quick Fix: Use GHC's -i Flag

This is the simplest way to get things running without restructuring your project:

  • First, fix a common Haskell gotcha: module names must match filenames (case-sensitive). Rename librarymodule.hs to LibraryModule.hs so you can properly import it as import LibraryModule in script1.hs.
  • To handle the config file path reliably: Don't use a relative path like "./libraryconfig.txt" in LibraryModule.hs (since your working directory might be root or scripts when running the script). Instead, use a path relative to your project root:
    import System.FilePath ((</>))
    import System.Directory (getCurrentDirectory)
    
    getConfigPath :: IO FilePath
    getConfigPath = do
        projectRoot <- getCurrentDirectory
        return $ projectRoot </> "source" </> "libraryconfig.txt"
    
  • Compile or run script1.hs from the root directory with the -i flag to tell GHC where to find the source module:
    # Compile to an executable
    ghc -isource scripts/script1.hs
    # Or run directly without compiling
    runhaskell -isource scripts/script1.hs
    

Maintainable Setup: Cabal Project

If you plan to expand this project, using Cabal (Haskell's official build tool) will make dependency and resource management easier:

  1. Create a myproject.cabal file in your root directory with this content:
    cabal-version: 3.0
    name: myproject
    version: 0.1.0.0
    
    executable script1
        main-is: script1.hs
        hs-source-dirs: scripts, source
        build-depends: base >=4.14 && <5
        default-language: Haskell2010
        data-files: source/libraryconfig.txt
    
  2. Update LibraryModule.hs to use Cabal's auto-generated path module to find the config file (this works even if you install the project later):
    import Paths_myproject (getDataFileName)
    
    getConfigPath :: IO FilePath
    getConfigPath = getDataFileName "source/libraryconfig.txt"
    
  3. Run your script with:
    cabal run script1
    

Alternative: Stack Project

If you prefer using Stack (a popular Haskell toolchain manager), follow these steps:

  1. Run stack init in your root directory to generate a package.yaml and stack.yaml.
  2. Edit package.yaml to include your source directories and config file:
    name: myproject
    version: 0.1.0.0
    
    executables:
      script1:
        main: script1.hs
        source-dirs: scripts, source
        ghc-options:
          - -threaded
          - -rtsopts
          - -with-rtsopts=-N
        dependencies:
          - base >=4.14 && <5
        data-files: source/libraryconfig.txt
    
  3. Use the same Paths_myproject approach in LibraryModule.hs as the Cabal solution to fetch the config path.
  4. Run your script with:
    stack run script1
    

内容的提问来源于stack exchange,提问作者JonasAnon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:42:56