1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Standard functions for the interpreter, and the tools to define new native functions.
//!
//! # Defining native functions
//!
//! There are several ways to define new native functions:
//!
//! - Implement [`NativeFn`] manually. This is the most versatile approach, but it can be overly
//!   verbose.
//! - Use [`FnWrapper`] or the [`wrap`] function. This allows specifying arguments / output
//!   with custom types (such as `bool` or a [`Number`]).
//!
//! [`Number`]: crate::Number

use core::{cmp::Ordering, fmt};

use once_cell::unsync::OnceCell;

#[cfg(feature = "std")]
pub use self::std::Dbg;
pub use self::{
    array::{All, Any, Array, Filter, Fold, Len, Map, Merge, Push},
    assertions::{Assert, AssertClose, AssertEq, AssertFails},
    flow::{If, While},
    wrapper::{
        wrap, Binary, ErrorOutput, FnWrapper, FromValueError, FromValueErrorKind,
        FromValueErrorLocation, IntoEvalResult, Quaternary, Ternary, TryFromValue, Unary,
    },
};
use crate::{
    alloc::{vec, Vec},
    error::AuxErrorInfo,
    CallContext, Error, ErrorKind, EvalResult, Function, NativeFn, OpaqueRef, SpannedValue, Value,
};

mod array;
mod assertions;
mod flow;
#[cfg(feature = "std")]
mod std;
mod wrapper;

fn extract_primitive<T, A>(
    ctx: &CallContext<'_, A>,
    value: SpannedValue<T>,
    error_msg: &str,
) -> Result<T, Error> {
    match value.extra {
        Value::Prim(value) => Ok(value),
        _ => Err(ctx
            .call_site_error(ErrorKind::native(error_msg))
            .with_location(&value, AuxErrorInfo::InvalidArg)),
    }
}

fn extract_array<T, A>(
    ctx: &CallContext<'_, A>,
    value: SpannedValue<T>,
    error_msg: &str,
) -> Result<Vec<Value<T>>, Error> {
    if let Value::Tuple(array) = value.extra {
        Ok(array.into())
    } else {
        let err = ErrorKind::native(error_msg);
        Err(ctx
            .call_site_error(err)
            .with_location(&value, AuxErrorInfo::InvalidArg))
    }
}

fn extract_fn<T, A>(
    ctx: &CallContext<'_, A>,
    value: SpannedValue<T>,
    error_msg: &str,
) -> Result<Function<T>, Error> {
    if let Value::Function(function) = value.extra {
        Ok(function)
    } else {
        let err = ErrorKind::native(error_msg);
        Err(ctx
            .call_site_error(err)
            .with_location(&value, AuxErrorInfo::InvalidArg))
    }
}

/// Comparator functions on two primitive arguments. All functions use [`Arithmetic`] to determine
/// ordering between the args.
///
/// # Type
///
/// ```text
/// fn(Num, Num) -> Ordering // for `Compare::Raw`
/// fn(Num, Num) -> Num // for `Compare::Min` and `Compare::Max`
/// ```
///
/// [`Arithmetic`]: crate::arith::Arithmetic
///
/// # Examples
///
/// Using `min` function:
///
/// ```
/// # use arithmetic_parser::grammars::{F32Grammar, Parse, Untyped};
/// # use arithmetic_eval::{fns, Environment, ExecutableModule, Value};
/// # fn main() -> anyhow::Result<()> {
/// let program = "
///     // Finds a minimum number in an array.
///     extended_min = |...xs| fold(xs, INFINITY, min);
///     extended_min(2, -3, 7, 1, 3) == -3
/// ";
/// let program = Untyped::<F32Grammar>::parse_statements(program)?;
/// let module = ExecutableModule::new("test_min", &program)?;
///
/// let mut env = Environment::new();
/// env.insert("INFINITY", Value::Prim(f32::INFINITY))
///     .insert_native_fn("fold", fns::Fold)
///     .insert_native_fn("min", fns::Compare::Min);
/// assert_eq!(module.with_env(&env)?.run()?, Value::Bool(true));
/// # Ok(())
/// # }
/// ```
///
/// Using `cmp` function with [`Comparisons`](crate::env::Comparisons).
///
/// ```
/// # use arithmetic_parser::grammars::{F32Grammar, Parse, Untyped};
/// # use arithmetic_eval::{fns, env::Comparisons, Environment, ExecutableModule, Value};
/// # fn main() -> anyhow::Result<()> {
/// let program = "
///     map((1, -7, 0, 2), |x| cmp(x, 0)) == (GREATER, LESS, EQUAL, GREATER)
/// ";
/// let program = Untyped::<F32Grammar>::parse_statements(program)?;
/// let module = ExecutableModule::new("test_cmp", &program)?;
///
/// let mut env = Environment::new();
/// env.extend(Comparisons::iter());
/// env.insert_native_fn("map", fns::Map);
/// assert_eq!(module.with_env(&env)?.run()?, Value::Bool(true));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum Compare {
    /// Returns an [`Ordering`] wrapped into an [`OpaqueRef`](crate::OpaqueRef),
    /// or [`Value::void()`] if the provided values are not comparable.
    Raw,
    /// Returns the minimum of the two values. If the values are equal / not comparable, returns the first one.
    Min,
    /// Returns the maximum of the two values. If the values are equal / not comparable, returns the first one.
    Max,
}

