Functional Data Structures for Typed Racket. Hari Prashanth and Sam Tobin-Hochstadt Northeastern University

Similar documents
Typed Racket: Racket with Static Types

The Typed Racket Guide

The Typed Racket Reference

Typed Scheme: Scheme with Static Types

Advanced Programming Handout 5. Purely Functional Data Structures: A Case Study in Functional Programming

Advanced Programming Handout 6. Purely Functional Data Structures: A Case Study in Functional Programming

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Racket: Modules, Contracts, Languages

Objectives. Upon completion you will be able to:

Logic - CM0845 Introduction to Haskell

The Typed Racket Guide

The Role of Lazy Evaluation in Amortized Data Structures

Extensible Pattern Matching

Languages as Libraries

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

SYSC 2006 Winter 2012 Linear Collections: Queues

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

COS 226 Algorithms and Data Structures Fall Midterm

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

1 P age DS & OOPS / UNIT II

CS450 - Structure of Higher Level Languages

Name: After line 1: After line 3: After line 5: After line 8: After line 11:

Lecture Notes on Priority Queues

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Streams and Evalutation Strategies

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Data Structures Question Bank Multiple Choice

CS350: Data Structures Heaps and Priority Queues

Problem 1: Binary Trees: Flatten

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

DIT960 Datastrukturer

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.

Overview. CS301 Session 3. Problem set 1. Thinking recursively

ITI Introduction to Computing II

Course Review. Cpt S 223 Fall 2009

Part I: Short Answer (12 questions, 65 points total)

Linked Data Structures. Linked lists. Readings: CP:AMA The primary goal of this section is to be able to use linked lists and trees.

Functional Programming. Pure Functional Programming

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

Balanced Trees. Nate Foster Spring 2018

Section 5.3: Event List Management

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

INSTITUTE OF AERONAUTICAL ENGINEERING

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

Outline for Today. Analyzing data structures over the long term. Why could we construct them in time O(n)? A simple and elegant queue implementation.

MLR Institute of Technology

8. Binary Search Tree

(add1 3) 4 (check-expect (add1 3) 4)

Balanced Trees. Prof. Clarkson Fall Today s music: Get the Balance Right by Depeche Mode

Honu. Version November 6, 2010

CSU2510H Exam 1 Practice Problems

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Module 9: Binary trees

COMP 250 Midterm #2 March 11 th 2013

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

How to Design Programs

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

THE UNIVERSITY OF WESTERN AUSTRALIA

CS 314 Principles of Programming Languages

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CS 314 Principles of Programming Languages. Lecture 16

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Data: Data Structures

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Syntax Color: Utilities

Quiz 1. Queues , Fall 2007 Recitation 10 Solutions 1

CMSC 631 Program Analysis and Understanding. Dynamic Typing, Contracts, and Gradual Typing

ITERATORS AND STREAMS 9

COP 3502 (Computer Science I) Test #3: Data Structures Date: 11/1/2013 VERSION A

Balanced Trees Part Two

Lecture 17 Notes Priority Queues

Course Review for Finals. Cpt S 223 Fall 2008

182 review 1. Course Goals

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Scheme as implemented by Racket

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Lecture 17 Priority Queues

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

CS 216 Exam 1 Fall SOLUTION

STACKS AND QUEUES. Problem Solving with Computers-II

Functional Programming. Pure Functional Languages

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Lecture Notes on Binary Search Trees

Types of recursion. Readings: none. In this module: a glimpse of non-structural recursion. CS 135 Winter : Types of recursion 1

Module 8: Binary trees

CS115 - Module 10 - General Trees

Balanced Trees. Nate Foster Spring 2019

Transcription:

Functional Data Structures for Typed Racket Hari Prashanth and Sam Tobin-Hochstadt Northeastern University 1

Motivation Typed Racket has very few data structures 2

Motivation Typed Racket has very few data structures Lists 3

Motivation Typed Racket has very few data structures Lists Vectors 4

Motivation Typed Racket has very few data structures Lists Vectors Hash Tables 5

Motivation Typed Racket has very few data structures Lists Vectors Hash Tables Practical use of Typed Racket 6

