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

Make GHC ignore not-in-scope errors

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

是否可以使用一些标志组合来使 GHC 发出警告而不是 "name foo is not in scope" 的错误?

(实际上,如果 GHC 可以为不存在的名称选择一个类型签名,并确认程序仍然可以进行类型检查,那就太好了。这 nearly "type holes" 功能的作用 — 但要使用它,您仍然必须手动 定义 所有标识符。)

使用命名 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 -> b0 以及 x :: sf :: s -> c 的上下文。大多数时候类型检查器可以推断出这些类型(这是 TypedHoles 的重点),你可以给它们命名。如果需要,您可以使用 _ 作为符号名称的第一个字符来定义所有函数,然后使用编辑器将 _(.+)\b 替换为 </code>。如果您想解决使用 <code>_name 作为记录字段的镜头约定,那么只需在您的孔名上加上 2 个下划线即可。

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

我通常的做法是将缺失的函数或值定义为undefined。它很容易定义,并为您留下一个方便的标记,表明所讨论的函数尚未定义。

我知道这并没有回答 OP 的问题,因为函数仍然需要手动定义,但我认为它还是有用的。