use crate::Error;
use crate::limits::Space;
use std::fmt::{self, Display, Formatter};
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Model {
pub worlds: Vec<World>,
pub world: u32,
}
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct World {
pub atoms: Vec<String>,
pub falsity: bool,
pub arrows: Vec<u32>,
}
impl Model {
pub const fn new(worlds: Vec<World>, world: u32) -> Self {
Self { worlds, world }
}
pub fn check_world(&self, world: u32) -> Result<(), Error> {
if (world as usize) < self.worlds.len() {
Ok(())
} else {
Err(Error::IndexOutOfBounds {
space: Space::World,
index: world as usize,
len: self.worlds.len(),
})
}
}
}
impl World {
pub const fn new(atoms: Vec<String>, falsity: bool, arrows: Vec<u32>) -> Self {
Self {
atoms,
falsity,
arrows,
}
}
}
impl Display for Model {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for (n, world) in self.worlds.iter().enumerate() {
if n > 0 {
writeln!(f)?;
}
write!(f, "world {n}")?;
if n == self.world as usize {
write!(f, " (the model's world)")?;
}
let mut forced: Vec<&str> = world.atoms.iter().map(String::as_str).collect();
if world.falsity {
forced.push("⊥");
}
if forced.is_empty() {
write!(f, ": forces nothing")?;
} else {
write!(f, ": forces {}", forced.join(", "))?;
}
if !world.arrows.is_empty() {
let above: Vec<String> = world.arrows.iter().map(u32::to_string).collect();
write!(f, "; below {}", above.join(", "))?;
}
}
Ok(())
}
}