Outline Motivation Typed Racket in a Nutshell Purely Functional Data Structures Benchmarks Typed Racket Evaluation Conclusion 7

Function definition in Racket #lang racket ; Computes the length of a given list of elements ; length : list-of-elems -> natural (define (length list) (if (null? list) 0 (add1 (length (cdr list))))) 8

Function definition in Typed Racket #lang typed/racket ; Computes the length of a given list of integers (: length : (Listof Integer) -> Natural) (define (length list) (if (null? list) 0 (add1 (length (cdr list))))) 9

Function definition in Typed Racket #lang typed/racket ; Computes the length of a given list of elements (: length : (All (A) ((Listof A) -> Natural))) (define (length list) (if (null? list) 0 (add1 (length (cdr list))))) 10

Data definition in Racket #lang racket ; Data definition of tree of integers ; A Tree is one of ; - null ; - BTree (define-struct BTree (left elem right)) ; left and right are of type Tree ; elem is an Integer 11

Data definition in Typed Racket #lang typed/racket ; Data definition of tree of integers (define-type Tree (U Null BTree)) (define-struct: BTree ([left : Tree] [elem : Integer] [right : Tree])) 12

Data definition in Typed Racket #lang typed/racket ; Polymorphic definition of Tree (define-type (Tree A) (U Null (BTree A))) (define-struct: (A) BTree ([left : (Tree A)] [elem : A] [right : (Tree A)])) 13

Outline Motivation Typed Racket in a Nutshell Purely Functional Data Structures Benchmarks Typed Racket Evaluation Conclusion 14

Destructive and Non-destructive update e 15

Destructive and Non-destructive update e Destructive update 16

Destructive and Non-destructive update e Non-destructive update 17

Functional Queue (define-struct: (A) Queue ([front : (Listof A)] [rear : (Listof A)])) 18

Functional Queue (define-struct: (A) Queue ([front : (Listof A)] [rear : (Listof A)])) 19

Functional Queue (: dequeue : (All (A) ((Queue A) -> (Queue A)))) (define (dequeue que) (let ([front (cdr (Queue-front que))] [rear (Queue-rear que)]) (if (null? front) (Queue (reverse rear) null) (Queue front rear)))) 20

Functional Queue Queue q (for ([id (in-range 100)]) (dequeue q)) 21

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem 22

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem (: val : (Promise Exact-Rational)) (define val (delay (/ 5 0))) 23

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem Streams (define-type (Stream A) (Pair A (Promise (Stream A)))) 24

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem (define-struct: (A) Queue ([front : (Stream A)] [lenf : Integer] [rear : (Stream A)] [lenr : Integer])) Invariant lenf >= lenr 25

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem (: check : (All (A) (Stream A) Integer (Stream A) Integer -> (Queue A))) (define (check front lenf rear lenr) (if (>= lenf lenr) (make-queue front lenf rear lenr) (make-queue (stream-append front (stream-reverse rear)) (+ lenf lenr) null 0))) 26

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem (make-queue (stream-append front (stream-reverse rear)) (+ lenf lenr) null 0) 27

Banker s Queue [Okasaki 1998] Lazy evaluation solves this problem Amortized running time of O(1) for the operations enqueue, dequeue and head 28

Real-Time Queues [Hood & Melville 81] 29

Real-Time Queues [Hood & Melville 81] Eliminating amortization by Scheduling 30

Real-Time Queues [Hood & Melville 81] Eliminating amortization by Scheduling Banker s Queue - reverse is a forced completely 31

Real-Time Queues [Hood & Melville 81] Eliminating amortization by Scheduling (: rotate : (All (A) ((Stream A) (Listof A) (Stream A) -> (Stream A)))) (define (rotate front rear accum) (if (empty-stream? front) (stream-cons (car rear) accum) (stream-cons (stream-car front) (rotate (stream-cdr front) (cdr rear) (stream-cons (car rear) accum))))) Incremental reversing 32

Real-Time Queues [Hood & Melville 81] Eliminating amortization by Scheduling Worst-case running time of O(1) for the operations enqueue, dequeue and head 33

