sim-lib-numbers-func 0.3.1

Callable numeric function values over symbolic or native bodies.
Documentation
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
//! The `Func` function value: variables plus a labelled CAS/native body, with
//! its metadata and arithmetic over function values.

use std::{any::Any, sync::Arc};

use sim_kernel::{
    Args, Callable, ClassRef, Cx, DefaultFactory, Error, Expr, Factory, NumberValue, Object,
    ObjectEncode, Result, ShapeRef, Symbol, Value,
};
use sim_lib_numbers_cas::{CasExpr, cas_expr_to_surface_expr, free_vars, value_to_cas_expr};
use sim_lib_numbers_cas_eval::eval_cas;
use sim_shape::{AnyShape, ListShape, Shape, shape_value};

use super::domain::{func_class_symbol, func_domain_symbol};
use super::function::{child_env_with_args, vars_expr};

mod ops;

pub(crate) use ops::register_value_ops;

/// A native (Rust-backed) function body: a closure invoked with the runtime
/// context and the evaluated argument values, used when a `Func` has no CAS body.
pub type NativeFn = Arc<dyn Fn(&mut Cx, &[Value]) -> Result<Value> + Send + Sync>;

/// Out-of-band annotations attached to a [`Func`] value.
#[derive(Clone, Default)]
pub struct FuncMetadata {
    /// Symbol identifying where this function came from (for example, an
    /// elementary-function name), when known.
    pub source: Option<Symbol>,
    /// Optional hint naming the differentiator that should handle `grad`/`diff`
    /// for this function.
    pub differentiator_hint: Option<Symbol>,
    /// Arbitrary caller-supplied value carried alongside the function.
    pub payload: Option<Value>,
}

#[derive(Clone)]
enum FuncBody {
    Symbolic(CasExpr),
    Native {
        native: NativeFn,
        symbolic_status: SymbolicStatus,
    },
    Dual {
        cas: CasExpr,
        native: NativeFn,
    },
}

/// Whether a [`Func`] exposes an exact symbolic body, is differentiable through
/// a named hint, or has lost its symbolic body for a named reason.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SymbolicStatus {
    /// A CAS body is available and can be inspected by symbolic tools.
    Available,
    /// The function is native-only, but metadata names a differentiator that
    /// can handle it numerically or exactly outside the CAS path.
    ProvidedByHint,
    /// The function is native-only because symbolic information was lost.
    Lost {
        /// The machine-readable reason for the loss.
        reason: Symbol,
    },
}

impl SymbolicStatus {
    /// Reason used for ordinary native functions with no symbolic body.
    pub fn native_only() -> Self {
        Self::Lost {
            reason: Symbol::qualified("numbers/func", "native-only"),
        }
    }

    /// Reason used when arithmetic combines a symbolic body with a native-only
    /// body and must keep only the executable native result.
    pub fn mixed_native() -> Self {
        Self::Lost {
            reason: Symbol::qualified("numbers/func", "mixed-native"),
        }
    }

    fn status_symbol(&self) -> Symbol {
        match self {
            Self::Available => Symbol::qualified("numbers/func", "available"),
            Self::ProvidedByHint => Symbol::qualified("numbers/func", "provided-by-hint"),
            Self::Lost { .. } => Symbol::qualified("numbers/func", "lost"),
        }
    }

    fn reason(&self) -> Option<&Symbol> {
        match self {
            Self::Lost { reason } => Some(reason),
            _ => None,
        }
    }
}

/// A callable function value in the `Func` number domain: its bound variables
/// plus one labelled symbolic, native, or internal dual body.
#[derive(Clone)]
pub struct Func {
    /// The ordered parameter symbols bound when the function is invoked.
    pub vars: Vec<Symbol>,
    body: FuncBody,
    /// Out-of-band metadata describing the function.
    pub metadata: FuncMetadata,
}

impl Func {
    fn new(vars: Vec<Symbol>, body: FuncBody, metadata: FuncMetadata) -> Self {
        Self {
            vars,
            body,
            metadata,
        }
    }

    /// Builds a function with a symbolic (CAS) body and default metadata.
    pub fn symbolic(vars: Vec<Symbol>, body_cas: CasExpr) -> Self {
        Self::symbolic_with(vars, body_cas, FuncMetadata::default())
    }

    /// Builds a function with a symbolic (CAS) body and caller-supplied metadata.
    pub fn symbolic_with(vars: Vec<Symbol>, body_cas: CasExpr, metadata: FuncMetadata) -> Self {
        Self::new(vars, FuncBody::Symbolic(body_cas), metadata)
    }

