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 在ghc源文件中键入ghci最接近的等效项是什么?_Debugging_Haskell_Types - Fatal编程技术网

Debugging 在ghc源文件中键入ghci最接近的等效项是什么?

Debugging 在ghc源文件中键入ghci最接近的等效项是什么?,debugging,haskell,types,Debugging,Haskell,Types,如果我想比较类型或只是在Haskell源文件中打印类型信息,我有哪些选项? 使用GHC编译或加载到GHCi将提供: Found hole ‘_’ with type: Int Relevant bindings include x :: a foo :: a -> Int 我最近发现的一个简便技巧是将键入的孔与asTypeOf1结合使用 如果您有可编译的代码,并且您想知道其中表达式的类型,那么将该表达式替换为一个洞有时会把事情搞砸,如: -- what is

如果我想比较类型或只是在Haskell源文件中打印类型信息,我有哪些选项?

使用GHC编译或加载到GHCi将提供:

Found hole ‘_’ with type: Int
Relevant bindings include
  x :: a
  foo :: a -> Int

我最近发现的一个简便技巧是将键入的孔与
asTypeOf
1结合使用

如果您有可编译的代码,并且您想知道其中表达式的类型,那么将该表达式替换为一个洞有时会把事情搞砸,如:

           -- what is the type of this part, I wonder?
f xs = 3 * length xs
替换
长度xs
报告:

foo.hs:1:12: Warning:
    Found hole ‘_’ with type: a
    Where: ‘a’ is a rigid type variable bound by
               the inferred type of f :: t -> a at foo.hs:1:1
length xs
肯定不是
a
类型

但是如果您使用
asTypeOf
,您可以将
长度xs保留在那里,并插入一个必须与之具有相同类型的孔:

f xs = 3 * (length xs `asTypeOf` _)
现在我们得到:

foo.hs:1:34: Warning:
    Found hole ‘_’ with type: Int
好多了


1
asTypeOf
const
完全相同,它返回第一个参数,完全忽略第二个参数。但是,它的类型强制第二个参数与第一个参数相同;它被设计成带有倒钩的中缀

它是为当你有一个太多态的子表达式,并且GHC抱怨不明确的类型变量而设计的;您可以使用内联类型声明,但如果没有
ScopedTypeVariables
扩展,有时这是不可能的。如果您有另一个正确类型的值,则可以使用
asTypeOf
从多态表达式中“选择”适当的大小写,而不更改表达式返回的值

我在这里使用它是“倒退”从预期的情况;我希望左侧的对象约束右侧(忽略的)孔的类型,而不是相反

foo.hs:1:34: Warning:
    Found hole ‘_’ with type: Int