Dependently Typed Data Structures Hongwei Xi presented by James Hook Pacific Software Research Center Oregon Graduate.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Dependent Types in Practical Programming Hongwei Xi Oregon Graduate Institute.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Dependently Typed Pattern Matching Hongwei Xi Boston University.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
May 1, 2003May 1, Imperative Programming with Dependent Types Hongwei Xi Boston University.
Facilitating Program Verification with Dependent Types Hongwei Xi Boston University.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Facilitating Programming Verification with Dependent Types Hongwei Xi University of Cincinnati.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
October 16, Algorithms and Data Structures Lecture IX Simonas Šaltenis Aalborg University
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Data Structures AVL Trees.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
More Trees. Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
G64ADS Advanced Data Structures
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
CSCI Trees and Red/Black Trees
Red-Black Trees.
Imperative Programming with Dependent Types
Design and Analysis of Algorithms
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees.
CMSC 341 (Data Structures)
UMBC CSMC 341 Red-Black-Trees-1
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and Black.
Red-Black Trees.
Algorithms and Data Structures Lecture VIII
Red-Black Trees.
Red-black tree properties
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Properties of Red-black trees
Presentation transcript:

Dependently Typed Data Structures Hongwei Xi presented by James Hook Pacific Software Research Center Oregon Graduate Institute

Hongwei’s Program Extend ML-like typechecking with computationally tractable mechanisms modeled on dependent type systems to get more expressive type systems without sacrificing practicality. Dependent ML --- his thesis at CMU de Caml --- a prototype implementation based on Caml

This Talk Apply “Hongwei’s types” to “Chris’ programs” Goal is to express invariants of data structures in the extended type system I will focus on red black trees

The Types ML types indexed by integers and integer expressions datatype ‘a list with nat = nil(0) | {n:nat} cons(n+1) of ‘a * ‘a list(n) Nil is the list of length 0 Cons builds a list of length n+1 from an element and a list of length n {}’s are explicit universal quantifiers for constraints Index expression

Example let rec append = function ([], ys) -> ys | (x :: xs, ys) -> x :: append(xs, ys) withtype {m:nat}{n:nat} ‘a list(m) * ‘a list(n) -> ‘a list(m+n) Given a list of length m and a list of length n append produces a list of length m+n.

Typechecking de Caml source Caml type check plus constraint generation ML-style type errors Constraint Solver Constraint failures

Red Black Trees Balanced, ordered, binary trees, values at nodes Every node is colored red or black Balance Invariant: –No red node has a red node as a child –There is an equal number of black nodes on all root/leaf paths

Example Insert 8 88 Violates equal number of black nodes on all leaf root paths 8 All insertions must be red to maintain the global invariant of equal numbers of black nodes on leaf root paths

Example Insert 2 Now we have a “red red” violation Red red violations are repairable; they are limited to the path to the root

Repairing a Red Red Violation x z y a bc d y xz abcd

y xz abcd x z y a bc d x z y a b c d z y x a b c d z x y a bc d

Example Insert 2 x z y a bc d y xz abcd 2 1 3

Example Insert

Okasaki’s Solution data Color = R | B data RedBlackSet a = E | T Color (RedBlackSet a) a (RedBlackSet a) The Types Capture the Tree Structure:

Okasaki’s Solution The balance function does the rotation if there is a red red conflict y xz abcd x z y a bc d x z y a b c d z y x a b c d z x y a bc d balance :: Color -> RedBlackSet a -> a -> RedBlackSet a -> RedBlackSet a balance B (T R (T R a x b) y c) z d = T R (T B a x b) y (T B c z d) balance B (T R a x (T R b y c)) z d = T R (T B a x b) y (T B c z d) balance B a x (T R (T R b y c) z d) = T R (T B a x b) y (T B c z d) balance B a x (T R b y (T R c z d)) = T R (T B a x b) y (T B c z d) balance color a x b = T color a x b When given two trees of equal black height these clauses produce a tree of black height one greater

data Color = R | B data RedBlackSet a = E | T Color (RedBlackSet a) a (RedBlackSet a) balance :: Color -> RedBlackSet a -> a -> RedBlackSet a -> RedBlackSet a insert :: a -> RedBlackSet a -> RedBlackSet a insert x s = T B a y b where T _ a y b = ins s ins E = T R E x E ins color a y b) | x < y = balance color (ins a) y b | x > y = balance color a y (ins b) | True = s Okasaki’s Solution Where do red red conflicts come from? To answer this question we expand ins to distinguish on color and we recall that balance only rotates trees with black roots

Okasaki’s Solution y a b ins a balance :: Color -> RedBlackSet a -> a -> RedBlackSet a -> RedBlackSet a insert :: Ord a => a -> RedBlackSet a -> RedBlackSet a insert x s = T B a y b where T _ a y b = ins s ins E = T R E x E ins B a y b) | x < y = balance B (ins a) y b | x > y = balance B a y (ins b) | True = s ins R a y b) | x < y = T R (ins a) y b | x > y = T R a y (ins b) | True = s ins on black yields red black tree ins on red may yield one red red violation at the root insert always yields a good tree because it recolors root

