- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一些需要 -XUndecidableInstances 来编译的 Haskell 代码。我确实理解为什么会发生这种情况,因为违反了某个条件,因此 GHC 大喊大叫。
但是,我从未遇到过类型检查器实际上会挂起或陷入无限循环的情况。
非终止实例定义是什么样的 - 你能举个例子吗?
最佳答案
this paper from HQ 中有一个经典的,有点令人震惊的例子(涉及与函数依赖的交互)。 :
class Mul a b c | a b -> c where
mul :: a -> b -> c
instance Mul a b c => Mul a [b] [c] where
mul a = map (mul a)
f b x y = if b then mul x [y] else y
mul x [y]
具有与
y
相同的类型,所以,取
x :: x
和
y :: y
, 我们需要
instance Mul x [y] y
y ~ [z]
对于一些
z
这样
instance Mul x y z
instance Mul x [z] z
Mul
instance 看起来它的递归在结构上是减少的,这与 Petr 的答案中明显的病态实例不同。然而,它使 GHC 循环(使用无聊阈值来避免挂起)。
y ~ [z]
。尽管
z
在功能上取决于
y
.如果我们对函数依赖使用函数表示法,我们可能会看到约束表示
y ~ Mul x [y]
并拒绝替换,因为它违反了发生检查。
class Mul' a b where
type MulT a b
mul' :: a -> b -> MulT a b
instance Mul' a b => Mul' a [b] where
type MulT a [b] = [MulT a b]
mul' a = map (mul' a)
g b x y = if b then mul' x [y] else y
UndecidableInstances
启用,循环超时需要相当长的时间。与
UndecidableInstances
禁用,实例仍然被接受并且类型检查器仍然循环,但是截止发生得更快。
关于Haskell/GHC UndecidableInstances - 非终止类型检查的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14661073/
考虑以下代码示例: {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE UndecidableInstances #-} -- Is there a way
是的,我知道 UndecidableInstances可能很糟糕。我真的很努力地设计我的模块,以便它不需要它但是我有这样的东西: instance Foo x (C x y) => Bar (C x
我有一个 UndecidableInstances我无法弄清楚如何避免使用 newtype 的问题.这是我最初的: {-# LANGUAGE TypeFamilies, FlexibleContext
我写了一些需要 -XUndecidableInstances 来编译的 Haskell 代码。我确实理解为什么会发生这种情况,因为违反了某个条件,因此 GHC 大喊大叫。 但是,我从未遇到过类型检查器
我是 Haskell 新手,只是玩了一段时间。 我编写了一个轻量级的 OOP 模拟: --OOP.hs {-# LANGUAGE MultiParamTypeClasses, FlexibleIn
我想在库中做一些魔术,允许多态地解构产品类型。这是一个或多或少的工作模型,说明了我想做的事情: {-# LANGUAGE MultiParamTypeClasses, FunctionalDepend
在之前使用 UndecidableInstances 编写一些代码时,我遇到了一些我觉得很奇怪的东西。我无意中创建了一些我认为不应该进行类型检查的代码: {-# LANGUAGE FlexibleIn
假设 Haskell 库设计者决定使用 UndecidableInstances 由于某些原因。该库编译良好。现在假设某个程序使用该库(例如定义其类型类的一些实例),但不使用扩展。编译失败(不会终止)
在 GHC 8.0.1 中,我尝试为类型级列表实现类型级 Length 函数。它编译: {-# LANGUAGE DataKinds, TypeOperators, TypeFamilies #-}
正在关注 this tutorial ,我有以下代码: {-# LANGUAGE DataKinds, TypeFamilies #-} data Nat = Z | S Nat type famil
我知道the documentation for -XUndecidableInstances ,但我想我会要求详细说明。 假设我有两个多参数类型类(-XMultiParamTypeClasses 允
我一直在尝试了解免费的 monad;作为学习辅助工具,我成功地为以下 Free 类型编写了一个 Show 实例: {-# LANGUAGE FlexibleContexts, UndecidableI
我是一名优秀的程序员,十分优秀!