implicit-predicates-gen 0.0.2

Code generator for exact geometric predicates over explicit and implicit points
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
//! Declarations of point types: what is read, and the errors for what is not.

use implicit_predicates_gen::{
    LinearSum, ParamType, PointGraph, PointNode, PointType, parse, parse_points,
};

const POINTS: &str = include_str!("fixtures/points.pred");
const ORIENT3D: &str = include_str!("fixtures/orient3d.pred");

/// Asserts that `source` is rejected at `(line, column)` of `t.pred` with a
/// message containing `fragment`.
fn check(source: &str, (line, column): (usize, usize), fragment: &str) {
    let e = parse_points(source, "t.pred").expect_err("the source must be rejected");
    assert_eq!(e.file, "t.pred");
    assert_eq!((e.line, e.column), (line, column), "{source}: {e}");
    assert!(e.message.contains(fragment), "{source}: {e}");
}

/// Evaluates coordinate `axis` of `point` for the given values: the value of
/// each parameter is a `[f64; 3]`, of which a scalar uses the first entry.
fn evaluate(graph: &PointGraph, axis: usize, values: &[[f64; 3]]) -> f64 {
    let mut v: Vec<f64> = Vec::new();
    for node in &graph.nodes {
        v.push(match *node {
            PointNode::Coord { param, axis } => values[param][usize::from(axis)],
            PointNode::Scalar(param) => values[param][0],
            PointNode::Int(i) => i as f64,
            PointNode::Neg(x) => -v[x],
            PointNode::Add(x, y) => v[x] + v[y],
            PointNode::Sub(x, y) => v[x] - v[y],
            PointNode::Mul(x, y) => v[x] * v[y],
        });
    }
    v[graph.roots[axis]]
}

#[test]
fn the_fixture_has_an_explicit_and_a_linear_point() {
    let points = parse_points(POINTS, "points.pred").unwrap();
    let names: Vec<&str> = points.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(names, ["Explicit", "Lnc"]);
    assert!(points[0].is_explicit());
    assert!(!points[1].is_explicit());
    assert_eq!((points[0].position.line, points[0].position.column), (3, 7));
    assert_eq!((points[1].position.line, points[1].position.column), (5, 7));
}

#[test]
fn the_parameters_are_read_with_their_types() {
    let points = parse_points(POINTS, "points.pred").unwrap();
    let lnc = &points[1];
    let params: Vec<(&str, ParamType)> =
        lnc.params.iter().map(|p| (p.name.as_str(), p.ty)).collect();
    assert_eq!(
        params,
        [
            ("p", ParamType::F64x3),
            ("q", ParamType::F64x3),
            ("t", ParamType::F64)
        ]
    );
    assert_eq!(
        (lnc.params[2].position.line, lnc.params[2].position.column),
        (5, 31)
    );
}

#[test]
fn the_coordinates_of_lnc_are_q_plus_t_times_p_minus_q() {
    let lnc = &parse_points(POINTS, "points.pred").unwrap()[1];
    let values = [[1.0, 2.0, 3.0], [5.0, -7.0, 11.0], [0.25, 0.0, 0.0]];
    for axis in 0..3 {
        let (p, q, t) = (values[0][axis], values[1][axis], values[2][0]);
        assert_eq!(evaluate(&lnc.graph, axis, &values), q + t * (p - q));
    }
    // `p - q` is computed once per coordinate and shared by nothing else, but
    // the three coordinates share the scalar `t`.
    let scalars = lnc
        .graph
        .nodes
        .iter()
        .filter(|n| matches!(n, PointNode::Scalar(2)))
        .count();
    assert_eq!(scalars, 1);
}

#[test]
fn the_explicit_point_returns_its_parameter() {
    let explicit = &parse_points(POINTS, "points.pred").unwrap()[0];
    for axis in 0..3 {
        let root = explicit.graph.roots[axis];
        assert_eq!(
            explicit.graph.nodes[root],
            PointNode::Coord {
                param: 0,
                axis: axis as u8
            }
        );
    }
    assert!(explicit.constraints.is_empty());
}

