liveBook · Manning

Arity

As explained in Building User-Friendly DSLs

The number of subexpressions or operands that an expression concept instance can contain.

Arity counts how many subexpressions, or operands, an AST object for an expression concept can contain. Properties whose values are not subexpressions do not contribute. Most expression concepts have arity 0, 1, 2, or 3. section 11.5.1

Common arities have specific names:

  • Nullary expressions contain no subexpressions, as with literals and other references to AST objects. section 11.5.1
  • Unary expressions contain one subexpression, as with logical negation, arithmetic negation, grouping, and increment or decrement. section 11.5.1
  • Binary expressions contain two subexpressions, commonly named the left and right operands. section 11.5.1
  • Ternary expressions contain three subexpressions; variadic expressions contain a variable number, as in function calls or array literals. section 11.5.1

Arity and compositionality

A positive-arity expression can contain other expressions as operands. Replacing a subexpression with the value produced by evaluating it leaves the whole expression's result unchanged. Repeated composition can therefore construct ASTs of arbitrary size from a small set of expression concepts. section 11.5.2

Table 11.1 Expression concepts and their properties
Concept
Arity (properties containing subexpressions)
Other properties
Attribute Reference
0
"attribute" (a reference to an Attribute)
Binary Operation
2 ("left operand", "right operand")
"operator" (a value from a small, fixed set of strings)
Number
0
"value" (a string that represents a number value)

Compare expression concepts by separating their subexpression-bearing properties from their other properties. section 11.5.1

How Building User-Friendly DSLs covers it

How deep it goes
Defines arity, excludes non-operand properties, and connects positive arity to recursive AST construction and compositionality.
Examples
Uses literals, unary negation, binary multiplication, ternary conditionals, function calls, and arrays to illustrate common arities.
What it assumes
Assumes expressions and AST objects while defining arity and compositionality in context.
What you can do after
Evaluate/understand how expression concepts compose into larger ASTs and why a small set of concepts can provide broad expressiveness.
How it connects
Connects arity to expressions, ASTs, operands, binary operations, compositionality, and DSL expressiveness.

Also explained in

Compare how 5 books explain arity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.