Higher-order abstract syntax
From Wikipedia, the free encyclopedia
In computer science, higher-order abstract syntax (abbreviated HOAS) is a technique for the representation of abstract syntax trees for languages with variable binders.
Contents |
[edit] Relation to first-order abstract syntax
An abstract syntax tree is abstract because it is a mathematical object that has certain structure by its 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 in order 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.
[edit] 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.
[edit] 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 in order to encode the binding structure of the object language.
For instance, the logical framework LF has a λ-construct, which has arrow (→) type. A first-order encoding of an object language construct let
would be (using Twelf syntax):
exp : type. var : type. v : var -> exp. let : exp -> var -> exp -> exp.
Here, exp
is the family of object language expressions. The family var
is the representation of variables (implemented perhaps as natural numbers, which is not shown); the constant v
witnesses the fact that variables are expressions. The constant let
is an expression that takes three arguments: an expression (that is being bound), a variable (that it is bound to) and another expression (that the variable is bound within).
The canonical HOAS representation of the same object language would be:
exp : type. let : exp -> (exp -> exp) -> exp.
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
let x = 1 + 2 in x + 3
(assuming the natural constructors for numbers and addition) using the HOAS signature above as
let (plus 1 2) ([y] plus y 3)
where [y] e
is Twelf's syntax for the function λ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.
Because this technique reuses the mechanism of the meta-language to encode a concept in the object language, it is generally only applicable when the meta-language and object-language notions of binding coincide. This is often the case, but not always: for instance, it is unlikely that a HOAS encoding of dynamic scope such as in Lisp would be possible in a statically-scoped language like LF.
[edit] References
- Dale Miller and Gopalan Nadathur (1987). "A Logic Programming Approach to Manipulating Formulas and Programs". IEEE Symposium on Logic Programming: 379-388.
- Frank Pfenning, Conal Elliott (1988). "Higher-order abstract syntax". Proceedings of the ACM SIGPLAN PLDI '88: 199–208. doi:10.1145/53990.54010. ISBN 0-89791-269-1.
- J. Despeyroux, A. Felty, A. Hirschowitz (1995). "Higher-Order Abstract Syntax in Coq". Lecture Notes in Computer Science 902: 124–138. ISBN 3-540-59048-X.
- Martin Hofmann (1999). "Semantical analysis of higher-order abstract syntax". 14th Annual IEEE Symposium on Logic in Computer Science: 204. ISBN 0-7695-0158-3.
- Dale Miller (2000). "Abstract Syntax for Variable Binders: An Overview". Computational Logic - {CL} 2000: 239-253.
- Eli Barzilay, Stuart Allen (2002). "Reflecting Higher-Order Abstract Syntax in Nuprl". Theorem Proving in Higher-Order Logics 2002: 23–32. ISBN 3-540-44039-9.
- Eli Barzilay (2006). "A Self-Hosting Evaluator using HOAS". ICFP Workshop on Scheme and Functional Programming 2006.