我试图破坏 Haskell 并从 GHC 得到一个 "Inaccessiable code" 错误。这是什么意思?

I was trying to break Haskell and got an "Inaccessiable code" error from GHC. What does it mean?

所以,我有以下代码:

{-# LANGUAGE GADTs #-}

import Data.Coerce
import Data.Functor.Fixedpoint --Although I'm not using these yet, they provide "context"

data Refl a b where
    Refl :: Refl a a

weird :: Refl a [a] -> a
weird Refl = [[], [[], []]]

我想你能看到我要去的地方。如果不是,我想做的是通过给 Refl 参数强制 Haskell 认为 a[a] 是同一类型。这将允许我做恶作剧。当我编译它时,ghci 给我这个错误:

[1 of 1] Compiling Main             ( pad'.hs, interpreted )

pad'.hs:9:7:
    Couldn't match type ‘a’ with ‘[a]’
      ‘a’ is a rigid type variable bound by
          the type signature for weird :: Refl a [a] -> [a] at pad'.hs:8:10
    Inaccessible code in
      a pattern with constructor
        Refl :: forall a. Refl a a,
      in an equation for ‘weird’
    Relevant bindings include
      weird :: Refl a [a] -> [a] (bound at pad'.hs:9:1)
    In the pattern: Refl
    In an equation for ‘weird’: weird Refl = [[], [[], []]]
Failed, modules loaded: none.

Inaccessible code是什么意思?一般来说,是否有与我正在尝试做的事情相关的任何资源?

这只是意味着无法使用非底部参数调用函数 weird(即,使用 终止、非异常引发表达式)。

这是因为没有构造函数(或者更准确地说,没有 WHNF)可以将 Refl a [a] 作为其类型。事实上,类型 a[a] 肯定是不同的,无论 a 可能是什么(这可以在统一期间检查)。

由于这通常是编程错误的来源,因此 GHC 错误会大声向程序员抱怨。

如果采用 "impossible" 输入是您真正想要做的,请改用此方法:

{-# LANGUAGE EmptyCase #-}
weird :: Refl a [a] -> a
weird x = case x of { }

甚至

{-# LANGUAGE EmptyCase #-}
weird :: Refl a [a] -> b
weird x = case x of { }

的确,weird 实际上可以生成任何类型!由于无法调用,它可以为其输出声明任何类型。这遵循逻辑原则 "ex falso quod libet":从一个错误的前提(例如 a[a] 是同一类型)你可以推断出任何你想要的。

最后一点,既然你导入了 Data.Functor.Fixedpoint,我猜你想使用 Fix []。好吧,那是 同构 [Fix []] -- 但不相等!所以,同样,你不能有类型为 Refl (Fix []) [Fix []].

的非底部表达式