5
$\begingroup$

When describing the syntax of a given programming language the words "expression" and "term" are often used to seemingly describe the same things. Are these words interchangeable in the context of programming languages?

$\endgroup$
  • 1
    $\begingroup$ I don't know of a definitive source on this, but from absolutely everything I've read, the answer is "yes". $\endgroup$ – gardenhead Oct 16 '16 at 23:13
  • 3
    $\begingroup$ It probably depends on the programming language and its semantics, as to whether or not there's a distinction. In logic programming (e.g. Prolog), "expression" and "term" mean very different things. $\endgroup$ – Pseudonym Oct 16 '16 at 23:34
  • $\begingroup$ I would say that they are often interchangeable. In my experience, there is no universal hard convention, except both always are syntactic entities. $\endgroup$ – chi Oct 16 '16 at 23:40
  • $\begingroup$ For the most part yes. Any given paper or topic will likely have specific meanings. You can't rely on it being consistent. For instance a 'term' in a procedural language might not be an expression. $\endgroup$ – Jake Oct 17 '16 at 0:59
  • $\begingroup$ Look for side-effects as a potentially distinguishing criterion. $\endgroup$ – Raphael Oct 17 '16 at 9:01
8
$\begingroup$

The two words expression and term have largely the same sets of possible meaning, but in a specific presentation, they may not be synonyms.

In rewriting theory, a term is something that conforms to a certain syntax, and for which a notion of computation (rewriting) is defined. In programming language theory, term can mean specifically one of the two aspects. And so can expression. Which means which depends on the author, and not all authors use both words in this way.

  • One possible meaning of the word is “something that can be computed”. In this sense, if the computation terminates, you end up with a value.
  • Another possible meaning of the word is “something that conforms to a certain syntax”. There is often a computation mechanism defined over this syntax, but not always.

In the theory of a programming language, there are often multiple syntactic objects: the core language, types, kinds, objects, classes, modules, interfaces, … Something like function x : int => x + 1 may be a core term which contains the type term int, or a core expression which contains the type expression int. It's common but not universal to use one of the two nouns to mean “core” (and “core” is my choice of terminology), e.g. “function x : int => x + 1 is an expression” or “function x : int => x + 1 is a term”. Different authors use different words; communities of a specific language tend to align on terminology, but that isn't systematic.

For example, in the world of ML, “expression” with no qualifier is normally reserved for the core language, and there are additionally “module expressions” in the module language, as well as “type expressions” and “signature expressions”. A “term” can be anything conforming to one of the syntactic forms, although many authors (e.g. the SML definition) don't use “term” at all.

But in Coq, a “term” is the most important syntactic form. Due to the nature of the language, the syntax of types is the same as terms. The Coq manual uses “expression” to designate any family of syntactic form.

If you're reading a text on programming languages, the meaning of the words should be explained or (in less formal texts) clear from context. If you're writing a text, try to use the terminology that's common in your community, and always make sure that the definitions you use are clear.

$\endgroup$
6
$\begingroup$

These terms do not have any universal meaning in computer science theory. They are popular choices for the names of productions in particular grammars, but they only have meaning within the context of a specific grammar. By convention "expression" is often used for the top-level construct in a grammar (so that in some sense "everything is an expression"), while "term" is used for a much more specific construct, often an operand to an addition or subtraction operator. The word "term" may be used with other meanings in other contexts, for example it has a particular meaning in some flavours of BNF.

$\endgroup$
2
$\begingroup$

In predicate logic, you can have both function symbols and relation symbols. The terms in this context are the valid combinations of variables, constants and function symbols. The valid combinations of terms, relation symbols and equality are called atomic formulas, the valid combinations of atomic formulas, logical operators and logical quantifiers are called formulas.

In programming languages like C or C++, things like typedefs or function definitions and declarations are probably not expressions. Control structures like for, if, and while are probably not expressions either (because they don't need to be terminated by a semicolon, and because they don't have a type). The language specifies exactly what counts as an expression, and I guess that an expression (in C/C++) always has a type.

$\endgroup$
  • 1
    $\begingroup$ This doesn't really answer the question, which is about their use in PL theory. I've heard them both used there, so it's definitely not as separate as this answer makes it out to be. In particular, people talk about a Type/Term distinction in languages, so clearly languages can have terms. $\endgroup$ – jmite Oct 17 '16 at 2:40
  • $\begingroup$ @jmite Of course you have a Type/Term distinction. A term has a type, which is differently from being a type. The part about predicate logic belongs into at least one answer to this question, because it explains how the word "term" even entered into the game of describing syntactic forms. In normal language, the word "term" is a synonym of the word "notion". I'm less sure whether the part about C/C++ is relevant. I wanted to have examples of syntactic form that are not expressions, but I wasn't even too sure about whether those example are really correct. $\endgroup$ – Thomas Klimpel Oct 17 '16 at 5:35
  • $\begingroup$ @ThomasKlimpel: "term" is not a synonym for "notion". The word "term" refers to a word (or phrase); "notion" refers to an idea or concept. $\endgroup$ – psmears Oct 17 '16 at 9:20
  • $\begingroup$ @psmears Indeed, it is not a synonym, because term refers to a word (or phrase) more as the syntactic concept, while notion refers to a word (or phrase) more as the semantic concept. Hence mathematical logic was exactly right to adopt the word "term" to refer to certain syntactic forms. $\endgroup$ – Thomas Klimpel Oct 17 '16 at 9:28
  • $\begingroup$ A command/statement can actually be viewed as an expression with unit type, as mentioned in lucacardelli.name/papers/typesystems.pdf. $\endgroup$ – Hongxu Chen Nov 6 '16 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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