impl Compare {
    fn extract_primitives<T>(
        mut args: Vec<SpannedValue<T>>,
        ctx: &mut CallContext<'_, T>,
    ) -> Result<(T, T), Error> {
        ctx.check_args_count(&args, 2)?;
        let y = args.pop().unwrap();
        let x = args.pop().unwrap();
        let x = extract_primitive(ctx, x, COMPARE_ERROR_MSG)?;
        let y = extract_primitive(ctx, y, COMPARE_ERROR_MSG)?;
        Ok((x, y))
    }
}

const COMPARE_ERROR_MSG: &str = "Compare requires 2 primitive arguments";

impl<T> NativeFn<T> for Compare {
    fn evaluate(&self, args: Vec<SpannedValue<T>>, ctx: &mut CallContext<'_, T>) -> EvalResult<T> {
        let (x, y) = Self::extract_primitives(args, ctx)?;
        let maybe_ordering = ctx.arithmetic().partial_cmp(&x, &y);

        if let Self::Raw = self {
            Ok(maybe_ordering.map_or_else(Value::void, Value::opaque_ref))
        } else {
            let ordering =
                maybe_ordering.ok_or_else(|| ctx.call_site_error(ErrorKind::CannotCompare))?;
            let value = match (ordering, self) {
                (Ordering::Equal, _)
                | (Ordering::Less, Self::Min)
                | (Ordering::Greater, Self::Max) => x,
                _ => y,
            };
            Ok(Value::Prim(value))
        }
    }
}

/// Allows to define a value recursively, by referencing a value being created.
///
/// It works like this:
///
/// - Provide a function as the only argument. The (only) argument of this function is the value
///   being created.
/// - Do not use the uninitialized value synchronously; only use it in inner function definitions.
/// - Return the created value from a function.
///
/// # Examples
///
/// ```
/// # use arithmetic_parser::grammars::{F32Grammar, Parse, Untyped};
/// # use arithmetic_eval::{fns, env::Comparisons, Environment, ExecutableModule, Value};
/// # fn main() -> anyhow::Result<()> {
/// let program = "
///     recursive_fib = defer(|fib| {
///         |n| if(n >= 0 && n <= 1, || n, || fib(n - 1) + fib(n - 2))()
///     });
///     recursive_fib(10)
/// ";
/// let program = Untyped::<F32Grammar>::parse_statements(program)?;
/// let module = ExecutableModule::new("test_defer", &program)?;
///
/// let mut env = Environment::new();
/// env.extend(Comparisons::iter());
/// env.insert_native_fn("if", fns::If).insert_native_fn("defer", fns::Defer);
/// assert_eq!(module.with_env(&env)?.run()?, Value::Prim(55.0));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Defer;

