1use dive_ir::StdOp;
15
16use crate::eval::Machine;
17use crate::node::Kind;
18use crate::plan::{Code, P};
19use crate::{Error, Handle, TlcClass};
20
21mod bags;
22mod community;
23mod graphs;
24mod higher;
25mod tlc;
26
27#[must_use]
29pub fn implements(op: StdOp) -> bool {
30 deferred(op)
31 || bags::implements(op)
32 || tlc::implements(op)
33 || community::implements(op)
34 || graphs::implements(op)
35}
36
37#[must_use]
41pub fn effectful(op: StdOp) -> bool {
42 implements(op) && op.impure() && !deferred(op)
43}
44
45#[must_use]
54pub(crate) fn deferred(op: StdOp) -> bool {
55 matches!(op, StdOp::TlcExtTrace | StdOp::TlcExtCounterExample)
56 || (matches!(op.module(), "Json" | "IOUtils")
57 && !(bags::implements(op)
58 || tlc::implements(op)
59 || community::implements(op)
60 || graphs::implements(op)))
61}
62
63#[must_use]
65pub(crate) fn deferred_refusal(op: StdOp) -> Error {
66 let what = if op.module() == "TLCExt" {
67 "trace introspection operators"
68 } else {
69 "Json and IOUtils operators"
70 };
71 refuse(
72 op,
73 &format!("evaluated; {what} are not implemented (M1 core)"),
74 )
75}
76
77#[must_use]
80pub fn higher_order(op: StdOp) -> bool {
81 higher::implements(op)
82}
83
84#[must_use]
87pub fn op_arity(op: StdOp, i: usize) -> Option<usize> {
88 higher::op_arity(op, i)
89}
90
91#[must_use]
95pub fn takes_op_value(op: StdOp) -> bool {
96 higher::takes_op_value(op)
97}
98
99#[derive(Clone, Copy, Debug)]
101pub struct OpFn<'a> {
102 code: &'a Code,
103 params: &'a [u32],
104 body: P,
105}
106
107impl<'a> OpFn<'a> {
108 #[must_use]
110 pub fn new(code: &'a Code, params: &'a [u32], body: P) -> Self {
111 Self { code, params, body }
112 }
113
114 pub fn apply(&self, m: &mut Machine, args: &[Handle]) -> Result<Handle, Error> {
121 if args.len() != self.params.len() {
122 return Err(Error::internal("an operator argument's arity"));
123 }
124 let saved: Vec<Handle> = self.params.iter().map(|&s| m.slot(s)).collect();
125 for (&s, &v) in self.params.iter().zip(args) {
126 m.set_slot(s, v);
127 }
128 let r = m.eval_norm(self.code, self.body);
129 for (&s, &v) in self.params.iter().zip(&saved) {
130 m.set_slot(s, v);
131 }
132 r
133 }
134}
135
136#[derive(Clone, Copy, Debug)]
138pub enum Ho<'a> {
139 Value(Handle),
141 Op(OpFn<'a>),
143}
144
145pub fn call_ho(m: &mut Machine, op: StdOp, args: &[Ho<'_>]) -> Result<Handle, Error> {
150 higher::call(m, op, args)
151}
152
153pub fn call(m: &mut Machine, op: StdOp, args: &[Handle]) -> Result<Handle, Error> {
159 if deferred(op) {
160 return Err(deferred_refusal(op));
161 }
162 if bags::implements(op) {
163 return bags::call(m, op, args);
164 }
165 if tlc::implements(op) {
166 return tlc::call(m, op, args);
167 }
168 if community::implements(op) {
169 return community::call(m, op, args);
170 }
171 if graphs::implements(op) {
172 return graphs::call(m, op, args);
173 }
174 Err(Error::internal(format!(
175 "{} is not a core library operator",
176 op.qualified_name()
177 )))
178}
179
180fn enumerable(m: &mut Machine, op: StdOp, s: Handle) -> Result<Option<Vec<Handle>>, Error> {
186 let set = s.is_compound()
187 && !matches!(
188 m.heap.view(s).kind,
189 Kind::IntFcn
190 | Kind::Fcn
191 | Kind::Lambda
192 | Kind::BigInt
193 | Kind::Nat
194 | Kind::Int
195 | Kind::SeqSet
196 );
197 if !set || !m.heap.is_finite(s) {
198 return Ok(None);
199 }
200 if m.heap.is_lazy(s) {
201 return Err(refuse(op, "a set the core keeps lazy"));
202 }
203 m.work_elements(s).map(Some)
204}
205
206fn arg(args: &[Handle], i: usize) -> Result<Handle, Error> {
208 args.get(i)
209 .copied()
210 .ok_or_else(|| Error::internal("missing argument"))
211}
212
213fn exc(code: u32, what: impl Into<String>) -> Error {
215 Error::eval_class(TlcClass::eval_exception(code), what)
216}
217
218fn wrong_value(op: &str, what: &str) -> Error {
220 exc(
221 2176,
222 format!("Applying {op} to the following value,\nwhich is not {what}:"),
223 )
224}
225
226fn arg_error(which: &str, op: &str, what: &str) -> Error {
228 exc(
229 2169,
230 format!("The {which} argument of {op} should be a {what}, but instead it is:"),
231 )
232}
233
234fn refuse(op: StdOp, why: &str) -> Error {
236 Error::unsupported(format!("{} ({why})", op.qualified_name()))
237}