Semantics
Table of Contents
Definition
Semantics is the mapping from syntax (the written form of a program) to meaning (what the program does or computes). Two programs can share identical syntax but have different semantics if the language rules assign different meanings to the same constructs.
PLAI's Treatment (Ch.3)
PLAI introduces semantics with the "which of these is the same?" question:
| Expression | Language |
|---|---|
1 + 2 |
Python 3 (integers) |
1 + 2 |
C (32-bit signed int) |
'1' + '2' |
Python (string concatenation → '12') |
'1' + '2' |
JavaScript (string coercion → '12') |
Same syntax, different semantics. The meaning of + depends entirely on the
language rules — not on the symbol itself.
Semantics as an Interpreter Choice
When an interpreter is written by delegating to the host language (e.g.
writing (+ (interp l) (interp r)) where + is Racket's addition), the
interpreter silently inherits the host language's semantics for that operator.
This includes:
- Number type (fixnum, bignum, float, complex)
- Overflow behaviour
- Whether
+is applicable to non-numbers
A different semantic choice (e.g. fixed-width 8-bit arithmetic, or modular arithmetic) would require explicitly implementing that choice rather than delegating to Racket/CL.
Tolk Example
Tolk's arithmetic interpreter uses CL's + and * via the stored function
slots. This means tolk inherits CL's arithmetic semantics:
- Bignums (arbitrary precision integers)
- Floats coexist with integers
- No overflow
An alternative interpret-mod-4 (shown in the tolk README) demonstrates a
different semantic choice, using defmethod-bind just as the standard interpreter does:
(defmethod-bind interpret-mod-4 ((num n))
()
(mod n 4))
(defmethod-bind interpret-mod-4 ((arith-binary-op op l r))
()
(mod (funcall op (interpret-mod-4 l) (interpret-mod-4 r)) 4))
The surface syntax is identical; the semantics differ.