#[test]
fn the_where_clause_is_kept_as_sums() {
    let lnc = &parse_points(POINTS, "points.pred").unwrap()[1];
    let sum = |params: &[usize], constant| LinearSum {
        params: params.to_vec(),
        constant,
    };
    assert_eq!(lnc.constraints.len(), 2);
    assert_eq!(lnc.constraints[0].lhs, sum(&[], 0));
    assert_eq!(lnc.constraints[0].rhs, sum(&[2], 0));
    assert_eq!(lnc.constraints[1].lhs, sum(&[2], 0));
    assert_eq!(lnc.constraints[1].rhs, sum(&[], 1));
    assert_eq!(
        (
            lnc.constraints[0].position.line,
            lnc.constraints[0].position.column
        ),
        (6, 11)
    );

    let source = "point Tpi(p: F64x3, q: F64x3, r: F64x3, u: f64, v: f64) = \
                  p + u*(q - p) + v*(r - p) where 0 <= u, 0 <= v, u + v <= 1;";
    let tpi = &parse_points(source, "t.pred").unwrap()[0];
    assert_eq!(tpi.constraints.len(), 3);
    assert_eq!(tpi.constraints[2].lhs, sum(&[3, 4], 0));
    assert_eq!(tpi.constraints[2].rhs, sum(&[], 1));
}

#[test]
fn points_and_predicates_can_share_a_file() {
    let source = format!("{POINTS}\n{ORIENT3D}\npoint Again(p: F64x3) = p + p;\n");
    let points = parse_points(&source, "t.pred").unwrap();
    let names: Vec<&str> = points.iter().map(|p| p.name.as_str()).collect();
    assert_eq!(names, ["Explicit", "Lnc", "Again"]);
    let predicates = parse(&source, "t.pred").unwrap();
    assert_eq!(predicates.len(), 1);
    assert_eq!(predicates[0].name, "orient3d");
    let alone = parse(ORIENT3D, "t.pred").unwrap();
    assert_eq!(predicates[0].main, alone[0].main);
    assert_eq!(predicates[0].exact, alone[0].exact);
}

#[test]
fn a_file_without_points_has_none() {
    assert_eq!(parse_points(ORIENT3D, "t.pred").unwrap(), []);
    assert_eq!(parse_points("", "t.pred").unwrap(), []);
    assert_eq!(parse_points("// nothing\n", "t.pred").unwrap(), []);
}

#[test]
fn a_function_named_point_is_not_a_declaration() {
    let source = "fn point(a: Point3) -> Sign { a.x }\npoint E(p: F64x3) = p;";
    assert_eq!(parse_points(source, "t.pred").unwrap().len(), 1);
    assert_eq!(parse(source, "t.pred").unwrap()[0].name, "point");
}

#[test]
fn a_file_of_points_has_no_predicates_but_is_not_an_error() {
    assert_eq!(parse(POINTS, "t.pred").unwrap(), []);
    let e = parse("", "t.pred").unwrap_err();
    assert!(e.message.contains("no predicate"), "{e}");
}

#[test]
fn explicit_means_one_vector_returned_as_is() {
    let is_explicit = |s: &str| -> bool { parse_points(s, "t.pred").unwrap()[0].is_explicit() };
    assert!(is_explicit("point A(a: F64x3) = a;"));
    assert!(!is_explicit("point A(a: F64x3) = a + a;"));
    assert!(!is_explicit("point A(a: F64x3) = -a;"));
    assert!(!is_explicit("point A(a: F64x3, t: f64) = a * t;"));
    // A `where` clause names an `f64` parameter, which the expression must
    // use, so a type with one is never explicit.
    assert!(!is_explicit(
        "point A(a: F64x3, t: f64) = a * t where 0 <= t;"
    ));
}

#[test]
fn arithmetic_is_per_coordinate_with_the_usual_precedence() {
    let source = "point A(a: F64x3, b: F64x3, s: f64, t: f64) = \
                  -a - b * s * 2 + (t + 3) * a - -b;";
    let point: PointType = parse_points(source, "t.pred").unwrap().remove(0);
    let values = [
        [1.5, -2.0, 4.0],
        [0.5, 8.0, -3.0],
        [3.0, 0.0, 0.0],
        [7.0; 3],
    ];
    for axis in 0..3 {
        let (a, b, s, t) = (values[0][axis], values[1][axis], values[2][0], values[3][0]);
        let expected = -a - b * s * 2.0 + (t + 3.0) * a - -b;
        assert_eq!(evaluate(&point.graph, axis, &values), expected);
    }
}

#[test]
fn equal_subexpressions_share_a_node() {
    let source = "point A(a: F64x3, t: f64) = (a * t) + (a * t);";
    let point = parse_points(source, "t.pred").unwrap().remove(0);
    let products = point
        .graph
        .nodes
        .iter()
        .filter(|n| matches!(n, PointNode::Mul(..)))
        .count();
    assert_eq!(products, 3);
    for (axis, &root) in point.graph.roots.iter().enumerate() {
        let PointNode::Add(x, y) = point.graph.nodes[root] else {
            panic!("the root must be a sum");
        };
        assert_eq!(x, y, "axis {axis}");
    }
}

