#![cfg(all(feature = "parse", feature = "serialize", feature = "interactive"))]
use deduxit::linear::search::Engine;
use deduxit::{
Limits,
linear::{
Criterion, Forest, Interactive, Mode, Named, Options, ProofStructure, Sequent, Step,
VertexId, ViewOptions,
},
};
use std::fmt::Write as _;
fn sequent(text: &str) -> Sequent {
text.parse().unwrap()
}
fn json<T: serde::Serialize>(value: &T) -> String {
serde_json::to_string(value).unwrap()
}
fn lines() -> String {
let mut out = String::new();
let mut pin = |name: &str, json: String| writeln!(out, "{name}: {json}").unwrap();
let classical = Mode::CLASSICAL;
let intuitionistic = Mode::INTUITIONISTIC;
let affine = Mode::CLASSICAL.with_affine();
let mix = Mode::CLASSICAL.with_mix();
let options = Options::default();
for (name, text) in [
("sequent-mll", "A * B |- B * A"),
("sequent-units", "1, bot |- top, 0"),
("sequent-ll", "!(A & B), ?C |- A -o ~B + C"),
("sequent-empty", "|-"),
("sequent-repeated", "A, A |- A * A"),
] {
pin(name, json(&sequent(text)));
}
for (name, text, mode) in [
("outcome-mll", "A * B |- B * A", classical),
("outcome-mall", "A & B |- A + B", classical),
("outcome-mell", "!A |- A * !A", classical),
("outcome-units", "1, bot |- 1 * bot", classical),
("outcome-intuitionistic", "A, A -o B |- B", intuitionistic),
("outcome-affine", "A, B |- A", affine),
("outcome-mix", "A, B |- A, B", mix),
("outcome-horn", "!(a -o a * b), a |- a * b * b", classical),
("outcome-unbalanced", "A |- A * A", classical),
("outcome-one-sided", "|- ?A, A", classical),
("outcome-equation", "|- A * ~A", classical),
("outcome-exhausted", "A & B |- A * B", classical),
(
"outcome-state-equation",
"!(A -o A * A), !(B * B -o C), A, B |- C",
classical,
),
] {
let outcome = deduxit::linear::prove(&sequent(text), mode, &options).unwrap();
pin(name, json(&outcome));
}
let hard = sequent("!(A & B) |- !A * ?B");
let tiny = Limits::default().with_memory_bytes(Some(100));
pin(
"outcome-memory-limit",
json(&deduxit::linear::prove_within(&hard, classical, &options, &tiny, |_| false).unwrap()),
);
let stopped =
deduxit::linear::prove_within(&hard, classical, &options, &Limits::default(), |_| true)
.unwrap();
pin("outcome-stopped", json(&stopped));
let bounded = options.clone().with_copies(Some(0));
pin(
"outcome-copy-bound",
json(&deduxit::linear::prove(&hard, classical, &bounded).unwrap()),
);
let forced = options.clone().with_engine(Some(Engine::Focus));
pin(
"outcome-forced-engine",
json(&deduxit::linear::prove(&sequent("A * B |- B * A"), classical, &forced).unwrap()),
);
let proved = |text: &str, mode| match deduxit::linear::prove(&sequent(text), mode, &options)
.unwrap()
.verdict
{
deduxit::linear::Verdict::Proved(proof) => proof,
other => panic!("{text}: {other:?}"),
};
for (name, text, mode) in [
("proof-mll", "A * B |- B * A", classical),
("proof-ll", "!(A & B) |- !A * ?B", classical),
("proof-intuitionistic", "A, A -o B |- B", intuitionistic),
("proof-affine", "A, B |- A", affine),
("proof-mix", "A, B |- A, B", mix),
] {
pin(name, json(&proved(text, mode)));
}
let swap = proved("A * B |- B * A", classical);
pin(
"structure-from-proof",
json(
&ProofStructure::from_proof(&swap, Criterion::MLL, &Limits::default(), |_| false)
.unwrap(),
),
);
let modus = sequent("A, A -o B |- B");
let partial = ProofStructure::from_links(
Forest::new(&modus).unwrap(),
Criterion::MLL.with_mix(),
&[(VertexId::new(0), VertexId::new(2))],
)
.unwrap();
pin("structure-partial", json(&partial));
let mut session = Interactive::new(&sequent("A * B |- B * A"), classical).unwrap();
pin("session-open", json(&session));
let root = session.goals().next().unwrap();
session
.apply(root, &Step::new(0, "par".parse::<Named>().unwrap()))
.unwrap();
pin("session-step", json(&session));
session.close_all(
&options,
&ViewOptions::default(),
&Limits::default(),
|_| false,
);
pin("session-closed", json(&session));
let mut session = Interactive::new(&sequent("A, A -o B |- B"), intuitionistic).unwrap();
session.close_all(
&options,
&ViewOptions::default(),
&Limits::default(),
|_| false,
);
pin("session-intuitionistic", json(&session));
pin(
"outcome-classical",
json(&deduxit::linear::prove(&sequent("A + B |- A"), classical, &options).unwrap()),
);
out
}
#[test]
fn json_lock() {
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/lock/json.txt");
let got = lines();
if std::env::var_os("BLESS").is_some() {
std::fs::write(&path, &got).unwrap();
return;
}
let pinned = std::fs::read_to_string(&path).unwrap_or_default();
let changed: Vec<&str> = got
.lines()
.zip(pinned.lines())
.filter(|(a, b)| a != b)
.map(|(a, _)| a.split(':').next().unwrap())
.collect();
assert!(
changed.is_empty() && got.lines().count() == pinned.lines().count(),
"a JSON form changed (BLESS=1 rewrites the file): {changed:?}, {} lines where {} are pinned",
got.lines().count(),
pinned.lines().count()
);
}