Hongwei’s Solution data Color = R | B data RedBlackSet a = E | T Color (RedBlackSet a) a (RedBlackSet a) sort color == {a:int | 0 <= a <= 1};; datatype tree with (color, nat, nat) = (* color, black height, violation *) E(0, 0, 0) | {cl:color}{cr:color}{bh:nat} B(0, bh+1, 0) of tree(cl, bh, 0) * key * tree(cr, bh, 0) | {cl:color}{cr:color}{bh:nat}{sl:nat}{sr:nat} R(1, bh, cl+cr) of tree(cl, bh, 0) * key * tree(cr, bh, 0) ;; General Approach: Index the datatype to record “black height” and to detect “red red violations” Detecting red red violations in the type indexs requires having an arithmetic encoding of color in the type index set as well as the value distinction in the program

Reading the Datatype sort color == {a:int | 0 <= a <= 1};; datatype tree with (color, nat, nat) = (* color, black height, violation *) E(0, 0, 0) | {cl:color}{cr:color}{bh:nat} B(0, bh+1, 0) of tree(cl, bh, 0) * key * tree(cr, bh, 0) | {cl:color}{cr:color}{bh:nat} R(1, bh, cl+cr) of tree(cl, bh, 0) * key * tree(cr, bh, 0) ;; Convention: 0 = black, 1 = red (permits detecting red red with +) The empty tree is black, has height 0, and no violations A black node of height bh + 1 with no violations is constructed from two nodes of arbitrary color of height bh A red node of height bh + 1 is constructed from two nodes of arbitrary color of height bh. The violation value of the node is the sum of the colors of its children, I.e. non-zero if either child is red. The children contain no violations.

Hongwei’s Solution let balance = function (R(R(a, x, b), y, c), z, d) -> R(B(a, x, b), y, B(c, z, d)) | (R(a, x, R(b, y, c)), z, d) -> R(B(a, x, b), y, B(c, z, d)) | (a, x, R(R(b, y, c), z, d)) -> R(B(a, x, b), y, B(c, z, d)) | (a, x, R(b, y, R(c, z, d))) -> R(B(a, x, b), y, B(c, z, d)) | (a, x, b) -> B(a, x, b) withtype {cl:color}{cr:color}{bh:nat}{vl: nat}{vr:nat | vl+vr <= 1} tree(cl, bh, vl) * key * tree(cr, bh, vr) -> [c:color] tree(c, bh+1, 0) ;; balance :: Color -> RedBlackSet a -> a -> RedBlackSet a -> RedBlackSet a Recall from Okasaki’s solution: As in red red analysis, Hongwei only calls balance on black nodes, hence Color argument is eliminated

Type of Balance withtype {cl:color}{cr:color}{bh:nat}{vl: nat}{vr:nat | vl+vr <= 1} tree(cl, bh, vl) * key * tree(cr, bh, vr) -> [c:color] tree(c, bh+1, 0) Give two trees of equal height bh, arbitrary color, and at most one red red violation, balance yields a tree with unspecified color of height bh+1 containing no violations

Hongwei’s Solution let rec ins = function E -> R(E, x, E) | B(a, y, b) -> if x < y then balance(ins a, y, b) else if y < x then balance(a, y, ins b) else raise Item_already_exists | R(a, y, b) -> if x < y then R(ins a, y, b) else if y < x then R(a, y, ins b) else raise Item_already_exists withtype {c:color}{bh:nat} tree(c, bh, 0) -> [c':color][v:nat | v <= c] tree(c', bh, v) ins is essentially as before The type of ins is now dramatically more expressive! ins produces a tree of unspecified color with height equal to its input. If the root of the argument was red the tree may contain a violation. If it was black it contains no violations.

Hongwei’s Solution let insert x t = let rec ins =... withtype {c:color}{bh:nat} tree(c, bh, 0) -> [c':color][v:nat | v <= c] tree(c', bh, v) in match ins t with R(a, y, b) -> B(a, y, b) | t -> t withtype {c:color}{bh:nat} key -> tree(c, bh, 0) -> [bh’:nat] tree(0, bh’, 0) ;; Insert is also essentially unchanged The type of insert now shows that both invariants are maintained by the operation. In particular, given a key and a red black tree of any height containing no violations, insert produces a tree with black root of some height containing no violations.

The Paper Braun Trees –The type of size guarantees it computes the size Random-Access Lists Binomial Heaps

Limitations Sometimes the programmer knows more than de Caml can figure out Not all integer constraints are decidable

Related Work  Refinement types (Freeman, Davies, Pfenning)  Indexed types (Zenger)  Sized types (Hughes, Pareto, Sabry)  Nested datatypes (Bird & Meertens, Okasaki, Hinze, etc)

Contacting Hongwei http// Tel