Formal Methods for Software Development | TDA294/DIT271, LP1, HT2020 | ||||||||||
Exercises: First-Order Logic + JML | |||||||||||
Solutions DownloadDownload the solutions for the propositional logic exercises in KeY here.Download the solutions for the first-order logic exercises in KeY here. Download the solutions for exercises 2 to 5 here. Propositional Logic and KeYSequent CalculusProve the following formulas using the sequent calculus as presented on the slides by pen and paper. See this PDF for a reminder of the inference rules.
Using KeY
Proving with KeYHere are the KeY files for the previous exercise. Your task is now:
First-Order LogicExercise 1
JMLOnline interface for OpenJML Exercise 2Consider an array b with n integer elements. Let j and k be two indices, 0 <= j < k < n. In the questions below, by 'b[j..k]' we mean a segment of the array b (but we cannot just write 'b[j..k]' in JML).Write JML expressions which precisely describe the following: 2a 2b 2c 2d 2e 2f 2g Exercise 3Consider the class EntryList: /* A simple linked list of entries. */ public class EntryList { Object first; EntryList rest; EntryList( Object first, EntryList rest ) { this.first = first; this.rest = rest; } // some methods, among them: int size() { // ... } } Specify size() using JML. This is not as trivial as one might think. Please think about it before the exercise session. Even if you try in vain, having tried will make you remember the key idea behind the solution. Exercise 4In this assignment, we are going to specify an implementation of a Queue. We do not actually implement it. Consider the following half-done class description of a Queue: public class Queue { Object[] arr; int size; int first; int next; Queue( int max ) { // ... } public int size() { // ... } public void enqueue( Object x ) { // ... } public Object dequeue() { // ... } } Give a JML specification of the Queue. It should consist of: class invariant(s), and pre- and post-conditions for each method. Exercise 5Your task is to provide specifications for a class implementing a maze game. A maze is described as a two dimensional array, where all rows have the same number of columns. The class Maze declares
The skeleton for the Maze class: public class Maze { // CONSTANTS -- MOVE public final static int MOVE_UP = 0; public final static int MOVE_DOWN = 1; public final static int MOVE_LEFT = 2; public final static int MOVE_RIGHT = 3; // CONSTANTS -- FIELDS public final static int FREE = 0; public final static int WALL = 1; public final static int EXIT = 2; /** * The playfield is given as a rectangle where * * - Walls are represented by entries of value Maze.WALL ('1') * - The exit is represented as an entry of value Maze.EXIT ('2') * - All other entries are MAZE.FREE ('0') * - A playfield has exactly one exit. * - The first number determines the column, the second determines * the row. Row and column numbers start at 0. * - Each row has the same number of columns. */ private int[][] maze; /** * Player position: * * - The position of a player must always denote a field inside the * maze which is not a wall */ private int playerRow, playerCol; public Maze(int[][] maze, int startRow, int startCol) { this.maze = maze; // set player on start position this.playerRow = startRow; this.playerCol = startCol; } /** * Returns true if the player has reached the exit field; * the method does not affect the state. */ public boolean won() { // TO BE IMPLEMENTED throw new RuntimeException(); } /** * A move to position (newRow,newCol) is possible iff the * field is inside the maze and free (i.e. not a wall); * the method does not affect the state. */ public boolean isPossible(int newRow, int newCol) { // TO BE IMPLEMENTED throw new RuntimeException(); } /** * Takes a direction and performs the move if possible, otherwise * the player stays at the current position; the direction must be * one of the defined move directions (see constants * MOVE_xyz). The return value indicates if the move was successful. */ public boolean move(int direction) { // TO BE IMPLEMENTED throw new RuntimeException(); } } Your task is now to provide
| |||||||||||
W. Ahrendt, Oct 13, 2020 |