23

This is a sentence from Eric Lippert's blog:

Given that unfortunate situation, it makes sense to emphasize the storage mechanism first, and then the semantics second.

It's easy to get a dictionary definition of what "semantic" means but what does it mean in terms of computer jargon?

1
  • 2
    This question appears to be off-topic because it is about general programming, not a programming problem. Best fit for programmers.stackexchange.
    – nawfal
    May 22, 2014 at 8:37

7 Answers 7

26

but what does it mean in terms of computer jargon?

Essentially the same thing. Example:

x = 5;

The above is the syntax (representation). The meaning (i.e. the semantics) of this term is to assign the value 5 to a symbol (variable, whatever) called x. Different languages offer different syntaxes to provide the same semantics. For example, the above assignment would be written as

x := 5;

in Pascal, and as

x <- 5

in several other languages. In all cases, the meaning is essentially the same. But sometimes, the same syntaxes can also have different meanings, depending on the language and/or context. VB for example redefines the equals operator to mean two different things. First, an assignment, just as above.

Secondly, in the following code sippet, rather than assigning, it takes the meaning of comparing two values:

If x = 5 Then Console.WriteLine("x is 5")
3
  • This may be a meaning of "semantic" but I don't see how it fits the context (or common usage for that matter) at all. May 27, 2009 at 17:07
  • 2
    Not sure if this answer relates to the blog article that Richard posted (haven't read it), but if this answer relates to the question of this thread "In terms of programming, what do semantics mean?" then I'd say that your answer is concise and satisfying.
    – Petrus K.
    Oct 10, 2011 at 13:11
  • And may I add that in VHDL, signal assignment (with <=) is different from variable assignment (with :=) in that while a signal assignment a <= b may only be executed once, it says that b is now the driver of signal a; Thus if the driving signal b changes (by some other assignment in the future), then so will the driven signal a, without any need for "propagating" the assignment. In effect you are wiring a to b as opposed to setting it to the current value and letting them change independently after the assignment. This is another potential semantic of assignment. Nov 11, 2014 at 5:41
6

Semantics is WHAT we mean the program should do. Sytax is language-specific constraint on how we express the semantics.

In theory, as long as a program's semantics are correct, it doesn't matter what language was used to write it.

6

The dictionary definition applies.

Semantics is (are?) all about the meaning of words.

For example, if you use the .NET type KeyValuePair to represent something OTHER than a key and value, you've got a semantic problem. It may work... it may be the best solution, but it isn't semantically accurate.

This is exactly what he's talking about in that blog post. We list return types first in C-like languages, but really the return comes last. That's semantically inaccurate.

5

In terms of programming-language jargon, there are several notions of semantics:

  • Static semantics tells you which programs that are grammatical are also well formed. Many languages either have no static semantics (Scheme, Ruby, Python, Icon, Lua, Perl) or have a static semantics that is primarily about implementing a type system (Java, C, C#, Haskell). "Declaration required before use" is another possible static semantics. Static semantics answers the question "is this program meaningful?" and does so at compile time.

  • Dynamic semantics tells you one of two things:

    • Given that a program is meaningful, what is its meaning? Meaning has been defined mathematically in many, many different ways. A classic dynamic semantics might define a function (or a relation) between a program's inputs and the program's outputs. Meaning functions ("denotational semantics") were pioneered by Dana Scott and Christopher Strachey; meaning relations ("axiomatic semantics") were pioneered by Tony Hoare. Scott and Hoare won Turing awards; Strachey probably would have, but he died young. A good way to get introduced would be to read Tony Hoare's book Essays in Computing Science.
    • Given that a program is meaningful, how will it behave when executed?. This kind of semantics is usually called an "operational semantics" and describes the execution of the program on some kind of abstract machine. Again, there are many, many varieties. Today operational semantics is the tool of choice because there are powerful proof techniques, so for example, using operational semantics you can prove that there is never a memory error in managed code. Robin Milner got his Turing award in part for different operational techniques used to describe concurrent or multithreaded programs (CCS and the pi calculus). His 1999 book on Communicating and Mobile Systems is also a very good read if you skip the proofs :-)

If you read the word "semantics" in a manual or article, and the context is informal English rather than precise mathematical description, the author is probably referring to the dynamic operational behavior—if you will, an operational semantics informally described. This kind of informal description can be quite helpful to compiler writers and programmers.

3

As I understand it, semantics is "what it all means to a human". It's the what it does part, not the how it does it.

1
  • 1
    That's how I interpret the meaning of semantics in programming context too.
    – Petrus K.
    Oct 10, 2011 at 13:07
3

A little more context from the blog:

Therefore in C you put the storage metadata first (static int customerCount;) rather than the semantics first (it could have been var customerCount: static int;).

He's saying that "static int" appears before "customerCount". Calling "static int" storage metadata and "customerCount" semantics. The storage metadata is information about what the variable holds - implicit in it is how many bits it occupies, what values it may hold, whether it's shared among instances, and its volatility.

The semantics, the variable name, is information for readers of the code about what should be stored in the variable. What the variable means. You could call it "x" and the program would work just as well, but it would be hard for a programmer to understand it. Calling it "customerCount" provides the variable with meaning, and that's semantics.

3

In other words, given the context of the article, he is saying that it would have been better for the developers of C# to concentrate on how they would store the data internally when a variable was declared and not care so much about specifying the precise method of declaring the variable.

In other words by doing this in C#

static int customerCount

you are telling the compiler to prepare storage for a statically accessable integer and then telling it to label that storage as customerCount

whereas in VB you would use this line

dim shared customerCount as Integer

telling the compiler, in theory, that you have a variable called customerCount that it should store and make statically available, and oh by the way, it happens to be an Integer.

It all really a fine line distinction kinda thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.