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.

67afd2b4c98c9befd18c19f0ee9d94dc?s=128

Tim McGilchrist

July 25, 2018
Tweet

Transcript

  1. Code reuse through polymorphic variants FP-SYD 2018

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

    • Where are polymorphic variants? • Conservative language use • Examples in OCaml
  3. –Jacques Garrigue “… support for code reuse has made object-

    oriented languages popular”
  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
  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}
  6. Variants? • Variants are just sum types in OCaml •

    Also called a variant record, choice type, discriminated union or disjoint union.
  7. None
  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
  9. Code

  10. –French National Assembly “They must consider that great responsibility follows

    inseparably from great power”
  11. Error Handling • Model error states as data types, rather

    than exceptions • Manual composition of data types
  12. Code

  13. Advantages • composition using result monad • separation of code

    and error handling • clear errors as types • exhaustiveness checking • reusable constructors
  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
  15. Advantages • composition of types allows function reuse • typed

    list of properties checked by compiler • extending / mixing existing types together • exhaustiveness checking
  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
  17. Alternatives

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