$30 off During Our Annual Pro Sale. View Details »

Types in ML-Inspired Languages

nt591
July 31, 2019
140

Types in ML-Inspired Languages

Learning about types via the OCaml and Elm

nt591

July 31, 2019
Tweet

Transcript

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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’

    View Slide

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

    View Slide

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

    View Slide

  10. Safe partial application

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. Pattern Matching

    View Slide

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

    View Slide

  19. Live Code

    View Slide

  20. Other languages

    View Slide

  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/

    View Slide