- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码示例:
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-} -- Is there a way to avoid this?
-- A generic class with a generic function.
class Foo a where
foo :: a -> a
-- A specific class with specific functions.
class Bar a where
bar :: a -> a
baz :: a -> a
-- Given the specific class functions, we can implement the generic class function.
instance Bar a => Foo a where
foo = bar . baz
-- So if a type belongs to the specific class...
instance Bar String where
bar = id
baz = id
-- We can invoke the generic function on it.
main :: IO ()
main =
putStrLn (foo "bar")
UndecidableInstances
这里需要 - 类型参数
a
在
Bar a => Foo a
的两边出现一次,所以我希望事情“正常工作”。我显然在这里遗漏了一些东西。但无论如何,有没有办法在不使用
UndecidableInstances
的情况下做到这一点? ?
最佳答案
您可以采取几种方法;我认为您没有提供足够的上下文来确定哪个是最合适的。如果您使用 GHC-7.4,您可能想尝试 DefaultSignatures
扩大。
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DefaultSignatures #-}
-- A generic class with a generic function.
class Foo a where
foo :: a -> a
default foo :: Bar a => a -> a
foo = bar . baz
-- A specific class with specific functions.
class Bar a where
bar :: a -> a
baz :: a -> a
instance Bar String where
bar = id
baz = id
instance Foo String
main :: IO ()
main =
putStrLn (foo "bar")
Foo
的一个实例。 ,但您不需要重复方法声明,因为将使用默认实现。
Foo
的功能例如,您可以包装
Bar
新类型中的实例。
newtype FooBar a = FooBar { unFooBar :: a }
instance Bar a => Foo (FooBar a) where
foo = FooBar . bar . baz . unFooBar
-- imported from a library or something...
needsFoo :: Foo a => a -> b
myFunc = needsFoo (FooBar someBar)
foo
来解决问题。具有正常功能,或为
Bar
制作专用版本实例:
-- if every `Foo` is also a `Bar`, you can just do this. No need for `Foo` at all!
foo :: Bar a => a -> a
foo = bar . baz
-- if most `Foo`s aren't `Bar`s, you may be able to use this function when you have a `Bar`
fooBar :: Bar a => a -> a
foo = bar . baz
Foo
手动实例。尽管可能有很多不同的可以想象的实例,但代码库只有少数实际使用的实例是相当普遍的。如果这里是真的,那么只写出您需要的 3 或 4 个实例而不是尝试实现更通用的解决方案可能会减少工作量。
OverlappingInstances
让它工作(如果你不需要
OverlappingInstances
,那么你不需要
Foo
类)。这是允许 GHC 在有多个可用匹配项时选择“最具体的实例”的扩展。这或多或少会起作用,尽管您可能无法得到您期望的结果。
class Foo a where
foo :: a -> a
class Bar a where
bar :: a -> a
baz :: a -> a
instance Bar String where
bar = id
baz = id
instance Bar a => Foo a where
foo = bar . baz
instance Foo [a] where
foo _ = []
main :: IO ()
main =
print (foo "foo")
main
打印一个空字符串。有两个
Foo
实例,对于
a
和
[a]
.后者更具体,因此它被选为
foo "foo"
因为字符串的类型为
[Char]
,尽管您可能想要前者。所以现在你还需要写
instance Foo String where
foo = bar . baz
Bar a => Foo a
完全实例。
关于Haskell 子类型类需要 UndecidableInstances?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11707171/
考虑以下代码示例: {-# 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
我是一名优秀的程序员,十分优秀!