使GHC忽略非范围内的错误

Mat*_*hid 6 haskell ghc

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

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

(如果GHC可以为不存在的名称选择一个类型签名,并且确认程序仍然可以进行类型检查,那将是很好的.这几乎就是"类型漏洞"功能所做的 - 但是要使用那,你仍然需要手动定义所有的标识符.)

bhe*_*ilr 11

使用命名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
Run Code Online (Sandbox Code Playgroud)

所以这给你,_g :: b0 -> c_h :: s -> s -> b0与上下文x :: sf :: s -> c.类型检查器可以在大多数时间推断这些类型(这是重点TypedHoles),你可以给它们命名.如果你愿意,你可以定义所有的功能与_作为符号名的第一个字符,然后再使用您的编辑器替换_(.+)\b\1.如果你想解决_name用于记录领域的镜头惯例,那么只需在你的孔名称上加上2个下划线.

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

  • @MathematicalOrchid正如dfeuer和leftaroundabout所指出的那样,使用类型签名和`undefined`定义它们不需要任何额外的标志来编译,并且会改进错误消息以及确保表达式具有所需的类型而不是任何需要的类型使它成为一个类型.我想你可以为<editor>编写一个插件,它可以从一个带有"错误"孔名"`和推断类型签名的表达式中提取一个命名空洞,然后将这些空洞分解出去将是一件轻而易举的"全部提取"命令. (2认同)