-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcross_function.ail
More file actions
59 lines (52 loc) · 1.77 KB
/
Copy pathcross_function.ail
File metadata and controls
59 lines (52 loc) · 1.77 KB
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
-- examples/runnable/contracts/cross_function.ail
-- Cross-function contract verification demonstration
-- Shows Z3 inlining callee definitions via (define-fun) to verify callers
-- Demonstrates: transitive calls, callee chain, compositional verification
module examples/runnable/contracts/cross_function
-- Delivery pricing with layered business rules
export type Region = DOMESTIC | INTERNATIONAL
export type Priority = EXPRESS | STANDARD | ECONOMY
-- Base shipping cost depends on region
export func baseCost(region: Region) -> int ! {}
ensures { result >= 0 }
{
match region {
DOMESTIC => 5,
INTERNATIONAL => 15
}
}
-- Priority multiplier (1x, 2x, or 3x)
export func priorityMultiplier(priority: Priority) -> int ! {}
ensures { result >= 1 }
{
match priority {
ECONOMY => 1,
STANDARD => 2,
EXPRESS => 3
}
}
-- Total shipping cost — CALLS baseCost AND priorityMultiplier
-- Z3 inlines both callees and proves result >= 0
export func shippingCost(region: Region, priority: Priority) -> int ! {}
ensures { result >= 0 }
{
baseCost(region) * priorityMultiplier(priority)
}
-- Apply a discount (clamped to never go below zero)
-- CALLS shippingCost (transitive: shippingCost -> baseCost, priorityMultiplier)
-- Z3 verifies the full 3-function call chain
export func discountedCost(region: Region, priority: Priority, discount: int) -> int ! {}
requires { discount >= 0 }
ensures { result >= 0 }
{
let total = shippingCost(region, priority);
if total >= discount then total - discount else 0
}
-- Entry point for testing
export func main() -> int ! {}
{
let domestic = shippingCost(DOMESTIC, STANDARD); -- 10
let intl = shippingCost(INTERNATIONAL, EXPRESS); -- 45
let discounted = discountedCost(DOMESTIC, ECONOMY, 3); -- 2
domestic + intl + discounted
}