More work on typestate. Sketched out code for computing and checking prestates and poststates. Still a long ways away.

This commit is contained in:
Tim Chevalier 2011-04-04 14:31:49 -07:00 committed by Graydon Hoare
parent 4fc8de1969
commit 86d4601827
4 changed files with 513 additions and 85 deletions

View file

@ -12,12 +12,21 @@ type precond = bitv.t; /* 1 means "this variable must be initialized"
type postcond = bitv.t; /* 1 means "this variable is initialized"
0 means "don't know about this variable */
type prestate = bitv.t; /* 1 means "this variable is definitely initialized"
0 means "don't know whether this variable is
initialized" */
type poststate = bitv.t; /* 1 means "this variable is definitely initialized"
0 means "don't know whether this variable is
initialized" */
/* named thus so as not to confuse with prestate and poststate */
type pre_and_post = rec(precond precondition, postcond postcondition);
/* FIXME: once it's implemented: */
// : ((*.precondition).nbits == (*.postcondition).nbits);
type ts_ann = pre_and_post;
type pre_and_post_state = rec(prestate prestate, poststate poststate);
type ts_ann = rec(pre_and_post conditions, pre_and_post_state states);
fn true_precond(uint num_vars) -> precond {
be bitv.create(num_vars, false);
@ -27,11 +36,16 @@ fn true_postcond(uint num_vars) -> postcond {
be true_precond(num_vars);
}
fn empty_pre_post(uint num_vars) -> @pre_and_post {
ret(@rec(precondition=true_precond(num_vars),
fn empty_pre_post(uint num_vars) -> pre_and_post {
ret(rec(precondition=true_precond(num_vars),
postcondition=true_postcond(num_vars)));
}
fn empty_states(uint num_vars) -> pre_and_post_state {
ret(rec(prestate=true_precond(num_vars),
poststate=true_postcond(num_vars)));
}
fn get_pre(&pre_and_post p) -> precond {
ret p.precondition;
}
@ -57,5 +71,10 @@ fn pps_len(&pre_and_post p) -> uint {
impure fn require_and_preserve(uint i, &pre_and_post p) -> () {
// sets the ith bit in p's pre and post
bitv.set(p.precondition, i, true);
bitv.set(p.postcondition, i, false);
}
bitv.set(p.postcondition, i, true);
}
fn implies(bitv.t a, bitv.t b) -> bool {
bitv.difference(b, a);
ret (bitv.equal(b, bitv.create(b.nbits, false)));
}