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

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. Introductions • They’re hard, like naming things and cache invalidation

    • Where are polymorphic variants? • Conservative language use • Examples in OCaml
  2. 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
  3. 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}
  4. Variants? • Variants are just sum types in OCaml •

    Also called a variant record, choice type, discriminated union or disjoint union.
  5. 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
  6. Error Handling • Model error states as data types, rather

    than exceptions • Manual composition of data types
  7. Advantages • composition using result monad • separation of code

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

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