    /// Builds a function with a native (Rust closure) body and default metadata.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use sim_kernel::Symbol;
    /// use sim_lib_numbers_func::Func;
    ///
    /// let func = Func::native(
    ///     vec![Symbol::new("x")],
    ///     Arc::new(|_cx, args| Ok(args[0].clone())),
    /// );
    /// assert_eq!(func.vars, vec![Symbol::new("x")]);
    /// assert!(func.body_cas().is_none());
    /// assert!(func.is_native());
    /// ```
    pub fn native(vars: Vec<Symbol>, body_native: NativeFn) -> Self {
        Self::native_with(vars, body_native, FuncMetadata::default())
    }

    /// Builds a function with a native body and caller-supplied metadata.
    pub fn native_with(vars: Vec<Symbol>, body_native: NativeFn, metadata: FuncMetadata) -> Self {
        let status = if metadata.differentiator_hint.is_some() {
            SymbolicStatus::ProvidedByHint
        } else {
            SymbolicStatus::native_only()
        };
        Self::native_with_status(vars, body_native, metadata, status)
    }

    fn native_with_status(
        vars: Vec<Symbol>,
        body_native: NativeFn,
        metadata: FuncMetadata,
        symbolic_status: SymbolicStatus,
    ) -> Self {
        Self::new(
            vars,
            FuncBody::Native {
                native: body_native,
                symbolic_status,
            },
            metadata,
        )
    }

    /// Builds an internal dual-body function whose native body is derived from
    /// the same operation as the symbolic body.
    pub(crate) fn dual_with(
        vars: Vec<Symbol>,
        body_cas: CasExpr,
        body_native: NativeFn,
        metadata: FuncMetadata,
    ) -> Self {
        Self::new(
            vars,
            FuncBody::Dual {
                cas: body_cas,
                native: body_native,
            },
            metadata,
        )
    }

    /// Returns the symbolic body advertised by this function, when available.
    pub fn body_cas(&self) -> Option<&CasExpr> {
        match &self.body {
            FuncBody::Symbolic(body) | FuncBody::Dual { cas: body, .. } => Some(body),
            FuncBody::Native { .. } => None,
        }
    }

    fn body_native(&self) -> Option<&NativeFn> {
        match &self.body {
            FuncBody::Native { native: body, .. } | FuncBody::Dual { native: body, .. } => {
                Some(body)
            }
            FuncBody::Symbolic(_) => None,
        }
    }

    /// Returns whether this function carries a native body.
    pub fn is_native(&self) -> bool {
        self.body_native().is_some()
    }

    /// Returns the symbolic-body status for this function.
    pub fn symbolic_status(&self) -> SymbolicStatus {
        match &self.body {
            FuncBody::Symbolic(_) | FuncBody::Dual { .. } => SymbolicStatus::Available,
            FuncBody::Native {
                symbolic_status, ..
            } => symbolic_status.clone(),
        }
    }

    fn native_extension_payload(&self) -> Expr {
        let status = self.symbolic_status();
        let mut fields = vec![(
            Expr::Symbol(Symbol::new("symbolic-status")),
            Expr::Symbol(status.status_symbol()),
        )];
        if let Some(reason) = status.reason() {
            fields.push((
                Expr::Symbol(Symbol::new("symbolic-loss-reason")),
                Expr::Symbol(reason.clone()),
            ));
        }
        if let Some(hint) = &self.metadata.differentiator_hint {
            fields.push((
                Expr::Symbol(Symbol::new("differentiator-hint")),
                Expr::Symbol(hint.clone()),
            ));
        }
        Expr::Map(fields)
    }

    fn invoke(&self, cx: &mut Cx, args: &[Value]) -> Result<Value> {
        if args.len() != self.vars.len() {
            return Err(Error::Eval(format!(
                "function expected {} arguments but received {}",
                self.vars.len(),
                args.len()
            )));
        }
        if let Some(body_native) = self.body_native() {
            return body_native(cx, args);
        }
        let body_cas = self
            .body_cas()
            .expect("FuncBody always contains a symbolic or native body");
        let env = child_env_with_args(cx.env(), &self.vars, args)?;
        cx.with_env(env.clone(), |cx| eval_cas(cx, body_cas, &env))
    }
}

