自然编程语言......你想看到什么?

时间:2022-07-12 22:35:18

I am looking at writing a compiler and after I complete something in a "C" style I am looking at adapting it to other models. What are some syntactical constructs you would expect to see in a "natural" programming language?

我正在寻找编写一个编译器,在我完成一个“C”风格的东西后,我正在寻找适应其他模型。您期望在“自然”编程语言中看到哪些语法结构?

The target platform for this compiler will be the CLR and I am currently using Oslo+MGrammar for the lexer/parser (as you can probably tell this is really just an excuse to play)

这个编译器的目标平台将是CLR,我目前正在使用Oslo + MGrammar作为词法分析器/解析器(你可能会说这真的只是一个借口)

One of the goals of my project would be to allow programming to feel more like a conversation than structured syntax and demands.

我的项目的目标之一是让编程更像是一个对话,而不是结构化的语法和要求。

Guess I should extend this out a little. One of the ideas I am working with is having a class declaration read like a paragraph.

我猜我应该把它扩展一点。我正在使用的一个想法是将类声明读成段落。

    A Dog is a mammal.  It may Bark and Run.  To Run it
uses its feet to move forward. It does Lay.

...would translate too...

......也会翻译......

public class Dog : Mammal{

    public Feet Feet { get; set;}

    public virtual void Bark() {}
    public virtual void Run() {
        this.Feet.MoveForward();
    }
    public void Lay(){}
}

9 个解决方案

#1


If you want your design to be informed by something that has gone to an extreme in the direction of "naturalness", Graham Nelson has done some really stunning new work on a domain-specific programming language that is based on natural language. The system is called Inform 7 and in my opinion will amply repay analysis.

如果你希望你的设计能够被一种在“自然”方向上走向极端的东西所吸引,那么Graham Nelson就基于自然语言的特定领域编程语言做了一些非常令人惊叹的新工作。该系统被称为Inform 7,在我看来将充分回报分析。

One issue with Inform 7 is that the presentation is totally geared toward non-programmers. It's not easy to discover even what the syntax is! But I'm quite impressed with the results, and I believe there are some novel binding constructs that offer genuinely new ideas---it's not just old stuff in a very attractive package. Well worth checking out!

Inform 7的一个问题是演示文稿完全面向非程序员。即使语法是什么也不容易发现!但是我对结果印象深刻,我相信有一些新颖的装订结构可以提供真正的新想法 - 它不仅仅是一个非常有吸引力的包装中的旧东西。非常值得一试!

#2


Really, I don't think it is a good idea to make code more like natural language. It will become overly verbose. There's a reason why nobody uses COBOL very few people willingly uses COBOL :)

实际上,我认为让代码更像自然语言并不是一个好主意。它会变得过于冗长。有没有人使用COBOL的原因很少有人愿意使用COBOL :)

It might be a better idea to come up with a more standard way of pronouncing code out loud (it would help a lot if you're trying to explain code to somebody but can't show it to them).

提出一种更为标准的大声发音代码的方式可能是一个更好的主意(如果你试图向某人解释代码但却无法向他们展示代码,那将会有很大帮助)。

#3


I'll second the suggestion to take a look at Inform 7. Over the past month or so I have been writing some interactive fiction with Inform7 and it really is quite an impressive language. I find it to be a refreshing departure from the languages I code in normally, because I still am creating something, yet describing it in a much more natural way.

我将第二个建议来看看Inform 7.在过去一个月左右的时间里,我一直在用Inform7编写一些互动小说,这真的是一种令人印象深刻的语言。我发现它与我正常编写的语言有一个令人耳目一新的背离,因为我仍在创造一些东西,但却以更自然的方式描述它。

For instance, this would be a perfectly valid (albeit short) program in Inform7:

例如,这将是Inform7中完全有效(尽管很短)的程序:

The Building Lobby is a room.  "You are standing in the lobby of a building."
There is a supporter called desk here.  "A large reception desk is to your right."  
There is a device called lamp on the desk which is switched off.  
There is a container called trash can on the floor next to the desk. 
There is a thing called crumpled paper in the trash can.

#4


