Whiley / RFCs

Request for Comment (RFC) proposals for substantial changes to the Whiley language.
3 stars 2 forks source link

Improved Property Syntax #81

Open DavePearce opened 4 years ago

DavePearce commented 4 years ago

Currently, the syntax for properties is pretty awful. Here's an example from the standard library:

// Check if two arrays equal for a given subrange
public property equals<T>(T[] lhs, T[] rhs, int start, int end)
// Arrays must be big enough to hold subrange
where |lhs| >= end && |rhs| >= end
// All items in subrange match
where all { i in start..end | lhs[i] == rhs[i] }

The essential point of properties (as it stands) are that they receive special status within the theorem prover. Specifically, properties can be inlined during the verification process. This provides significant additional expressivity over functions which are treated as uninterpreted during verification. In some sense, properties are like macros. Key points arising:

Example 1

property sum(int[] arr, int i) -> int:
    if i >= |arr|:
        0
    else:
        arr[i] + sum(arr,i+1)

Example 2

type Link is { int data, LinkedList next }
type LinkedList is null | &Link

property disjoint(LinkstList l1, LinkedList l2):
    if l1 is null || l2 is null:
       true
    else:
       l1 != l2 && disjoint(l1->next, l2)

This is an interesting example as I'm trying to use a property in an impure fashion.