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

Haskell ghci调试器无法显示变量值的问题求助

Haskell调试器中无法访问模块内变量的解决方法

问题背景

在Stack项目中使用ghci-dap调试时,模块Lib中定义的变量a始终提示Not in scope: ‘a’,即使调试器停在a的定义行,直接输入变量名或使用:print a都无法获取其值,按旧版GHCi教程操作也无效。

模块代码

module Lib
    ( someFunc
    ) where

a :: Integer
a = 5

testme :: IO ()
testme = print $ "abc" ++ "def"

someFunc :: IO ()
someFunc = do
    print a
    testme

调试操作过程

% stack ghci --with-ghc=ghci-dap --test --no-load --no-build --main-is TARGET

Warning: The following GHC options are incompatible with GHCi and have not been passed to it: -threaded.
         
Configuring GHCi with the following packages: testproj.
[DAP][INFO] start ghci-dap-0.0.25.0.
GHCi, version 9.6.7: https://www.haskell.org/ghc/  :? for help

ghci> :add Main

<no location info>: warning: [GHC-32850] [-Wmissing-home-modules]
    Modules are not listed in options for ‘main’ but needed for compilation:
        Lib
[1 of 3] Compiling Lib              ( /home/example/testproj/src/Lib.hs, interpreted )
[2 of 3] Compiling Main             ( /home/example/testproj/app/Main.hs, interpreted )
Ok, two modules loaded.

ghci> :break someFunc 
Breakpoint 0 activated at /home/example/testproj/src/Lib.hs:(14,12)-(16,10)

ghci> main
Stopped in Lib.someFunc, /home/example/testproj/src/Lib.hs:(14,12)-(16,10)
_result :: IO () = _
[/home/example/testproj/src/Lib.hs:(14,12)-(16,10)] ghci> :print a

<interactive>:1:1: error: Not in scope: ‘a’
[/home/example/testproj/src/Lib.hs:(14,12)-(16,10)] ghci> :step
Stopped in Lib.someFunc, /home/example/testproj/src/Lib.hs:15:5-11
_result :: IO () = _
[/home/example/testproj/src/Lib.hs:15:5-11] ghci> :step
Stopped in Lib.a, /home/example/testproj/src/Lib.hs:6:5
_result :: Integer = _
[/home/example/testproj/src/Lib.hs:6:5] ghci> :print a

<interactive>:1:1: error: Not in scope: ‘a’
[/home/example/testproj/src/Lib.hs:6:5] ghci> :list
5  a :: Integer
6  a = 5
7  
[/home/example/testproj/src/Lib.hs:6:5] ghci> a

<interactive>:15:1: error: [GHC-88464] Variable not in scope: a
[/home/example/testproj/src/Lib.hs:6:5] ghci> :step
5
[/home/mark/haskell/t/testproj/src/Lib.hs:6:5] ghci> :ste
5
Stopped in Lib.testme, /home/mark/haskell/t/testproj/src/Lib.hs:9:10-31
_result :: IO () = _
[/home/mark/haskell/t/testproj/src/Lib.hs:9:10-31] ghci> :step
"Stopped in Lib.testme, /home/mark/haskell/t/testproj/src/Lib.hs:9:18-31
_result :: [Char] = _
[/home/mark/haskell/t/testproj/src/Lib.hs:9:18-31] ghci> :print a

<interactive>:1:1: error: Not in scope: ‘a’

环境信息

  • Stack: Version 3.5.1, Git revision 5c774d7ca92f4716ef7a51d5da21a5c3713517a5 x86_64 hpack-0.38.0
  • GHC: ghc-9.6.7
  • OS: Debian Linux

解决方法

原因分析

Lib模块的导出列表仅包含someFunc,变量a属于模块内部未导出的名称,默认情况下GHCi的顶层作用域无法直接访问未导出的模块成员。

具体方案

  1. 修改模块导出列表
    编辑Lib.hs,将a加入导出列表:

    module Lib
        ( someFunc
        , a  -- 新增导出变量a
        ) where
    

    重新加载模块(:reload)后,调试时直接输入a或:print a即可获取值。

  2. 使用限定名称访问
    无需修改代码,调试时通过模块限定名访问未导出变量:

    ghci> :print Lib.a
    -- 或直接输入
    ghci> Lib.a
    

    即使变量未导出,GHCi调试器允许通过模块限定名访问内部成员。

  3. 导入模块所有名称到当前作用域
    在GHCi中执行:module + Lib,将Lib模块的所有名称(包括未导出的)导入到当前作用域,之后即可直接使用a:

    ghci> :module + Lib
    ghci> :print a
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 00:16:05