impl<T: Clone + 'static> NativeFn<T> for Defer {
    fn evaluate(
        &self,
        mut args: Vec<SpannedValue<T>>,
        ctx: &mut CallContext<'_, T>,
    ) -> EvalResult<T> {
        const ARG_ERROR: &str = "Argument must be a function";

        ctx.check_args_count(&args, 1)?;
        let function = extract_fn(ctx, args.pop().unwrap(), ARG_ERROR)?;
        let cell = OpaqueRef::with_identity_eq(ValueCell::<T>::default());
        let spanned_cell = ctx.apply_call_location(Value::Ref(cell.clone()));
        let return_value = function.evaluate(vec![spanned_cell], ctx)?;

        let cell = cell.downcast_ref::<ValueCell<T>>().unwrap();
        // ^ `unwrap()` is safe by construction
        cell.set(return_value.clone());
        Ok(return_value)
    }
}

#[derive(Debug)]
pub(crate) struct ValueCell<T> {
    inner: OnceCell<Value<T>>,
}

impl<T> Default for ValueCell<T> {
    fn default() -> Self {
        Self {
            inner: OnceCell::new(),
        }
    }
}

impl<T: 'static + fmt::Debug> From<ValueCell<T>> for Value<T> {
    fn from(cell: ValueCell<T>) -> Self {
        Self::Ref(OpaqueRef::with_identity_eq(cell))
    }
}

impl<T> ValueCell<T> {
    /// Gets the internally stored value, or `None` if the cell was not initialized yet.
    pub fn get(&self) -> Option<&Value<T>> {
        self.inner.get()
    }

    fn set(&self, value: Value<T>) {
        self.inner
            .set(value)
            .map_err(drop)
            .expect("Repeated `ValueCell` assignment");
    }
}

#[cfg(test)]
mod tests {
    use arithmetic_parser::grammars::{F32Grammar, Parse, Untyped};
    use assert_matches::assert_matches;

    use super::*;
    use crate::{
        env::Environment,
        exec::{ExecutableModule, WildcardId},
    };

    #[test]
    fn if_basics() -> anyhow::Result<()> {
        let block = "
            x = 1.0;
            if(x < 2, x + 5, 3 - x)
        ";
        let block = Untyped::<F32Grammar>::parse_statements(block)?;
        let module = ExecutableModule::new(WildcardId, &block)?;
        let mut env = Environment::new();
        env.insert_native_fn("if", If);
        assert_eq!(module.with_env(&env)?.run()?, Value::Prim(6.0));
        Ok(())
    }

    #[test]
    fn if_with_closures() -> anyhow::Result<()> {
        let block = "
            x = 4.5;
            if(x < 2, || x + 5, || 3 - x)()
        ";
        let block = Untyped::<F32Grammar>::parse_statements(block)?;
        let module = ExecutableModule::new(WildcardId, &block)?;
        let mut env = Environment::new();
        env.insert_native_fn("if", If);
        assert_eq!(module.with_env(&env)?.run()?, Value::Prim(-1.5));
        Ok(())
    }

    #[test]
    fn cmp_sugar() -> anyhow::Result<()> {
        let program = "x = 1.0; x > 0 && x <= 3";
        let block = Untyped::<F32Grammar>::parse_statements(program)?;
        let module = ExecutableModule::new(WildcardId, &block)?;
        assert_eq!(
            module.with_env(&Environment::new())?.run()?,
            Value::Bool(true)
        );

        let bogus_program = "x = 1.0; x > (1, 2)";
        let bogus_block = Untyped::<F32Grammar>::parse_statements(bogus_program)?;
        let bogus_module = ExecutableModule::new(WildcardId, &bogus_block)?;

        let err = bogus_module
            .with_env(&Environment::new())?
            .run()
            .unwrap_err();
        let err = err.source();
        assert_matches!(err.kind(), ErrorKind::CannotCompare);
        assert_eq!(err.location().in_module().span(bogus_program), "(1, 2)");
        Ok(())
    }

