- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Haskell可以正确建模吗?我对Haskell的经验很少。我从未见过可以使用模型的通用命令式oop编程语言。
对于那些初学者来说,模型基本上是类型的集合。这是对象为类型的类别。然后使用自然变换对模型进行建模。
很简单:
class mB;
class mD : mB { mD foo(); };
class MB;
class MD : MB { MD foo(); };
class MB : mB
class MD : mD, MB { MD foo(); };
mB -> MB
| |
v v
mD -> MD
model M : m;
import std.stdio, std.traits;
struct ModelA
{
// D only allows single inheritance, must use interfaces
interface iAnimal
{
string Type();
string Name();
void Attack(iAnimal who);
iFood LikesWhichFood();
}
interface iCat : iAnimal
{
void Meow();
}
interface iDog : iAnimal
{
void Bark();
}
interface iFood
{
}
class Animal : iAnimal
{
void Attack(iAnimal who) { writeln(Name, " is attacking ", who.Name, "!"); }
string Type() { return "Unknown Animal Type"; }
override string Name() { return "Unknown Animal"; }
iFood LikesWhichFood() { writeln("Food D Type: ", fullyQualifiedName!iFood); return null; }
}
class Cat : Animal, iCat
{
string name = "Unknown Cat";
override string Type() { return "Cat"; }
override string Name() { return name; }
void Meow() { writeln("Meow!"); }
this() { }
this(string n) { name = n; }
}
class Dog : Animal, iDog
{
string name = "Unknown Dog";
override string Type() { return "Dog"; }
override string Name() { return name; }
void Bark() { writeln("Bark!"); }
this() { }
this(string n) { name = n; }
}
class Food : iFood
{
}
}
// Model B, It is "derived" from A, meaning Model B could, in theory, substitute for Model A as long as everything is designed correctly
// In this case we will create a ViewModel, a gui framework for ModelA. We actually cannot do this naturally in D since it does not support multiple inheritance.
struct ModelB
{
interface iAnimal : ModelA.iAnimal
{
override iFood LikesWhichFood();
}
interface iCat : iAnimal, ModelA.iAnimal
{
}
interface iDog : iAnimal, ModelA.iAnimal
{
}
interface iFood : ModelA.iFood
{
void IsItTasty();
}
class Animal : ModelA.Animal, iAnimal
{
//
override iFood LikesWhichFood() { return cast(iFood)super.LikesWhichFood; }
}
class Cat : ModelA.Cat, iAnimal, iCat // We need to derive from Animal, not iAnimal, to provide proper ModelB implementation of Animal
{
alias Attack = Animal.Attack; // Required by D
// In D, ModelA.Cat's implement is not provided as default, we have to reimplement everything. Or is Animal providing any implementation
override string Type() { return super.Type; }
override string Name() { return super.Name; }
override void Meow() { super.Meow; }
void Attack(iAnimal who) { super.Attack(who); }
override void Attack(ModelA.iAnimal who) { super.Attack(who); }
override iFood LikesWhichFood() { writeln("Food D Type: ", fullyQualifiedName!iFood); return new Cabbage; }
this() { }
this(string n) { name = n; }
}
class Dog : ModelA.Dog, iAnimal, iDog
{
alias Attack = Animal.Attack;
override string Type() { return super.Type; }
override string Name() { return super.Name; }
override void Bark() { super.Bark; }
void Attack(iAnimal who) { super.Attack(who); }
override void Attack(ModelA.iAnimal who) { super.Attack(who); }
override iFood LikesWhichFood() { writeln("Food D Type: ", fullyQualifiedName!iFood); return new Donuts; }
this() { }
this(string n) { name = n; }
}
class Food : iFood
{
void IsItTasty() { writeln("Unknown Food"); }
}
class Donuts : Food
{
override void IsItTasty() { writeln("YUK!"); }
}
class Cabbage : Food
{
override void IsItTasty() { writeln("YUM!"); }
}
}
void main()
{
{
ModelA.iAnimal animal1 = new ModelA.Cat("Mittens");
ModelA.iAnimal animal2 = new ModelA.Dog("Sparky");
writeln(animal1.Name);
writeln(animal2.Name);
animal1.Attack(animal2);
animal1.LikesWhichFood;
}
writeln("\n----------\n");
{
ModelB.iAnimal animal1 = new ModelB.Cat("Super Mittens");
ModelB.iAnimal animal2 = new ModelB.Dog("Super Sparky");
writeln(animal1.Name);
writeln(animal2.Name);
animal1.Attack(animal2);
auto f = animal1.LikesWhichFood;
//f.IsItTasty; // Error: no property `IsItTasty` for type `Models.ModelA.iFood`. It should return a ModelB.iFood, we are inside ModelB, never any risk
(cast(ModelB.iFood)f).IsItTasty; // We can, of course, force it, but that is the rub, we don't have to, that is why we want to have a concept of a model, it tells the compiler that there is something more going on and it can reduce all this overhead. We can't even override this because of the contravariance rule.
}
writeln("\n----------\n");
// This is the magic, ModelB is now substituted in Model A. It's basically still oop but our entire derived model is(or should be) used.
// We can substitute the new model in all places where the old was used. This is the easy way to do ModelViewModel, we simply extend the model and add the view, no complex bridging, adapting, maintance, dependencies, etc.
{
ModelA.iAnimal animal1 = new ModelB.Cat("Super Mittens");
ModelA.iAnimal animal2 = new ModelB.Dog("Super Sparky");
writeln(animal1.Name);
writeln(animal2.Name);
animal1.Attack(animal2);
animal1.LikesWhichFood;
auto f = animal2.LikesWhichFood;
//f.IsItTasty; // This Error is ok, we are inside ModelA, ModelA would never use IsItTasty and it would be wrong to do so(it's only wrong because it should be impossible for ModelA to know about ModelB, else we create a dependency between models and really end up with one combined model rather than two separate models). But note that we could cast
(cast(ModelB.iFood)f).IsItTasty; // We can, of course, force it though(only because we know for a fact we are actually dealing with a ModelB disugised as a ModelA, this is generally not the case), but this then shows a dependency. Note that it is exactly like the above model though... but there is a huge difference. In the first case it is afe, in this case it is not.. and the only difference is the model we are working in.
}
}
最佳答案
您应该了解的第一件事是Haskell没有对象,继承,覆盖或任何类似性质的东西。既然您已经根据这些概念定义了问题,简单的答案是:不,Haskell不允许您为对象继承图定义模板,然后再橡皮图章几次,因为Haskell不会。甚至没有继承权。
但是,如果我通过抛弃所有OOP概念来对您的问题进行非常详尽的解释,那么我会得出:Haskell是否有一种方法来定义统一的接口(interface)(如函数集合;不是OOP接口(interface))可以与某些固定的数据类型集合多态使用?答案是肯定的,使用type families。
这是受动物/食物D代码启发而使用类型家族的示例:
{-# LANGUAGE TypeFamilies #-}
data Animal = Cat String | Dog String deriving (Eq, Show)
data Food = Donut | Cabbage deriving (Eq, Show)
data Widget a = Widget a Int -- let's say that the Int is a handle to some graphics object...
class AnimalModel animal where
type FoodFor animal
animalName :: animal -> String
likesWhichFood :: animal -> FoodFor animal
eat :: animal -> FoodFor animal -> IO ()
-- So here we'll define the "business model" functions:
instance AnimalModel Animal where
type FoodFor Animal = Food
animalName (Cat name) = name
animalName (Dog name) = name
likesWhichFood (Cat _) = Cabbage
likesWhichFood (Dog _) = Donut
eat animal food = print message
where
message = if likesWhichFood animal == food then show animal ++ " eats the " ++ show food else show animal ++ " refuses the " ++ show food
-- And here we'll define *just* the parts of the "view model" functions that don't depend on the specifics of the underlying model:
instance (AnimalModel animal) => AnimalModel (Widget animal) where
type FoodFor (Widget animal) = Widget (FoodFor animal)
animalName (Widget a _) = animalName a
likesWhichFood (Widget a _) = Widget (likesWhichFood a) (-1) -- because the widget hasn't been initialized yet??? IDK, this is a silly example
eat (Widget a _) (Widget f _) = eat a f
main = eat (Widget (Cat "Sparky") 2) (Widget Donut 3)
关于haskell - Haskell可以建模吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837648/
在 Haskell 中,类型声明使用双冒号,即 (::),如 not::Bool -> Bool。 但是在许多语法与 Haskell 类似的语言中,例如榆树、 Agda 、他们使用单个冒号(:)来声明
insertST :: StateDecoder -> SomeState -> Update SomeState SomeThing insertST stDecoder st = ... Stat
如果这个问题有点含糊,请提前道歉。这是一些周末白日梦的结果。 借助 Haskell 出色的类型系统,将数学(尤其是代数)结构表达为类型类是非常令人愉快的。我的意思是,看看 numeric-prelud
我有需要每 5 分钟执行一次的小程序。 目前,我有执行该任务的 shell 脚本,但我想通过 CLI 中的键为用户提供无需其他脚本即可运行它的能力。 实现这一目标的最佳方法是什么? 最佳答案 我想你会
RWH 面世已经有一段时间了(将近 3 年)。在在线跟踪这本书的渐进式写作之后,我渴望获得我的副本(我认为这是写书的最佳方式之一。)在所有相当学术性的论文中,作为一个 haskell 学生,读起来多么
一个经典的编程练习是用 Lisp/Scheme 编写一个 Lisp/Scheme 解释器。可以利用完整语言的力量来为该语言的子集生成解释器。 Haskell 有类似的练习吗?我想使用 Haskell
以下摘自' Learn You a Haskell ' 表示 f 在函数中用作“值的类型”。 这是什么意思?即“值的类型”是什么意思? Int 是“值的类型”,对吗?但是 Maybe 不是“值的类型”
现在我正在尝试创建一个基本函数,用于删除句子中的所有空格或逗号。 stringToIntList :: [Char] -> [Char] stringToIntList inpt = [ a | a
我是 Haskell 的新手,对模式匹配有疑问。这是代码的高度简化版本: data Value = MyBool Bool | MyInt Integer codeDuplicate1 :: Valu
如何解释这个表达式? :t (+) (+3) (*100) 自 和 具有相同的优先级并且是左结合的。我认为这与 ((+) (+3)) (*100) 相同.但是,我不知道它的作用。在 Learn
这怎么行 > (* 30) 4 120 但这不是 > * 30 40 error: parse error on input ‘*’ 最佳答案 (* 30) 是一个 section,它仍然将 * 视为
我想创建一个函数,删除满足第二个参数中给定谓词的第一个元素。像这样: removeFirst "abab" ( 'b') = "abab" removeFirst [1,2,3,4] even =
Context : def fib(n): if n aand returns a memoized version of the same function. The trick is t
我明白惰性求值是什么,它是如何工作的以及它有什么优势,但是你能解释一下 Haskell 中什么是严格求值吗?我似乎找不到太多关于它的信息,因为惰性评估是最著名的。 他们各自的优势是什么。什么时候真正使
digits :: Int -> [Int] digits n = reverse (x) where x | n digits 1234 = [3,1,2,4]
我在 F# 中有以下代码(来自一本书) open System.Collections.Generic type Table = abstract Item : 'T -> 'U with ge
我对 Haskell 比较陌生,过去几周一直在尝试学习它,但一直停留在过滤器和谓词上,我希望能得到帮助以帮助理解。 我遇到了一个问题,我有一个元组列表。每个元组包含一个 (songName, song
我是 haskell 的初学者,我试图为埃拉托色尼筛法定义一个简单的函数,但它说错误: • Couldn't match expected type ‘Bool -> Bool’
我是 Haskell 语言的新手,我在使用 read 函数时遇到了一些问题。准确地说,我的理解是: read "8.2" + 3.8 应该返回 12.0,因为我们希望返回与第二个成员相同的类型。我真正
当我尝试使用真实项目来驱动它来学习 Haskell 时,我遇到了以下定义。我不明白每个参数前面的感叹号是什么意思,我的书上好像也没有提到。 data MidiMessage = MidiMessage
我是一名优秀的程序员,十分优秀!