#[test]
fn an_unused_parameter_is_an_error_at_its_name() {
    check(
        "point A(p: F64x3, q: F64x3) = p;",
        (1, 19),
        "unused parameter `q`",
    );
    check(
        "point A(p: F64x3, t: f64) = p\n    where 0 <= t;",
        (1, 19),
        "unused parameter `t`",
    );
}

#[test]
fn an_unknown_type_is_an_error_at_the_type() {
    check("point A(p: Point3) = p;", (1, 12), "`F64x3` or `f64`");
    check("point A(p: f32) = p;", (1, 12), "`F64x3` or `f64`");
    check("point A(p) = p;", (1, 10), "`:`");
    check("point A(p: ) = p;", (1, 12), "type");
}

#[test]
fn a_product_of_two_vectors_is_an_error_at_the_operator() {
    check(
        "point A(p: F64x3, q: F64x3) = p * q;",
        (1, 33),
        "product of two `F64x3`",
    );
    check(
        "point A(p: F64x3, q: F64x3, t: f64) = t * p\n  + p * (q - p);",
        (2, 7),
        "product of two `F64x3`",
    );
}

#[test]
fn a_sum_of_a_vector_and_a_scalar_is_an_error_at_the_operator() {
    check(
        "point A(p: F64x3, t: f64) = p + t;",
        (1, 31),
        "an `F64x3` and an `f64`",
    );
    check(
        "point A(p: F64x3, t: f64) = t - p;",
        (1, 31),
        "an `f64` and an `F64x3`",
    );
    check(
        "point A(p: F64x3, t: f64) = p + 1;",
        (1, 31),
        "an `F64x3` and an `f64`",
    );
}

#[test]
fn the_value_must_be_a_vector() {
    check("point A(t: f64) = t;", (1, 19), "must be an `F64x3`");
    check(
        "point A(p: F64x3, t: f64) =\n    t * 2 + 1;",
        (2, 5),
        "must be an `F64x3`",
    );
}

#[test]
fn a_where_clause_over_a_vector_is_an_error_at_the_name() {
    check(
        "point A(p: F64x3, t: f64) = p * t where p <= 1;",
        (1, 41),
        "`p` is an `F64x3`",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= t, 2 + p <= 3;",
        (1, 53),
        "`p` is an `F64x3`",
    );
}

#[test]
fn a_where_clause_must_be_well_formed() {
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 < t;",
        (1, 43),
        "`<=`",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 >= t;",
        (1, 43),
        "`<=`",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= u;",
        (1, 46),
        "unknown name `u`",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= t,;",
        (1, 48),
        "expected",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where ;",
        (1, 41),
        "expected",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where t;",
        (1, 42),
        "`<=`",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= t - 1;",
        (1, 48),
        "only sums",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= (t);",
        (1, 46),
        "expected",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= 9223372036854775807 + 1;",
        (1, 68),
        "does not fit",
    );
    check(
        "point A(p: F64x3, t: f64) = p * t where 1.5 <= t;",
        (1, 41),
        "float",
    );
}

#[test]
fn a_comparison_without_parameters_is_an_error_at_its_start() {
    check(
        "point A(p: F64x3, t: f64) = p * t where 0 <= t, 0 <= 1;",
        (1, 49),
        "names no parameter",
    );
    check(
        "point A(a: F64x3) = a where 1 + 2 <= 3;",
        (1, 29),
        "names no parameter",
    );
}

#[test]
fn two_points_cannot_share_a_name() {
    check(
        "point A(p: F64x3) = p;\n\npoint A(q: F64x3) = q;",
        (3, 7),
        "`A` is already defined",
    );
}

#[test]
fn reserved_words_cannot_name_a_point_or_a_parameter() {
    for word in ["fn", "match", "Self", "r#type", "_", "where", "self"] {
        check(
            &format!("point {word}(p: F64x3) = p;"),
            (1, 7),
            "cannot be used as the name",
        );
    }
    check(
        "point A(match: F64x3) = match;",
        (1, 9),
        "cannot be used as the name",
    );
}

