assura-types 0.4.3

Type checking for the Assura contract language
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
use super::*;

// --- T053 test helpers ---

fn make_fn_def(name: &str, params: Vec<(&str, &[&str])>, clauses: Vec<AstClause>) -> AstFnDef {
    AstFnDef {
        name: name.into(),
        is_ghost: false,
        is_lemma: false,
        params: params
            .into_iter()
            .map(|(n, ty)| {
                let tokens: Vec<String> = ty.iter().map(|s| s.to_string()).collect();
                AstParam {
                    name: n.into(),
                    ty: assura_parser::ast::try_parse_type_tokens(&tokens),
                }
            })
            .collect(),
        return_ty: assura_parser::ast::try_parse_type_tokens(&["Int".to_string()]),
        clauses,
    }
}

fn decreases_clause(body: SpExpr) -> AstClause {
    AstClause {
        kind: ClauseKind::Other("decreases".into()),
        body,
        effect_variables: vec![],
    }
}

fn requires_clause(body: SpExpr) -> AstClause {
    AstClause {
        kind: ClauseKind::Requires,
        body,
        effect_variables: vec![],
    }
}

fn partial_clause() -> AstClause {
    AstClause {
        kind: ClauseKind::Other("partial".into()),
        body: Spanned::no_span(AstExpr::Literal(AstLit::Bool(true))),
        effect_variables: vec![],
    }
}

fn ensures_with_recursive_call(fn_name: &str, args: Vec<SpExpr>) -> AstClause {
    AstClause {
        kind: ClauseKind::Ensures,
        body: Spanned::no_span(AstExpr::Call {
            func: Box::new(Spanned::no_span(AstExpr::Ident(fn_name.into()))),
            args,
        }),
        effect_variables: vec![],
    }
}

#[test]
fn totality_non_recursive_trivially_total() {
    // Non-recursive function passes without decreases
    let fn_def = make_fn_def("add", vec![("a", &["Int"]), ("b", &["Int"])], vec![]);
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..10));
    assert!(
        errors.is_empty(),
        "non-recursive function should be trivially total"
    );
}

