use oxilean_kernel::{BinderInfo, Declaration, Environment, Expr, Level, Name, ReducibilityHint};
pub struct EnvBuilder {
env: Environment,
errors: Vec<String>,
}
impl EnvBuilder {
pub fn new(env: Environment) -> Self {
Self {
env,
errors: Vec::new(),
}
}
pub fn fresh() -> Self {
Self::new(Environment::new())
}
pub fn add_axiom(&mut self, name: Name, ty: Expr) {
let result = self.env.add(Declaration::Axiom {
name,
univ_params: vec![],
ty,
});
if let Err(e) = result {
self.errors.push(e.to_string());
}
}
pub fn axiom(&mut self, name: &str, ty: Expr) -> &mut Self {
self.add_axiom(Name::str(name), ty);
self
}
pub fn add_definition(&mut self, name: Name, ty: Expr, value: Expr) {
let result = self.env.add(Declaration::Definition {
name,
univ_params: vec![],
ty,
val: value,
hint: ReducibilityHint::Regular(0),
});
if let Err(e) = result {
self.errors.push(e.to_string());
}
}
pub fn def(&mut self, name: &str, ty: Expr, value: Expr) -> &mut Self {
self.add_definition(Name::str(name), ty, value);
self
}
pub fn theorem(&mut self, name: &str, ty: Expr, proof: Expr) -> &mut Self {
let result = self.env.add(Declaration::Theorem {
name: Name::str(name),
univ_params: vec![],
ty,
val: proof,
});
if let Err(e) = result {
self.errors.push(e.to_string());
}
self
}
pub fn sorry(&mut self, name: &str, ty: Expr) -> &mut Self {
self.axiom(name, ty)
}
pub fn finish(self) -> Result<Environment, String> {
if self.errors.is_empty() {
Ok(self.env)
} else {
Err(self.errors.join("; "))
}
}
pub fn finish_or_panic(self) -> Environment {
match self.finish() {
Ok(env) => env,
Err(e) => panic!("EnvBuilder errors: {}", e),
}
}
pub fn env(&self) -> &Environment {
&self.env
}
pub fn is_ok(&self) -> bool {
self.errors.is_empty()
}
pub fn errors(&self) -> &[String] {
&self.errors
}
pub fn contains(&self, name: &str) -> bool {
self.env.get(&Name::str(name)).is_some()
}
}