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

Haskell项目配置Parsec库后仅Main.hs可见,src模块报错

Parsec库在src模块中无法识别的问题

问题背景

我正在编写解析器模块,需要接入Parsec库。已经在package.yaml中配置了依赖,但Parsec仅在Main.hs中能被识别,src目录下Parser/Myfile.hs引用该库时报错。

package.yaml配置

dependencies:
- base >= 4.7 && < 5
- parsec
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints

library:
  source-dirs: src
  dependencies :
    - parsec

executables:
  haskell-lab4-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - haskell-lab4
    - parsec

tests:
  haskell-lab4-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - haskell-lab4

项目结构

|-app
|  \- Main.hs
|
|-src
|  \-Parser
|   |    \-Myfile.hs <-- 需要在此文件中使用parsec
|   | 
|   |-Lib.hs
|
|-test

报错信息

找不到模块‘Text.Parsec.String.Combinator’
    你可能指的是Text.Parsec.Combinator(来自parsec-3.1.15.0)
    使用-v(或在ghci中执行`:set -v`)查看搜索的文件列表。

解决步骤

  1. 修正模块导入路径
    报错的核心是引用了不存在的模块:

    • 错误写法:import Text.Parsec.String.Combinator
    • 正确写法:import Text.Parsec.Combinator

    如果需要针对String类型的解析器,单独导入Text.Parsec.String模块即可:

    import Text.Parsec.String (Parser)
    import Text.Parsec.Combinator (some, many)
    import Text.Parsec.Char (char, digit) -- 按需导入其他模块
    
  2. 验证项目配置与构建
    package.yaml配置本身是正确的——library部分已添加parsec依赖,确保src下所有模块都能访问该库。如果修正导入后仍有问题:

    • 清理构建缓存:stack clean
    • 重新构建项目:stack build
    • 通过ghci验证:运行stack ghci haskell-lab4:lib,在交互环境中尝试导入Parsec模块,确认是否能正常加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 04:40:35