gpt4 book ai didi

haskell - 是否有被视为 'safe' 的 GHC 扩展列表?

转载 作者:行者123 更新时间:2023-12-03 07:50:52 26 4
gpt4 key购买 nike

有时,如果没有至少一个语言扩展,我想编写的一段代码是不合法的。当试图在研究论文中实现想法时尤其如此,这些论文倾向于使用撰写论文时可用的任何漂亮的、超扩展的 GHC 版本,而没有明确说明实际需要哪些扩展。

结果是我经常在我的 .hs 文件的顶部得到这样的东西:

{-# LANGUAGE TypeFamilies
, MultiParamTypeClasses
, FunctionalDependencies
, FlexibleContexts
, FlexibleInstances
, UndecidableInstances
, OverlappingInstances #-}

我不介意,但我常常觉得我在做盲目的牺牲来安抚GHC的大神。它提示某段代码在没有语言扩展 X 的情况下无效,所以我为 X 添加了一个 pragma。然后它要求我启用 Y,所以我为 Y 添加一个 pragma。到这完成时,我已经启用三四种我不太了解的语言扩展,我不知道哪些是“安全的”。

解释我所说的“安全”是什么意思:
  • 我了解 UndecidableInstances是安全的,因为虽然它可能会导致编译器不终止,但只要代码编译它就不会产生意想不到的副作用。
  • 另一方面,OverlappingInstances显然是不安全的,因为它让我很容易不小心编写出运行时错误的代码。

  • 所以我的问题是:

    Is there a list of GHCextensions which are considered 'safe' and which are 'unsafe'?

    最佳答案

    最好看一下SafeHaskell允许:

    Safe Language

    The Safe Language (enabled through -XSafe) restricts things in two different ways:

    1. Certain GHC LANGUAGE extensions are disallowed completely.
    2. Certain GHC LANGUAGE extensions are restricted in functionality.

    Below is precisely what flags and extensions fall into each category:

    • Disallowed completely: GeneralizedNewtypeDeriving, TemplateHaskell
    • Restricted functionality: OverlappingInstances, ForeignFunctionInterface, RULES, Data.Typeable
      • See Restricted Features below
    • Doesn't Matter: all remaining flags.

    Restricted and Disabled GHC Haskell Features

    In the Safe language dialect we restrict the following Haskell language features:

    • ForeignFunctionInterface: This is mostly safe, but foreign import declarations that import a function with a non-IO type are be disallowed. All FFI imports must reside in the IO Monad.
    • RULES: As they can change the behaviour of trusted code in unanticipated ways, violating semantic consistency they are restricted in function. Specifically any RULES defined in a module M compiled with -XSafe are dropped. RULES defined in trustworthy modules that M imports are still valid and will fire as usual.
    • OverlappingInstances: This extension can be used to violate semantic consistency, because malicious code could redefine a type instance (by containing a more specific instance definition) in a way that changes the behaviour of code importing the untrusted module. The extension is not disabled for a module M compiled with -XSafe but restricted. While M can define overlapping instance declarations, they can only be used in M. If in a module N that imports M, at a call site that uses a type-class function there is a choice of which instance to use (i.e overlapping) and the most specific choice is from M (or any other Safe compiled module), then compilation will fail. It is irrelevant if module N is considered Safe, or Trustworthy or neither.
    • Data.Typeable: We allow instances of Data.Typeable to be derived but we don't allow hand crafted instances. Derived instances are machine generated by GHC and should be perfectly safe but hand crafted ones can lie about their type and allow unsafe coercions between types. This is in the spirit of the original design of SYB.

    In the Safe language dialect we disable completely the following Haskell language features:

    • GeneralizedNewtypeDeriving: It can be used to violate constructor access control, by allowing untrusted code to manipulate protected data types in ways the data type author did not intend. I.e can be used to break invariants of data structures.
    • TemplateHaskell: Is particularly dangerous, as it can cause side effects even at compilation time and can be used to access abstract data types. It is very easy to break module boundaries with TH.

    我记得读过 FunctionalDependencies 的交互和 UndecidableInstances也可能是不安全的,因为除了允许无限的上下文堆栈深度 UndecidableInstances也解除所谓的 coverage condition (第 7.6.3.2 节),但目前我找不到此引用。
    编辑 2015-10-27:自从 GHC 获得对类型角色的支持以来, GeneralizedNewtypeDeriving不再不安全。 (我不确定还有什么可能发生的变化。)

    关于haskell - 是否有被视为 'safe' 的 GHC 扩展列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10830757/

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