#[test]
fn totality_recursive_with_valid_decreases() {
    // factorial(n) with decreases n, recursive call factorial(n - 1)
    let fn_def = make_fn_def(
        "factorial",
        vec![("n", &["Nat"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
            ensures_with_recursive_call(
                "factorial",
                vec![Spanned::no_span(AstExpr::BinOp {
                    lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                    op: AstBinOp::Sub,
                    rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
                })],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..20));
    assert!(
        errors.is_empty(),
        "valid decreasing measure should pass: {errors:?}"
    );
}

#[test]
fn totality_recursive_without_decreases_a09001() {
    // Recursive function without decreases clause -> A09001
    let fn_def = make_fn_def(
        "loop_forever",
        vec![("n", &["Int"])],
        vec![ensures_with_recursive_call(
            "loop_forever",
            vec![Spanned::no_span(AstExpr::Ident("n".into()))],
        )],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..10));
    assert_eq!(errors.len(), 1);
    assert_eq!(errors[0].code, "A09001");
}

#[test]
fn totality_non_decreasing_measure_deferred_to_smt() {
    // Recursive call with same argument (not decreasing) is now deferred to SMT
    // instead of immediately producing A09002. The SMT solver will find that
    // n < n is unsatisfiable and report the error.
    let fn_def = make_fn_def(
        "spin",
        vec![("n", &["Nat"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
            ensures_with_recursive_call("spin", vec![Spanned::no_span(AstExpr::Ident("n".into()))]),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, pending) = checker.check_function_totality(&fn_def, &(0..10));
    // No syntactic error; the check is deferred to SMT
    assert!(
        errors.is_empty(),
        "non-decreasing measure should be deferred to SMT, not produce syntactic error: {errors:?}"
    );
    assert!(
        !pending.is_empty(),
        "non-decreasing measure should produce a pending SMT check"
    );
    // The pending check should reference the spin function
    assert_eq!(pending[0].fn_name, "spin");
}

#[test]
fn totality_measure_not_well_founded_a09003() {
    // decreases n but no requires n >= 0 and param type is Int, not Nat
    let fn_def = make_fn_def(
        "bad_rec",
        vec![("n", &["Int"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
            ensures_with_recursive_call(
                "bad_rec",
                vec![Spanned::no_span(AstExpr::BinOp {
                    lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                    op: AstBinOp::Sub,
                    rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
                })],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..10));
    assert!(
        errors.iter().any(|e| e.code == "A09003"),
        "missing well-foundedness should produce A09003: {errors:?}"
    );
}

#[test]
fn totality_well_founded_with_requires_clause() {
    // decreases n with requires n >= 0 should NOT produce A09003
    let fn_def = make_fn_def(
        "count_down",
        vec![("n", &["Int"])],
        vec![
            requires_clause(Spanned::no_span(AstExpr::BinOp {
                lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                op: AstBinOp::Gte,
                rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("0".into())))),
            })),
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
            ensures_with_recursive_call(
                "count_down",
                vec![Spanned::no_span(AstExpr::BinOp {
                    lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                    op: AstBinOp::Sub,
                    rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
                })],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..20));
    assert!(
        !errors.iter().any(|e| e.code == "A09003"),
        "requires n >= 0 should establish well-foundedness: {errors:?}"
    );
}

#[test]
fn totality_partial_escape_hatch() {
    // Partial function skips termination checking
    let fn_def = make_fn_def(
        "diverge",
        vec![("n", &["Int"])],
        vec![
            partial_clause(),
            ensures_with_recursive_call(
                "diverge",
                vec![Spanned::no_span(AstExpr::Ident("n".into()))],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..10));
    assert!(
        errors.is_empty(),
        "partial function should skip totality check"
    );
}

#[test]
fn totality_partial_via_register() {
    // Partial registered via mark_partial
    let fn_def = make_fn_def(
        "diverge2",
        vec![("n", &["Int"])],
        vec![ensures_with_recursive_call(
            "diverge2",
            vec![Spanned::no_span(AstExpr::Ident("n".into()))],
        )],
    );
    let mut checker = TotalityChecker::new();
    checker.mark_partial("diverge2".into());
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..10));
    assert!(errors.is_empty(), "registered partial should skip check");
}

#[test]
fn totality_lexicographic_measures() {
    // Ackermann-like: decreases (m, n) with call (m - 1, n)
    let fn_def = make_fn_def(
        "ack",
        vec![("m", &["Nat"]), ("n", &["Nat"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("m".into()))),
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
            ensures_with_recursive_call(
                "ack",
                vec![
                    Spanned::no_span(AstExpr::BinOp {
                        lhs: Box::new(Spanned::no_span(AstExpr::Ident("m".into()))),
                        op: AstBinOp::Sub,
                        rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
                    }),
                    Spanned::no_span(AstExpr::Ident("n".into())),
                ],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..20));
    assert!(
        errors.is_empty(),
        "lexicographic decrease in first component should pass: {errors:?}"
    );
}

#[test]
fn totality_mutual_recursion_no_decreases_a09004() {
    // Two functions calling each other with no decreases -> A09004
    let fn_a = make_fn_def(
        "even",
        vec![("n", &["Nat"])],
        vec![ensures_with_recursive_call(
            "odd",
            vec![Spanned::no_span(AstExpr::BinOp {
                lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                op: AstBinOp::Sub,
                rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
            })],
        )],
    );
    let fn_b = make_fn_def(
        "odd",
        vec![("n", &["Nat"])],
        vec![ensures_with_recursive_call(
            "even",
            vec![Spanned::no_span(AstExpr::BinOp {
                lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                op: AstBinOp::Sub,
                rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
            })],
        )],
    );

    let checker = TotalityChecker::new();
    let span_a = 0..10;
    let span_b = 10..20;
    let fn_defs: Vec<(&AstFnDef, &Range<usize>)> = vec![(&fn_a, &span_a), (&fn_b, &span_b)];
    let errors = checker.check_mutual_recursion(&fn_defs);
    assert!(
        errors.iter().any(|e| e.code == "A09004"),
        "mutual recursion without decreases should produce A09004: {errors:?}"
    );
}

#[test]
fn totality_mutual_recursion_with_decreases_passes() {
    // Two functions calling each other, one has decreases -> passes
    let fn_a = make_fn_def(
        "even2",
        vec![("n", &["Nat"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
            ensures_with_recursive_call(
                "odd2",
                vec![Spanned::no_span(AstExpr::BinOp {
                    lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                    op: AstBinOp::Sub,
                    rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
                })],
            ),
        ],
    );
    let fn_b = make_fn_def(
        "odd2",
        vec![("n", &["Nat"])],
        vec![ensures_with_recursive_call(
            "even2",
            vec![Spanned::no_span(AstExpr::BinOp {
                lhs: Box::new(Spanned::no_span(AstExpr::Ident("n".into()))),
                op: AstBinOp::Sub,
                rhs: Box::new(Spanned::no_span(AstExpr::Literal(AstLit::Int("1".into())))),
            })],
        )],
    );

    let checker = TotalityChecker::new();
    let span_a = 0..10;
    let span_b = 10..20;
    let fn_defs: Vec<(&AstFnDef, &Range<usize>)> = vec![(&fn_a, &span_a), (&fn_b, &span_b)];
    let errors = checker.check_mutual_recursion(&fn_defs);
    assert!(
        errors.is_empty(),
        "mutual recursion with decreases should pass: {errors:?}"
    );
}

#[test]
fn totality_structural_recursion_on_list() {
    // list_len(xs) with decreases xs, recursive call list_len(xs.tail)
    let fn_def = make_fn_def(
        "list_len",
        vec![("xs", &["List"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("xs".into()))),
            ensures_with_recursive_call(
                "list_len",
                vec![Spanned::no_span(AstExpr::Field(
                    Box::new(Spanned::no_span(AstExpr::Ident("xs".into()))),
                    "tail".into(),
                ))],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..20));
    assert!(
        errors.is_empty(),
        "structural recursion on .tail should pass: {errors:?}"
    );
}

#[test]
fn totality_structural_recursion_on_tree() {
    // tree_depth(node) with decreases node, calls tree_depth(node.left)
    let fn_def = make_fn_def(
        "tree_depth",
        vec![("node", &["Tree"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("node".into()))),
            ensures_with_recursive_call(
                "tree_depth",
                vec![Spanned::no_span(AstExpr::Field(
                    Box::new(Spanned::no_span(AstExpr::Ident("node".into()))),
                    "left".into(),
                ))],
            ),
        ],
    );
    let checker = TotalityChecker::new();
    let (errors, _pending) = checker.check_function_totality(&fn_def, &(0..20));
    assert!(
        errors.is_empty(),
        "structural recursion on .left should pass: {errors:?}"
    );
}

#[test]
fn totality_extract_no_decreases() {
    let fn_def = make_fn_def("f", vec![], vec![]);
    let checker = TotalityChecker::new();
    assert!(checker.extract_decreases_measure(&fn_def).is_none());
}

#[test]
fn totality_extract_single_decreases() {
    let fn_def = make_fn_def(
        "f",
        vec![("n", &["Nat"])],
        vec![decreases_clause(Spanned::no_span(AstExpr::Ident(
            "n".into(),
        )))],
    );
    let checker = TotalityChecker::new();
    let measure = checker.extract_decreases_measure(&fn_def);
    assert!(
        matches!(measure, Some(DecreasesMeasure::Natural(_))),
        "single decreases should yield Natural"
    );
}

#[test]
fn totality_extract_lexicographic_decreases() {
    let fn_def = make_fn_def(
        "f",
        vec![("m", &["Nat"]), ("n", &["Nat"])],
        vec![
            decreases_clause(Spanned::no_span(AstExpr::Ident("m".into()))),
            decreases_clause(Spanned::no_span(AstExpr::Ident("n".into()))),
        ],
    );
    let checker = TotalityChecker::new();
    let measure = checker.extract_decreases_measure(&fn_def);
    assert!(
        matches!(measure, Some(DecreasesMeasure::Lexicographic(ref v)) if v.len() == 2),
        "two decreases should yield Lexicographic(2)"
    );
}

#[test]
fn totality_checker_debug() {
    let checker = TotalityChecker::new();
    let dbg = format!("{checker:?}");
    assert!(dbg.contains("TotalityChecker"));
}

#[test]
fn totality_checker_default() {
    let checker = TotalityChecker::default();
    assert!(!checker.is_partial(&make_fn_def("f", vec![], vec![])));
}

// -----------------------------------------------------------------------