    #[test]
    fn while_basic() -> anyhow::Result<()> {
        let program = "
            // Finds the greatest power of 2 lesser or equal to the value.
            discrete_log2 = |x| {
                while(0, |i| 2^i <= x, |i| i + 1) - 1
            };

            (discrete_log2(1), discrete_log2(2),
                discrete_log2(4), discrete_log2(6.5), discrete_log2(1000))
        ";
        let block = Untyped::<F32Grammar>::parse_statements(program)?;

        let module = ExecutableModule::new(WildcardId, &block)?;
        let mut env = Environment::new();
        env.insert_native_fn("while", While)
            .insert_native_fn("if", If);

        assert_eq!(
            module.with_env(&env)?.run()?,
            Value::from(vec![
                Value::Prim(0.0),
                Value::Prim(1.0),
                Value::Prim(2.0),
                Value::Prim(2.0),
                Value::Prim(9.0),
            ])
        );
        Ok(())
    }

    #[test]
    fn max_value_with_fold() -> anyhow::Result<()> {
        let program = "
            max_value = |...xs| {
                fold(xs, -Inf, |acc, x| if(x > acc, x, acc))
            };
            max_value(1, -2, 7, 2, 5) == 7 && max_value(3, -5, 9) == 9
        ";
        let block = Untyped::<F32Grammar>::parse_statements(program)?;

        let module = ExecutableModule::new(WildcardId, &block)?;
        let mut env = Environment::new();
        env.insert("Inf", Value::Prim(f32::INFINITY))
            .insert_native_fn("fold", Fold)
            .insert_native_fn("if", If);

        assert_eq!(module.with_env(&env)?.run()?, Value::Bool(true));
        Ok(())
    }

    #[test]
    fn reverse_list_with_fold() -> anyhow::Result<()> {
        const SAMPLES: &[(&[f32], &[f32])] = &[
            (&[1.0, 2.0, 3.0], &[3.0, 2.0, 1.0]),
            (&[], &[]),
            (&[1.0], &[1.0]),
        ];

        let program = "
            reverse = |xs| {
                fold(xs, (), |acc, x| merge((x,), acc))
            };
            xs = (-4, 3, 0, 1);
            reverse(xs) == (1, 0, 3, -4)
        ";
        let block = Untyped::<F32Grammar>::parse_statements(program)?;
        let module = ExecutableModule::new(WildcardId, &block)?;

        let mut env = Environment::new();
        env.insert_native_fn("merge", Merge)
            .insert_native_fn("fold", Fold);

        assert_eq!(module.with_mutable_env(&mut env)?.run()?, Value::Bool(true));

        let test_block = Untyped::<F32Grammar>::parse_statements("reverse(xs)")?;
        let test_module = ExecutableModule::new("test", &test_block)?;

        for &(input, expected) in SAMPLES {
            let input = input.iter().copied().map(Value::Prim).collect();
            let expected = expected.iter().copied().map(Value::Prim).collect();
            env.insert("xs", Value::Tuple(input));
            assert_eq!(test_module.with_env(&env)?.run()?, Value::Tuple(expected));
        }
        Ok(())
    }

    #[test]
    fn error_with_min_function_args() -> anyhow::Result<()> {
        let program = "5 - min(1, (2, 3))";
        let block = Untyped::<F32Grammar>::parse_statements(program)?;
        let module = ExecutableModule::new(WildcardId, &block)?;
        let mut env = Environment::new();
        env.insert_native_fn("min", Compare::Min);

        let err = module.with_env(&env)?.run().unwrap_err();
        let err = err.source();
        assert_eq!(err.location().in_module().span(program), "min(1, (2, 3))");
        assert_matches!(
            err.kind(),
            ErrorKind::NativeCall(ref msg) if msg.contains("requires 2 primitive arguments")
        );
        Ok(())
    }

    #[test]
    fn error_with_min_function_incomparable_args() -> anyhow::Result<()> {
        let program = "5 - min(1, NAN)";
        let block = Untyped::<F32Grammar>::parse_statements(program)?;
        let module = ExecutableModule::new(WildcardId, &block)?;
        let mut env = Environment::new();
        env.insert("NAN", Value::Prim(f32::NAN))
            .insert_native_fn("min", Compare::Min);

        let err = module.with_env(&env)?.run().unwrap_err();
        let err = err.source();
        assert_eq!(err.location().in_module().span(program), "min(1, NAN)");
        assert_matches!(err.kind(), ErrorKind::CannotCompare);
        Ok(())
    }
}