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

Types in ML-Inspired Languages

A67a1f4adc81511d76952999ea240b42?s=47 nt591
July 31, 2019
24

Types in ML-Inspired Languages

Learning about types via the OCaml and Elm

A67a1f4adc81511d76952999ea240b42?s=128

nt591

July 31, 2019
Tweet

Transcript

  1. Types in ML-Influenced Languages Exploring modern type inference

  2. 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
  3. 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
  4. Type Inference: Example 1 What type is the argument? What

    is the return type?
  5. What type is the argument? What is the return type?

    Type Inference: Example 2
  6. What types are the arguments? What is the return type?

    Type Inference: Example 3
  7. 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’
  8. Currying and Partial Application • A curried function is a

    function that takes multiple arguments before executing, but accepts one argument at a time.
  9. Currying and Partial Application Typically, you get both for free

    and safely in typed, functional languages. All functions are curried by default.
  10. Safe partial application

  11. 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
  12. 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
  13. Algebraic Data Types - Sum Types (aka Union Types) •

    A type that represents a limited variety of arbitrary, user defined types
  14. Algebraic Data Types - Constructors with Data • Our types

    can hold onto data • Note the * in definition - refers to a “product type”
  15. How are types not classes? • Classes encapsulate behavior and

    state, types focus on data
  16. Pattern Matching This enables exhaustive type checking from the compiler

    when using a type!
  17. Pattern Matching

  18. Representing null - the option type • Also called optionals

    (Swift), Maybe (Haskell, Elm)
  19. Live Code

  20. Other languages

  21. 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/