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

The correctness-by-design duck will bite PHP

97d21da8e0ffa8f81218a293482c253a?s=47 Matheus
October 07, 2017

The correctness-by-design duck will bite PHP

In this talk, y'all will be introduced to Quack; a type-safe, multi-paradigm, metaprogrammable and extensible programming language which enables you to write consistent and legible code and learn how a language that targets PHP and whose compiler is entirely written in PHP can help you coding better.

97d21da8e0ffa8f81218a293482c253a?s=128

Matheus

October 07, 2017
Tweet

Transcript

  1. Hello,PHPeste 2017. The correctness-by-design duck will bite PHP • The

    7th of October, 2017. матеус албукерки • @ythecombinator • www.ythecombinator.me
  2. fn main() do print(“ The correctness-by-design duck will bite PHP”)

    end
  3. None
  4. Big fan of pixel art.

  5. Keen on learning new languages.

  6. Known as ythecombinator over the interwebs.

  7. Front-End Engineer @ Beakyn Mobile Development Intern @ Apple Developer

    Academy Evangelist @ Quack Language Contributor @ Lambda I/O, NUG-CE, Golang Brazil, Ionic Brazil and others.
  8. Front-End Engineer @ Beakyn Mobile Development Intern @ Apple Developer

    Academy Curious @ Quack Language Contributor @ Lambda I/O, NUG-CE, Golang Brazil, Ionic Brazil and others.
  9. Front-End Engineer @ Beakyn Mobile Development Intern @ Apple Developer

    Academy Evangelist @ Quack Language Contributor @ Lambda I/O, NUG-CE, Golang Brazil, Ionic Brazil and others.
  10. Motivation. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

  11. None
  12. Mariko Kosaka • Web developer @ Google. HOW TO BE

    A COMPILER #BrazilJS 2017.
  13. None
  14. × × × × × ×

  15. When it comes to PLT, there’s much awesome stuff happening

    here in ! of which many people out there just have no idea.
  16. None
  17. None
  18. Origin. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

  19. https://github.com/rawr-php/rawr-development

  20. https://wiki.php.net/rfc/pipe-operator

  21. Safer language without any runtime surprises Solve a few problems

    from PHP Humanized compiler
  22. And by humanized I mean…

  23. http://seriouspony.com/blog/2013/7/24/your-app-makes-me-fat

  24. http://seriouspony.com/blog/2013/7/24/your-app-makes-me-fat

  25. http://robotlolita.me/2016/01/09/no-i-dont-want-to-configure-your-app.html

  26. Intermission! THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

  27. Compilers 101

  28. Tokenization Parsing Transformation Generation

  29. Separate each keywords (called tokens) by white space. While we

    separate them, we also assign types to each token (e.g. “T_ECHO” or “T_FOREACH”). Tokenization Parsing Transformation Generation
  30. Tokenization Parsing Transformation Generation Once a blob of text is

    separated into tokens, we go through each of them and try to find a relationship between tokens. By doing this, we start seeing a structure of the code.
  31. Tokenization Parsing Transformation Generation Once we analyze syntax by parsing,

    we transform the structure to something suitable to the final result.
  32. Tokenization Parsing Transformation Generation Lastly, we make the compiled result.

  33. Tokenization Parsing Transformation Generation

  34. Back-End Front-End

  35. Deals with the language itself: tokenizing, parsing etc. Front-End

  36. Back-End Deals with the target system: object code formats, the

    machine code itself etc.
  37. Features. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

  38. Implicit currying Ad-hoc polymorphism Parametric polymorphism Structural type system Pattern

    matching Immutable by default
  39. Implicit currying Ad-hoc polymorphism Parametric polymorphism Structural type system Pattern

    matching Immutable by default
  40. Currying is the technique of transforming a function that takes

    multiple arguments in such a way that it can be called as a chain of functions each with a single argument. - Wikipedia
  41. In computer science, partial application (or partial function application) refers

    to the process of fixing a number of arguments to a function, producing another function of smaller arity. - Wikipedia
  42. Currying always produces nested unary (1-ary) functions. The transformed function

    is still largely the same as the original. Partial application produces functions of arbitrary arity. The transformed function is different from the original – it needs less arguments. - 2Ality
  43. $add = partial(function($x, $y, $z) { return $x + $y

    + $z; }); $a = $add; $b = $add(10); $c = $add(10, 20); $d = $add(10, 20, 30); echo $a(10, 20, 30), PHP_EOL; echo $b(20, 30), PHP_EOL; echo $c(30), PHP_EOL; echo $d, PHP_EOL; https://github.com/haskellcamargo/php-partial-function-application
  44. $add = partial(function($x, $y, $z) { return $x + $y

    + $z; }); $a = $add; $b = $add(10); $c = $add(10, 20); $d = $add(10, 20, 30); echo $a(10, 20, 30), PHP_EOL; echo $b(20, 30), PHP_EOL; echo $c(30), PHP_EOL; echo $d, PHP_EOL; https://github.com/haskellcamargo/php-partial-function-application
  45. $add = partial(function($x, $y, $z) { return $x + $y

    + $z; }); $a = $add; $b = $add(10); $c = $add(10, 20); $d = $add(10, 20, 30); echo $a(10, 20, 30), PHP_EOL; echo $b(20, 30), PHP_EOL; echo $c(30), PHP_EOL; echo $d, PHP_EOL; https://github.com/haskellcamargo/php-partial-function-application
  46. $add = partial(function($x, $y, $z) { return $x + $y

    + $z; }); $a = $add; $b = $add(10); $c = $add(10, 20); $d = $add(10, 20, 30); echo $a(10, 20, 30), PHP_EOL; echo $b(20, 30), PHP_EOL; echo $c(30), PHP_EOL; echo $d, PHP_EOL; https://github.com/haskellcamargo/php-partial-function-application
  47. If your functions are in curried form, that makes it

    more convenient to partially apply them.
  48. Implicit currying Ad-hoc polymorphism Parametric polymorphism Structural type system Pattern

    matching Immutable by default
  49. A kind of polymorphism in which polymorphic functions can be

    applied to arguments of different types, because a polymorphic function can denote a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. - Wikipedia
  50. a.k.a function/operator overloading

  51. https://github.com/garex/php-ad-hoc-polymorphism-polyfill

  52. Allows classes and methods to accept types as parameters, meaning

    that classes can be abstracted with respect to types.
  53. https://github.com/TimeToogo/PHP-Generics

  54. https://wiki.php.net/rfc/generics

  55. Implicit currying Ad-hoc polymorphism Parametric polymorphism Structural type system Pattern

    matching Immutable by default
  56. A structural type system (or property-based type system) is a

    major class of type system, in which type compatibility and equivalence are determined by the type's actual structure or definition, and not by other characteristics such as its name or place of declaration. - Wikipedia
  57. interface A { value: number } interface B { value:

    number } A "# B $% False &' Nominal Type Systems A "# B $% True &' Structural Type Systems
  58. Implicit currying Ad-hoc polymorphism Parametric polymorphism Structural type system Pattern

    matching Immutable by default
  59. Pattern matching is not somehow an alternative of switch statement;

    it’s much more like another way of doing dynamic dispatch in OOP. They try to do the same thing: calling a different version of the function based on the dynamic type of the arguments. - Someone over the internet
  60. Pattern matches can act upon ints, floats, strings and other

    types & objects. Method dispatch requires an object. Pattern matches can act upon several different values simultaneously. Method dispatch is limited to the single this case in mainstream languages. Patterns can be nested, allowing dispatch over trees of arbitrary depth. Method dispatch is limited to the non-nested case.
  61. Pattern matches can act upon ints, floats, strings and other

    types as well as objects. Method dispatch requires an object. Pattern matches can act upon several different values simultaneously. Method dispatch is limited to the single this case in mainstream languages. Patterns can be nested, allowing dispatch over trees of arbitrary depth. Method dispatch is limited to the non-nested case.
  62. Pattern matches can act upon ints, floats, strings and other

    types as well as objects. Method dispatch requires an object. Pattern matches can act upon several different values simultaneously. Method dispatch is limited to the single this case in mainstream languages. Patterns can be nested, allowing dispatch over trees of arbitrary depth. Method dispatch is limited to the non-nested case.
  63. Pattern matches can act upon ints, floats, strings and other

    types & objects. Method dispatch requires an object. Pattern matches can act upon several different values simultaneously. Method dispatch is limited to the single this case in mainstream languages. Patterns can be nested, allowing dispatch over trees of arbitrary depth. Method dispatch is limited to the non-nested case.
  64. fn fact(n :: number) *→ number :- match n with

    0 *→ 1 n *→ n * fact(n - 1) end
  65. Implicit currying Ad-hoc polymorphism Parametric polymorphism Structural type system Pattern

    matching Immutable by default
  66. Mutability is evil! It can set your house on fire,

    kill your cat and buy costumes on e-bay using your credit card! Be careful! - Real world functional programming in JS, by Marcelo Camargo (@haskellcamargo)
  67. Weirdo Stuff. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE

    2017
  68. No nulls. No errors. Just monads. Explicit side-effects Code with

    no side-effects is compile- time optimized Tail Call Optimization over the language AST Pluggable back-ends Syntax-highlighted REPL (which shouts Quack)
  69. No nulls. No errors. Just monads. Explicit side-effects Code with

    no side-effects is compile- time optimized Tail Call Optimization over the language AST Pluggable back-ends Syntax-highlighted REPL (which shouts Quack)
  70. No nulls. No errors. Just monads. Explicit side-effects Code with

    no side-effects is compile- time optimized Tail Call Optimization over the language AST Pluggable back-ends Syntax-highlighted REPL (which shouts Quack)
  71. No nulls. No errors. Just monads. Explicit side-effects Code with

    no side-effects is compile- time optimized Tail Call Optimization over the language AST Pluggable back-ends Syntax-highlighted REPL (which shouts Quack)
  72. No nulls. No errors. Just monads. Explicit side-effects Code with

    no side-effects is compile- time optimized Tail Call Optimization over the language AST Pluggable back-ends Syntax-highlighted REPL (which shouts Quack)
  73. No nulls. No errors. Just monads. Explicit side-effects Code with

    no side-effects is compile- time optimized Tail Call Optimization over the language AST Pluggable back-ends Syntax-highlighted REPL (which shouts Quack)
  74. Fun Facts. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE

    2017
  75. 95% of PHP + 5% of Quack ~3 years of

    research over the compiler Self-compiled compiler = ~100KB
  76. Status. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

  77. Both lexer and parser are finished. Type system in final

    stages. Final code generation tool in progress.
  78. Mission. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

  79. Is PHP going to die?

  80. https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js

  81. https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js ~250

  82. #1

  83. Compile-to-language-x- languages (generally) don’t intend to kill x; they push

    its spec forward.
  84. #2

  85. When it comes to PLT, there’s much awesome stuff happening

    here in ! of which many people out there just have no idea.
  86. https://elixirconf.com

  87. https://elixirconf.com QUACK Quack XX

  88. https://elixirconf.com QUACK Quack XX

  89. Who knows?

  90. Thanks . матеус албукерки • @ythecombinator • www.ythecombinator.me