Formal methods build a beautiful hype that software can be verified to be correct given formal specifications and formal execution models that check if the implementation satisfies all the required constraints. A example of the math function abs() is shown below.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AbsTest { | |
public static int absSpec(int x) { | |
return x < 0 ? -x : x; | |
} | |
public static int absImpl(int x) { | |
int signbits = x >> 31; | |
int xor = x ^ signbits; | |
return xor + (signbits & 0x01); | |
} | |
public static void main(String []args) { | |
for(int i = -3; i < 5; i++) | |
System.out.printf("abs(%d): %s\n", | |
i, absImpl(i) == absSpec(i) ? "passed" : "failed"); | |
} | |
} |
No comments:
Post a Comment