use serde::{Deserialize, Serialize};
pub const FRONTIER_BOUNDS_SCHEMA: &str = "vela.frontier-bounds.v1";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum BoundDirection {
Lower,
Upper,
#[default]
Unknown,
}
impl BoundDirection {
pub fn as_str(self) -> &'static str {
match self {
BoundDirection::Lower => "lower",
BoundDirection::Upper => "upper",
BoundDirection::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FrontierBound {
pub problem: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<f64>,
pub direction: BoundDirection,
pub source_text: String,
pub accepted: bool,
}
impl FrontierBound {
pub fn from_prose(problem: impl Into<String>, source_text: impl Into<String>) -> Self {
let source_text = source_text.into();
let (value, direction) = parse_bound_prose(&source_text);
FrontierBound {
problem: problem.into(),
value,
direction,
source_text,
accepted: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FrontierBoundsDoc {
pub schema: String,
pub source: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub note: String,
pub bounds: Vec<FrontierBound>,
}
impl FrontierBoundsDoc {
pub fn new(
source: impl Into<String>,
note: impl Into<String>,
mut bounds: Vec<FrontierBound>,
) -> Self {
bounds.sort_by(|a, b| a.problem.cmp(&b.problem));
FrontierBoundsDoc {
schema: FRONTIER_BOUNDS_SCHEMA.to_string(),
source: source.into(),
note: note.into(),
bounds,
}
}
}
pub fn parse_bound_prose(text: &str) -> (Option<f64>, BoundDirection) {
let norm = text
.replace("\\geqslant", ">=")
.replace("\\leqslant", "<=")
.replace("\\geq", ">=")
.replace("\\leq", "<=")
.replace("\\ge", ">=")
.replace("\\le", "<=")
.replace("\\gg", ">=")
.replace("\\ll", "<=");
let bytes = norm.as_bytes();
let mut i = 0;
while i < bytes.len() {
let (dir, skip) = match bytes[i] {
b'>' => {
let s = if bytes.get(i + 1) == Some(&b'=') {
2
} else {
1
};
(BoundDirection::Lower, s)
}
b'<' => {
let s = if bytes.get(i + 1) == Some(&b'=') {
2
} else {
1
};
(BoundDirection::Upper, s)
}
_ => {
i += 1;
continue;
}
};
let mut j = i + skip;
while j < bytes.len() && !bytes[j].is_ascii_digit() {
if bytes[j].is_ascii_alphabetic() {
break;
}
j += 1;
}
if j < bytes.len() && bytes[j].is_ascii_digit() {
let start = j;
while j < bytes.len() && (bytes[j].is_ascii_digit() || bytes[j] == b'.') {
j += 1;
}
if let Ok(v) = norm[start..j].parse::<f64>() {
return (Some(v), dir);
}
}
return (None, dir);
}
(None, BoundDirection::Unknown)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_lower_bound_with_geq() {
let (v, d) = parse_bound_prose("best known >= 7");
assert_eq!(v, Some(7.0));
assert_eq!(d, BoundDirection::Lower);
}
#[test]
fn parses_latex_geq() {
let (v, d) = parse_bound_prose("the best known bound is $\\geq 24$");
assert_eq!(v, Some(24.0));
assert_eq!(d, BoundDirection::Lower);
}
#[test]
fn parses_upper_bound() {
let (v, d) = parse_bound_prose("establishing an upper bound of <= 12");
assert_eq!(v, Some(12.0));
assert_eq!(d, BoundDirection::Upper);
}
#[test]
fn prose_without_comparator_is_unknown() {
let (v, d) = parse_bound_prose("Sawhney has provided the following proof.");
assert_eq!(v, None);
assert_eq!(d, BoundDirection::Unknown);
}
#[test]
fn comparator_over_symbol_keeps_direction_no_value() {
let (v, d) = parse_bound_prose("constructed such that |A| >= N");
assert_eq!(v, None);
assert_eq!(d, BoundDirection::Lower);
}
#[test]
fn from_prose_is_unattested_and_keeps_source_text() {
let b = FrontierBound::from_prose("erdos/12", "best known >= 7");
assert!(!b.accepted);
assert_eq!(b.problem, "erdos/12");
assert_eq!(b.source_text, "best known >= 7");
assert_eq!(b.value, Some(7.0));
assert_eq!(b.direction, BoundDirection::Lower);
}
#[test]
fn doc_round_trips_and_sorts_by_problem() {
let doc = FrontierBoundsDoc::new(
"erdos-problems",
"test",
vec![
FrontierBound::from_prose("erdos/20", "upper bound <= 5"),
FrontierBound::from_prose("erdos/3", "best known >= 9"),
],
);
assert_eq!(doc.bounds[0].problem, "erdos/20");
assert_eq!(doc.bounds[1].problem, "erdos/3");
let s = serde_json::to_string(&doc).unwrap();
let back: FrontierBoundsDoc = serde_json::from_str(&s).unwrap();
assert_eq!(doc, back);
assert_eq!(back.schema, FRONTIER_BOUNDS_SCHEMA);
}
}