There is one form of English I know that attempts to be as rigorous as a programming language (and fails): legalese. An attorney is trained to write unambiguously in English, or something vaguely resembling English. (Lots of Latin, some magic phrases, etc.)

我知道有一种形式的英语,它试图像编程语言一样严谨(并且失败):legalese。律师经过培训,可以用英语明确地写作,或者用类似英语的东西。 (很多拉丁语,一些神奇的短语等)

If you want a general-purpose programming language, it isn't going to resemble natural language. Natural language is ambiguous, and relies on highly intelligent processing and, in many cases, a question-and-answer exercise to remove remaining ambiguity. Well, to attempt to remove it; in many cases, if person A says something to person B, A's opinion of what B understood is not the same as B's understanding.

如果你想要一种通用的编程语言,它就不会像自然语言那样。自然语言含糊不清,依赖于高度智能的处理,并且在许多情况下,还需要进行问答练习以消除残留的歧义。好吧,试图将其删除;在很多情况下,如果A人对B表示某事,A对B所理解的内容的看法与B的理解不同。

This is not really a problem with technology, but rather with the nature of natural vs. artificial languages.

这不是技术问题,而是自然语言与人工语言的本质。

Of course, you could try implementing some form of ambiguity in your language, but I don't think it'd be useful. It might be fun to play with, though, and that's your goal.

当然,您可以尝试在您的语言中实现某种形式的歧义,但我认为它不会有用。但是,玩起来可能很有趣,这就是你的目标。

#5


Look into fluent interfaces.

查看流畅的界面。

#6


Your statement:

A Dog is a mammal.  
It may Bark and Run.  
To Run it uses its feet to move forward. 
It does Lay.

Doesn't sound like a real natural language, but a form of controlled language.

听起来不像真正的自然语言,而是一种受控语言。

Two examples which have machine comprehensible semantics are Attempto Controlled English which maps to conceptual graphs, and Gellish which is used as a data modelling language.

具有机器可理解语义的两个例子是映射到概念图的Attempto受控英语,以及用作数据建模语言的Gellish。

I can't think of a direct translation of your statements about 'A Dog', as the first statement appears to be talking about the sub-type of mammals which are dogs, but then you start talking about a single instance; you'd need to be a bit more rigorous to use existing controlled languages, something like.

我不能想到你的关于'狗'的陈述的直接翻译,因为第一个陈述似乎是在讨论作为狗的哺乳动物的子类型,但是你开始谈论一个例子;你需要更严格地使用现有的受控语言,比如。

Every dog is a mammal.
Every dog may bark, or run.
To run is a forward movement.
Every dog uses its feet to run.
Every dog does lay.

( though this loses any idea of from time to time about the laying )

(虽然这不时有任何关于铺设的想法)


What would "uses" imply?

“使用”意味着什么?

What did "uses" imply in your example?

在你的例子中,“使用”意味着什么?

And I was describing the instance methods of the object “Dog”. I never said anything like “Spot is a Dog. Spot starts to run.”

我正在描述对象“Dog”的实例方法。我从来没有说过“Spot is a Dog”。现货开始运行。“

Is "A Dog" an object, or were you referring to the class of all dogs? You appeared to be referring to all dogs, and most controlled languages require that distinction. "A Dog is a mammal" vs "A Dog is in the garden". It's called "the elephant problem" in nlp books.

“A Dog”是一个对象,还是指的是所有狗的类?你似乎指的是所有的狗,大多数受控语言都要求区分。 “狗是哺乳动物”vs“狗在花园里”。它在nlp书中被称为“大象问题”。

To me this would create the instance and tell the instance to run.

对我来说,这将创建实例并告诉实例运行。

I don’t know how I would describe a static method for a dog.

我不知道如何描述狗的静态方法。

There's no such thing as a 'static method' in natural language, so why would you expect to be able to describe such a thing in something derived from a natural language?

在自然语言中没有“静态方法”这样的东西,那么为什么你能期望能够用自然语言来描述这样的东西呢?

You can scope define a relation which has a scope "If the topic of discussion is Dogs, then bark is a verb", but there isn't much call for defining the scope of a relation ( a static method is just a function with its scope defined within a class ); normally its not ambiguous.

