$30 off During Our Annual Pro Sale. View Details »

Code reuse through polymorphic variants

Code reuse through polymorphic variants

Tim will *WOW* you with the advanced features of a strange non-Haskell language OCaml, showing you what Polymorphic Variants are and how they might be useful. This
talk could also be taken as cautionary tale about why advanced type features
aren't always what they are cracked up to be.

Tim McGilchrist

July 25, 2018
Tweet

More Decks by Tim McGilchrist

Other Decks in Programming

Transcript

  1. Code reuse through
    polymorphic variants
    FP-SYD 2018

    View Slide

  2. Introductions
    • They’re hard, like naming things and cache invalidation

    • Where are polymorphic variants?

    • Conservative language use

    • Examples in OCaml

    View Slide

  3. –Jacques Garrigue
    “… support for code reuse has made object-
    oriented languages popular”

    View Slide

  4. What is OCaml?
    • OCaml is an industrial strength
    programming languages
    supporting functional,
    imperative and object-oriented
    styles.

    • powerful type system

    • algebraic data types and
    pattern matching

    • sophisticated module system

    View Slide

  5. Type Signatures
    Types:
    Haskell -> () Int Float Char String
    OCaml -> unit int float char string
    Composite Types:
    Haskell -> (Int, Int) [Bool]
    OCaml -> int * int bool list
    Data Types:
    Haskell -> data Tree a = Node a (Tree a) (Tree a) | Leaf
    OCaml -> type 'a tree = Node of 'a * 'a tree * 'a tree | Leaf
    Records:
    Haskell -> data Vinyl = Vinyl {artist :: Text, tracks :: [string]}
    OCaml -> type vinyl = {artist : Text; tracks : string list}

    View Slide

  6. Variants?
    • Variants are just sum types in OCaml

    • Also called a variant record, choice type, discriminated
    union or disjoint union.

    View Slide

  7. View Slide

  8. Polymorphic Variants
    • Polymorphic variants loosen the restriction that a
    constructor can only belong to one type.

    • Type of a variant will be inferred independently for each of
    its uses.

    • Prefix their names with backquote

    View Slide

  9. Code

    View Slide

  10. –French National Assembly
    “They must consider that great responsibility
    follows inseparably from great power”

    View Slide

  11. Error Handling
    • Model error states as data types, rather than exceptions

    • Manual composition of data types

    View Slide

  12. Code

    View Slide

  13. Advantages
    • composition using result monad

    • separation of code and error handling

    • clear errors as types

    • exhaustiveness checking

    • reusable constructors

    View Slide

  14. Domain Specific Language
    • Capture the transformation of natural language queries in
    a domain specific area.

    • Turn queries into an execution plan

    • Support constructing queries via data types

    • Make invalid states unrepresentable

    View Slide

  15. Advantages
    • composition of types allows function reuse

    • typed list of properties checked by compiler

    • extending / mixing existing types together

    • exhaustiveness checking

    View Slide

  16. Disadvantages
    • type checking is more complicated ([> ...], [<...])

    • more requirement for explicit type annotations

    • spelling errors as types

    • exhaustiveness checking (maybe)

    • lack of static information allows less optimisation

    • another feature to teach people

    View Slide

  17. Alternatives

    View Slide

  18. Resources
    • Programming with Polymorphic Variants by Jacques
    Garrigue

    • Code reuse through polymorphic variants by Jacques
    Garrigue

    • Composable error handling in OCaml (http://
    keleshev.com/composable-error-handling-in-ocaml)

    • OCaml for Haskellers (http://blog.ezyang.com/2010/10/
    ocaml-for-haskellers/)

    View Slide