Skip to content

Commit aaa2ac8

Browse files
committed
Add documentation to src/chc/*.rs
1 parent fde32b3 commit aaa2ac8

7 files changed

Lines changed: 93 additions & 1 deletion

File tree

src/chc/clause_builder.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
//! A builder for [`Clause`]s.
2+
//!
3+
//! This module provides [`ClauseBuilder`], a helper for constructing [`Clause`]s. It is
4+
//! particularly useful for managing the universally quantified variables of a clause. It can
5+
//! automatically create fresh [`TermVarIdx`] for variables from other domains (e.g.,
6+
//! [`crate::rty::FunctionParamIdx`]), simplifying the generation of clauses from higher-level
7+
//! representations.
8+
19
use std::any::{Any, TypeId};
210
use std::collections::HashMap;
311
use std::fmt::Debug;
@@ -8,6 +16,7 @@ use rustc_index::IndexVec;
816

917
use super::{Atom, Body, Clause, DebugInfo, Sort, TermVarIdx};
1018

19+
/// A convenience trait to represent constraints on variables used in [`ClauseBuilder`] at once.
1120
pub trait Var: Eq + Ord + Hash + Copy + Debug + 'static {}
1221
impl<T: Eq + Ord + Hash + Copy + Debug + 'static> Var for T {}
1322

@@ -54,6 +63,17 @@ impl Hash for dyn Key {
5463
}
5564
}
5665

66+
/// A builder for a [`Clause`].
67+
///
68+
/// [`Clause`] contains a list of universally quantified variables, a head atom, and a body formula.
69+
/// When building the head and body, we usually have some formulas that represents variables using
70+
/// something other than [`TermVarIdx`] (e.g. [`crate::rty::FunctionParamIdx`] or [`crate::refine::Var`]).
71+
/// These variables are usually OK to be universally quantified in the clause, so we want to keep
72+
/// the mapping of them to [`TermVarIdx`] and use it to convert the variables in the formulas
73+
/// during the construction of the clause.
74+
///
75+
/// Also see [`crate::rty::ClauseBuilderExt`], which provides a higher-level API on top of this
76+
/// to build clauses from [`crate::rty::Refinement`]s.
5777
#[derive(Clone, Default)]
5878
pub struct ClauseBuilder {
5979
vars: IndexVec<TermVarIdx, Sort>,

src/chc/debug.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! Attachable debug information for CHC clauses.
2+
//!
3+
//! The [`DebugInfo`] struct captures contextual information (like `tracing` spans) at the time
4+
//! of a clause's creation. This information is then pretty-printed as comments in the
5+
//! generated SMT-LIB2 file, which helps in tracing a clause back to its origin in the
6+
//! Thrust codebase.
7+
18
#[derive(Debug, Clone)]
29
pub struct Display<'a> {
310
inner: &'a DebugInfo,
@@ -19,6 +26,7 @@ impl<'a> std::fmt::Display for Display<'a> {
1926
}
2027
}
2128

29+
/// A purely informational metadata that can be attached to a clause.
2230
#[derive(Debug, Clone, Default)]
2331
pub struct DebugInfo {
2432
contexts: Vec<(String, String)>,

src/chc/format_context.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
//! A context for formatting a CHC system into SMT-LIB2.
2+
//!
3+
//! This module provides [`FormatContext`], which is responsible for translating parts of [`chc::System`]
4+
//! into a representation that is compatible with SMT solvers. It handles tasks like
5+
//! monomorphization of polymorphic datatypes and applying solver-specific workarounds.
6+
//! The [`super::smtlib2`] module uses this context to perform the final rendering to the SMT-LIB2 format.
7+
18
use std::collections::BTreeSet;
29

310
use crate::chc::{self, hoice::HoiceDatatypeRenamer};
411

12+
/// A context for formatting a CHC system.
13+
///
14+
/// This subsumes a representational difference between [`chc::System`] and resulting SMT-LIB2.
15+
/// - Gives a naming convention of symbols to represent built-in datatypes of [`chc::System`] in SMT-LIB2,
16+
/// - Gives a stringified representation of [`chc::Sort`]s,
17+
/// - Monomorphizes polymorphic datatypes of [`chc::System`] to be compatible with several CHC solvers,
18+
/// - Renames datatypes to be compatible with Hoice (see [`HoiceDatatypeRenamer`]),
19+
/// - etc.
520
#[derive(Debug, Clone)]
621
pub struct FormatContext {
722
renamer: HoiceDatatypeRenamer,

src/chc/hoice.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/// hopv/hoice#73
1+
//! A workaround for a bug in the Hoice CHC solver.
2+
23
use std::collections::{HashMap, HashSet};
34

45
use crate::chc;
@@ -53,6 +54,10 @@ impl<'a> SortDatatypes<'a> {
5354
}
5455
}
5556

57+
/// Rename to ensure the referring datatype name is lexicographically larger than the referred one.
58+
///
59+
/// Workaround for <https://github.com/hopv/hoice/issues/73>. Applied indirectly via
60+
/// [`crate::chc::format_context::FormatContext`] when formatting [`crate::chc::System`] as SMT-LIB2.
5661
#[derive(Debug, Clone, Default)]
5762
pub struct HoiceDatatypeRenamer {
5863
prefixes: HashMap<chc::DatatypeSymbol, String>,

src/chc/smtlib2.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
//! Wrappers around CHC structures to display them in SMT-LIB2 format.
2+
//!
3+
//! The main entry point is the [`System`] wrapper, which takes a [`chc::System`] and provides a
4+
//! [`std::fmt::Display`] implementation that produces a complete SMT-LIB2.
5+
//! It uses [`FormatContext`] to handle the complexities of the conversion,
6+
//! such as naming convention and solver-specific workarounds.
7+
//! The output of this module is what gets passed to the external CHC solver.
8+
19
use crate::chc::{self, format_context::FormatContext};
210

11+
/// A helper struct to display a list of items.
312
#[derive(Debug, Clone)]
413
struct List<T> {
514
open: Option<&'static str>,
@@ -79,6 +88,7 @@ impl<T> List<T> {
7988
}
8089
}
8190

91+
/// A wrapper around a [`chc::Term`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
8292
#[derive(Debug, Clone)]
8393
struct Term<'ctx, 'a> {
8494
ctx: &'ctx FormatContext,
@@ -202,6 +212,7 @@ impl<'ctx, 'a> Term<'ctx, 'a> {
202212
}
203213
}
204214

215+
/// A wrapper around a [`chc::Atom`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
205216
#[derive(Debug, Clone)]
206217
pub struct Atom<'ctx, 'a> {
207218
ctx: &'ctx FormatContext,
@@ -242,6 +253,7 @@ impl<'ctx, 'a> Atom<'ctx, 'a> {
242253
}
243254
}
244255

256+
/// A wrapper around a [`chc::Formula`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
245257
#[derive(Debug, Clone)]
246258
pub struct Formula<'ctx, 'a> {
247259
ctx: &'ctx FormatContext,
@@ -278,6 +290,7 @@ impl<'ctx, 'a> Formula<'ctx, 'a> {
278290
}
279291
}
280292

293+
/// A wrapper around a [`chc::Body`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
281294
#[derive(Debug, Clone)]
282295
pub struct Body<'ctx, 'a> {
283296
ctx: &'ctx FormatContext,
@@ -304,6 +317,7 @@ impl<'ctx, 'a> Body<'ctx, 'a> {
304317
}
305318
}
306319

320+
/// A wrapper around a [`chc::Clause`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
307321
#[derive(Debug, Clone)]
308322
pub struct Clause<'ctx, 'a> {
309323
ctx: &'ctx FormatContext,
@@ -340,6 +354,7 @@ impl<'ctx, 'a> Clause<'ctx, 'a> {
340354
}
341355
}
342356

357+
/// A wrapper around a [`chc::DatatypeSelector`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
343358
#[derive(Debug, Clone)]
344359
pub struct DatatypeSelector<'ctx, 'a> {
345360
ctx: &'ctx FormatContext,
@@ -363,6 +378,7 @@ impl<'ctx, 'a> DatatypeSelector<'ctx, 'a> {
363378
}
364379
}
365380

381+
/// A wrapper around a [`chc::DatatypeCtor`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
366382
#[derive(Debug, Clone)]
367383
pub struct DatatypeCtor<'ctx, 'a> {
368384
ctx: &'ctx FormatContext,
@@ -386,6 +402,7 @@ impl<'ctx, 'a> DatatypeCtor<'ctx, 'a> {
386402
}
387403
}
388404

405+
/// A wrapper around a slice of [`chc::Datatype`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
389406
#[derive(Debug, Clone)]
390407
pub struct Datatypes<'ctx, 'a> {
391408
ctx: &'ctx FormatContext,
@@ -423,6 +440,8 @@ impl<'ctx, 'a> Datatypes<'ctx, 'a> {
423440
}
424441
}
425442

443+
/// A wrapper around a [`chc::Datatype`] that provides a [`std::fmt::Display`] implementation for the
444+
/// discriminant function in SMT-LIB2 format.
426445
#[derive(Debug, Clone)]
427446
pub struct DatatypeDiscrFun<'ctx, 'a> {
428447
ctx: &'ctx FormatContext,
@@ -458,6 +477,8 @@ impl<'ctx, 'a> DatatypeDiscrFun<'ctx, 'a> {
458477
}
459478
}
460479

480+
/// A wrapper around a [`chc::Datatype`] that provides a [`std::fmt::Display`] implementation for the
481+
/// matcher predicate in SMT-LIB2 format.
461482
#[derive(Debug, Clone)]
462483
pub struct MatcherPredFun<'ctx, 'a> {
463484
ctx: &'ctx FormatContext,
@@ -507,6 +528,7 @@ impl<'ctx, 'a> MatcherPredFun<'ctx, 'a> {
507528
}
508529
}
509530

531+
/// A wrapper around a [`chc::System`] that provides a [`std::fmt::Display`] implementation in SMT-LIB2 format.
510532
#[derive(Debug, Clone)]
511533
pub struct System<'a> {
512534
ctx: FormatContext,

src/chc/solver.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! A generic interface for running external command-line CHC solvers.
2+
//!
3+
//! This module provides the [`Config`] struct for configuring and running an external
4+
//! CHC solver. It supports setting the solver command, arguments, and timeout, and can
5+
//! be configured through environment variables.
6+
7+
/// An error that can occur when solving a [`crate::chc::System`].
18
#[derive(Debug, thiserror::Error)]
29
pub enum CheckSatError {
310
#[error("unsat")]
@@ -12,6 +19,7 @@ pub enum CheckSatError {
1219
Io(#[from] std::io::Error),
1320
}
1421

22+
/// A configuration for running a command-line CHC solver.
1523
#[derive(Debug, Clone)]
1624
pub struct CommandConfig {
1725
pub name: String,
@@ -84,6 +92,13 @@ impl CommandConfig {
8492
}
8593
}
8694

95+
/// A configuration for solving a [`crate::chc::System`].
96+
///
97+
/// This struct holds the configuration for the solver, including the solver command, its
98+
/// arguments, and a timeout. It can also be configured to run a preprocessor on the SMT-LIB2
99+
/// file before passing it to the solver.
100+
///
101+
/// The configuration can be loaded from environment variables using [`Config::from_env`].
87102
#[derive(Debug, Clone)]
88103
pub struct Config {
89104
pub solver: CommandConfig,

src/chc/unbox.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! An optimization that removes `Box` sorts and terms from a CHC system.
2+
13
use super::*;
24

35
fn unbox_term(term: Term) -> Term {
@@ -118,6 +120,11 @@ fn unbox_datatype(datatype: Datatype) -> Datatype {
118120
}
119121
}
120122

123+
/// Remove all `Box` sorts and `Box`/`BoxCurrent` terms from the system.
124+
///
125+
/// The box values in Thrust represent an owned pointer, but are logically equivalent to the inner type.
126+
/// This pass removes them to reduce the complexity of the CHCs sent to the solver.
127+
/// This function traverses a [`System`] and removes all `Box` related constructs.
121128
pub fn unbox(system: System) -> System {
122129
let System {
123130
clauses,

0 commit comments

Comments
 (0)