Debugging 查看cabal repl/ghci中通用容器的数据内容
假设我有以下几点:Debugging 查看cabal repl/ghci中通用容器的数据内容,debugging,haskell,ghci,Debugging,Haskell,Ghci,假设我有以下几点: --Main.hs module Main where import Data.Vector as V import Test main = do let v = V.fromList ([1,2]::[Int]) print (getLength v) 以及: --Test.hs module Test where import Data.Vector as V getLength :: (Show a) => V.Vector a -> Int
--Main.hs
module Main where
import Data.Vector as V
import Test
main = do
let v = V.fromList ([1,2]::[Int])
print (getLength v)
以及:
--Test.hs
module Test where
import Data.Vector as V
getLength :: (Show a) => V.Vector a -> Int
getLength vec = let l = V.length vec in -- line 6
l
尝试从getLength
内打印vec
时遇到以下问题:
> cabal repl
> :break Test 6
> main
Stopped at Test.hs:6:25-36
_result :: Int = _
vec :: Vector a = _
> vec
<interactive>:4:1:
No instance for (Show a) arising from a use of ‘print’
Cannot resolve unknown runtime type ‘a’
Use :print or :force to determine these types
Relevant bindings include
it :: Vector a (bound at <interactive>:4:1)
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 45 others
In a stmt of an interactive GHCi command: print it
>阴谋集团repl
>:断裂试验6
>主要
在测试时停止。hs:6:25-36
_结果::Int=_
向量a=_
>vec
:4:1:
没有因使用“打印”而导致(显示a)的实例
无法解析未知的运行时类型“a”
使用:打印或:强制确定这些类型
相关绑定包括
它::向量a(在:4:1处绑定)
注意:有几个潜在的例子:
实例Show Double——在“GHC.Float”中定义
实例Show Float——在“GHC.Float”中定义
实例(积分a,显示a)=>Show(GHC.Real.Ratio a)
--在“GHC.Real”中定义
…再加上45个人
在交互式GHCi命令的stmt中:打印它
我尝试过使用
:force
和:print
但都没有帮助。奇怪的是,当getLength
放在Main.hs
中时,我就可以从getLength
中打印出vec
,您可以使用toList
来获取底层数据:
λ :print vec
vec = (_t1::Vector a)
λ :force vec
vec = Data.Vector.Vector 0 2 _
λ :print vec
vec = Data.Vector.Vector 0 2 (_t2::GHC.Prim.Array# a)
λ let as = toList vec
λ :print as
as = (_t6::[a])
λ :force as
as = [1,2]
λ :print vec
vec = Data.Vector.Vector 0 2 (_t7::GHC.Prim.Array# Int)
λ :force vec
vec = Data.Vector.Vector 0 2 _
λ print vec
fromList [1,2]
谢谢你知道为什么当
getLength
在一个单独的文件中时我必须这样做,而当getLength
在同一个文件中(即在Main.hs
中)时我不必这样做吗?artella:不太清楚。我猜当它全部在一个模块中编译时,编译器会执行一些调试器可以检测到的优化?