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

Introduction to type systems & Elm

Anler
April 21, 2017

Introduction to type systems & Elm

A different introduction to functional programming in Elm through types and type systems.

Anler

April 21, 2017
Tweet

More Decks by Anler

Other Decks in Programming

Transcript

  1. Disclaimer This is not a rant, this is a sales

    pitch. I don’t want to attack your community, but I do want to steal some brains for mine I love the work you do as a community and I wish we could have the same in ours.
  2. History of Programming as seen from JS JavaScript Assembly M

    aintainability problem s M em ory m anagem ent
 problem s C Java
  3. History of Programming as seen from JS JavaScript Assembly M

    aintainability problem s M em ory m anagem ent
 problem s Expressivity problem s C Java
  4. History of Programming as seen from JS JavaScript Assembly M

    aintainability problem s M em ory m anagem ent
 problem s Expressivity problem s M aintainability problem s C Java
  5. There’s a limit in the amount of things we can

    handle at a time. In order to increase that limit we need abstractions
  6. When writing software, program correctness is an issue of increasing

    importance because we are living in a world with increasing reliance on computer programs
  7. Lightweight Formal methods (of much more modest power) Model checkers

    Run-time monitoring Type systems the most popular
  8. A type system is a tractable syntactic method for proving

    the absence of certain program behaviours by classifying phrases according to the kinds of values they compute
  9. Strong Weak Static Dynamic The type system is more pedantic

    and usually forces you to be more explicit. Ex: 
 - 3 + “hola” is not accepted ❌ - if “hola” … ❌ The type system is less strict and usually performs implicit conversions
 for you. Ex: - 3 + “hola” is accepted ✅ - if “hola” … is accepted ✅ The type analysis of expressions is performed at compile time. The type analysis of expressions is performed at run time.
  10. A type system is usually conservative, it has to be

    because a type system can prove the absence of some bad program behaviours, but they cannot prove their presence!
  11. A type system is usually conservative, it has to be

    because a type system can prove the absence of some bad program behaviours, but they cannot prove their presence! Program testing can be used to prove the presence of bugs, but never show their absence!
  12. Detecting errors
 Documentation Abstraction Language Safety Efficiency Those are key

    for effective Functional Programming What type systems are good for? And here I will talk again about FP :P
  13. When I refer to FP I mean constructing the whole

    program as a composition of referentially transparent functions, not programs where you just use map, filter or lambdas.
  14. 3. Functions always compose ➡ ➡ ➡ The same can’t

    be said of OOP, the answer would be: it depends. Also I don’t refer to JS functions, those are procedures and their composition is not guaranteed
  15. No Tail Call Elimination
 FP langs transform tail-call (mutually-)recursive functions

    into while loops Performance penalty of using functions FP langs eliminate functions by inlining-them Performance penalty of boxed data types FP langs eliminate those boxes FP problems when used outside functional languages
  16. Greenspun’s Tenth Rule: 
 
 Any sufficiently complicated C or

    Fortran program contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp. Steele & Crockford’s Corollary: 
 
 Any sufficiently interesting JavaScript library contains an ad hoc, informally-specified, bug-ridden, slow implementation of half of Haskell.
  17. Elm is a purely-functional static programming language for frontend web

    development, that forces the usage of total functions and thus is claimed as a zero- runtime errors language.
  18. head : List a -> a If you invent a

    new value null and treat it as an ‘a’ you are augmenting the set ‘a’ with a new possibility (null) that has to be checked always. What happens if the list if empty?
  19. a

  20. A partial (useless) total function because null will never have

    the same properties as a head : List a -> a someOp = head [] + 3 Nonsense, null with a number has no well defined meaning! This leads to null propagation. null
  21. Elm has no null, instead you use the Maybe type

    to express possibility of a value that might not be there.
  22. A partial total function head : List a -> Maybe

    a someOp = withDefault 0 (head []) + 3 We are forced to contemplate the possibility of failure and act
 accordingly Nothing Maybe
  23. A type is a name for a collection of values

    : Bool True False The Bool type Inhabitants
  24. a -> b String Char Bool number The type of

    functions from a to b The type of all strings The type of all characters The type of all bool (True and False) The type of all 
 numbers
  25. A constructor is the constant that represents or the function

    that constructs a value of a type : Bool True False I’m the constant representing the falsy value I’m the constant representing the truthy value
  26. It is a way of defining a new type or

    a shape that represents several types type Bool = True | False > True : Bool > False : Bool Bool type defined Type can be constructed with either True or False constants (product)
  27. It is a way of defining a new type or

    a shape that represents several types type Duple a b = Duple a b
 
 > Duple True False
 : Duple Bool Bool
 > Duple “hello” ‘h’
 : Duple String Char Shape of all the duple types defined I’m the function that knows how to construct a value of type ‘Duple a b’ Need both: a and b (coproduct or sum)
  28. It is just another name for an existing type or

    type shape type alias Binary = Bool
  29. It is just like a named tuple > r =

    { x = “foo”, y = False } r : { x : String, y : Bool } > r.x “foo” : String > r.y False : Bool
  30. It is the way to know which constructor was used

    for creating a value of a given data type case boolValue of True -> “created by True” False -> “created by False” case stringValue of “hi” -> “created by “hi”” _ -> “dunno”
  31. Functional programming combined with a good type system is a

    very good idea, maximises the advantages of each one (FP & type systems) and gives the developer more grip at controlling a project’s complexity because it makes explicit the side-effects in your application and brings them under control. 109
  32. Programming languages are all about tradeoffs. In my case I’m

    more inclined to give up on a lot of things in exchange of fewer runtime errors.