Binary Random Access Lists [Okasaki 1998] Nat is one of - 0 - (add1 Nat) List is one of - null - (cons elem List) 34

Binary Random Access Lists [Okasaki 1998] Nat is one of - 0 - (add1 Nat) List is one of - null - (cons elem List) cons corresponds to increment cdr corresponds to decrement append corresponds to addition 35

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 36

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) (define-type (Digit A) (U Zero (One A))) 37

Binary Random Access Lists [Okasaki 1998] (define-struct: Zero ()) 38

Binary Random Access Lists [Okasaki 1998] (define-struct: Zero ()) (define-struct: (A) One ([fst : (Tree A)])) 39

Binary Random Access Lists [Okasaki 1998] (define-type (Tree A) (U (Leaf A) (Node A))) 40

Binary Random Access Lists [Okasaki 1998] (define-type (Tree A) (U (Leaf A) (Node A))) (define-struct: (A) Leaf ([fst : A])) 41

Binary Random Access Lists [Okasaki 1998] (define-type (Tree A) (U (Leaf A) (Node A))) (define-struct: (A) Leaf ([fst : A])) (define-struct: (A) Node ([size : Integer] [left : (Tree A)] [right : (Tree A)])) 42

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 43

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 44

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 45

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 46

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 47

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 48

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) 49

Binary Random Access Lists [Okasaki 1998] (define-type (RAList A) (Listof (Digit A))) Worst-case running time of O(log n) for the operations cons, car, cdr, lookup and update 50

VLists [Bagwell 2002] (define-struct: (A) Base ([previous : (U Null (Base A))] [elems : (RAList A)])) (define-struct: (A) VList ([offset : Natural] [base : (Base A)] [size : Natural])) 51

VLists [Bagwell 2002] List with one element - 6 52

VLists [Bagwell 2002] cons 5 and 4 to the previous list 53

VLists [Bagwell 2002] cons 3 and 2 to the previous list 54

VLists [Bagwell 2002] cdr of the previous list 55

VLists [Bagwell 2002] Random access takes O(1) average and O(log n) in worst-case. 56

Our library Library has 30 data structures which include Variants of Queues Variants of Deques Variants of Heaps Variants of Lists Red-Black Trees Tries Sets Hash Lists 57

Our library Library has 30 data structures 58

Our library Library has 30 data structures Data structures have several utility functions 59

Our library Library has 30 data structures Data structures have several utility functions Our implementations follows the original work 60

Outline Motivation Typed Racket in a Nutshell Purely Functional Data Structures Benchmarks Typed Racket Evaluation Conclusion 61

Benchmarks (foldl enqueue que list-of-100000-elems) 62

Benchmarks 63

Benchmarks 64

Benchmarks 65

Benchmarks 66

Outline Motivation Typed Racket in a Nutshell Purely Functional Data Structures Benchmarks Typed Racket Evaluation Conclusion 67

ML to Typed Racket ML idioms can be easily ported to Typed Racket 68

ML to Typed Racket ML idioms can be easily ported to Typed Racket type 'a Queue = int * 'a Stream * int * 'a Stream (define-struct: (A) Queue ([lenf : Integer] [front : (Stream A)] [lenr : Integer] [rear : (Stream A)])) 69

ML to Typed Racket ML idioms can be easily ported to Typed Racket type 'a Queue = 'a list * int * 'a list susp * int * 'a list (define-struct: (A) Queue ([pref : (Listof A)] [lenf : Integer] [front : (Promise (Listof A))] [lenr : Integer] [rear : (Listof A)])) 70

Optimizer in Typed Racket Optimizer based on type information 71

Optimizer in Typed Racket 72

Polymorphic recursion (define-type (Seq A) (Pair A (Seq (Pair A A)))) Non-uniform type 73

Polymorphic recursion (define-type (EP A) (U A (Pair (EP A) (EP A)))) (define-type (Seq A) (Pair (EP A) (Seq A))) Uniform type 74

Conclusion Typed Racket is useful for real-world software. Functional data structures in Typed Racket are useful and performant. A comprehensive library of data structures is now available. 75

Thank you... Library is available for download from http://planet.racket-lang.org/ 76