dependently typed heaps
play

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 -


  1. Dependently Typed Heaps https://github.com/brunjlar/heap

  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

  3. Agenda - Motivation - Leftist Heaps - Proving Theorems in Haskell - Dependently Typed Heaps - Reflection on Results - Questions & Comments

  4. Motivation

  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.

  6. Leftist Heaps

  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.)

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

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

  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. Leftist 2 property 2 rk 2 violated! rk 3 3 2 3 2 rk 1 rk 1 rk 2 rk 2 4 6 4 8 6 rk 1 rk 1 rk 1 rk 1 rk 1 leftist not leftist

  11. Merging Leftist Heaps

  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?

  13. Proving Theorems in Haskell

  14. Technical Tools - Constraints to express statements - reified statements (dictionaries) (see “constraints” library by Kmett on Hackage) - Singleton Types (see “singletons” library by Eisenberg & Stolarek on Hackage)

  15. A Simple Example

  16. Dependently Typed Heaps

  17. Dependently Typed Heaps

  18. Type-Safe Merging

  19. Reflection on Results

  20. Benchmark

  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.

  22. Questions & Comments

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend