Slide 1

Slide 1 text

Hello,PHPeste 2017. The correctness-by-design duck will bite PHP • The 7th of October, 2017. матеус албукерки • @ythecombinator • www.ythecombinator.me

Slide 2

Slide 2 text

fn main() do print(“ The correctness-by-design duck will bite PHP”) end

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Big fan of pixel art.

Slide 5

Slide 5 text

Keen on learning new languages.

Slide 6

Slide 6 text

Known as ythecombinator over the interwebs.

Slide 7

Slide 7 text

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.

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

Motivation. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Mariko Kosaka • Web developer @ Google. HOW TO BE A COMPILER #BrazilJS 2017.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

× × × × × ×

Slide 15

Slide 15 text

When it comes to PLT, there’s much awesome stuff happening here in ! of which many people out there just have no idea.

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Origin. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Safer language without any runtime surprises Solve a few problems from PHP Humanized compiler

Slide 22

Slide 22 text

And by humanized I mean…

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Compilers 101

Slide 28

Slide 28 text

Tokenization Parsing Transformation Generation

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

Tokenization Parsing Transformation Generation Once we analyze syntax by parsing, we transform the structure to something suitable to the final result.

Slide 32

Slide 32 text

Tokenization Parsing Transformation Generation Lastly, we make the compiled result.

Slide 33

Slide 33 text

Tokenization Parsing Transformation Generation

Slide 34

Slide 34 text

Back-End Front-End

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Back-End Deals with the target system: object code formats, the machine code itself etc.

Slide 37

Slide 37 text

Features. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

$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

Slide 44

Slide 44 text

$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

Slide 45

Slide 45 text

$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

Slide 46

Slide 46 text

$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

Slide 47

Slide 47 text

If your functions are in curried form, that makes it more convenient to partially apply them.

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

a.k.a function/operator overloading

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Allows classes and methods to accept types as parameters, meaning that classes can be abstracted with respect to types.

Slide 53

Slide 53 text

https://github.com/TimeToogo/PHP-Generics

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

interface A { value: number } interface B { value: number } A "# B $% False &' Nominal Type Systems A "# B $% True &' Structural Type Systems

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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.

Slide 61

Slide 61 text

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.

Slide 62

Slide 62 text

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.

Slide 63

Slide 63 text

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.

Slide 64

Slide 64 text

fn fact(n :: number) *→ number :- match n with 0 *→ 1 n *→ n * fact(n - 1) end

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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)

Slide 67

Slide 67 text

Weirdo Stuff. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 68

Slide 68 text

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)

Slide 69

Slide 69 text

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)

Slide 70

Slide 70 text

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)

Slide 71

Slide 71 text

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)

Slide 72

Slide 72 text

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)

Slide 73

Slide 73 text

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)

Slide 74

Slide 74 text

Fun Facts. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 75

Slide 75 text

95% of PHP + 5% of Quack ~3 years of research over the compiler Self-compiled compiler = ~100KB

Slide 76

Slide 76 text

Status. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 77

Slide 77 text

Both lexer and parser are finished. Type system in final stages. Final code generation tool in progress.

Slide 78

Slide 78 text

Mission. THE CORRECTNESS-BY-DESIGN DUCK WILL BITE PHP • PHPESTE 2017

Slide 79

Slide 79 text

Is PHP going to die?

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

#1

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

#2

Slide 85

Slide 85 text

When it comes to PLT, there’s much awesome stuff happening here in ! of which many people out there just have no idea.

Slide 86

Slide 86 text

https://elixirconf.com

Slide 87

Slide 87 text

https://elixirconf.com QUACK Quack XX

Slide 88

Slide 88 text

https://elixirconf.com QUACK Quack XX

Slide 89

Slide 89 text

Who knows?

Slide 90

Slide 90 text

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