# what is semantics?

There are many popular languages. But, computer scientists tell us that in order to understand the behaviour of programs in those languages definitely and unambiguously argue upon program behavior (e.g. prove their identity), we need to translate them into another, well understood language. They call such language "a semantics". Authors propose one of many semantics. They explain the meaning of their constructions and how you can translate your language into their. Once you do that, everybody will understand your program certainly, they say.

Looks good, yet, I do not understand something. Do they tell us that they introduce another language to understand the first one? Why do we understand it better than the original one? Why this semantics is better than that? Why not to learn semantics of C right away instead of inventing another language, for describing semantics of C? The same applies to the syntax. Why don't I ask the same question regarding the syntax?

PS In the comments I hear that semantics does not mean another language or translation into it. But Formal Semantics for VHDL says that if you understand something in only one way then you do not understand it and "meaning of meaning" can be specified if we supply a language with a mechanism that translates it into another (known) language. That is, "semantics is a Relation between formal systems". Hennessy, in Semantics of Programming Languages, says that semantics allows for formal processing of the program "meaning", when semantics is supplied as BNF or Syntax Diagram. What is a formal system if not a language?

PS2 Can I say that HW synthesis of given HDL program into interconnection of gates, is a process of semantics extraction? We translate (high-level) description into the (low-level) language that we understand, afterwards.

-
What kind of diagrams do you mean? I work with semantics, but virtually never use diagrams. – Dave Clarke Nov 6 '12 at 17:08
I would not call semantics a "language". Semantics define meaning of languages. For example, the syntax of binary numbers it $\{0,1\}^+$. Only with the semantics $\operatorname{value}(w) = \sum_{i=1}^{|w|} w_i 2^{n-i+1}$ do we get actual numbers; note that this is not the only possible semantics, there are also BCD, 2-complement and many more. – Raphael Nov 6 '12 at 20:44
@Dave Excuse me, I created my question under impression of Assignment Decision Diagrams. I also just Formal Semantics for VHDL, a book where authors propose different semantics, lots of diagrams in each. I was thinking that this is a language we map to. – Val Nov 7 '12 at 10:30
This sounds a lot less like semantics, and more like an intermediate step on the way to compilation to hardware. – Dave Clarke Nov 7 '12 at 10:34
Yes, but they extract the "meaning" from the description. – Val Nov 7 '12 at 10:36

Why not to learn semantics of C right away instead of inventing another language, for describing semantics of C?

Because in order to define the semantics of C you need some kind of language, for example English. English can be ambiguous, and especially is the C99 semantics.

The computer science notion of semantics is generally a mathematical description of a program, and this description is not exactly a translation. There are several kind of semantics, that can be about different things:

1. about which mathematical function the program computes (denotational semantics), $$⟦ \verb!lambda x: x + x !⟧ = (x \mapsto 2x)$$
2. about properties that shall hold before and after the program (axiomatic semantics), $$\{\verb.a. > 0\} \quad \verb! x = a; y = 0; while(1 < x){ x = x/2; y++; } ! \quad \{\verb.y. = \lfloor\log(\verb.a.)\rfloor\}$$
3. about how the program will reduce (operational semantics). $$(σ, \verb! while(x){x--;y++;}!) ⇒ σ'$$ where $σ, σ'$ represents your memory before and after. In this case $σ=(x↦a,y↦b)$ and $σ'=(x↦a+b,y↦0)$ if $x$ and $y$ are the memory cells for $\verb.x.$ and $\verb.y.$, and if $a∈ℕ$.

A little sadly for now, universally understanding a program through its semantics is neither easy nor the intention of it all. It is not the former because, well, those math can be messy. It is not the latter because semantics provide indeed provide a common and unambiguous description, but it is not used to understand a program.

If reading the semantics is not that much meaningful, reading and agreeing on the semantics on the basic elements of the language is very important, for example to explain unambiguously how C's $\verb.continue.$ primitive works.

In conclusion:

Do they tell us that they introduce another language to understand the first one?

Technically no, the language is math. Also it is basically the same for all programming languages, so even introducing a new language, if it is the same for all, would be pretty much OK.

Why do we understand it better than the original one?

Because it is math so it may be harder to understand, but it is way more precise and complete.

The same applies to the syntax.

There are works on verified parsers, but the question of what would mean a semantics of a parser is not trivial.

-
"math [...] is way more precise and complete." -- which of course begs the question for formal syntax and semantics of mathematics itself. ;) – Raphael Nov 6 '12 at 20:46