“语义”的简单定义因为它通常与编程语言/API 相关使用?

发布于 2024-09-10 06:26:04 字数 412 浏览 5 评论 0原文

今天我突然想到,虽然我在提到语言元素和命名约定时已经采用并且不经常使用“语义”一词,但我对正式定义没有任何感觉。

我试图在编程领域找到一个正式的定义,这让我的目光变得呆滞。

我从我遇到的上下文以及它在语言学方面更常见的用法中了解了它的含义,并且我通常使用该术语来指语言元素,或者命名法对其所命名的意图、行为或功能的忠实度

不过,这个定义是我自己的想法。我主修人类学/英语,从未上过计算机科学课程。这是CS里教的吗?

是否有一个更准确的定义可以归结为一两个陈述,而不是一篇详尽无遗的文章?

It occurred to me today that although I've adopted and don't infrequently use the term "semantics" when referring to language elements and naming conventions, I don't have any sense of a formal definition.

My attempt to find a formal definition in the programming domain made my eyes glaze over.

I have a sense of its meaning from the contexts in which I've encountered it, and from its more common usage with respect to linguistics, and I typically use the term to refer to the meaning or expressiveness of the language element, or the fidelity of nomenclature to the intent, behaviour, or function of that which it names.

This definition, though, is my own idea. I was an Anthropology/English major and have never taken a Computer Science course. Is this taught in CS?

Is there a more accurate definition that can be boiled down to a statement or two, rather than an exhaustive and exhausting article?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在你怀里撒娇 2024-09-17 06:26:04

定义摘自此处编程语言的语义描述语法和计算模型之间的关系。。这几乎正​​是您推断的定义。症结所在,也是让你目光呆滞的地方,正是计算模型的精确定义。

有许多不同的形式计算模型,每种模型都会导致不同形式的语义。操作语义可能最接近大多数人非正式地建模语义的方式,让每个代码片段通过解释器运行并更改抽象例程的状态。这种方法在许多并发性或不确定性等模型中都失败了,因此还有其他语义更适合这些情况。

A definition cribbed from here is The semantics of a programming language describe the relationship between the syntax and the model of computation.. This is pretty much exactly what your inferred definition was. The sticking point, and what made your eyes glaze over is precisely defining the model of computation.

There are many different formal models of computation and each leads to a different form of semantics. Operational Semantics is probably closest to the way most people informally model the semantics, having each fragment of code run through an interpreter and changing the state of an abstract routine. This approach breaks down in many models where concurrency or nondeterminism, etc., so there are other sematics better suited to these situations.

佞臣 2024-09-17 06:26:04

这是语言元素在计算方面的正式含义(通常,这是操作语义)。这意味着它表达了您的语言的术语的有效功能,假设一种依赖于我们正在讨论的语义的基础模型。

正如您从维基百科页面中看到的,您主要有 3 种语义:

  • 操作语义通过指定抽象虚拟机在执行术语时的行为方式来表达语言的含义。 (例如:+:从堆栈中弹出两个元素并压入总和。这是NOT正式的,也NOT您应该如何真正考虑它只是为了给你一个想法)。这是描述“正常”编程语言语义最常用的一种。例如,对于 Java,对于每个可能的术语,您可以拥有一系列 JVM 指令,旨在执行该序列来对该术语进行建模。 可能当您询问语义的含义时,这就是您正在寻找的。
  • 指称语义是一种不同的方法:您为每个给出语言术语由数学函数表示的含义。 的语义(有效含义)
  • 因此,对于前面的示例,您将有一个与 + 关联的函数 f,其中包含术语公理语义 是一种注释语言术语的方法,表达它们如何改变您想要通过程序验证的某些逻辑公式的有效性。您应该考虑阅读,因为所使用的推理规则和公理与您开发此类的方式类似语义,但它以实用的方式解释

从这个描述中,您了解到语义是在上下文中明确定义的东西,并且您需要指定的上下文,否则您无法为您的语言提供其术语的正式定义。

It's the meaning of the language elements in terms of what they formally mean in terms of computation (usually, and this is the operational semantics). This means that it expresses what a term of your language effectively does assuming an underlying kind of model that depends on which semantic we are talking about.

