Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Journey to Type-safe Vectors in F#

A Journey to Type-safe Vectors in F#

cannorin

April 17, 2020
Tweet

More Decks by cannorin

Other Decks in Programming

Transcript

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  4. Type-level Naturals, at the very beginning
    Naturals as Peano numbers

    View full-size slide

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

    View full-size slide

  6. Type-level Naturals, at the very beginning
    Now we must bring it to the type-level,
    but how to define addition for this?

    View full-size slide

  7. Exploiting overload resolution
    “Inline” functions can use ad-hoc polymorphism

    View full-size slide

  8. This allows us to write overloaded functions
    Overload resolution = Matching against types
    Exploiting overload resolution

    View full-size slide

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

    View full-size slide

  10. We need type-level Booleans, too

    View full-size slide

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

    View full-size slide

  12. Vector implementation, the first try
    And now we can implement vectors!

    View full-size slide

  13. We can also implement type-safe index access
    thanks to type-level naturals, but...
    Vector implementation, the first try

    View full-size slide

  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)

    View full-size slide

  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

    View full-size slide

  16. Creating type naturals from compile-time constants
    We can use Type Providers for this

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  19. n-tuples (n > 7) have nested representations under the hood
    Using tuples for vector creation

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  22. Finally, we get this...
    • Clean, intuitive, and type-safe
    • Very small run-time overhead (no reflection/boxing)

    View full-size slide

  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/

    View full-size slide

  24. One more thing...

    View full-size slide