您可以在范围内定义一个具有范围的关系“如果讨论的主题是Dogs,那么bark就是动词”,但是没有太多要求定义关系的范围(静态方法只是一个函数,其中范围内定义的范围);通常它不含糊。

#7


this sentence is false

这句话是假的

I like maths and programming because they are fairly absolute.

我喜欢数学和编程,因为它们相当绝对。

Human language communicates emotion and ambiguity. That's why I always end up arguing with people. If you don't want your computer to argue with you, and your compiler to make abstract random presumptions then stick with logical languages I recon.

人类语言传达情感和模糊性。这就是为什么我总是最终与人争论。如果你不希望你的计算机与你争论,并且你的编译器做出抽象的随机假设,那么我会坚持使用逻辑语言。

I imagine translating natural language into concrete semantics is much harder that extracting vague meaning from a sentence through NLP. Short of sentences like:

我认为将自然语言翻译成具体的语义要比通过NLP从句子中提取模糊的意义要困难得多。没有像这样的句子:

call method on sigkill then quit program

Which is pretty much what SQL-like in it's structure.

这几乎就像它的结构中的SQL一样。

You could go for some form of self-defining, axiomatic-like semantic setup (like Self or similar) with some lambda calculus affinity. edit: Sounds like Lisp. I retract that statement .. ())((()))()()

您可以采用某种形式的自定义,公理式语义设置(如Self或类似),并具有一些lambda演算亲和力。编辑:听起来像Lisp。我收回那句话..())((()))()()

Two examples from related question are LOLCODE and Inform7 ... both of which I would hate to program in.

相关问题的两个例子是LOLCODE和Inform7 ......我不想编程。

#8


From what I've seen recently, the most "natural" programming languages can be constructed for specific domains. Its when you get into general use programming languages that things have to become more general and as a result they read less naturally. I would consider CSS to be a pretty natural way of programming which is why non-programmers can pick it up and apply it pretty well. I think you will have a difficult time making a general use language more natural to read than some of the well composed Ruby code I've seen. On the other hand, if you are dealing with a specific domain, you can do some amazing things when you only have to handle a limited vocabulary.

从我最近看到的,可以为特定域构建最“自然”的编程语言。当你进入一般使用编程语言时,事情必须变得更加通用,因此它们的阅读不那么自然。我认为CSS是一种非常自然的编程方式,这就是为什么非程序员可以把它拿起并很好地应用它。我认为你会比一些我见过的组合良好的Ruby代码更难以阅读一般用法语言。另一方面,如果您正在处理特定域,那么当您只需要处理有限的词汇时,您可以做一些了不起的事情。

#9


Take a look at Rebol Parsing feature. It can define DSL based on a rule. You could meet your expectations easily with minimal code

看看Rebol Parsing功能。它可以根据规则定义DSL。您可以使用最少的代码轻松满足您的期望

#1


If you want your design to be informed by something that has gone to an extreme in the direction of "naturalness", Graham Nelson has done some really stunning new work on a domain-specific programming language that is based on natural language. The system is called Inform 7 and in my opinion will amply repay analysis.

如果你希望你的设计能够被一种在“自然”方向上走向极端的东西所吸引,那么Graham Nelson就基于自然语言的特定领域编程语言做了一些非常令人惊叹的新工作。该系统被称为Inform 7,在我看来将充分回报分析。

One issue with Inform 7 is that the presentation is totally geared toward non-programmers. It's not easy to discover even what the syntax is! But I'm quite impressed with the results, and I believe there are some novel binding constructs that offer genuinely new ideas---it's not just old stuff in a very attractive package. Well worth checking out!

Inform 7的一个问题是演示文稿完全面向非程序员。即使语法是什么也不容易发现!但是我对结果印象深刻,我相信有一些新颖的装订结构可以提供真正的新想法 - 它不仅仅是一个非常有吸引力的包装中的旧东西。非常值得一试!

#2


Really, I don't think it is a good idea to make code more like natural language. It will become overly verbose. There's a reason why nobody uses COBOL very few people willingly uses COBOL :)

实际上,我认为让代码更像自然语言并不是一个好主意。它会变得过于冗长。有没有人使用COBOL的原因很少有人愿意使用COBOL :)

