A variable binding library based on well-scoped de Bruijn indices.
Please see the README on GitHub at https://github.com/sweirich/rebound
Rebound
Rebound is a variable binding library based on well-scoped de Bruijn indices and environments.
This library is represents variables using the index type Fin n; a type of bounded natural numbers. The key way to manipulate these indices is using an environment, a simultaneous substitutions similar to a function of type Fin n -> Exp m. Applying an environment converts an expression in scope n to one in scope m.
Design goals
The goal of this library is to be an effective tool for language experimentation. Say you want to implement a new language idea that you have read about in a PACMPL paper? This library will help you put together a prototype implementation quickly.
Correctness: This library uses Dependent Haskell to statically track the scopes of bound variables. Because variables are represented by de Bruijn indices, scopes are represented by natural numbers, bounding the indices that can be used. If the scope is 0, then the term must be closed.
Convenience: The library is based on a type-directed approach to binding, where AST terms can indicate binding structure through the use of types defined in this library. As a result the library provides a clean, uniform, and automatic interface to common operations such as substitution, alpha-equality, and scope change.
Efficiency: Behind the scenes, the library uses explicit substitutions (environments) to delay the execution of operations such as shifting and substitution. However, these environments are accessible to library users who would like fine control over when these operations.
Accessibility: This library comes with several examples demonstrating how to use it effectively. Many of these are also examples of programming with Dependent Haskell.
Examples
Calculi
Defines the syntax and substitution functions for the untyped lambda calculus. Uses these definitions to implement several interpreters.
Untyped lambda calculus with let rec and nested lets
Example of advanced binding forms: recursive definitions and sequenced definitions.
Untyped lambda calculus with pattern matching
Extends the lambda calculus example with pattern matching.
Working with two separate scopes (type and term variables) is tricky. This example shows one way to do it.
An alternative way of defining System F, using one single syntactic class. Also demonstrates how to use the
ScopedReadermonad for typechecking and pretty-printing.Simple implementation of dependent types
An implementation of a simple type checker for a dependent-type system. Language includes Pi and Sigma types.
A dependent type system with nested, dependent pattern matching. Patterns may also include scoped terms.
A linear version of the (simply typed) lambda calculus. Demonstrates how to thread a typing context using the
ScopedStatemonad.
Working with well-scoped expressions
Demonstrates how to convert a "named" (or nominal) expression to a well-scoped expression.
Demonstrates the use of well-scoped terms with QuickCheck.
Demonstrates how to layer a HOAS representation on top of a de Bruijn representation. Based on Conor McBride's "Classy Hack".
A variant of the Pat example, which demonstrates how generic programming can be used to derive some definitions.
Related libraries
Boundis the most closely related library. LikeRebound, it is a scope-safe approach to de Bruijn indices in Haskell. The key difference is thatboundrequires fewer language extensions by using nested datatypes instead of GADTs. Use this library if you would like to avoid extensions such asGADTs,DataKinds, andTypeFamilies.The
Unboundlibrary uses a locally-nameless reprsentation.Rebounddraws inspiration for its design from the type-directed approach to the binding interface found inUnbound. However,Unboundis not not-scope safe. As a result it is easier to get started. However, working with a locally nameless representation requires a monad for fresh name generation. It also can be slow.GHC internally uses a nominal representation of binding, where both bound and free variables are represented by names. In this approach, users must rename the bound variable in abstraction if it is already in the current scope.
Uses HOAS.