aprender-contracts 0.34.0

Papers to Math to Contracts in Code — YAML contract parsing, validation, scaffold generation, and Kani harness codegen for provable Rust kernels
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
    /// Verify wired test generation handles empty property names gracefully
    #[test]
    fn wired_empty_property_name() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Empty prop"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: invariant
    property: ""
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: f
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("prop_obligation_0"));
    }

    /// Verify code generation for mixed obligation types (invariant, bound, equivalence, monotonicity)
    #[test]
    fn wired_multiple_obligations_mixed() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Mixed"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: invariant
    property: "Output finite"
    tolerance: 1.0e-6
    applies_to: all
  - type: bound
    property: "Output bounded"
    applies_to: all
  - type: equivalence
    property: "SIMD matches"
    tolerance: 8.0
    applies_to: simd
  - type: monotonicity
    property: "Order preserved"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: softmax
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("Invariant:"));
        assert!(code.contains("Bound:"));
        assert!(code.contains("SIMD equivalence"));
        assert!(code.contains("Monotonicity:"));
    }

    /// Verify missing binding generates an ignored test with "no binding available"
    #[test]
    fn wired_no_binding_generates_ignored() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Test"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: invariant
    property: "output finite"
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: OTHER.yaml
    equation: f
    module_path: "test::f"
    function: f
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("no binding available"));
    }

    /// Verify idempotency code generation for a non-softmax free function
    #[test]
    fn wired_idempotency_non_softmax_free_fn() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Idemp non-softmax"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: idempotency
    property: "Idemp check"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: relu
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("relu(&x)"));
        assert!(code.contains("relu(&once)"));
    }

    /// Verify idempotency code generation for a struct method binding
    #[test]
    fn wired_idempotency_struct_method() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Idemp struct"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: idempotency
    property: "Idemp struct check"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::S"
    function: "S::forward"
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("once = x.clone()"));
        assert!(code.contains("twice = once.clone()"));
    }

    /// Verify bound obligation generates a TODO stub for struct method binding
    #[test]
    fn wired_bound_struct_method() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Bound struct"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: bound
    property: "Bounded output"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::S"
    function: "S::forward"
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("Bound:"));
        assert!(code.contains("TODO: wire up struct"));
    }

    /// Verify monotonicity code generation for a tensor method binding
    #[test]
    fn wired_monotonicity_tensor_method() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Mono tensor"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: monotonicity
    property: "Order preserved"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::Tensor"
    function: "Tensor::matmul"
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("Monotonicity:"));
        assert!(code.contains("x.matmul(&x)"));
    }

    /// Verify invariant code generation includes the formal specification string
    #[test]
    fn wired_invariant_with_formal() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Formal"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: invariant
    property: "Sum is one"
    formal: "|sum - 1| < eps"
    tolerance: 1.0e-6
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: f
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("Formal: |sum - 1| < eps"));
    }

    /// Verify symmetry obligation generates code with "symmetry:" marker
    #[test]
    fn wired_symmetry_obligation() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Sym test"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: symmetry
    property: "Permutation invariant"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: f
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("symmetry:"));
    }

    /// Verify associativity obligation generates code with "associativity:" marker
    #[test]
    fn wired_associativity_obligation() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Assoc test"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: associativity
    property: "Grouping invariant"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: f
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("associativity:"));
    }

    /// Verify invariant code generation for tensor method uses self-call syntax
    #[test]
    fn wired_invariant_tensor_method() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Inv tensor"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: invariant
    property: "Output finite"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::Tensor"
    function: "Tensor::matmul"
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("x.matmul(&x)"));
        assert!(code.contains("Invariant:"));
    }

    /// Verify default tolerance of 1e-6 is used when no tolerance is specified
    #[test]
    fn wired_default_tolerance() {
        let contract_yaml = r#"
metadata:
  version: "1.0.0"
  description: "Default tol"
  references: ["Paper"]
equations:
  f:
    formula: "f(x) = x"
proof_obligations:
  - type: invariant
    property: "Finite output"
    applies_to: all
falsification_tests: []
"#;
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::f"
    function: f
    status: implemented
"#;
        let contract = parse_contract_str(contract_yaml).unwrap();
        let binding = parse_binding_str(binding_yaml).unwrap();
        let code = generate_wired_probar_tests(&contract, "test.yaml", &binding);
        assert!(code.contains("1e-6"));
    }

    /// Verify `emit_imports` deduplicates identical module paths
    #[test]
    fn emit_imports_deduplicates() {
        let binding_yaml = r#"
version: "1.0.0"
target_crate: test
bindings:
  - contract: test.yaml
    equation: f
    module_path: "test::mod1"
    function: f
    status: implemented
  - contract: test.yaml
    equation: g
    module_path: "test::mod1"
    function: g
    status: implemented
"#;
        let binding = parse_binding_str(binding_yaml).unwrap();
        let refs: Vec<&KernelBinding> = binding.bindings.iter().collect();
        let mut out = String::new();
        emit_imports(&mut out, &refs);
        assert_eq!(out.matches("use test::mod1;").count(), 1);
    }