It might be a better idea to come up with a more standard way of pronouncing code out loud (it would help a lot if you're trying to explain code to somebody but can't show it to them).

提出一种更为标准的大声发音代码的方式可能是一个更好的主意(如果你试图向某人解释代码但却无法向他们展示代码,那将会有很大帮助)。

#3


I'll second the suggestion to take a look at Inform 7. Over the past month or so I have been writing some interactive fiction with Inform7 and it really is quite an impressive language. I find it to be a refreshing departure from the languages I code in normally, because I still am creating something, yet describing it in a much more natural way.

我将第二个建议来看看Inform 7.在过去一个月左右的时间里,我一直在用Inform7编写一些互动小说,这真的是一种令人印象深刻的语言。我发现它与我正常编写的语言有一个令人耳目一新的背离,因为我仍在创造一些东西,但却以更自然的方式描述它。

For instance, this would be a perfectly valid (albeit short) program in Inform7:

例如,这将是Inform7中完全有效(尽管很短)的程序:

The Building Lobby is a room.  "You are standing in the lobby of a building."
There is a supporter called desk here.  "A large reception desk is to your right."  
There is a device called lamp on the desk which is switched off.  
There is a container called trash can on the floor next to the desk. 
There is a thing called crumpled paper in the trash can.

#4


There is one form of English I know that attempts to be as rigorous as a programming language (and fails): legalese. An attorney is trained to write unambiguously in English, or something vaguely resembling English. (Lots of Latin, some magic phrases, etc.)

我知道有一种形式的英语,它试图像编程语言一样严谨(并且失败):legalese。律师经过培训,可以用英语明确地写作,或者用类似英语的东西。 (很多拉丁语,一些神奇的短语等)

If you want a general-purpose programming language, it isn't going to resemble natural language. Natural language is ambiguous, and relies on highly intelligent processing and, in many cases, a question-and-answer exercise to remove remaining ambiguity. Well, to attempt to remove it; in many cases, if person A says something to person B, A's opinion of what B understood is not the same as B's understanding.

如果你想要一种通用的编程语言,它就不会像自然语言那样。自然语言含糊不清,依赖于高度智能的处理,并且在许多情况下,还需要进行问答练习以消除残留的歧义。好吧,试图将其删除;在很多情况下,如果A人对B表示某事,A对B所理解的内容的看法与B的理解不同。

This is not really a problem with technology, but rather with the nature of natural vs. artificial languages.

这不是技术问题,而是自然语言与人工语言的本质。

Of course, you could try implementing some form of ambiguity in your language, but I don't think it'd be useful. It might be fun to play with, though, and that's your goal.

当然,您可以尝试在您的语言中实现某种形式的歧义,但我认为它不会有用。但是,玩起来可能很有趣,这就是你的目标。

#5


Look into fluent interfaces.

查看流畅的界面。

#6


Your statement:

A Dog is a mammal.  
It may Bark and Run.  
To Run it uses its feet to move forward. 
It does Lay.

Doesn't sound like a real natural language, but a form of controlled language.

听起来不像真正的自然语言,而是一种受控语言。

Two examples which have machine comprehensible semantics are Attempto Controlled English which maps to conceptual graphs, and Gellish which is used as a data modelling language.

具有机器可理解语义的两个例子是映射到概念图的Attempto受控英语,以及用作数据建模语言的Gellish。

I can't think of a direct translation of your statements about 'A Dog', as the first statement appears to be talking about the sub-type of mammals which are dogs, but then you start talking about a single instance; you'd need to be a bit more rigorous to use existing controlled languages, something like.

我不能想到你的关于'狗'的陈述的直接翻译,因为第一个陈述似乎是在讨论作为狗的哺乳动物的子类型,但是你开始谈论一个例子;你需要更严格地使用现有的受控语言,比如。

Every dog is a mammal.
Every dog may bark, or run.
To run is a forward movement.
Every dog uses its feet to run.
Every dog does lay.

( though this loses any idea of from time to time about the laying )

(虽然这不时有任何关于铺设的想法)


What would "uses" imply?

“使用”意味着什么?

What did "uses" imply in your example?

在你的例子中,“使用”意味着什么?

