Overview
A thread-safe library to manipulate first-order terms. A first-order term is defined by the following grammar:
t := c | f(t1, ..., tn) | u64
where f is a function symbol with arity n > 0 and a name, c is a function
symbol with arity zero and u64 is a numerical term. As such f(a, g(b)) is an
example of a term with constants a and b. However, in practice we can also
represent expressions such as 5 + 7 * 3 > 2 as terms or even computations such
as sort([3, 1, 2]), using appropriate function symbols for list concatenation
and the integers. These expressions are then typically manipulated by a term rewrite
engine, such as the one provided in merc_sabre.
Terms are stored maximally shared in the global aterm pool, meaning that two terms are structurally equivalent if and only if they have the same memory address. This allows for very efficient equality checking and compact memory usage.
Terms are immutable, but can be accessed concurrently in different threads. They are periodically garbage collected when they are no longer reachable. This is ensured by thread-local protection sets that keep track of reachable terms. Note that the name stands for Annotated terms, but this is only a leftover from the history, as of today terms can no longer be annotated with additional information.
Usage
The main trait of the library is the Term trait, which is implemented by every
struct that behaves as a first-order term, and can be used to generically deal
with terms. The Symb trait does the same for function symbols.
The main implementations of this trait are the ATerm and ATermRef structs,
which represent owned and borrowed terms respectively. The ATermRef struct
carries a lifetime to ensure that borrowed terms are not used after they are no
longer protected, and as such avoid use-after-free errors.
In general terms can simply be created by using their constructors:
use ;
let a = constant;
let f = with_args; // Creates f(a, a)
let g = from_string.unwrap; // Parses the term from a string
assert!; // Access first argument of g
Details
The crate is heavily optimised for performance, avoiding unnecessary allocations
for looking up terms that already exist, and avoiding protections when possible,
by using the ATermRef struct and the Return struct to cheaply return terms
without taking ownership. Furthermore, the Protected struct can be used to
cheaply store many terms in a single protection, for example by using
Protected<Vec<ATermRef<'static>>> to store a vector of terms.
The crate also provides serialization of terms to the same binary format that is
used in the mCRL2 toolset (implemented in the aterm_binary_stream module),
allowing compact storage of terms.
Macros
The merc_derive_terms proc macro can be used to generate the necessary boilerplate
code for structs to behave as aterms with additional structure. This is
heavily used in the merc_data crate, but also in the term library to define
lists and integers. The macro must be added to a module that contains the
definitions for the underlying data types for which the boilerplate code should
be generated, as shown in the example below:
use merc_derive_terms;
use ATerm;
use Term;
use Symbol;
use DataExpression;
use DataExpressionRef;
// Here we can now use the generated code:
let expr = with_sort;
let expr_ref: DataExpressionRef = expr.copy;
Safety
This crate does use unsafe for some of the more intricate parts of the
library, but every module that only uses safe Rust is marked with
#![forbid(unsafe_code)]. This crate is a full reimplementation of the ATerm
library used in the mCRL2 toolset.
Misuse of GcMutex, used by the Protected struct, can lead to undefined
behaviour in safe code. Ensure that the guards returned from GcMutex are not
used after the THREAD_TERM_POOL is dropped, which happens when the thread
terminates. The same applies to the Return<T> struct, which also holds a
reference to the thread local term pool. Alternatively, we could have required
access to THREAD_TERM_POOL to only be called through the closures, but this
would have made the API more cumbersome to use.
Furthermore, the protect and protect_symbol methods of Protected must be
used in a way that the resulting term or symbol is inserted into the container,
otherwise undefined behaviour may occur after garbage collection. This is
checked in debug mode, but the checks are far too expensive to be performed in
release mode.
Reading the value() of an ATermInt that is not actually a valid integer term
leads to undefined behaviour. This is also only checked in debug mode.
Changelog
2.0.0
Removed the ahash, arbitrary, arbtest, and rayon dependencies since
their use was only minimal. Fixed a bug where garbage collection of terms was
not triggered properly.
Reduced the size of terms by introducing fixed-size storages for small terms,
and moving annotations to only the ATermInt type. Furthermore, use a block
allocator to avoid the overhead of many small allocations for terms. Garbage
collection is now triggered properly and no longer leads to subtle bugs, and
some unsound implementation of Return<T> has been removed.
Related work
Further details on the implementation are explained in the following paper:
Jan Friso Groote, Kevin H.J. Jilissen, Maurice Laveaux, Flip van Spaendonck. Using the Parallel ATerm Library for Parallel Model Checking and State Space Generation. A Journey from Process Algebra via Timed Automata to Model Learning. DOI.
The initial ATerm library was presented by the following article:
M.G.J. van den Brand, H.A. de Jong, P. Klint, P.A. Olivier. Efficient annotated terms. Software: Practice and Experience. DOI.
Authors
This crate was heavily inspired by the original ATerm library, and many ideas from the original authors.
Minimum Supported Rust Version
We do not maintain an official minimum supported rust version (MSRV), and it may be upgraded at any time when necessary.
License
All MERC crates are licensed under the BSL-1.0 license. See the LICENSE file in the repository root for more information.