简体   繁体   English

是否存在具有以下功能的编程语言?

[英]Does a programming language with the following features exist?

Is there a language which will support the following concept or is there a pattern to achieve something similar with existing one? 是否有一种语言可以支持以下概念,或者是否有一种模式可以实现与现有概念类似的东西?

Concept 概念

I want to define a Rectangle with the following properties: Length , Height , Area , Perimeter ; 我想定义一个具有以下属性的RectangleLengthHeightAreaPerimeter ; where Area = Length * Height and Perimeter = (2 * Length) + (2 * Height) . 其中Area = Length * Height and Perimeter = (2 * Length) + (2 * Height)

Given the statement above, if I want to create a Rectangle by giving it a Length and a Height , it should of course automatically fill out the rest of the properties. 鉴于上面的陈述,如果我想通过给它一个Length和一个Height来创建一个Rectangle ,它当然应该自动填写其余的属性。

However, it should go further and automatically allow you to create a Rectangle with any two properties (say Height and Perimeter ) because that is also mathematically enough to create the same Rectangle . 但是,它应该更进一步,并自动允许您创建具有任何两个属性(例如HeightPerimeter )的Rectangle ,因为这在数学上也足以创建相同的Rectangle

Example

To help explain the idea, take this example: 为了帮助解释这个想法,请举个例子:

//Declaration
Rectangle
{
    Height, Length, Area, Perimeter;

    Area = Height * Length;
    Perimeter = (2 * Length) + (2 * Height);
}

//Usage
main()
{
    var rectangleA = new Rectangle(Height, Length);
    var rectangleB = new Rectangle(Height, Area);

    Assert(rectangleA == rectangleB);
}

Notice how I didn't need to define constructors for Rectangle . 注意我不需要为Rectangle定义构造函数。 Notice I did not need specify the specific logic needed if a Rectangle was created using Height and Area . 注意如果使用HeightArea创建Rectangle ,我不需要指定所需的特定逻辑。

Edit: Should be rectangle and not a square for a proper example. 编辑:对于正确的示例,应该是矩形而不是正方形。

What you are looking for is a language with an integrated computer algebra system. 您正在寻找的是具有集成计算机代数系统的语言。 It has to be able to resolve equations with respect to different variables. 它必须能够解决关于不同变量的方程。

While it would be possible to implement something like this, I doubt that it would make sense because in many cases there will be either no solution or multiple solutions. 虽然有可能实现这样的东西,但我怀疑它是否有意义,因为在许多情况下,将无法解决方案或多种解决方案。

Even your simple example will not work if only area and perimeter are given because there will usually be two solutions. 如果仅给出区域和周长,即使您的简单示例也不会起作用,因为通常会有两个解决方案。 (I assume that your class actually represents a rectangle and not a square, otherwise you should not have separate variables for length and height.) (我假设你的类实际上代表一个矩形而不是一个正方形,否则你不应该有长度和高度的单独变量。)

Example: 例:

Input: area = 2, perimeter = 6
Solution 1: length = 2, height = 1
Solution 2: length = 1, height = 2

Another remark not really related to your question: Your class obviously contains redundant member variables. 另一个与您的问题无关的评论:您的类显然包含冗余成员变量。 This is a bad thing for various reasons, the most important being the possibility of inconsistencies. 由于各种原因,这是一件坏事,最重要的是可能出现不一致。 Unless you have very strict performance constraints, you should store only two of them, say length and width, and provide methods to calculate the others when needed. 除非你有非常严格的性能限制,否则你应该只存储其中的两个,比如长度和宽度,并提供在需要时计算其他的方法。

Of course such a language exists. 当然存在这样的语言。 Many do, as you've now pointed out in your own comment to this answer. 很多人都这样做,正如您现在在自己对这个答案的评论中指出的那样。

In the example below I'll be using the Powerloom representation system, implemented in a language called STELLA . 在下面的示例中,我将使用Powerloom表示系统,该系统使用名为STELLA的语言实现。 You can play with it from within a Common Lisp environment. 您可以在Common Lisp环境中使用它。 Once you have everything installed you can load the language by running: 安装好所有内容后,您可以通过运行以下命令加载语言:

(cl:load "load-powerloom.lisp")
(in-package "STELLA")
(in-dialect "KIF")

That's about all you need to start building awesome geometrical objects. 这就是你开始构建令人敬畏的几何对象所需要的一切。 Within STELLA you may define a concept with the primitive defconcept : 在STELLA中,您可以使用原始defconcept定义一个概念:

(defconcept Rectangle (?r)
  :documentation "Curious geometrical objects that live on a plane.")

And define its properties with deffunction : 并使用deffunction定义其属性:

(deffunction rect-height ((?t Rectangle)) :-> (?n INTEGER))
(deffunction rect-length ((?t Rectangle)) :-> (?n INTEGER))
(deffunction area ((?t Rectangle)) :-> (?n INTEGER))
(deffunction perimeter ((?t Rectangle)) :-> (?n INTEGER))

To make the relations between area, perimeter and the sides of your rectangle, you'll have to make some assertions. 要建立区域,周长和矩形边之间的关系,您必须做出一些断言。 That's what you'll have assert for. 这就是你所assert的。

