1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// ::formally - the open-source formal methods toolchain
//
// Copyright (c) 2026 Nicola Gigante
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//! SMT backend(s) and facilities to write them.
//!
//! This module provides the supported SMT backends and facilities to write new ones.
//!
//! Currently, we provide a [Z3](z3::Z3) and a [cvc5](cvc5::Cvc5) backends, with others following
//! later.
//!
//! The backend is chosen when instantiating a [TermManager](smt::TermManager) or, as a shortcut,
//! with the [Solver::with_backend()](smt::Solver::with_backend) function when constructing
//! a [Solver](smt::Solver).
//!
//! ```
//! # mod formally {
//! # pub extern crate formally_support as support;
//! # pub extern crate formally_smt as smt;
//! # }
//! # use formally::support::*;
//! use formally::smt::{*, backends::z3::Z3};
//! # fn main() -> Result<()> {
//! let config = Config::default();
//! let manager = TermManager::with_backend(Z3)?;
//! let solver = Solver::with_manager(&config, manager)?;
//!
//! // ...
//!
//! # Ok(())
//! # }
//! ```
//!
//! # How to write new backends
//!
//! A backend is a type implementing the [Backend] trait. This trait only provides three methods:
//! 1. [name()](Backend::name()), which just returns a string with the name of the backend.
//! 2. [manager()](Backend::manager()), which returns an instance of a backend's *term manager*
//! (also called *context* in some backends).
//! 2. [solver()](Backend::solver()), which returns an instance of a backend's *solver*
//!
//! A high-level user of the framework never calls these methods.
//! 1. The backend type is what you pass around to tell *which* backend you want to use, e.g. in the
//! [Config] of a [Solver].
//! 2. The [backends::Manager](Manager) and [backends::Solver](Manager) instances do the actual job,
//! but are used internally by the [Solver](smt::Solver) type.
//!
//! # The API facade
//!
//! The [api] module contains two types, [SolverFacade](api::SolverFacade) and
//! [ManagerFacade](api::ManagerFacade), that help implementing a backend based on common
//! programmatic APIs (as opposed to calling a command-line tool). Using the API facade should be
//! preferred respecting to implementing [Backend] directly because it hides a non-trivial amounts
//! of complexity. We refer to the documentation of the [api] module for details.
//!
//! In any case, remember to register your backend using the [backend](smt::backend) attribute for
//! it to be available when looking up backends by name.
//!
//! # How to write a backend from scratch
//!
//! When the API facade is not sufficient or suitable, a [Backend] instance has to be written from
//! scratch. The documentation of the [Backend], [Manager] and [Solver] traits provide specific
//! details. What follows explain general concepts and requirements.
//!
//! ## Managers and Solvers
//!
//! Most SMT APIs distinguish between a *term manager* (or *context*) which handles the life of
//! terms, and a *solver* which actually perform the reasoning process. The backend thus provides
//! an instance [Manager] for the former and [Solver] for the latter. the [manager](Manager) is
//! meant to be used exclusively by the corresponding [Solver] and the trait does not actually
//! provide any method beside [Manager::backend()]. Since `Manager: Any`, under the hood the backend
//! can downcast `&dyn Manager` to `&T` for any specific type `T` and use it as needed.
//!
//! Note that different [solvers][Solver] can be created with the same [Manager] through the
//! [Backend::solver()] method, so the backend code is expected to account for this fact.
//!
//! ## Theories and logics
//!
//! The logic to instantiate the backend with is set by name in the [Config] object. The backend
//! instance is then expected to provide a `&dyn Logic` reference through the
//! [logic()](Solver::logic()) method. For standard SMT-LIBv2 logics, backends can use the
//! [standard_logic()](logics::standard_logic()) function to lookup a standard logic by name.
//!
//! However, note that backends do not necessarily have to associate names of standard logics (e.g.
//! `"LIA"`) to the corresponding types declared in the [logics] module. What *is* expected is that
//! the symbols provided by the background theories of standard logics correspond to those
//! provided in the [theories](mod@smt::theories) module.
//!
//! For instance, if a backend supports some non-standard extension of `"LIA"` (e.g. an additional
//! function symbol), it can declare a custom `"LIA"` logic that combines the standard `Ints` theory
//! with a custom theory that declares the additional symbol. On the converse, if a solver supports
//! `"LIA"` partially, e.g. everything except the `abs` function, it can redeclare a custom logic
//! based on the standard [Ints](smt::theories::Ints) theory and add a syntactic requirement that
//! scan terms to forbid the use of `abs`.
//!
//! ## Error handling in backends
//!
//! In order to simplify the life of backend developers and provide a consistent experience to
//! users of different backends, backends *do not* directly emit their errors as diagnostics.
//! Instead, the methods of [Solver] return a [Result] whose error type is [Error](struct@Error),
//! which provides a non-exhaustive taxonomy of the possible errors that a backend may encounter.
//! [Error](struct@Error) is [Diagnosable], so users of the backend interface (e.g. [Solver]) can
//! still easily emit those errors as diagnostics later.
//!
//! ## Assumptions and guarantees
//!
//! The documentation of each method in [Solver] provides a set of preconditions that the methods
//! can assume to hold. When the backend instance methods are invoked by [Solver], these assumptions
//! are guaranteed to hold. However, to guarantee stability even in the presence of bugs (both
//! in the framework and in the backend), backends are expected to behave as follows.
//!
//! 1. any internal error should *not* cause *undefined behavior*, i.e. unsafe code in the backend
//! cannot depend on external preconditions to hold. This includes C or C++ code called inside
//! the backend code to interface with external APIs.
//! 2. any internal error should *not* cause a Rust panic or a C++ exception to unwind past the
//! boundary of the [Solver] methods. If the backend contains code that may panic or throw an
//! exception, that code must be wrapped either inside a [catch_unwind](std::panic::catch_unwind)
//! call in Rust or a `try { ... } catch { ... }` block in C++, and the error turned into a
//! [Error](struct@Error) of kind [ErrorKind::Internal].
//! 3. a violated precondition *can* result either into a [Error](struct@Error) of kind
//! [ErrorKind::ViolatedPrecondition] or into a logical misbehaviour, i.e., wrong answers,
//! deadlocks, etc.. In other words, backends are not *forced* to detect the violation of the
//! preconditions, and can have arbitrary (but *not* undefined) behavior in those cases.
pub use *;
use crateformally;
use ;
use ;
use Display;
use Error;
/// The error type for backends.
///
/// This error type is [Diagnosable] so it is emitted as a diagnostic when converted to
/// [DiagnosticEmitted](formally::support::DiagnosticEmitted) (e.g. via the `?` operator).
/// An enumeration of possible errors for [Error](struct@Error).
/// The trait for SMT backends.
///
/// Types implementing this trait represent backends. See the top-level documentation
/// for details about [how to write a new backend](smt::backends).
///
/// The trait requires`'static'`, [Send] and [Sync] but that is trivial to satisfy because the
/// backend types themselves are usually just unit structs doing nothing. The returned [Manager] and
/// [Solver] types do *not* need to (and usually cannot) implement [Send] nor [Sync].
/// The trait for *term managers* of SMT backends.
///
/// This trait does not provide any method besides [backend()](Manager::backend()) because it is
/// only meant to be used in `dyn` objects which are downcasted as needed by the corresponding
/// [solver][Solver].
/// The trait for *solvers* of SMT backends.
///
/// Types implementing this trait are the ones doing the hard work for backends.
///
/// The documentation of each method lists its intended purpose and what external preconditions
/// the method can assume to hold when the backend is used through a [Solver].
///
/// However, please read before the documentation on [how to write a new backend](smt::backends).