所以,我有以下代码:
{-# 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 = [[], [[], []]]
我想你能明白我要去哪里。如果不是,我想做的是通过给 Haskell 赋予 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.
无法访问的代码
是什么意思?一般来说,是否有与我想做的事情相关的资源?
最佳答案
这只是意味着无法调用函数 weird
带有非底部参数(即带有终止、非引发异常的表达式)。
这是因为没有构造函数(或者更准确地说,没有 WHNF)可以拥有 Refl a [a]
作为其类型。事实上,类型 a
和[a]
无论如何,肯定是不同的a
可能是(这可以在统一期间检查)。
由于这通常是编程错误的根源,因此 GHC 错误会向程序员大声提示。
如果您真正想做的是“不可能”的输入,请改用:
{-# 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 []]
类型的非底部表达式。 .
关于haskell - 我试图破解 Haskell,并从 GHC 收到 "Inaccessiable code"错误。这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33926459/