Dependently Typed Heaps https://github.com/brunjlar/heap About Me - - PowerPoint PPT Presentation

dependently typed heaps
SMART_READER_LITE
LIVE PREVIEW

Dependently Typed Heaps https://github.com/brunjlar/heap About Me - - PowerPoint PPT Presentation

Dependently Typed Heaps https://github.com/brunjlar/heap About Me Lars Brnjes (PhD) (Pure) Mathematician, Lead Software Architect based in Regensburg, Germany Email: brunjlar@gmail.com Github: https://github.com/brunjlar Agenda -


slide-1
SLIDE 1

Dependently Typed Heaps

https://github.com/brunjlar/heap

slide-2
SLIDE 2

About Me

Lars Brünjes (PhD) (Pure) Mathematician, Lead Software Architect based in Regensburg, Germany Email: brunjlar@gmail.com Github: https://github.com/brunjlar

slide-3
SLIDE 3

Agenda

  • Motivation
  • Leftist Heaps
  • Proving Theorems in Haskell
  • Dependently Typed Heaps
  • Reflection on Results
  • Questions & Comments
slide-4
SLIDE 4

Motivation

slide-5
SLIDE 5

Motivation

  • Types help catching errors at compile time.
  • Some invariants cannot be expressed by “simple” types.
  • Haskell steadily moves towards dependent types.
  • I wanted to see whether it is possible to prove theorems in Haskell...
  • ...and use this to encode some non-trivial invariants.
  • Heaps seem to be a good example.
slide-6
SLIDE 6

Leftist Heaps

slide-7
SLIDE 7

Heap

We consider binary trees whose nodes carry some payload and a priority (a natural number): data Tree a = Empty | Node Natural a (Tree a) (Tree a) Such a tree is a heap if it satisfies the heap property: The priority of a node is not bigger than the priority of any of its children. (So in a non-empty heap, the root has minimal priority.)

slide-8
SLIDE 8

Heap

2 2 3 3 6 4 6 4 7 This is a heap. This is not a heap. Heap property violated!

slide-9
SLIDE 9

Rank

The rank of a heap is the length of its right spine. 2 2 3 6 4 2 2 3 6 4 Rank 2 Rank 3

slide-10
SLIDE 10

Leftist Heap

A heap is leftist if in each node, the rank of the left child is not smaller than the rank of the right child. 2

rk 2

2

rk 1

3

rk 1

6

rk 1

4

rk 1

2

rk 3

2

rk 2

3

rk 2

6

rk 1

4

rk 1

leftist not leftist Leftist property violated! 8

rk 1

slide-11
SLIDE 11

Merging Leftist Heaps

slide-12
SLIDE 12

Weakly typed heaps

  • Neither heap property nor leftist property are enforced by the compiler.
  • “Classical solution”: smart constructors, but those only catch errors at

runtime.

  • Algorithms like “merge” can easily be done wrong.
  • Can we define leftist heaps in such a way that the compiler prevents us from

constructing “illegal” heaps?

slide-13
SLIDE 13

Proving Theorems in Haskell

slide-14
SLIDE 14

Technical Tools

  • Constraints to express statements
  • reified statements (dictionaries)

(see “constraints” library by Kmett

  • n Hackage)
  • Singleton Types (see “singletons” library by Eisenberg & Stolarek on Hackage)
slide-15
SLIDE 15

A Simple Example

slide-16
SLIDE 16

Dependently Typed Heaps

slide-17
SLIDE 17

Dependently Typed Heaps

slide-18
SLIDE 18

Type-Safe Merging

slide-19
SLIDE 19

Reflection on Results

slide-20
SLIDE 20

Benchmark

slide-21
SLIDE 21

Type Safety

  • Haskell’s type system is powerful enough to encode non-trivial invariants on

the type-level.

  • Haskell functions can serve as “proofs” for statements about types.
  • Caution: Compiler gives no termination guarantee, so would accept a

non-terminating proof.

slide-22
SLIDE 22

Questions & Comments