Slide 1

Slide 1 text

Code reuse through polymorphic variants FP-SYD 2018

Slide 2

Slide 2 text

Introductions • They’re hard, like naming things and cache invalidation • Where are polymorphic variants? • Conservative language use • Examples in OCaml

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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}

Slide 6

Slide 6 text

Variants? • Variants are just sum types in OCaml • Also called a variant record, choice type, discriminated union or disjoint union.

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Code

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Error Handling • Model error states as data types, rather than exceptions • Manual composition of data types

Slide 12

Slide 12 text

Code

Slide 13

Slide 13 text

Advantages • composition using result monad • separation of code and error handling • clear errors as types • exhaustiveness checking • reusable constructors

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Advantages • composition of types allows function reuse • typed list of properties checked by compiler • extending / mixing existing types together • exhaustiveness checking

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Alternatives

Slide 18

Slide 18 text

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