gpt4 book ai didi

haskell - 让 GHC 忽略不在范围内的错误

转载 作者:行者123 更新时间:2023-12-04 13:26:45 25 4
gpt4 key购买 nike

我正在构建一个模块,每次我编写一个函数时,它都会调用十几个尚不存在的其他函数。显然它们最终会存在,但是在我完成编写代码之前能够进行语法检查会很好。

是否有一些标志组合我可以用来使 GHC 发出警告而不是“名称 foo 不在范围内”的错误?

(实际上,如果 GHC 可以为不存在的名称选择类型签名,并确认程序仍然可以进行类型检查,那就太好了。这几乎是“类型漏洞”功能所做的 - 但要使用也就是说,您仍然必须手动定义所有标识符。)

最佳答案

使用名为 TypedHoles :

> let f x = _g . _h x $ x
Found hole ‘_g’ with type: b0 -> c
Where: ‘b0’ is an ambiguous type variable
‘c’ is a rigid type variable bound by
the inferred type of f :: s -> c at <interactive>:2:5
Relevant bindings include
x :: s (bound at <interactive>:2:7)
f :: s -> c (bound at <interactive>:2:5)
In the first argument of ‘(.)’, namely ‘_g’
In the expression: _g . _h x
In the expression: _g . _h x $ x

Found hole ‘_h’ with type: s -> s -> b0
Where: ‘b0’ is an ambiguous type variable
‘s’ is a rigid type variable bound by
the inferred type of f :: s -> c at <interactive>:2:5
Relevant bindings include
x :: s (bound at <interactive>:2:7)
f :: s -> c (bound at <interactive>:2:5)
In the expression: _h
In the second argument of ‘(.)’, namely ‘_h x’
In the expression: _g . _h x

所以这给了你 _g :: b0 -> c_h :: s -> s -> b0x :: s 的上下文中和 f :: s -> c .类型检查器大部分时间都可以推断出这些类型(这是 TypedHoles 的重点),您可以给它们命名。如果需要,您可以使用 _ 定义所有函数作为符号名称的第一个字符,然后使用您的编辑器替换 _(.+)\b\1 .如果您想解决使用 _name 的镜头约定对于记录字段,只需在您的孔名称上加上 2 个下划线。

这仍然会阻止您的代码编译,但如果您将它与 -fdefer-type-errors 结合使用它们将被报告为警告,从而允许您的类型错误在运行时发生。

关于haskell - 让 GHC 忽略不在范围内的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com