A Journey to Type-safe Vectors in F#

452d2be966ed20b894b1fa66ce44b1a1?s=47 cannorin
April 17, 2020

A Journey to Type-safe Vectors in F#

452d2be966ed20b894b1fa66ce44b1a1?s=128

cannorin

April 17, 2020
Tweet

Transcript

  1. A Journey to Type-safe Vectors in F# 2020/04/17 @ Kata-System

    Matsuri Online cannorin
  2. $ whoami cannorin Member of F# Software Foundation Member of

    Ionide Maintainer of / Contributing to: - fsprojects/FSharpPlus - ionide/Ionide-vim - ocaml/ocaml-lsp, etc Visit 7colou.red for more details
  3. SPOILER ALERT! • Type-level naturals and booleans • Efficient and

    type-safe vectors (fixed-length array) • Clean and intuitive syntax to use them • As a vanilla F# library (not as a language extension!)
  4. Type-level Naturals, at the very beginning Naturals as Peano numbers

  5. Type-level Naturals, at the very beginning Defining addition for Peano

    naturals
  6. Type-level Naturals, at the very beginning Now we must bring

    it to the type-level, but how to define addition for this?
  7. Exploiting overload resolution “Inline” functions can use ad-hoc polymorphism

  8. This allows us to write overloaded functions Overload resolution =

    Matching against types Exploiting overload resolution
  9. We can define addition (and other operations) with recursive overload

    resolution Exploiting overload resolution
  10. We need type-level Booleans, too

  11. We need type-level Booleans, too So that we can have

    type-level assertions
  12. Vector implementation, the first try And now we can implement

    vectors!
  13. We can also implement type-safe index access thanks to type-level

    naturals, but... Vector implementation, the first try
  14. The Cons (definitely not about the list constructor ) •

    Type naturals are tedious to write by hand (1) • Vector creation is not really type-safe (2)
  15. The solution (also definitely not about that thing in Visual

    Studio) ➔ Create type naturals from compile-time constants ➔ Use tuples and count their arity to create vectors
  16. Creating type naturals from compile-time constants We can use Type

    Providers for this
  17. Creating type naturals from compile-time constants Type Providers: • Compile-time

    type generator, basically • Can take constant values (such as JSON schemas) as input and generate types based on them • Fully integrated into the language = Nice IDE support • No deep dive today, would deserves a separate talk
  18. Here TypeNat<_> and Vector.Get<_> are type providers, which takes an

    integer and generates a specialized type to work with the corresponding type-level natural Creating type naturals from compile-time constants
  19. n-tuples (n > 7) have nested representations under the hood

    Using tuples for vector creation
  20. Being nested means we can effectively match against them Using

    tuples for vector creation
  21. • Count the arity of a given tuple with type

    naturals • Extract elements from the tuple and store them in an array • All done with overload resolution, with no reflection involved Using tuples for vector creation
  22. Finally, we get this... • Clean, intuitive, and type-safe •

    Very small run-time overhead (no reflection/boxing)
  23. Looks awesome? You can use them soon™! Included as part

    of upcoming FSharpPlus[1] v2.0 release with a lot of familiar and useful functions such as: [1]: https://fsprojects.github.io/FSharpPlus/
  24. One more thing...