Hari Prashanth and Sam Tobin-Hochstadt Northeastern...

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

Transcript of Hari Prashanth and Sam Tobin-Hochstadt Northeastern...

Page 1: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Functional Data Structures for Typed Racket

Hari Prashanth and Sam Tobin-Hochstadt

Northeastern University

1

Page 2: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Motivation

Typed Racket has very few data structures

2

Page 3: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Motivation

Typed Racket has very few data structures

Lists

3

Page 4: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Motivation

Typed Racket has very few data structures

Lists

Vectors

4

Page 5: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Motivation

Typed Racket has very few data structures

Lists

Vectors

Hash Tables

5

Page 6: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Motivation

Typed Racket has very few data structures

Lists

Vectors

Hash Tables

Practical use of Typed Racket

6

Page 7: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Outline

Motivation

Typed Racket in a Nutshell

Purely Functional Data Structures

Benchmarks

Typed Racket Evaluation

Conclusion

7

Page 8: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 9: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 10: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 11: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Data definition in Racket

#lang racket ; Data definition of tree of integers ; A Tree is one of; - null; - BTree (define-struct BTree (left

elemright))

; left and right are of type Tree; elem is an Integer

11

Page 12: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 13: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 14: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Outline

Motivation

Typed Racket in a Nutshell

Purely Functional Data Structures

Benchmarks

Typed Racket Evaluation

Conclusion

14

Page 15: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Destructive and Non-destructive update

e

15

Page 16: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Destructive and Non-destructive update

e ⇒

Destructive update

16

Page 17: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Destructive and Non-destructive update

e ⇒

Non-destructive update

17

Page 18: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Functional Queue

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

[rear : (Listof A)]))

18

Page 19: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Functional Queue

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

[rear : (Listof A)]))

19

Page 20: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 21: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Functional Queue

Queue q

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

21

Page 22: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

22

Page 23: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

(: val : (Promise Exact-Rational)) (define val (delay (/ 5 0)))

23

Page 24: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

Streams

(define-type (Stream A) (Pair A (Promise (Stream A))))

24

Page 25: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 26: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 27: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

(make-Queue (stream-append front (stream-reverse rear))

(+ lenf lenr) null 0)

27

Page 28: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

Amortized running time of O(1) for the operations

enqueue, dequeue and head

28

Page 29: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Real-Time Queues [Hood & Melville 81]

29

Page 30: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Real-Time Queues [Hood & Melville 81]

Eliminating amortization by Scheduling

30

Page 31: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Real-Time Queues [Hood & Melville 81]

Eliminating amortization by Scheduling

Banker’s Queue - reverse is a forced completely

31

Page 32: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 33: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 34: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

Nat is one of- 0- (add1 Nat)

List is one of- null- (cons elem List)

34

Page 35: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 36: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

36

Page 37: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

(define-type (Digit A) (U Zero (One A)))

37

Page 38: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-struct: Zero ())

38

Page 39: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-struct: Zero ())

(define-struct: (A) One ([fst : (Tree A)]))

39

Page 40: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (Tree A) (U (Leaf A) (Node A)))

40

Page 41: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (Tree A) (U (Leaf A) (Node A)))

(define-struct: (A) Leaf ([fst : A]))

41

Page 42: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 43: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

43

Page 44: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

44

Page 45: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

45

Page 46: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

46

Page 47: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

47

Page 48: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

48

Page 49: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Binary Random Access Lists [Okasaki 1998]

(define-type (RAList A) (Listof (Digit A)))

49

Page 50: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 51: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 52: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

VLists [Bagwell 2002]

List with one element - 6

52

Page 53: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

VLists [Bagwell 2002]

cons 5 and 4 to the previous list

53

Page 54: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

VLists [Bagwell 2002]

cons 3 and 2 to the previous list

54

Page 55: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

VLists [Bagwell 2002]

cdr of the previous list

55

Page 56: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

VLists [Bagwell 2002]

Random access takes O(1) average and O(log n) inworst-case.

56

Page 57: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 58: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Our library

Library has 30 data structures

58

Page 59: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Our library

Library has 30 data structures

Data structures have several utility functions

59

Page 60: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Our library

Library has 30 data structures

Data structures have several utility functions

Our implementations follows the original work

60

Page 61: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Outline

Motivation

Typed Racket in a Nutshell

Purely Functional Data Structures

Benchmarks

Typed Racket Evaluation

Conclusion

61

Page 62: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Benchmarks

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

62

Page 63: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Benchmarks

63

Page 64: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Benchmarks

64

Page 65: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Benchmarks

65

Page 66: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Benchmarks

66

Page 67: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Outline

Motivation

Typed Racket in a Nutshell

Purely Functional Data Structures

Benchmarks

Typed Racket Evaluation

Conclusion

67

Page 68: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

ML to Typed Racket

ML idioms can be easily ported to Typed Racket

68

Page 69: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 70: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 71: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Optimizer in Typed Racket

Optimizer based on type information

71

Page 72: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Optimizer in Typed Racket

72

Page 73: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Polymorphic recursion

(define-type (Seq A) (Pair A (Seq (Pair A A))))

Non-uniform type

73

Page 74: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

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

Page 75: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Conclusion

Typed Racket is useful for real-world software.

Functional data structures in Typed Racket are useful andperformant.

A comprehensive library of data structures is now available.

75

Page 76: Hari Prashanth and Sam Tobin-Hochstadt Northeastern Universityschemeworkshop.org/2010/prashanth-tobin-hochstadt-presentation.p… · Hari Prashanth and Sam Tobin-Hochstadt Northeastern

Thank you...

Library is available for download from

http://planet.racket-lang.org/

76