Slide 1

Slide 1 text

Types in ML-Influenced Languages Exploring modern type inference

Slide 2

Slide 2 text

What is ML? No, not machine learning ● “Meta Language” ● Originally designed as a language to build a theorem prover by Robin Milner ● Strong, static typing ● Enables powerful type inference via the Hindley-Milner algorithm ● We’ll focus on those last two pieces in modern implementations ● Code samples will be in OCaml

Slide 3

Slide 3 text

Type Inference ● Type inference refers to the methods that allow a machine to understand and enforce a type used in a function without the programmer actually declaring a type signature ● Allows the compiler to enforce types by checking all usages of functions and their type signatures before runtime

Slide 4

Slide 4 text

Type Inference: Example 1 What type is the argument? What is the return type?

Slide 5

Slide 5 text

What type is the argument? What is the return type? Type Inference: Example 2

Slide 6

Slide 6 text

What types are the arguments? What is the return type? Type Inference: Example 3

Slide 7

Slide 7 text

Parametric Polymorphism ● Similar to generics in other languages ● Given all the information at hand, the computer cannot tell exactly what type is required, therefore it must work for any type a’ and return any type b’

Slide 8

Slide 8 text

Currying and Partial Application ● A curried function is a function that takes multiple arguments before executing, but accepts one argument at a time.

Slide 9

Slide 9 text

Currying and Partial Application Typically, you get both for free and safely in typed, functional languages. All functions are curried by default.

Slide 10

Slide 10 text

Safe partial application

Slide 11

Slide 11 text

Advanced Types: Type Aliases A type alias, or type synonym, is a new name for an existing type. This opens the door for more expressive domain modeling

Slide 12

Slide 12 text

Type Aliases - records Records are a data structure in many languages designed to hold multiple pieces of data (like tuples!) but with named fields and accessors

Slide 13

Slide 13 text

Algebraic Data Types - Sum Types (aka Union Types) ● A type that represents a limited variety of arbitrary, user defined types

Slide 14

Slide 14 text

Algebraic Data Types - Constructors with Data ● Our types can hold onto data ● Note the * in definition - refers to a “product type”

Slide 15

Slide 15 text

How are types not classes? ● Classes encapsulate behavior and state, types focus on data

Slide 16

Slide 16 text

Pattern Matching This enables exhaustive type checking from the compiler when using a type!

Slide 17

Slide 17 text

Pattern Matching

Slide 18

Slide 18 text

Representing null - the option type ● Also called optionals (Swift), Maybe (Haskell, Elm)

Slide 19

Slide 19 text

Live Code

Slide 20

Slide 20 text

Other languages

Slide 21

Slide 21 text

If you want to learn more Effective ML by Yaron Minsky ● https://blog.janestreet.com/effective-ml-revisited/ ● https://blog.janestreet.com/effective-ml-video/ Domain Modeling Made Functional ● https://fsharpforfunandprofit.com/ddd/ Why Type-First Development Matters ● http://tomasp.net/blog/type-first-development.aspx/