#[test]
fn a_declaration_must_follow_the_grammar() {
    check("point", (1, 1), "a point name");
    check("point A", (1, 7), "`(`");
    check("point A(p: F64x3)", (1, 8), "`=`");
    check("point A(p: F64x3) =", (1, 19), "an expression");
    check("point A(p: F64x3) = p", (1, 21), "`;`");
    check("point A(p: F64x3) = p p;", (1, 23), "`;`");
    check("point A(p: F64x3, p: F64x3) = p;", (1, 19), "redefines");
    check(
        "point A(p: F64x3,, q: f64) = p;",
        (1, 18),
        "a parameter name",
    );
    check("point A(p: F64x3) = p.x;", (1, 22), "whole");
    check("point A(p: F64x3) = z;", (1, 21), "unknown name `z`");
    check("point A(p: F64x3) = p / 2;", (1, 23), "division");
    check("point A(p: F64x3) = 1.5 * p;", (1, 21), "float");
    check("point A() = p;", (1, 13), "unknown name");
}

#[test]
fn nesting_is_limited_to_32_brackets() {
    let nested = |depth: usize| {
        format!(
            "point A(p: F64x3) = {}p{};",
            "(".repeat(depth),
            ")".repeat(depth)
        )
    };
    assert_eq!(parse_points(&nested(32), "t.pred").unwrap().len(), 1);
    check(&nested(33), (1, 20 + 33), "nested more than 32 deep");
}

#[test]
fn parse_reports_an_invalid_point_instead_of_skipping_it() {
    let check_parse = |source: &str, (line, column): (usize, usize), fragment: &str| {
        let e = parse(source, "t.pred").expect_err("the source must be rejected");
        assert_eq!((e.line, e.column), (line, column), "{source}: {e}");
        assert!(e.message.contains(fragment), "{source}: {e}");
    };
    check_parse(
        "point A(p: F64x3) = p * p * fn;",
        (1, 23),
        "product of two `F64x3`",
    );
    check_parse(
        "point fn orient(a: Point3) -> Sign { a.x - a.x } ;",
        (1, 7),
        "cannot be used as the name",
    );
    check_parse(
        "point A(p: F64x3, q: F64x3) = p;\nfn f(a: Point3) -> Sign { a.x }",
        (1, 19),
        "unused parameter `q`",
    );
    check_parse(
        "point A(p: F64x3) = p;\npoint A(p: F64x3) = p;\n",
        (2, 7),
        "already defined",
    );
    check_parse("point A(p: F64x3) = p", (1, 21), "`;`");
}

#[test]
fn parse_points_rejects_what_it_would_otherwise_skip_over_a_point() {
    check("x point A(p: F64x3) = p;", (1, 1), "`point` or `fn`");
    check(
        "fn f(a: Point3) -> Sign { a.x }\n;\npoint A(p: F64x3) = p;",
        (2, 1),
        "`point` or `fn`",
    );
    // A function without a body would swallow the declaration after it.
    check(
        "fn f(a: Point3) -> Sign\npoint A(p: F64x3) = p;",
        (2, 1),
        "`{`",
    );
    check("#[exact]", (1, 2), "`fn`");
    check("#[exact] fn", (1, 10), "a function name");
}

#[test]
fn a_point_followed_by_a_predicate_gives_each_its_items() {
    let source = format!("{POINTS}\n{ORIENT3D}");
    assert_eq!(parse_points(&source, "t.pred").unwrap().len(), 2);
    assert_eq!(parse(&source, "t.pred").unwrap().len(), 1);
    let reversed = format!("{ORIENT3D}\n{POINTS}");
    assert_eq!(parse_points(&reversed, "t.pred").unwrap().len(), 2);
    assert_eq!(parse(&reversed, "t.pred").unwrap().len(), 1);
}

#[test]
fn a_function_without_a_body_cannot_swallow_a_point_before_a_later_body() {
    let source =
        "fn f(a: Point3) -> Sign\npoint A(p: F64x3) = p;\nfn g(a: Point3) -> Sign { a.x }\n";
    check(source, (2, 1), "`{`");
    check("fn f(a: Point3) Sign { a.x }", (1, 17), "`->`");
    check("fn f(a: Point3) -> Bool { a.x }", (1, 20), "`Sign`");
    check("fn f -> Sign { a.x }", (1, 6), "`(`");
}

#[test]
fn a_file_of_only_points_has_points_but_no_predicates() {
    assert_eq!(parse(POINTS, "t.pred").unwrap(), []);
    assert_eq!(parse_points(POINTS, "t.pred").unwrap().len(), 2);
}