Whiley / RFCs

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

Sound Flow Typing for Arrays #79

Open DavePearce opened 4 years ago

DavePearce commented 4 years ago

Recently, a situation like the following arose:

type Node is { int data }
type Link is null | Node

function f(Link[] ls))
requires some { i in 0..|ls| | ls[i] is Node && ls[i].data >= 0 }

(this is an artificial example, but it serves the point)

The problem is that this cannot type check! The current work around is to do this:

property validNode(Link ls)
where (ls is Node) && (ls.data >= 0)

function f(Link[] ls))
requires some { i in 0..|ls| | validNode(ls) }

However, this is a bit ugly, let's face it. There are at least two possible solutions:

To be honest, we probably want both! Also, the general logic here applies to both references and functions. Furthermore, we could extend it beyond expressions by allowing it up to invalidation. For example:

function f(Link[] ls, int i):
    if ls[i] is Node:
        Node n = ls[i]
        ...

This should be safe since nothing has invalidated ls[i]. Specifically, any assignment to ls would invalidate it.