use crate::export::Styles;
use crate::linear::proofs::cut;
#[cfg(feature = "interactive")]
use crate::linear::proofs::interactive;
use crate::linear::search::{Jobs, Options, Schedule, batch};
use crate::{
Limits, Phase, Progress,
linear::{InputOptions, ViewOptions},
ordinary,
};
use std::time::Duration;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
serde(default, deny_unknown_fields)
)]
pub struct Clock {
#[cfg_attr(feature = "serialize", serde(with = "crate::serialize::exact"))]
pub time_limit_ms: Option<u64>,
#[cfg_attr(feature = "serialize", serde(with = "crate::serialize::exact"))]
pub pool_after_ms: Option<u64>,
#[cfg_attr(feature = "serialize", serde(with = "crate::serialize::exact"))]
pub batch_time_limit_ms: Option<u64>,
}
impl Clock {
pub const DEFAULT_TIME_LIMIT_MS: u64 = 2000;
pub const DEFAULT_POOL_AFTER_MS: u64 = 100;
#[must_use]
pub const fn with_time_limit_ms(self, time_limit_ms: Option<u64>) -> Self {
Self {
time_limit_ms,
..self
}
}
#[must_use]
pub const fn with_pool_after_ms(self, pool_after_ms: Option<u64>) -> Self {
Self {
pool_after_ms,
..self
}
}
#[must_use]
pub const fn with_batch_time_limit_ms(self, batch_time_limit_ms: Option<u64>) -> Self {
Self {
batch_time_limit_ms,
..self
}
}
}
impl Default for Clock {
fn default() -> Self {
Self {
time_limit_ms: Some(Self::DEFAULT_TIME_LIMIT_MS),
pool_after_ms: Some(Self::DEFAULT_POOL_AFTER_MS),
batch_time_limit_ms: None,
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
serde(default, deny_unknown_fields)
)]
pub struct Settings {
pub clock: Clock,
pub limits: Limits,
pub search: Options,
pub view: ViewOptions,
pub styles: Styles,
pub batch: batch::Options,
pub ordinary: ordinary::Options,
#[cfg(feature = "interactive")]
pub interactive: interactive::Options,
pub input: InputOptions,
pub cut: cut::Options,
pub lambda: crate::lambda::Options,
}
impl Default for Settings {
fn default() -> Self {
Self {
clock: Clock::default(),
limits: Limits::default(),
search: Options::default().with_copies(None).with_jobs(Jobs::Auto),
view: ViewOptions::default(),
styles: Styles::default(),
batch: batch::Options::default(),
ordinary: ordinary::Options::default(),
#[cfg(feature = "interactive")]
interactive: interactive::Options::default(),
input: InputOptions::default(),
cut: cut::Options::default(),
lambda: crate::lambda::Options::default(),
}
}
}
impl Settings {
#[must_use]
pub fn with_clock(self, clock: Clock) -> Self {
Self { clock, ..self }
}
#[must_use]
pub fn with_limits(self, limits: Limits) -> Self {
Self { limits, ..self }
}
#[must_use]
pub fn with_search(self, search: Options) -> Self {
Self { search, ..self }
}
#[must_use]
pub fn with_input(self, input: InputOptions) -> Self {
Self { input, ..self }
}
#[must_use]
pub fn with_view(self, view: ViewOptions) -> Self {
Self { view, ..self }
}
#[must_use]
pub fn with_styles(self, styles: Styles) -> Self {
Self { styles, ..self }
}
#[must_use]
pub fn with_batch(self, batch: batch::Options) -> Self {
Self { batch, ..self }
}
#[must_use]
pub fn with_ordinary(self, ordinary: ordinary::Options) -> Self {
Self { ordinary, ..self }
}
#[must_use]
pub fn with_cut(self, cut: cut::Options) -> Self {
Self { cut, ..self }
}
#[cfg(feature = "interactive")]
#[must_use]
pub fn with_interactive(self, interactive: interactive::Options) -> Self {
Self {
interactive,
..self
}
}
#[cfg_attr(feature = "parse", doc = "```")]
#[cfg_attr(not(feature = "parse"), doc = "```ignore")]
pub fn stop<E: Fn() -> Duration>(&self, elapsed: E) -> impl Fn(Progress) -> bool + use<E> {
let limit = self.clock.time_limit_ms.map(Duration::from_millis);
move |progress: Progress| {
!matches!(progress.phase, Phase::Refute)
&& limit.is_some_and(|limit| elapsed() >= limit)
}
}
#[cfg_attr(feature = "parse", doc = "```")]
#[cfg_attr(not(feature = "parse"), doc = "```ignore")]
#[must_use]
pub fn deterministic(self) -> Self {
Self {
search: self
.search
.with_jobs(Jobs::Count(1))
.with_schedule(Schedule::Turns),
clock: Clock {
pool_after_ms: None,
..self.clock
},
..self
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::cell::Cell;
#[test]
fn deterministic_keeps_one_thread() {
let settings = Settings::default();
let one = settings.clone().deterministic();
assert_eq!(one.search.jobs, Jobs::Count(1));
assert_eq!(one.search.schedule, Schedule::Turns);
assert_eq!(one.clock.pool_after_ms, None);
assert_eq!(one.clock.time_limit_ms, settings.clock.time_limit_ms);
assert_eq!(one.limits, settings.limits);
}
#[test]
fn the_stop_lets_the_refutation_run_past_the_limit() {
let now = Cell::new(Duration::from_millis(1999));
let asked = Cell::new(0);
let elapsed = || {
asked.set(asked.get() + 1);
now.get()
};
let settings = Settings::default();
let stop = settings.stop(elapsed);
let at = |phase| Progress::new(phase, 0, 0);
assert!(!stop(at(Phase::Search)));
now.set(Duration::from_millis(2000));
assert!(stop(at(Phase::Search)));
assert!(stop(at(Phase::Check)));
let before = asked.get();
assert!(!stop(at(Phase::Refute)));
assert_eq!(asked.get(), before);
let unlimited = settings.with_clock(Clock::default().with_time_limit_ms(None));
assert!(!unlimited.stop(|| Duration::MAX)(at(Phase::Search)));
}
}