And I was describing the instance methods of the object “Dog”. I never said anything like “Spot is a Dog. Spot starts to run.”

我正在描述对象“Dog”的实例方法。我从来没有说过“Spot is a Dog”。现货开始运行。“

Is "A Dog" an object, or were you referring to the class of all dogs? You appeared to be referring to all dogs, and most controlled languages require that distinction. "A Dog is a mammal" vs "A Dog is in the garden". It's called "the elephant problem" in nlp books.

“A Dog”是一个对象,还是指的是所有狗的类?你似乎指的是所有的狗,大多数受控语言都要求区分。 “狗是哺乳动物”vs“狗在花园里”。它在nlp书中被称为“大象问题”。

To me this would create the instance and tell the instance to run.

对我来说,这将创建实例并告诉实例运行。

I don’t know how I would describe a static method for a dog.

我不知道如何描述狗的静态方法。

There's no such thing as a 'static method' in natural language, so why would you expect to be able to describe such a thing in something derived from a natural language?

在自然语言中没有“静态方法”这样的东西,那么为什么你能期望能够用自然语言来描述这样的东西呢?

You can scope define a relation which has a scope "If the topic of discussion is Dogs, then bark is a verb", but there isn't much call for defining the scope of a relation ( a static method is just a function with its scope defined within a class ); normally its not ambiguous.

您可以在范围内定义一个具有范围的关系“如果讨论的主题是Dogs,那么bark就是动词”,但是没有太多要求定义关系的范围(静态方法只是一个函数,其中范围内定义的范围);通常它不含糊。

#7


this sentence is false

这句话是假的

I like maths and programming because they are fairly absolute.

我喜欢数学和编程,因为它们相当绝对。

Human language communicates emotion and ambiguity. That's why I always end up arguing with people. If you don't want your computer to argue with you, and your compiler to make abstract random presumptions then stick with logical languages I recon.

人类语言传达情感和模糊性。这就是为什么我总是最终与人争论。如果你不希望你的计算机与你争论,并且你的编译器做出抽象的随机假设,那么我会坚持使用逻辑语言。

I imagine translating natural language into concrete semantics is much harder that extracting vague meaning from a sentence through NLP. Short of sentences like:

我认为将自然语言翻译成具体的语义要比通过NLP从句子中提取模糊的意义要困难得多。没有像这样的句子:

call method on sigkill then quit program

Which is pretty much what SQL-like in it's structure.

这几乎就像它的结构中的SQL一样。

You could go for some form of self-defining, axiomatic-like semantic setup (like Self or similar) with some lambda calculus affinity. edit: Sounds like Lisp. I retract that statement .. ())((()))()()

您可以采用某种形式的自定义,公理式语义设置(如Self或类似),并具有一些lambda演算亲和力。编辑:听起来像Lisp。我收回那句话..())((()))()()

Two examples from related question are LOLCODE and Inform7 ... both of which I would hate to program in.

相关问题的两个例子是LOLCODE和Inform7 ......我不想编程。

#8


From what I've seen recently, the most "natural" programming languages can be constructed for specific domains. Its when you get into general use programming languages that things have to become more general and as a result they read less naturally. I would consider CSS to be a pretty natural way of programming which is why non-programmers can pick it up and apply it pretty well. I think you will have a difficult time making a general use language more natural to read than some of the well composed Ruby code I've seen. On the other hand, if you are dealing with a specific domain, you can do some amazing things when you only have to handle a limited vocabulary.

从我最近看到的,可以为特定域构建最“自然”的编程语言。当你进入一般使用编程语言时,事情必须变得更加通用,因此它们的阅读不那么自然。我认为CSS是一种非常自然的编程方式,这就是为什么非程序员可以把它拿起并很好地应用它。我认为你会比一些我见过的组合良好的Ruby代码更难以阅读一般用法语言。另一方面,如果您正在处理特定域,那么当您只需要处理有限的词汇时,您可以做一些了不起的事情。

#9


Take a look at Rebol Parsing feature. It can define DSL based on a rule. You could meet your expectations easily with minimal code

看看Rebol Parsing功能。它可以根据规则定义DSL。您可以使用最少的代码轻松满足您的期望