impl Object for Func {
    fn display(&self, cx: &mut Cx) -> Result<String> {
        if let Some(body_cas) = self.body_cas() {
            return Ok(format!(
                "#<func {:?} -> {:?}>",
                self.vars,
                cas_expr_to_surface_expr(cx, body_cas)?
            ));
        }
        Ok(format!("#<native-func {:?}>", self.vars))
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl sim_kernel::ObjectCompat for Func {
    fn class(&self, cx: &mut Cx) -> Result<ClassRef> {
        if let Some(value) = cx.registry().class_by_symbol(&func_class_symbol()) {
            return Ok(value.clone());
        }
        DefaultFactory.class_stub(
            sim_kernel::CORE_NUMBER_CLASS_ID,
            Symbol::qualified("core", "Number"),
        )
    }
    fn as_expr(&self, cx: &mut Cx) -> Result<Expr> {
        let Some(body_cas) = self.body_cas() else {
            return Ok(Expr::Extension {
                tag: func_class_symbol(),
                payload: Box::new(self.native_extension_payload()),
            });
        };
        Ok(Expr::Call {
            operator: Box::new(Expr::Symbol(Symbol::new("fn"))),
            args: vec![
                vars_expr(&self.vars),
                cas_expr_to_surface_expr(cx, body_cas)?,
            ],
        })
    }
    fn as_table(&self, cx: &mut Cx) -> Result<Value> {
        let vars = cx.factory().list(
            self.vars
                .iter()
                .cloned()
                .map(|var| cx.factory().symbol(var))
                .collect::<Result<Vec<_>>>()?,
        )?;
        let body_expr = self
            .body_cas()
            .map(|body| cas_expr_to_surface_expr(cx, body))
            .transpose()?;
        let body = match self.body_cas() {
            Some(_) => cx
                .factory()
                .expr(body_expr.expect("body expr should exist when body_cas is present"))?,
            None => cx.factory().nil()?,
        };
        let native = cx.factory().bool(self.is_native())?;
        let symbolic_status = self.symbolic_status();
        let mut fields = vec![
            (Symbol::new("kind"), cx.factory().string("func".to_owned())?),
            (Symbol::new("vars"), vars),
            (Symbol::new("body"), body),
            (Symbol::new("native"), native),
            (
                Symbol::new("symbolic-status"),
                cx.factory().symbol(symbolic_status.status_symbol())?,
            ),
        ];
        if let Some(reason) = symbolic_status.reason() {
            fields.push((
                Symbol::new("symbolic-loss-reason"),
                cx.factory().symbol(reason.clone())?,
            ));
        }
        cx.factory().table(fields)
    }
    fn as_callable(&self) -> Option<&dyn Callable> {
        Some(self)
    }
    fn as_number_value(&self) -> Option<&dyn NumberValue> {
        Some(self)
    }
    fn as_object_encoder(&self) -> Option<&dyn ObjectEncode> {
        Some(self)
    }
}

impl Callable for Func {
    fn call(&self, cx: &mut Cx, args: Args) -> Result<Value> {
        self.invoke(cx, args.values())
    }

    fn browse_args_shape(&self, _cx: &mut Cx) -> Result<Option<ShapeRef>> {
        let items = self
            .vars
            .iter()
            .map(|_| Arc::new(AnyShape) as Arc<dyn Shape>)
            .collect();
        Ok(Some(shape_value(
            Symbol::qualified(func_class_symbol().to_string(), "args"),
            Arc::new(ListShape::new(items)),
        )))
    }
}

impl NumberValue for Func {
    fn number_domain(&self, _cx: &mut Cx) -> Result<Symbol> {
        Ok(func_domain_symbol())
    }
}

impl sim_citizen::Citizen for Func {
    fn citizen_symbol() -> Symbol {
        func_class_symbol()
    }

    fn citizen_version() -> u32 {
        0
    }

    fn citizen_arity() -> usize {
        2
    }

    fn citizen_fields() -> &'static [&'static str] {
        &["vars", "body"]
    }
}

/// Wraps a [`Func`] into a runtime [`Value`] in the `Func` number domain.
pub fn build_func_value(cx: &mut Cx, func: Func) -> Result<Value> {
    cx.factory().opaque(Arc::new(func))
}

pub(crate) fn build_constant_func_value(cx: &mut Cx, value: Value) -> Result<Value> {
    let body = value_to_cas_expr(cx, value)?;
    let vars = free_vars(&body);
    build_func_value(cx, Func::symbolic(vars, body))
}