use orchid_api_derive::Coding; use orchid_base::{FmtUnit, OrcRes, sym}; use orchid_extension::gen_expr::GExpr; use orchid_extension::tree::{GenMember, cnst, comments, fun, prefix}; use orchid_extension::{Atomic, Expr, TAtom, ThinAtom, ThinVariant, ToExpr, TryFromExpr}; #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, Coding)] pub struct Bool(pub bool); impl Atomic for Bool { type Variant = ThinVariant; type Data = Self; } impl ThinAtom for Bool { async fn print(&self) -> FmtUnit { self.0.to_string().into() } } impl TryFromExpr for Bool { async fn try_from_expr(expr: Expr) -> OrcRes { match TAtom::::downcast(expr.handle()).await { Err(e) => Err(e.mk_err().await), Ok(atom) => Ok(atom.value), } } } impl ToExpr for Bool { async fn to_gen(self) -> GExpr { if self.0 { sym!(std::true) } else { sym!(std::false) }.to_gen().await } } pub fn gen_bool_lib() -> Vec { prefix("std", [ comments( [ "Returns the second argument if the bool is true, the third argument otherwise", "|type: Bool -> T -> T -> T|", ], fun(true, "ifthenelse", async |Bool(b): Bool, t: Expr, f: Expr| if b { t } else { f }), ), cnst(true, "true", Bool(true)), cnst(true, "false", Bool(false)), ]) }