Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/debugging/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Debugging 查看cabal repl/ghci中通用容器的数据内容_Debugging_Haskell_Ghci - Fatal编程技术网

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:不太清楚。我猜当它全部在一个模块中编译时,编译器会执行一些调试器可以检测到的优化?