As you can see from wikipedia page you mainly have 3 kinds of semantics:

  • operational semantics express the meaning of the language by specifying how an abstract virtual machine behaves whenever it executes a term. (eg: +: pops two elements from the stack and push the sum. This is NOT formal and it is NOT how you should really consider it, it's just to give you an idea). This is the most used one to describe semantics of "normal" programming languages. For example for Java you could have, for every possible term, a sequence of JVM instruction meant to be executed to model that term. Probably when you asked for the meaning of semantics this is the one you were looking for.
  • denotational semantics is a different approach: you give for every term of the language a meaning that is represented by a mathematical function. So for previous example you would have a function f associated with + that contains what is the semantic (effective meaning) of the term
  • axiomatic semantics is a way to annotate the terms of your language expressing how they alter the validity of some logical formulas you want to verify over your program. You should consider reading this just because the inference rules and axioms used are similar in how you develop this kind of semantics but it's explained in a practical way

From this decription you understand that a semantic is something well defined inside a context, and you need a specified context otherwise you couldn't give you language a formal definition of what its terms do..

千柳 2024-09-17 06:26:04

从编程语言的理论和实践的角度来看,语言元素具有语义。命名约定则不然。语义与对任何事物的“忠诚度”无关,除非实现是正确的,有时它被称为“忠实于语义”。

除此之外,很难一概而论,因为语义有很多不同的风格。

  • Christopher Strachey 是真正推动这一想法的人,通常是一个短语(想想声明定义陈述表达式)由较小的子短语组成,并且较大短语的含义(语义)应该是组成子短语的含义的函数。在这种风格中,每个语法良好的子短语都有语义。听起来这就是您正在寻找的东西。

还有其他类型的语义,称为“操作语义”,其中给定一个程序,语义告诉您该程序将如何在抽象机器上执行(或者在另一种变体中,语义不说明程序将如何执行,而只说明结果会是什么)。

有一个“公理语义”,它大致是关于你可以证明关于单个程序的事实。公理语义是有效证明技术的集合。由实现来确保所有可证明的声明都是真实的。

还有“静态语义”,它广泛地表示在编译时强加的任何要求,以使程序被认为是“良好”或“格式良好”。像“变量必须在使用之前定义”之类的东西是静态语义。但大多数情况下,当人们说静态语义时,他们指的是类型检查。

最后,可以讨论抽象数据类型、类或接口等的“语义”。这种用法比较宽松,但它们归结为允许哪些行为的规范。我建议您在这种情况下避免使用“语义”一词,而使用“合同”或“规范”一词。它将避免混乱的事情。


评论:尝试将一个复杂的主题归结为一两句话并不总是有帮助的。当谈到编程语言时,不要在维基百科上寻找好的信息。维基百科的本意是好的,但它们往往很复杂、令人困惑,或者根本就是错误的。

From a perspective of the theory and practice of programming languages, language elements have semantics. Naming conventions don't. And semantics has nothing to do with 'fidelity' to anything, except perhaps that if an implementation is correct it is sometimes called "faithful to the semantics."

Beyond that it's hard to generalize because there are so many different styles of semantics.

  • Christopher Strachey was the one who really pushed the idea that normally a phrase (think declaration, definition, statement, or expression) is composed from smaller subphrases, and that the meaning (semantics) of the larger phrase should be a function of the meaning of the constituent subphrases. In this style, every syntactically well-formed subphrase has a semantics. It sounds like this is what you are looking for.

There are other styles of semantics, called "operational semantics", where given a program, the semantics tells you how that program will be executed on an abstract machine (or in another variant, the semantics says not how the program will be executed but only what the result will be).

There is "axiomatic semantics" which is roughly about what facts you can prove about individiual programs. The axiomatic semantics is a collection of valid proof techniques. It's up to the implementation to ensure that all provable claims are true.

There is also "static semantics", by which is meant broadly any requirements imposed at compile time for the program to be considered "good" or "well formed". Stuff like "variables have to be defined before they are used" is static semantics. But mostly when people say static semantics they mean type checking.

Finally, it is possible to speak of the "semantics" of an abstract data type, a class, or an interface, among others. This usage are a good deal looser, but they boil down to a specification of what behaviors are permissible. I advise you to avoid the word "semantics" in this context and to use the word "contract" or "specification" instead. It will avoid confusing things.


Commentary: it's not always helpful to try to boil down a complex subject to one or two sentences. And when it comes to programming languages, don't look for good information on Wikipedia. The Wikipedians mean well, but too often they are complex, confusing, or just plain wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文