lemma-engine 0.8.14

A language that means business.
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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use lemma::parsing::ast::{DateTimeValue, TimezoneValue};
use lemma::{Engine, LiteralValue, ValueKind};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;

fn source() -> lemma::SourceType {
    lemma::SourceType::Path(Arc::new(PathBuf::from("test.lemma")))
}

fn default_effective() -> DateTimeValue {
    DateTimeValue {
        year: 2026,
        month: 3,
        day: 7,
        hour: 12,
        minute: 0,
        second: 0,
        microsecond: 0,
        timezone: Some(TimezoneValue {
            offset_hours: 0,
            offset_minutes: 0,
        }),
    }
}

fn eval_literal_with_data(
    code: &str,
    spec_name: &str,
    rule_name: &str,
    data: HashMap<String, String>,
) -> LiteralValue {
    let mut engine = Engine::new();
    engine.load(code, source()).expect("Should parse and plan");
    let response = engine
        .run(
            None,
            spec_name,
            Some(&default_effective()),
            data,
            false,
            lemma::EvaluationRequest::default(),
        )
        .expect("Should evaluate");
    response
        .results
        .get(rule_name)
        .unwrap_or_else(|| panic!("Rule '{}' not found", rule_name))
        .result
        .value()
        .unwrap_or_else(|| panic!("Rule '{}' returned non-value", rule_name))
        .clone()
}

fn eval_literal(code: &str, spec_name: &str, rule_name: &str) -> LiteralValue {
    eval_literal_with_data(code, spec_name, rule_name, HashMap::new())
}

fn eval_rule(code: &str, spec_name: &str, rule_name: &str) -> String {
    eval_literal(code, spec_name, rule_name).to_string()
}

fn eval_bool(code: &str, spec_name: &str, rule_name: &str) -> bool {
    match eval_literal(code, spec_name, rule_name).value {
        ValueKind::Boolean(val) => val,
        other => panic!("Expected Boolean, got {:?}", other),
    }
}

fn expect_plan_error(code: &str, expected_fragment: &str) {
    let mut engine = Engine::new();
    let result = engine.load(code, source());
    assert!(result.is_err(), "Expected planning error");
    let combined = result
        .unwrap_err()
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
        .join("; ");
    assert!(
        combined.contains(expected_fragment),
        "Expected error containing '{}', got: {}",
        expected_fragment,
        combined
    );
}

// =============================================================================
// P. Number range
// =============================================================================

#[test]
fn p1_containment_inside() {
    let code = r#"spec test
rule ok: 50 in 0...100"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p2_containment_outside() {
    let code = r#"spec test
rule ok: 150 in 0...100"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn p3_containment_at_boundary_left() {
    let code = r#"spec test
rule ok: 0 in 0...100"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p4_containment_at_boundary_right_excluded() {
    let code = r#"spec test
rule ok: 100 in 0...100"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn p4b_containment_just_inside_right_boundary() {
    let code = r#"spec test
rule ok: 99 in 0...100"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p4c_empty_range_contains_nothing() {
    let code = r#"spec test
rule ok: 5 in 5...5"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn p5_containment_negative_range() {
    let code = r#"spec test
rule ok: -25 in -50...50"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p6_containment_fully_negative() {
    let code = r#"spec test
rule ok: -75 in -100...-50"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p7_containment_just_outside_left() {
    let code = r#"spec test
rule ok: -1 in 0...100"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn p8_span_gte() {
    let code = r#"spec test
rule ok: (0...100) >= 50"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p9_span_lt() {
    let code = r#"spec test
rule ok: (0...100) < 50"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn p10_span_negative_range() {
    let code = r#"spec test
rule ok: (-100...-50) >= 49"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p11_span_empty_range() {
    let code = r#"spec test
rule ok: (50...50) >= 1"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn p12_span_reversed_range_is_absolute() {
    let code = r#"spec test
rule ok: (100...0) >= 50"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p13_addition_uses_range_size() {
    let code = r#"spec test
rule value: (0...100) + 50"#;
    assert_eq!(eval_rule(code, "test", "value"), "150");
}

#[test]
fn p14_subtraction_uses_range_size() {
    let code = r#"spec test
rule value: (0...100) - 50"#;
    assert_eq!(eval_rule(code, "test", "value"), "50");
}

