sim-lib-numbers-rational 0.1.0

SIM workspace package for sim lib numbers rational.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! The `numbers/rational` domain object and `RationalNumbersLib`: the domain
//! and operator symbols and the `Lib` that registers the domain, its shapes,
//! value class, ops, and promotions to and from the integer and `f64` domains.

use std::sync::Arc;

use sim_kernel::{
    AbiVersion, ClassId, DefaultFactory, Dependency, Export, Expr, Factory, Lib, LibManifest,
    LibTarget, Linker, NumberDomain, NumberLiteral, Object, Result, Symbol, Value,
    ValuePromotionRule, Version,
};
use sim_lib_numbers_core::{
    DomainNumberValueShape, NumberDomainTableSpec, NumberLiteralClass, NumberLiteralShape,
    ScalarBinaryOp, ScalarOps, ScalarReductionOp, ScalarUnaryOp, class_surface_or_symbol, domains,
    install_scalar_ops, number_domain_table, shape_surface_or_symbol,
};
use sim_shape::shape_value;

use super::integer::compact_canonical;
use super::ops::{
    RationalRuleFn, ValueRuleFn, promote_f64_literal_to_rational, promote_f64_value_to_rational,
    promote_integer_literal_to_rational, promote_integer_value_to_rational,
    promote_rational_literal_to_f64, promote_rational_value_to_f64, rational_add_rule,
    rational_add_value_rule, rational_div_rule, rational_div_value_rule, rational_mul_rule,
    rational_mul_value_rule, rational_neg_rule, rational_neg_value_rule, rational_pow_rule,
    rational_pow_value_rule, rational_product_rule, rational_product_value_rule, rational_sub_rule,
    rational_sub_value_rule, rational_sum_rule, rational_sum_value_rule,
};
use super::value::{Rational, RationalValueClass};

/// The `numbers/rational` domain symbol shared by this crate's literals,
/// values, and ops.
pub fn number_domain() -> Symbol {
    domains::rational()
}

/// The symbol of the rational literal class (the `Expr::Number` literal shape
/// in canonical `num/den` form).
pub fn literal_class_symbol() -> Symbol {
    domains::literal_class("rational")
}

/// The symbol of the shape matching individual rational literals, derived from
/// [`literal_class_symbol`].
pub fn literal_instance_shape_symbol() -> Symbol {
    Symbol::qualified(literal_class_symbol().to_string(), "instance-shape")
}

/// The symbol of the `numbers/rational` value class, used to register the class
/// and to tag the extension encoding of non-compact rational values.
pub fn rational_value_class_symbol() -> Symbol {
    domains::rational_value_class()
}

pub(crate) fn value_shape_symbol() -> Symbol {
    sim_lib_numbers_core::value_shape_symbol(&number_domain())
}

/// The `numbers/f64` domain symbol, the far end of the rational <-> f64
/// promotion edges.
pub fn f64_domain() -> Symbol {
    domains::f64()
}

/// The `math/add` operator symbol this domain installs a rational rule for.
pub fn add_symbol() -> Symbol {
    Symbol::qualified("math", "add")
}

/// The `math/sub` operator symbol this domain installs a rational rule for.
pub fn sub_symbol() -> Symbol {
    Symbol::qualified("math", "sub")
}

/// The `math/mul` operator symbol this domain installs a rational rule for.
pub fn mul_symbol() -> Symbol {
    Symbol::qualified("math", "mul")
}

/// The `math/div` operator symbol this domain installs a rational rule for.
pub fn div_symbol() -> Symbol {
    Symbol::qualified("math", "div")
}

/// The `math/pow` operator symbol this domain installs a rational rule for.
pub fn pow_symbol() -> Symbol {
    Symbol::qualified("math", "pow")
}

#[sim_citizen_derive::non_citizen(
    reason = "numbers/rational number-domain marker; reconstruct by loading the rational number lib",
    kind = "marker"
)]
/// The exact rational number domain: parses `num/den` literals and declares the
/// promotion edges to and from the integer and `f64` domains.
pub struct RationalNumberDomain;

impl NumberDomain for RationalNumberDomain {
    fn symbol(&self) -> Symbol {
        number_domain()
    }

    fn parse_literal(&self, cx: &mut sim_kernel::Cx, text: &str) -> Result<Option<Value>> {
        let Some((numerator, denominator)) = super::ops::parse_rational_parts(text) else {
            return Ok(None);
        };
        cx.factory()
            .number_literal(number_domain(), format!("{numerator}/{denominator}"))
            .map(Some)
    }

    fn encode_literal(
        &self,
        cx: &mut sim_kernel::Cx,
        value: Value,
    ) -> Result<Option<NumberLiteral>> {
        match value.object().as_expr(cx)? {
            Expr::Number(number) if number.domain == number_domain() => Ok(Some(number)),
            _ => Ok(value
                .object()
                .downcast_ref::<Rational>()
                .cloned()
                .map(|rational| compact_canonical(cx, &rational.num, &rational.den))
                .transpose()?
                .flatten()
                .map(|canonical| NumberLiteral {
                    domain: number_domain(),
                    canonical,
                })),
        }
    }
}

