Functional Data Structures for Typed Racket Hari Prashanth and Sam - - PowerPoint PPT Presentation

functional data structures for typed racket
SMART_READER_LITE
LIVE PREVIEW

Functional Data Structures for Typed Racket Hari Prashanth and Sam - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

Functional Data Structures for Typed Racket

Hari Prashanth and Sam Tobin-Hochstadt Northeastern University

1

slide-2
SLIDE 2

Motivation

Typed Racket has very few data structures

2

slide-3
SLIDE 3

Motivation

Typed Racket has very few data structures Lists

3

slide-4
SLIDE 4

Motivation

Typed Racket has very few data structures Lists Vectors

4

slide-5
SLIDE 5

Motivation

Typed Racket has very few data structures Lists Vectors Hash Tables

5

slide-6
SLIDE 6

Motivation

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

6

slide-7
SLIDE 7

Outline

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

7

slide-8
SLIDE 8

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) (add1 (length (cdr list)))))

8

slide-9
SLIDE 9

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) (add1 (length (cdr list)))))

9

slide-10
SLIDE 10

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) (add1 (length (cdr list)))))

10

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

Outline

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

14

slide-15
SLIDE 15

Destructive and Non-destructive update

e

15

slide-16
SLIDE 16

Destructive and Non-destructive update

e

Destructive update

16

slide-17
SLIDE 17

Destructive and Non-destructive update

e

Non-destructive update

17

slide-18
SLIDE 18

Functional Queue

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

18

slide-19
SLIDE 19

Functional Queue

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

19

slide-20
SLIDE 20

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

slide-21
SLIDE 21

Functional Queue

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

21

slide-22
SLIDE 22

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

22

slide-23
SLIDE 23

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem (: val : (Promise Exact-Rational)) (define val (delay (/ 5 0)))

23

slide-24
SLIDE 24

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem Streams (define-type (Stream A) (Pair A (Promise (Stream A))))

24

slide-25
SLIDE 25

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

slide-26
SLIDE 26

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

slide-27
SLIDE 27

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem

(make-Queue (stream-append front (stream-reverse rear)) (+ lenf lenr) null 0)

27

slide-28
SLIDE 28

Banker’s Queue [Okasaki 1998]

Lazy evaluation solves this problem Amortized running time of O(1) for the operations enqueue, dequeue and head

28

slide-29
SLIDE 29

Real-Time Queues [Hood & Melville 81]

29

slide-30
SLIDE 30

Real-Time Queues [Hood & Melville 81]

Eliminating amortization by Scheduling

30

slide-31
SLIDE 31

Real-Time Queues [Hood & Melville 81]

Eliminating amortization by Scheduling Banker’s Queue - reverse is a forced completely

31

slide-32
SLIDE 32

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

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Binary Random Access Lists [Okasaki 1998]

Nat is one of

  • (add1 Nat)

List is one of

  • null
  • (cons elem List)

34

slide-35
SLIDE 35

Binary Random Access Lists [Okasaki 1998]

Nat is one of

  • (add1 Nat)

List is one of

  • null
  • (cons elem List)

cons corresponds to increment cdr corresponds to decrement append corresponds to addition

35

slide-36
SLIDE 36

Binary Random Access Lists [Okasaki 1998]

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

36

slide-37
SLIDE 37

Binary Random Access Lists [Okasaki 1998]

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

37

slide-38
SLIDE 38

Binary Random Access Lists [Okasaki 1998]

(define-struct: Zero ())

38

slide-39
SLIDE 39

Binary Random Access Lists [Okasaki 1998]

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

39

slide-40
SLIDE 40

Binary Random Access Lists [Okasaki 1998]

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

40

slide-41
SLIDE 41

Binary Random Access Lists [Okasaki 1998]

(define-type (Tree A) (U (Leaf A) (Node A))) (define-struct: (A) Leaf ([fst : A]))

41

slide-42
SLIDE 42

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

slide-43
SLIDE 43

Binary Random Access Lists [Okasaki 1998]

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

43

slide-44
SLIDE 44

Binary Random Access Lists [Okasaki 1998]

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

44

slide-45
SLIDE 45

Binary Random Access Lists [Okasaki 1998]

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

45

slide-46
SLIDE 46

Binary Random Access Lists [Okasaki 1998]

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

46

slide-47
SLIDE 47

Binary Random Access Lists [Okasaki 1998]

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

47

slide-48
SLIDE 48

Binary Random Access Lists [Okasaki 1998]

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

48

slide-49
SLIDE 49

Binary Random Access Lists [Okasaki 1998]

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

49

slide-50
SLIDE 50

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

slide-51
SLIDE 51

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

slide-52
SLIDE 52

VLists [Bagwell 2002]

List with one element - 6

52

slide-53
SLIDE 53

VLists [Bagwell 2002]

cons 5 and 4 to the previous list

53

slide-54
SLIDE 54

VLists [Bagwell 2002]

cons 3 and 2 to the previous list

54

slide-55
SLIDE 55

VLists [Bagwell 2002]

cdr of the previous list

55

slide-56
SLIDE 56

VLists [Bagwell 2002]

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

56

slide-57
SLIDE 57

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

slide-58
SLIDE 58

Our library

Library has 30 data structures

58

slide-59
SLIDE 59

Our library

Library has 30 data structures Data structures have several utility functions

59

slide-60
SLIDE 60

Our library

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

60

slide-61
SLIDE 61

Outline

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

61

slide-62
SLIDE 62

Benchmarks

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

62

slide-63
SLIDE 63

Benchmarks

63

slide-64
SLIDE 64

Benchmarks

64

slide-65
SLIDE 65

Benchmarks

65

slide-66
SLIDE 66

Benchmarks

66

slide-67
SLIDE 67

Outline

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

67

slide-68
SLIDE 68

ML to Typed Racket

ML idioms can be easily ported to Typed Racket

68

slide-69
SLIDE 69

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

slide-70
SLIDE 70

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

slide-71
SLIDE 71

Optimizer in Typed Racket

Optimizer based on type information

71

slide-72
SLIDE 72

Optimizer in Typed Racket

72

slide-73
SLIDE 73

Polymorphic recursion

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

73

slide-74
SLIDE 74

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

slide-75
SLIDE 75

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

slide-76
SLIDE 76

Thank you...

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

76