#[test]
fn p15_chained_arithmetic_uses_range_size() {
    let code = r#"spec test
rule base: 0...100
rule adjusted: base + 50 - 10"#;
    assert_eq!(eval_rule(code, "test", "adjusted"), "140");
}

#[test]
fn p16_negative_addition_uses_range_size() {
    let code = r#"spec test
rule value: (0...100) + -20"#;
    assert_eq!(eval_rule(code, "test", "value"), "80");
}

#[test]
fn p17_comparison_after_range_arithmetic() {
    let code = r#"spec test
rule ok: ((0...100) + 50) >= 149"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn p18_range_through_rules() {
    let code = r#"spec test
data lower: number -> default 0
data upper: number -> default 100
data value: number -> default 50
rule bounds: lower...upper
rule check: value in bounds"#;
    assert!(eval_bool(code, "test", "check"));
}

#[test]
fn p19_adjusted_range_through_rules() {
    let code = r#"spec test
data lower: 0
data upper: 100
data adjustment: 25
rule bounds: lower...upper
rule adjusted: bounds + adjustment"#;
    assert_eq!(eval_rule(code, "test", "adjusted"), "125");
}

#[test]
fn p20_user_declared() {
    let code = r#"spec test
data bounds: number range -> default 0...100
rule check: 50 in bounds"#;
    assert!(eval_bool(code, "test", "check"));
}

#[test]
fn p21_user_declared_dynamic() {
    let code = r#"spec test
data bounds: number range
rule check: 50 in bounds"#;
    let mut data = HashMap::new();
    data.insert("bounds".to_string(), "0...100".to_string());
    let lit = eval_literal_with_data(code, "test", "check", data);
    match lit.value {
        ValueKind::Boolean(val) => assert!(val),
        other => panic!("Expected Boolean, got {:?}", other),
    }
}

// =============================================================================
// Q. Quantity range
// =============================================================================

#[test]
fn q1_containment_same_unit() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 32 kilogram in 30 kilogram...35 kilogram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q2_containment_outside() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 36 kilogram in 30 kilogram...35 kilogram"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn q3_containment_cross_unit() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 32000 gram in 30 kilogram...35 kilogram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q4_containment_cross_unit_outside() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 29000 gram in 30 kilogram...35 kilogram"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn q5_containment_at_boundary_cross_unit() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 30000 gram in 30 kilogram...35 kilogram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q5b_containment_at_right_boundary_excluded() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 35 kilogram in 30 kilogram...35 kilogram"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn q5c_containment_just_inside_right_boundary() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: 34 kilogram in 30 kilogram...35 kilogram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q6_span_gte_same_unit() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: (30 kilogram...35 kilogram) >= 5 kilogram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q7_span_gte_cross_unit() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: (30 kilogram...35 kilogram) >= 5000 gram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q8_span_lt_cross_unit() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: (30 kilogram...35 kilogram) >= 6000 gram"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn q9_span_mixed_range_units() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: (1000 gram...5 kilogram) >= 4 kilogram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q12_subtraction_cross_unit_uses_range_size() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule lower_bound: ((30 kilogram...35 kilogram) - 500 gram) >= 4500 gram
rule upper_bound: ((30 kilogram...35 kilogram) - 500 gram) > 4500 gram"#;
    assert!(eval_bool(code, "test", "lower_bound"));
    assert!(!eval_bool(code, "test", "upper_bound"));
}

#[test]
fn q13_comparison_after_range_arithmetic() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule ok: ((30 kilogram...35 kilogram) + 2000 gram) >= 6500 gram"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn q14_range_through_rules() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
data min_weight: 30 kilogram
data max_weight: 35 kilogram
data sample: 32 kilogram
rule acceptable: min_weight...max_weight
rule span_with_allowance: acceptable + 500 gram
rule passes: sample in acceptable
rule margin_ok: span_with_allowance >= 5500 gram"#;
    assert!(eval_bool(code, "test", "passes"));
    assert!(eval_bool(code, "test", "margin_ok"));
}

#[test]
fn q15_user_declared() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
data acceptable: quantity range -> default 30 kilogram...35 kilogram
rule check: 32 kilogram in acceptable"#;
    assert!(eval_bool(code, "test", "check"));
}