(assert (forall (?t Rectangle)
                (= (area ?t) (* (rect-height ?t) (rect-length ?t)))))
(assert (forall (?t Rectangle)
                (= (perimeter ?t) (+ (* 2 (rect-height ?t))
                                     (* 2 (rect-length ?t))))))

You are telling STELLA that for all rectangles, the area is the product of height and length, and that for all rectangles, the perimeter is twice the height plus twice the length. 你告诉STELLA,对于所有矩形,该区域是高度和长度的乘积,对于所有矩形,周长是高度的两倍加上长度的两倍。

Now you can instantiate your objects, and it doesn't matter what properties you give it, as long as they make sense. 现在你可以实例化你的对象了,只要它们有意义,你给它的属性并不重要。

(definstance rect1 :Rectangle true :rect-height 10 :rect-length 10)
(definstance rect2 :Rectangle true :area 40 :rect-height 20)

Here you instantiate rect1 with height and length as parameters, and rect2 with area and height. 在这里,实例化rect1其中height和length为参数, rect2为area和height。

But its always good to check that the language is doing what you expect: 但是检查语言是否符合您的期望总是很好的:

STELLA> (retrieve all ?x (= (area rect1) ?x))
There is 1 solution:
  #1: ?X=100

STELLA> (retrieve all ?x (= (rect-length rect2) ?x))
There is 1 solution:
  #1: ?X=2

If you ever get tired of rectangles and decide to build a beautiful square, why not derive a concept? 如果你厌倦了长方形并决定建造一个美丽的广场,为什么不推出一个概念呢?

(defconcept Square ((?r Rectangle))
  :documentation "Weird rectangles that fascinated the Greeks"
  :<=> (= (rect-height ?r) (rect-length ?r)))

Simply tell STELLA that squares are rectangles where height and length are equal. 简单地告诉STELLA,正方形是高度和长度相等的矩形。

Now try it out: 现在尝试一下:

STELLA> (definstance nice-rectangle :Rectangle true :rect-length 10 :area 100)
|i|NICE-RECTANGLE
STELLA> (ask (Square nice-rectangle))
TRUE

I'm not an expert at all, but I find the language fascinating. 我根本不是专家,但我发现这种语言非常吸引人。 It's sad that there is so little information about it on the internet. 令人遗憾的是,互联网上关于它的信息很少。 Even the manual is incomplete. 甚至手册都不完整。 For more information I'd suggest starting with these slides . 有关更多信息,我建议从这些幻灯片开始。

The famous book SICP teaches how to build a nondeterministic evaluator for such a language here. 着名的书籍SICP教导如何在这里为这种语言建立一个不确定的评估者 And finally, a wonderful write up describing motivations and applications behind these ideas can be seen here . 最后,可以在这里看到描述这些想法背后的动机和应用的精彩写作。

In C#, you can use properties, which have implicit getters and setters. 在C#中,您可以使用具有隐式getter和setter的属性。 That way you can write something like: 这样你就可以写出类似的东西:

public class Square {
    public int Length {
        get { return length; }
        set { length = value; }
    }
    public int Area {
        get { return length * length; }
        set { length = Math.Sqrt(value); }
    }
    public int Perimeter {
        get { return length * 4; }
        set { length = value / 4; }
    }
    private int length;
}

Now you can write: 现在你可以写:

Square square = new Square();
square.Length = 2;
Console.WriteLine(square.Length);    // "2"
Console.WriteLine(square.Area);      // "4"
Console.WriteLine(square.Perimeter); // "8"
square.Area = 9;
Console.WriteLine(square.Length);    // "3"
Console.WriteLine(square.Area);      // "9"
Console.WriteLine(square.Perimeter); // "12"

Edit: 编辑:

C# also allows you name properties at your choosing when instantiating an object: C#还允许您在实例化对象时根据您的选择命名属性:

Square square1 = new Square { Perimeter = 12 };
Square square2 = new Square { Length = 4 };

I don't think something like this does exist in the form of a programming language. 我不认为这样的东西确实以编程语言的形式存在。

Ontology 本体论

However the first approach I can think about is defining an Ontology, I mean a set of rules about 然而,我能想到的第一种方法是定义一个Ontology,我指的是一套规则

  1. Entities: Rectangle, Square, Dog, Car, etc... 实体:矩形,方形,狗,汽车等......
  2. Attributes: Area, Height, Number of Wheels, etc... 属性:面积,高度,轮数等...
  3. Relations between (1) and (2): Rectangle's Area is Height * Width, ... (1)和(2)之间的关系:矩形的面积是高度*宽度,......

Now given a list of attributes and the required output Entity 现在给出一个属性列表和所需的输出实体

I have height and width and I need a Rectangle 我有heightwidth ,我需要一个Rectangle

the system could search for a path through the rules graph to produce the required outcome based on the provided inputs. 系统可以通过规则图搜索路径,以根据提供的输入生成所需的结果。

Real world example 现实世界的例子

Wolfram Alpha probably follows the technique described above Wolfram Alpha可能遵循上述技术

在此输入图像描述

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM