Download Implementation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

B-tree wikipedia , lookup

Quadtree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Interval tree wikipedia , lookup

Red–black tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Proving Correctness of Data
Structures using Agda
Why Agda?
Agda is a dependently typed, functional programming language
i.e. it allows definition of data types dependent on other
defined types.
It provides an interactive environment for proof
verification and analysis as well. Here, codes can be
‘loaded’ only when they adhere to the typing rules.
Project Overview
Initially, our work involved the analysis of data structures
described in the following two papers1) Oster, Julien. "An Agda implementation of deletion in
Left-leaning Red-Black trees." (2011).
2) Stolarek, Jan. "Verifying weight biased leftist heaps
using dependent types."
Project Overview
Part II involved attempts at constructing data structures
like Linked Lists, Binary Search Trees and AVL Trees and
implementing their update, query and deletion operations in
Agda such that the structural restraints are conserved in
the implementation.
We used the structures and functions provided in the papers
as reference to go through our implementation.
Data Structures Analysed
●
Left Leaning Red Black Trees
A left leaning red black tree is a type of a self balancing binary search tree. It is a
variant of a red-black tree and guarantees the asymptotic complexity for operation, but is
designed to be easier to implement.
●
Weight Biased Leftist Heap
A weight biased leftist heap is a normal heap with an additional constraint that the size
of the left subheap is greater than the size of the right subheap.
Source: Wikipedia
Weight Biased Leftist Heaps
Heaps are used for maintaining Priority Queues.
Each node of a Weight Biased Leftist Heaps should adhere to
the following 2 properties :-
1. Priority property : Priority of node > Priority of
children
2. Rank Property : Size of Left Child >= Size of Right Child
Implementation
makeT function simply takes 2 heaps and a priority for the
node itself, and whichever heap has a smaller rank is made
the right sub-heap.
Implementation
merge function takes 2 heaps and creates a new heap out of
them, taking care that the rank and priority properties are
conserved.
Implementation
data Heap : Priority → Set where
empty : {n : Priority} → Heap n
node : {n : Priority} → (p : Priority) → Rank → p ≥ n → Heap
p → Heap p → Heap n
Left Leaning Red Black Trees
In an LLRBT, only the left child of a black node can be red
in colour. All the right children are necessarily black.
Hence, the tree is ‘left leaning’.
The rest of the properties are the same as that of an RBT.
Implementation
Here, correctness is used as a technique to embed all
invariants that make up a valid Left-leaning Red-Black tree
in the data type and its constructors itself, unlike the
previous example.
Thus, every function that creates or transforms LLRB trees
using this data type will only type check if every invariant
is proven as part of its definition.
Implementation
Here the _isrightof_ and _isleftof_ functions return a set
and NOT a bool.
Implementation
_⇒’_ and _⇒l_ functions-
Implementation
The operations are as follows:
keep - Keep the current bound and move on to the next.
skip - Skip the current bound, because it is not needed anymore (possibly because its node
has been moved out of the path, or completely removed). The current bound is now the
formerly following bound.
cover - Insert a new bound after the current bound, because a new node appeared along the
path to the root (similarly to above, it may have been moved in or is a new node). This is
allowed in the case that the current bound is already a better bound than the new one: Let
a be any lower bound for x. If b ➔ a, then b ➔ x due to transitivity, and b is also a
lower bound for x. Analog with upper bounds. This operation requires us to specify a proof
of b ➔ a. The current bound is still the former, better bound, so that it may be involved
in another step.
■ -
Keep the rest of the bounds, starting from the current one, as it is.
Implementation
lower: c :: d :: β
upper: f :: γ
lower: b :: c :: d :: β
upper: γ
For the above transformations, the transformation rules will be cover b<c , ■ , for the lowerbounds list
skip ■ , for the upperbounds list
Implementation
The [[_]]r function, basically changes the x is leftof
parameter to x is leftof ’ parameter of the tree’s node.
Similar function is implemented by [[_]]l as well.
Implementation
Function extractminR- extracts the minimum element of the
heap from a red-rooted tree
Binary Search Trees
We implement binary search trees using two methods, one
where the data type itself does not ensure that the priority
property of the BST is conserved and another where the
definition itself enforces the BST property through typechecking.
Unverified Definition
Implementation
Insert operation-
A simple check-
Implementation
Deletion in BST-
Verified definition
Here the BST property is enforced by type-checking-
Implementation
In-order traversal
Implementation
Searching for an element-
Current status-Insertion
Insert function-