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

A Journey to Type-safe Vectors in F#

cannorin
April 17, 2020

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 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 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 Slide

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

    View Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. We need type-level Booleans, too

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 Slide

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

    View 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 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 Slide

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

    View Slide

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

    View 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 Slide

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

    View 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 Slide

  24. One more thing...

    View Slide