#[test]
fn q15_mixed_unit_endpoints_gram_to_kilogram() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
data band: quantity range -> default 3000 gram...35 kilogram
rule inside: 3200 gram in band
rule lower: 3000 gram in band
rule upper_excluded: 35 kilogram in band"#;
    assert!(eval_bool(code, "test", "inside"));
    assert!(eval_bool(code, "test", "lower"));
    assert!(!eval_bool(code, "test", "upper_excluded"));
}

// =============================================================================
// R. Ratio range
// =============================================================================

#[test]
fn r1_containment_inside() {
    let code = r#"spec test
rule ok: 25% in 10%...50%"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn r2_containment_outside() {
    let code = r#"spec test
rule ok: 75% in 10%...50%"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn r3_containment_at_boundary_right_excluded() {
    let code = r#"spec test
rule ok: 50% in 10%...50%"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn r3b_containment_just_inside_right_boundary() {
    let code = r#"spec test
rule ok: 49% in 10%...50%"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn r4_containment_zero() {
    let code = r#"spec test
rule ok: 0% in 0%...100%"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn r5_span_gte() {
    let code = r#"spec test
rule ok: (10%...50%) >= 30%"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn r6_span_lt() {
    let code = r#"spec test
rule ok: (10%...50%) >= 50%"#;
    assert!(!eval_bool(code, "test", "ok"));
}

#[test]
fn r7_addition_uses_range_size() {
    let code = r#"spec test
rule value: (10%...50%) + 10%"#;
    assert_eq!(eval_rule(code, "test", "value"), "50%");
}

#[test]
fn r8_subtraction_uses_range_size() {
    let code = r#"spec test
rule value: (10%...50%) - 10%"#;
    assert_eq!(eval_rule(code, "test", "value"), "30%");
}

#[test]
fn r9_comparison_after_range_arithmetic() {
    let code = r#"spec test
rule ok: ((10%...50%) + 10%) >= 45%"#;
    assert!(eval_bool(code, "test", "ok"));
}

#[test]
fn r10_range_through_rules() {
    let code = r#"spec test
data base_discount: 10%
data max_discount: 50%
data customer_discount: 35%
rule discount_range: base_discount...max_discount
rule qualifies: customer_discount in discount_range
rule generous_enough: discount_range >= 30%"#;
    assert!(eval_bool(code, "test", "qualifies"));
    assert!(eval_bool(code, "test", "generous_enough"));
}

// =============================================================================
// S. Range validation / rejection
// =============================================================================

#[test]
fn s1_date_vs_number() {
    let code = r#"spec test
rule bad: 2024-01-01...100"#;
    expect_plan_error(code, "range");
}

#[test]
fn s2_quantity_family_mismatch() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
data money: quantity -> unit eur 1.00
rule bad: 30 kilogram...100 eur"#;
    expect_plan_error(code, "range");
}

#[test]
fn s3_boolean_range() {
    let code = r#"spec test
rule bad: true...false"#;
    expect_plan_error(code, "range");
}

#[test]
fn s4_text_range() {
    let code = r#"spec test
rule bad: "a"..."z""#;
    expect_plan_error(code, "range");
}

#[test]
fn s5_date_vs_duration() {
    let code = r#"spec test
uses lemma si
rule bad: 2024-01-01...7 days"#;
    expect_plan_error(code, "range");
}

#[test]
fn s6_number_vs_ratio() {
    let code = r#"spec test
rule bad: 50...50%"#;
    expect_plan_error(code, "range");
}

// `(0...100) as days` span semantics: see `range_span_unit_conversion.rs`.

#[test]
fn s8_quantity_range_in_months() {
    let code = r#"spec test
data weight: quantity -> unit gram 1 -> unit kilogram 1000
rule bad: (30 kilogram...35 kilogram) as months"#;
    expect_plan_error(code, "convert");
}

#[test]
fn s9_quantity_times_number_range() {
    let code = r#"spec test
uses lemma si
data money: quantity -> unit eur 1.00
data rate: quantity
  -> unit eur_per_second eur/second
  -> unit eur_per_hour eur/hour
data hourly_rate: 50 eur_per_hour
rule bad: hourly_rate * (0...100)"#;
    expect_plan_error(code, "range");
}