ArticleslgStudy

science

Higher-order abstract syntax

Higher-order abstract syntax is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Higher-order abstract syntax rather than just read about it. In short: In computer science, higher-order abstract syntax (abbreviated HOAS) is a technique for the representation of abstract syntax trees for languages with variable binders. Relation to first-order abstract syntax An abstract syntax is abstract because it is represented by mathematical objects that have certain structure by their very nature.

Key takeaways

  • Higher-order abstract syntax belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Higher-order abstract syntax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Higher-order abstract syntax from memory before moving on to harder problems.

Reference excerpt

In computer science, higher-order abstract syntax (abbreviated HOAS) is a technique for the representation of abstract syntax trees for languages with variable binders.

Relation to first-order abstract syntax An abstract syntax is abstract because it is represented by mathematical objects that have certain structure by their very nature. For instance, in first-order abstract syntax (FOAS) trees, as commonly used in compilers, the tree structure implies the subexpression relation, meaning that no parentheses are required to disambiguate programs (as they are, in the concrete syntax). HOAS exposes additional structure: the relationship between variables and their binding sites. In FOAS representations, a variable is typically represented with an identifier, with the relation between binding site and use being indicated by using the same identifier. With HOAS, there is no name for the variable; each use of the variable refers directly to the binding site. There are a number of reasons why this technique is useful. First, it makes the binding structure of a program explicit: just as there is no need to explain operator precedence in a FOAS representation, there is no need to have the rules of binding and scope at hand to interpret a HOAS representation. Second, programs that are alpha-equivalent (differing only in the names of bound variables) have identical representations in HOAS, which can make equivalence checking more efficient.

Implementation One mathematical object that could be used to implement HOAS is a graph where variables are associated with their binding sites via edges. Another popular way to implement HOAS (in, for example, compilers) is with de Bruijn indices.

Use in logic programming The first programming language which directly supported λ-bindings in syntax was the higher-order logic programming language λProlog. The paper that introduced the term HOAS used λProlog code to illustrate it. Unfortunately, when one transfers the term HOAS from the logic programming to the functional programming setting, that term implies the identification of bindings in syntax with functions over expressions. In this latter setting, HOAS has a different and problematic sense. The term λ-tree syntax has been introduced to refer specifically to the style of representation available in the logic programming setting. While different in detail, the treatment of bindings in λProlog is similar to their treatment in logical frameworks, elaborated in the next section.

Use in logical frameworks In the domain of logical frameworks, the term higher-order abstract syntax is usually used to refer to a specific representation that uses the binders of the meta-language to encode the binding structure of the object language. For instance, the logical framework LF has a λ-construct, which has arrow (→) type. As an example, consider we wanted to formalize a very primitive language with untyped expressions, a built-in set of variables, and a let construct (let <var> = <exp> in <exp'>), which allows to bind variables var with definition exp in expressions exp'. In Twelf syntax, we could do as follows:

Here, exp is the type of all expressions and var the type of all built-in variables (implemented perhaps as natural numbers, which is not shown). The constant v acts as a casting function and witnesses the fact that variables are expressions. Finally, the constant let represents let constructs of the form let <var> = <exp> in <exp>: it accepts a variable, an expression (being bound by the variable), and another expression (that the variable is bound within). The canonical HOAS representation of the same object language would be:

In this representation, object level variables do not appear explicitly. The constant let takes an expression (that is being bound) and a meta-level function exp → exp (the body of the let). This function is the higher-order part: an expression with a free variable is represented as an expression with holes that are filled in by the meta-level function when applied. As a concrete example, we would construct the object level expression

(assuming the natural constructors for numbers and addition) using the HOAS signature above as

where [y] e is Twelf's syntax for the function λ y . e {\displaystyle \lambda y.e} . This specific representation has advantages beyond the ones above: for one, by reusing the meta-level notion of binding, the encoding enjoys properties such as type-preserving substitution without the need to define/prove them. In this way using HOAS can drastically reduce the amount of boilerplate code having to do with binding in an encoding. Higher-order abstract syntax is generally only applicable when object language variables can be understood as variables in the mathematical sense (that is, as stand-ins for arbitrary members of some domain). This is often, but not always, the case: for instance, there are no advantages to be gained from a HOAS encoding of dynamic scope as it appears in some dialects of Lisp because dynamically scoped variables do not act like mathematical variables.

See also Generalized algebraic data type Parametric higher-order abstract syntax (PHOAS)

References

Further reading J. Despeyroux; A. Felty; A. Hirschowitz (1995). "Higher-order abstract syntax in Coq". Typed Lambda Calculi and Applications. Lecture Notes in Computer Science. Vol. 902. pp. 124–138. doi:10.1007/BFb0014049. ISBN 978-3-540-59048-4. Archived from the original on 2006-08-30. Martin Hofmann (1999). Semantical analysis of higher-order abstract syntax. 14th Annual IEEE Symposium on Logic in Computer Science. p. 204. ISBN 0-7695-0158-3. Eli Barzilay; Stuart Allen (2002). Reflecting Higher-Order Abstract Syntax in Nuprl (PDF). Theorem Proving in Higher-Order Logics 2002. pp. 23–32. ISBN 3-540-44039-9. Archived from the original (PDF) on 2006-10-11. Eli Barzilay (2006). A Self-Hosting Evaluator using HOAS (PDF). ICFP Workshop on Scheme and Functional Programming 2006.

Worked examples

Example 1 — a first encounter with Higher-order abstract syntax

Start with the simplest possible case. Write down what Higher-order abstract syntax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Higher-order abstract syntax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Higher-order abstract syntax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Higher-order abstract syntax

In research
Higher-order abstract syntax appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Higher-order abstract syntax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Higher-order abstract syntax is common in secondary-school and first-year university syllabi. It links to neighbouring topics Dependently typed programming, Logic programming, Programming language theory, so understanding it makes those chapters shorter.
In everyday life
Look for Higher-order abstract syntax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Higher-order abstract syntax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Higher-order abstract syntax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Higher-order abstract syntax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Higher-order abstract syntax in simple terms?

In computer science, higher-order abstract syntax (abbreviated HOAS) is a technique for the representation of abstract syntax trees for languages with variable binders. Relation to first-order abstract syntax An abstract syntax is abstract because it is represented by mathematical objects that have…

Why does Higher-order abstract syntax matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Higher-order abstract syntax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Higher-order abstract syntax.

Tags

  • Dependently typed programming
  • Logic programming
  • Programming language theory
  • Type theory

Keep exploring