#![cfg(all(feature = "serialize", feature = "interactive"))]
use deduxit::wire::{self, Readable};
use deduxit::{
Limits,
linear::{
Disproof, Engine, IllSequent, Interactive, MellStructure, Outcome, Proof, ProofStructure,
Sequent,
},
};
fn read<T: Readable>(name: &str, json: &str) -> T {
let mut document = serde_json::Deserializer::from_str(json);
let value = wire::upgrade(&mut document, &Limits::default())
.unwrap_or_else(|error| panic!("{name}: {error}"));
document.end().unwrap();
value
}
fn again<T: Readable + serde::Serialize>(name: &str, json: &str) {
let value: T = read(name, json);
assert_eq!(
serde_json::to_string(&value).unwrap(),
written_now(json),
"{name}"
);
}
fn written_now(json: &str) -> String {
const KEY: &str = r#""deduxit":""#;
let Some(start) = json.find(KEY).map(|at| at + KEY.len()) else {
return json.to_owned();
};
let end = start + json[start..].find('"').unwrap();
format!(
"{}{}{}",
&json[..start],
env!("CARGO_PKG_VERSION"),
&json[end..]
)
}
#[test]
fn the_first_release_reads() {
let lines = include_str!("wire/level-1.txt");
let mut read_back = 0;
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
let verdict = |v: &str| json.contains(&format!(r#""verdict":"{v}""#));
match name.split('-').next().unwrap() {
"sequent" => again::<Sequent>(name, json),
"proof" => again::<Proof>(name, json),
"disproof" => again::<Disproof>(name, json),
"structure" => again::<ProofStructure>(name, json),
"session" => again::<Interactive>(name, json),
"outcome" if verdict("proved") => {
let _: Proof = read(name, json);
}
"outcome" if verdict("unprovable") => {
let _: Disproof = read(name, json);
}
"outcome" => continue,
other => panic!("{name}: no form {other}"),
}
read_back += 1;
}
assert_eq!(read_back, 37, "every line but the three unknown outcomes");
}
#[test]
fn the_second_level_reads() {
let lines = include_str!("wire/level-2.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"disproof" => again::<Disproof>(name, json),
"outcome" => {
let _: Disproof = read(name, json);
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 2);
}
#[test]
fn the_third_level_reads() {
let lines = include_str!("wire/level-3.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"disproof" => again::<Disproof>(name, json),
"outcome" => {
let disproof: Disproof = read(name, json);
assert_eq!(disproof.check(), Ok(()), "{name}");
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 2);
}
#[test]
fn the_fourth_level_reads() {
let lines = include_str!("wire/level-4.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"structure" => {
again::<MellStructure>(name, json);
let net: MellStructure = read(name, json);
assert_eq!(net.is_correct(|_| false), Ok(()), "{name}");
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 1);
}
#[test]
fn the_fifth_level_reads() {
let lines = include_str!("wire/level-5.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"sequent" => {
again::<IllSequent>(name, json);
let sequent: IllSequent = read(name, json);
assert_eq!(sequent.to_string(), "!(A ⊸ B), ⊤, 0 ⊢ (A ⊗ 1) & (B ⊕ ⊤)");
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 1);
}
#[test]
fn the_sixth_level_reads() {
let lines = include_str!("wire/level-6.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"proof" => {
again::<Proof>(name, json);
let proof: Proof = read(name, json);
assert_eq!(
proof.check(deduxit::linear::Mode::CLASSICAL),
Ok(()),
"{name}"
);
assert_eq!(proof.sequent().cuts().len(), 1, "{name}");
}
"session" => {
again::<Interactive>(name, json);
let session: Interactive = read(name, json);
assert_eq!(session.goals().count(), 1, "{name}");
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 2);
}
#[test]
fn the_seventh_level_reads() {
let lines = include_str!("wire/level-7.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"sequent" => {
again::<Sequent>(name, json);
let sequent: Sequent = read(name, json);
assert!(sequent.is_first_order(), "{name}");
}
"intuitionistic" => {
again::<IllSequent>(name, json);
let sequent: IllSequent = read(name, json);
assert!(sequent.sequent().fragment().has_quantifiers(), "{name}");
}
other => panic!("{other}: no form of level 7"),
}
}
}
#[test]
fn the_eighth_level_reads() {
use deduxit::ordinary;
let lines = include_str!("wire/level-8.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"sequent" => again::<ordinary::Sequent>(name, json),
"derivation" => {
again::<ordinary::Derivation>(name, json);
let derivation: ordinary::Derivation = read(name, json);
assert_eq!(derivation.check(&Limits::default(), |_| false), Ok(()));
}
"session" => {
again::<ordinary::Interactive>(name, json);
let session: ordinary::Interactive = read(name, json);
assert_eq!(session.goals().count(), 1, "{name}");
}
other => panic!("{other}: no form of level 8"),
}
}
assert_eq!(lines.lines().count(), 3);
}
#[test]
fn the_eleventh_level_reads() {
let lines = include_str!("wire/level-11.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"disproof" => {
again::<Disproof>(name, json);
let disproof: Disproof = read(name, json);
assert_eq!(disproof.check(), Ok(()), "{name}");
}
"outcome" => {
let disproof: Disproof = read(name, json);
assert_eq!(disproof.check(), Ok(()), "{name}");
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 2);
}
#[test]
fn the_twelfth_level_reads() {
use deduxit::ordinary;
let lines = include_str!("wire/level-12.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
again::<ordinary::Outcome>(name, json);
let outcome: ordinary::Outcome = read(name, json);
if let ordinary::Verdict::Valid(evidence) = &outcome.verdict {
let derivation = evidence.derivation().unwrap();
assert_eq!(derivation.check(&Limits::default(), |_| false), Ok(()));
}
}
assert_eq!(lines.lines().count(), 3);
}
#[test]
fn the_thirteenth_level_reads() {
use deduxit::{kripke, ordinary};
let lines = include_str!("wire/level-13.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"model" => again::<kripke::Model>(name, json),
"outcome" => {
again::<ordinary::Outcome>(name, json);
let outcome: ordinary::Outcome = read(name, json);
let countermodel = outcome.countermodel.expect("a countermodel");
assert_eq!(
countermodel.check(outcome.logic, &deduxit::Limits::default(), |_| false),
Ok(())
);
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 2);
}
#[test]
fn the_fourteenth_level_reads() {
use deduxit::ordinary;
let limits = deduxit::Limits::default();
let lines = include_str!("wire/level-14.txt");
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"table" => {
again::<ordinary::TruthTable>(name, json);
let table: ordinary::TruthTable = read(name, json);
assert_eq!(table.falsifying(), [2], "{name}");
}
"tableau" => {
again::<ordinary::Tableau>(name, json);
let tableau: ordinary::Tableau = read(name, json);
assert!(tableau.is_closed(), "{name}");
}
"outcome" => {
again::<ordinary::Outcome>(name, json);
let outcome: ordinary::Outcome = read(name, json);
match (&outcome.verdict, &outcome.countermodel) {
(ordinary::Verdict::Valid(evidence), None) => {
let derivation = evidence.derivation().unwrap();
assert_eq!(derivation.check(&limits, |_| false), Ok(()), "{name}");
}
(ordinary::Verdict::NotValid, Some(countermodel)) => assert_eq!(
countermodel.check(outcome.logic, &limits, |_| false),
Ok(()),
"{name}"
),
other => panic!("{name}: {other:?}"),
}
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 4);
let valid = lines
.lines()
.find(|l| l.starts_with("outcome-tableau"))
.unwrap();
let forged = valid
.split_once(": ")
.unwrap()
.1
.replace("\"valid\"", "\"not_valid\"");
assert!(serde_json::from_str::<ordinary::Outcome>(&forged).is_err());
}
#[test]
fn outcomes_read_back() {
let mut levels = std::collections::BTreeSet::new();
for line in include_str!("lock/json.txt").lines() {
let (name, json) = line.split_once(": ").unwrap();
if name.starts_with("outcome-") {
again::<Outcome>(name, json);
let level: serde_json::Value = serde_json::from_str(json).unwrap();
levels.insert(level["version"].as_u64().unwrap());
}
}
assert_eq!(levels.into_iter().collect::<Vec<_>>(), [1, 2]);
for line in include_str!("wire/level-1.txt").lines() {
let (name, json) = line.split_once(": ").unwrap();
if name.starts_with("outcome-") {
let _: Outcome = read(name, json);
}
}
let (name, json) = include_str!("lock/json.txt")
.lines()
.find_map(|line| {
line.split_once(": ")
.filter(|(name, _)| *name == "outcome-mll")
})
.unwrap();
let later = json.replace(r#""engine":"focus""#, r#""engine":"sat""#);
assert_ne!(later, json);
let outcome: Outcome = read(name, &later);
assert_eq!(outcome.engine, Engine::Unknown);
}
#[test]
fn the_fifteenth_level_reads() {
use deduxit::rules::{Calculus, Derivation, LK};
let lines = include_str!("wire/level-15.txt");
let mut calculi: Vec<Calculus> = vec![read("lk", LK)];
for line in lines.lines() {
let (name, json) = line.split_once(": ").unwrap();
match name.split('-').next().unwrap() {
"calculus" => {
again::<Calculus>(name, json);
calculi.push(read(name, json));
}
"derivation" => {
again::<Derivation>(name, json);
let derivation: Derivation = read(name, json);
let calculus = calculi
.iter()
.find(|c| c.name() == derivation.calculus())
.unwrap_or_else(|| panic!("{name}: no calculus"));
assert_eq!(
derivation.check(calculus, &Limits::default(), |_| false),
Ok(()),
"{name}"
);
}
other => panic!("{name}: no form {other}"),
}
}
assert_eq!(lines.lines().count(), 3);
}