impl Object for RationalNumberDomain {
    fn display(&self, _cx: &mut sim_kernel::Cx) -> Result<String> {
        Ok("#<number-domain numbers/rational>".to_owned())
    }

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

impl sim_kernel::ObjectCompat for RationalNumberDomain {
    fn class(&self, cx: &mut sim_kernel::Cx) -> Result<sim_kernel::ClassRef> {
        sim_lib_numbers_core::number_domain_class_stub(cx)
    }
    fn as_expr(&self, _cx: &mut sim_kernel::Cx) -> Result<Expr> {
        Ok(Expr::Symbol(number_domain()))
    }
    fn as_table(&self, cx: &mut sim_kernel::Cx) -> Result<Value> {
        let literal_class = class_surface_or_symbol(cx, literal_class_symbol())?;
        let instance_shape = shape_surface_or_symbol(cx, literal_instance_shape_symbol())?;
        let value_shape = shape_surface_or_symbol(cx, value_shape_symbol())?;
        number_domain_table(
            cx,
            NumberDomainTableSpec::new(
                number_domain(),
                "rational",
                "numerator/denominator",
                0,
                literal_class,
                instance_shape,
                value_shape,
            ),
        )
    }
    fn as_number_domain(&self) -> Option<&dyn NumberDomain> {
        Some(self)
    }
}

/// The library that installs the `numbers/rational` domain: its literal class
/// and shapes, the `Rational` value class, the reduced arithmetic ops, and the
/// promotion rules to and from the integer and `f64` domains.
///
/// # Examples
///
/// ```
/// use std::sync::Arc;
/// use sim_kernel::{Cx, DefaultFactory, NoopEvalPolicy};
/// use sim_lib_numbers_rational::{RationalNumbersLib, number_domain};
///
/// let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
/// cx.load_lib(&RationalNumbersLib::new()).unwrap();
///
/// // Rational literals parse into the domain even before any base scalar lib
/// // is loaded; arithmetic over them additionally needs the integer libs.
/// let value = cx
///     .factory()
///     .number_literal(number_domain(), "1/2".to_owned())
///     .unwrap();
/// let number = cx.number_value_ref(value).unwrap().unwrap();
/// assert_eq!(number.domain, number_domain());
/// ```
pub struct RationalNumbersLib;

impl RationalNumbersLib {
    /// Creates a new `numbers/rational` domain library.
    pub fn new() -> Self {
        Self
    }
}

impl Default for RationalNumbersLib {
    fn default() -> Self {
        Self::new()
    }
}

impl Lib for RationalNumbersLib {
    fn manifest(&self) -> LibManifest {
        LibManifest {
            id: number_domain(),
            version: Version(env!("CARGO_PKG_VERSION").to_owned()),
            abi: AbiVersion { major: 0, minor: 1 },
            target: LibTarget::HostRegistered,
            requires: Vec::<Dependency>::new(),
            capabilities: Vec::new(),
            exports: vec![
                Export::NumberDomain {
                    symbol: number_domain(),
                    number_domain_id: None,
                },
                Export::Class {
                    symbol: literal_class_symbol(),
                    class_id: None,
                },
                Export::Shape {
                    symbol: literal_instance_shape_symbol(),
                    shape_id: None,
                },
                Export::Shape {
                    symbol: value_shape_symbol(),
                    shape_id: None,
                },
                Export::Class {
                    symbol: rational_value_class_symbol(),
                    class_id: None,
                },
            ],
        }
    }

