- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当类型也在另一个类中时,我能否在类实例中提供方法的精细实现(又名 OOP 中的覆盖)?或者至少,如果该其他类是子类。
我有课C
用方法m
, 一个子类 S
的 C
用方法s
和一个类型 T a
所以有实例化
class C a where m :: [a] -> Bool
class C a => S a where s :: a -> a -> Bool
instance C a => C (T a) where m = ...
instance S a => S (T a) where s = ...
T a
在子类中(我不知道,因为它取决于
a
),方法
m
使用
s
可以更有效地实现(二次与指数时间) .
m
在实现中
instance S a => S (T a) where
s = ...
m = (all . uncurry) (=^=) . pairs -- override C.m
m
不是
S
的公共(public)方法.好吧,它不是,但它在 OO 意义上是继承的。
m
的专业版可用于所有实例;它不是在任何地方被覆盖的默认值。
Model
它有(除其他外)一个方法
con
检查列表的一致性。
class Model a where
con :: [a] -> Bool
data Arrow a b = [a] :->: b
lhs w = [ a | (u :->: _) <- w, a <- u ]
rhs w = [ b | (_ :->: b) <- w ]
Model (Arrow a b)
, 一般
con
实现非常昂贵(注意
powerset
在定义中)。
instance (Model a, Model b) => Model (Arrow a b) where
con w = all (\w' -> con (lhs w') `implies` con (rhs w')) (powerset w)
CoherentModel
的
Model
它有一个方法
(=^=)
检查两个对象的一致性。连贯模型的条件是,如果所有对都是一致的,则列表是一致的。
class Model a => CoherentModel a where
(=^=) :: a -> a -> Bool
a =^= b = con [a, b]
CoherentModel
在这一点上,文档多于功能。
instance (Model a, CoherentModel b) => CoherentModel (Arrow a b) where
(u :->: a) =^= (v :->: b) = con (u ++ v) `implies` a =^= b
con
可以使用实现
con = (all . uncurry) (=^=) . pairs
where
pairs :: [a] -> [(a,a)]
pairs [] = []
pairs [_] = []
pairs [x,y] = [(x,y)]
pairs (x:xs) = map ((,) x) xs ++ pairs xs
Arrow
,它适用于所有带参数的模型。我选择了
Arrow
因为改进是显着的。
最佳答案
这是个好问题。要记住的一件事是,数据类型是否是类型类的实例只是编译时信息——即,我们总是能够使用使用站点上的静态可用信息来选择要使用的实例,而多态性来自于能够从上下文中选择一个实例。一般来说,如果你问“a
是类型类的成员 B
吗?”,你能得到的唯一答案是"is"和“编译错误”。 (第二个观察结果稍有更改 OverlappingInstances
,但对您的情况似乎没有帮助)
因此,您的直接问题的答案是否定的。除非您是该类型类的方法,否则您无法决定类型在类型类中的成员资格。我们可以做的是将此决定添加为一种方法(使用 constraints
包)
import Data.Constraint
class Model a where
con :: [a] -> Bool
isCoherent :: Maybe (Dict (CoherentModel a))
isCoherent = Nothing
CoherentModel
在:
instance Model Foo where
con = ...
isCoherent = Just Dict
ScopedTypeVariables
和
TypeApplications
):
instance (Model a, Model b) => Model (Arrow a b) where
con | Just Dict <- isCoherent @b = -- efficient implementation
| otherwise = -- inefficient implementation
CoherentModel b
在上下文中。这有点酷。
con
的所有不同实现需要收集到一个地方。也太糟糕了
isCoherent
需要在每个相干的
Model
上手动实现实例,与其
CoherentModel
的位置分开实例是。
关于haskell - 根据子类优化父类(super class)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543400/
问题比较Java,但是我想在Android中实现: 假设有 3 个或更多类相互扩展: class A { ... int color; ... } class B extend
我知道标题听起来有点奇怪,但这正是我愿意做的。简单解释:A类是B类的子类,B类也是C类的子类>. 现在,所有这些类都包含方法m()。在我的 A 类中,这是我唯一可以访问的类,因为其他类仅在运行时可用,
我有一个 UIViewController 类 A 和 B。 A 使用以下方式加载 B:[A.view addSubView B.view]。 B 有一个带有“后退”按钮的导航栏。我想在单击时返回到
我有以下(第三方)类结构。我们将调用第三方项目 ProjectSeriously,并注意我使用 System.out.println 代替其他复杂的功能(100 行代码) . class A {
在下面的代码中,我从 Game 扩展了 MyGame。我有两个问题: 我们是否需要为所有render()、dispose()、pause()调用super方法 和 resize(w,h)?很多人都没有
例如,假设我想在调用 super.viewDidLoad() 时跳过一级。所以我希望能够做这样的事情: override func viewDidLoad() { super.super.vi
public class Faculty extends Employee { public static void main(String[] args) { new Fac
假设我有: class Superclass { //fields... methodA() {...} methodB() {...} ... } class Subclass exte
这个问题在这里已经有了答案: Why is super.super.method(); not allowed in Java? (22 个答案) 关闭 9 年前。 我怀疑我想做的事情是否可行。我有
我有一个实现 Initializable 的类。 public abstract class ExampleClass implements Initializable { public vo
我想知道,我有这个大数组,是否可以只在内存中使用一次而不是每个线程一次?以 stackoverflow 上的标签为例。他们几乎从不改变,为什么不为他们留下一个内存点呢?甚至可能将该数组永久保存在内存中
假设这三个类具有这个简单的层次结构: class A { func foo() { print("A") } } class B: A { override fu
有没有办法在 TypeScript 中调用 super.super.methodName。我想避免调用super.methodName,但我想调用二祖的methodName方法。 谢谢。 最佳答案 T
这个问题已经有答案了: When do I use super()? (11 个回答) 已关闭 7 年前。 package Geometry; public abstract class Geomet
我必须执行and()在我的实现 Predicate 的业务对象上. 出现问题的代码是 and() 行调用: Predicate predicate = new M
我有一个实现接口(interface)的抽象父类(super class): public abstract class FooMatrix implements Matrix { publi
我有四个 UIView:viewA 是 Root View ,它有 viewB 作为它的 subview 。 viewB 将 viewC 作为其 subview ,而 viewC 将 viewD 作为
有什么区别: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() 和:
我有一个通用接口(interface) interface ListList extends List> .由于某些原因,我无法转换 ListList至 List> .有什么方法可以做到吗?为什么它不
我想调用带有两个参数的父类(super class)的构造函数,所以我调用了 super(arguments),但是编译器说: “类 Person 中的构造函数 Person 不能应用于给定类型; 要
我是一名优秀的程序员,十分优秀!