use dive_ir::StdOp;
use crate::eval::Machine;
use crate::node::Kind;
use crate::plan::{Code, P};
use crate::{Error, Handle, TlcClass};
mod bags;
mod community;
mod graphs;
mod higher;
mod tlc;
#[must_use]
pub fn implements(op: StdOp) -> bool {
deferred(op)
|| bags::implements(op)
|| tlc::implements(op)
|| community::implements(op)
|| graphs::implements(op)
}
#[must_use]
pub fn effectful(op: StdOp) -> bool {
implements(op) && op.impure() && !deferred(op)
}
#[must_use]
pub(crate) fn deferred(op: StdOp) -> bool {
matches!(op, StdOp::TlcExtTrace | StdOp::TlcExtCounterExample)
|| (matches!(op.module(), "Json" | "IOUtils")
&& !(bags::implements(op)
|| tlc::implements(op)
|| community::implements(op)
|| graphs::implements(op)))
}
#[must_use]
pub(crate) fn deferred_refusal(op: StdOp) -> Error {
let what = if op.module() == "TLCExt" {
"trace introspection operators"
} else {
"Json and IOUtils operators"
};
refuse(
op,
&format!("evaluated; {what} are not implemented (M1 core)"),
)
}
#[must_use]
pub fn higher_order(op: StdOp) -> bool {
higher::implements(op)
}
#[must_use]
pub fn op_arity(op: StdOp, i: usize) -> Option<usize> {
higher::op_arity(op, i)
}
#[must_use]
pub fn takes_op_value(op: StdOp) -> bool {
higher::takes_op_value(op)
}
#[derive(Clone, Copy, Debug)]
pub struct OpFn<'a> {
code: &'a Code,
params: &'a [u32],
body: P,
}
impl<'a> OpFn<'a> {
#[must_use]
pub fn new(code: &'a Code, params: &'a [u32], body: P) -> Self {
Self { code, params, body }
}
pub fn apply(&self, m: &mut Machine, args: &[Handle]) -> Result<Handle, Error> {
if args.len() != self.params.len() {
return Err(Error::internal("an operator argument's arity"));
}
let saved: Vec<Handle> = self.params.iter().map(|&s| m.slot(s)).collect();
for (&s, &v) in self.params.iter().zip(args) {
m.set_slot(s, v);
}
let r = m.eval_norm(self.code, self.body);
for (&s, &v) in self.params.iter().zip(&saved) {
m.set_slot(s, v);
}
r
}
}
#[derive(Clone, Copy, Debug)]
pub enum Ho<'a> {
Value(Handle),
Op(OpFn<'a>),
}
pub fn call_ho(m: &mut Machine, op: StdOp, args: &[Ho<'_>]) -> Result<Handle, Error> {
higher::call(m, op, args)
}
pub fn call(m: &mut Machine, op: StdOp, args: &[Handle]) -> Result<Handle, Error> {
if deferred(op) {
return Err(deferred_refusal(op));
}
if bags::implements(op) {
return bags::call(m, op, args);
}
if tlc::implements(op) {
return tlc::call(m, op, args);
}
if community::implements(op) {
return community::call(m, op, args);
}
if graphs::implements(op) {
return graphs::call(m, op, args);
}
Err(Error::internal(format!(
"{} is not a core library operator",
op.qualified_name()
)))
}
fn enumerable(m: &mut Machine, op: StdOp, s: Handle) -> Result<Option<Vec<Handle>>, Error> {
let set = s.is_compound()
&& !matches!(
m.heap.view(s).kind,
Kind::IntFcn
| Kind::Fcn
| Kind::Lambda
| Kind::BigInt
| Kind::Nat
| Kind::Int
| Kind::SeqSet
);
if !set || !m.heap.is_finite(s) {
return Ok(None);
}
if m.heap.is_lazy(s) {
return Err(refuse(op, "a set the core keeps lazy"));
}
m.work_elements(s).map(Some)
}
fn arg(args: &[Handle], i: usize) -> Result<Handle, Error> {
args.get(i)
.copied()
.ok_or_else(|| Error::internal("missing argument"))
}
fn exc(code: u32, what: impl Into<String>) -> Error {
Error::eval_class(TlcClass::eval_exception(code), what)
}
fn wrong_value(op: &str, what: &str) -> Error {
exc(
2176,
format!("Applying {op} to the following value,\nwhich is not {what}:"),
)
}
fn arg_error(which: &str, op: &str, what: &str) -> Error {
exc(
2169,
format!("The {which} argument of {op} should be a {what}, but instead it is:"),
)
}
fn refuse(op: StdOp, why: &str) -> Error {
Error::unsupported(format!("{} ({why})", op.qualified_name()))
}