    fn load(&self, _cx: &mut sim_kernel::LoadCx, linker: &mut Linker<'_>) -> Result<()> {
        let instance_shape = Arc::new(NumberLiteralShape::new(
            number_domain(),
            "RationalLiteral",
            [
                "number literal in the numbers/rational domain",
                "matches Expr::Number where domain == numbers/rational",
            ],
        ));
        let literal_class = Arc::new(NumberLiteralClass::new(
            literal_class_symbol(),
            number_domain(),
            "rational",
            "numerator/denominator",
            literal_instance_shape_symbol(),
            instance_shape.clone(),
        ));
        let value_shape = Arc::new(DomainNumberValueShape::new(
            number_domain(),
            "RationalValue",
            [
                "number value in the numbers/rational domain",
                "accepts any NumberValue where domain == numbers/rational",
            ],
        ));

        linker.number_domain_value(
            number_domain(),
            DefaultFactory
                .opaque(Arc::new(RationalNumberDomain))
                .expect("number domain should be boxable"),
        )?;
        let literal_class_id = linker.class_value(
            literal_class_symbol(),
            DefaultFactory
                .opaque(literal_class.clone())
                .expect("number literal class should be boxable"),
        )?;
        literal_class.set_id(literal_class_id);
        register_rational_value_class(linker)?;

        linker.shape_value(
            literal_instance_shape_symbol(),
            shape_value(literal_instance_shape_symbol(), instance_shape),
        )?;
        linker.shape_value(
            value_shape_symbol(),
            shape_value(value_shape_symbol(), value_shape),
        )?;

        register_promotions(linker);
        let binary = [
            (
                add_symbol(),
                rational_add_rule as RationalRuleFn,
                rational_add_value_rule as ValueRuleFn,
            ),
            (sub_symbol(), rational_sub_rule, rational_sub_value_rule),
            (mul_symbol(), rational_mul_rule, rational_mul_value_rule),
            (div_symbol(), rational_div_rule, rational_div_value_rule),
            (pow_symbol(), rational_pow_rule, rational_pow_value_rule),
        ]
        .into_iter()
        .map(|(operator, literal_apply, value_apply)| ScalarBinaryOp {
            operator,
            literal_cost: 0,
            literal_apply,
            value_cost: 1,
            value_apply,
        })
        .collect();
        let ops = ScalarOps {
            domain: number_domain(),
            binary,
            unary: vec![ScalarUnaryOp {
                operator: Symbol::qualified("math", "neg"),
                literal_cost: 0,
                literal_apply: rational_neg_rule,
                value_cost: 1,
                value_apply: rational_neg_value_rule,
            }],
            reduction: vec![
                ScalarReductionOp {
                    operator: Symbol::qualified("math", "sum"),
                    literal_cost: 0,
                    literal_apply: rational_sum_rule,
                    value_cost: 1,
                    value_apply: rational_sum_value_rule,
                },
                ScalarReductionOp {
                    operator: Symbol::qualified("math", "product"),
                    literal_cost: 0,
                    literal_apply: rational_product_rule,
                    value_cost: 1,
                    value_apply: rational_product_value_rule,
                },
            ],
        };
        install_scalar_ops(linker, &ops);
        Ok(())
    }
}

fn register_rational_value_class(linker: &mut Linker<'_>) -> Result<ClassId> {
    let rational_value_class = Arc::new(RationalValueClass::new());
    let rational_class_id = linker.class_value(
        rational_value_class_symbol(),
        DefaultFactory
            .opaque(rational_value_class.clone())
            .expect("rational value class should be boxable"),
    )?;
    rational_value_class.set_id(rational_class_id);
    Ok(rational_class_id)
}

fn install_rational_value_citizen(linker: &mut Linker<'_>) -> Result<()> {
    register_rational_value_class(linker).map(|_| ())
}

fn conformance_rational_value_citizen(cx: &mut sim_kernel::Cx) -> Result<()> {
    let num = cx
        .factory()
        .number_literal(domains::i64(), "3".to_owned())?;
    let den = cx
        .factory()
        .number_literal(domains::bigint(), "5".to_owned())?;
    let value = super::value::make_rational(cx, num, den)?;
    sim_citizen::check_value_fixture(cx, value)
}

sim_citizen::inventory::submit! {
    sim_citizen::CitizenInfo {
        symbol: "numbers/Rational",
        version: 0,
        crate_name: env!("CARGO_PKG_NAME"),
        arity: 2,
        install: install_rational_value_citizen,
        conformance: conformance_rational_value_citizen,
    }
}

fn register_promotions(linker: &mut Linker<'_>) {
    for domain in integer_domains() {
        linker.promotion_rule(sim_kernel::PromotionRule {
            from_domain: domain.clone(),
            to_domain: number_domain(),
            cost: 1,
            convert: promote_integer_literal_to_rational,
        });
        linker.value_promotion_rule(ValuePromotionRule {
            from_domain: domain,
            to_domain: number_domain(),
            cost: 1,
            convert: promote_integer_value_to_rational,
        });
    }
    linker.promotion_rule(sim_kernel::PromotionRule {
        from_domain: f64_domain(),
        to_domain: number_domain(),
        cost: 1,
        convert: promote_f64_literal_to_rational,
    });
    linker.value_promotion_rule(ValuePromotionRule {
        from_domain: f64_domain(),
        to_domain: number_domain(),
        cost: 1,
        convert: promote_f64_value_to_rational,
    });
    linker.promotion_rule(sim_kernel::PromotionRule {
        from_domain: number_domain(),
        to_domain: f64_domain(),
        cost: 50,
        convert: promote_rational_literal_to_f64,
    });
    linker.value_promotion_rule(ValuePromotionRule {
        from_domain: number_domain(),
        to_domain: f64_domain(),
        cost: 50,
        convert: promote_rational_value_to_f64,
    });
}

fn integer_domains() -> Vec<Symbol> {
    domains::integer_domains()
}