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

Y-Combinator in PHP

Y-Combinator in PHP

This talk is about Y-Combinator and implementation in PHP

Article associated to this talk

https://blog.darkwood.fr/article/y-combinator-en-php

Video associated to this talk

https://www.youtube.com/watch?v=QSS_ZcO8Q1g

Mathieu Ledru

April 25, 2023
Tweet

More Decks by Mathieu Ledru

Other Decks in Programming

Transcript

  1. Y-Combinator
    @matyo91
    25-04-2023
    In PHP

    View full-size slide

  2. ⾠ I’m not an expert ⾠


    I will speak with my own words
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  3. Objectives : Go back up to theoretical concepts that solve an issue


    How I discovered Y-Combinator and why I do need them ?


    Why Y-Combinator ?


    Introduction to Lambda Calculus


    Exemples of basic Lambda Calculus and equivalent in PHP


    Y-Combinator in PHP (for me and others github repositories)
    Plan
    @matyo91
    25-04-2023 @matyo91

    View full-size slide

  4. How I discovered Y-Combinator ?
    @matyo91
    25-04-2023
    Why I need them ?

    View full-size slide

  5. In PHP https://github.com/darkwood-fr/flow
    In PHP https://github.com/phpflo/phpflo
    25-04-2023
    Flow Based Railway Flow Based
    MulByTwo MinusThree
    AddOne
    @matyo91

    View full-size slide

  6. Why Y-Combinator ?
    @matyo91
    25-04-2023
    What does it solve ?

    View full-size slide

  7. Do recursion in a language that
    do not have any recursion
    mechanism at all

    @matyo91
    25-04-2023

    View full-size slide

  8. Recursive


    Iterative


    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  9. Pipe


    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  10. A Y-combinator is a "functional" (a function that operates on other
    functions) that enables recursion, when you can't refer to the
    function from within itself.


    In computer-science theory, it generalises recursion, abstracting its
    implementation, and thereby separating it from the actual work of
    the function in question.


    This is applicable in languages that support Lambda-Calculus
    functions
    Y-Combinator
    @matyo91
    25-04-2023 @matyo91

    View full-size slide

  11. Lambda Calculus
    @matyo91
    25-04-2023

    View full-size slide

  12. Alonzo Church


    ( 1903 - 1995 )


    Inventor of Lambda Calculus
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  13. Church Turing
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  14. λ x . x + 1


    ( λ x . x + 1 ) ( 5 ) = 5 + 1 = 6
    @matyo91
    @matyo91
    25-04-2023
    X X + 1

    View full-size slide

  15. λ x . λ y . x+y


    ( λ x . λ y . x+y ) ( 5 , 6 ) = 5 + 6 = 11
    @matyo91
    @matyo91
    25-04-2023
    X
    X + Y
    Y

    View full-size slide

  16. There are variables.


    We got a way to build functions.


    We can apply functions to each others by providing elements of the
    language itself.


    All derivatives of lambda calculus language is something defined in
    terms of this definition.
    Lambda Calculus
    @matyo91
    25-04-2023 @matyo91

    View full-size slide

  17. True ?


    False ?
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  18. @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  19. True = λ x . λ y . x


    False = λ x . λ y . y
    @matyo91
    @matyo91
    25-04-2023
    X
    X
    Y
    True
    X
    Y
    Y
    False

    View full-size slide

  20. True = λ x . λ y . x
    @matyo91
    @matyo91
    25-04-2023
    False = λ x . λ y . y

    View full-size slide

  21. Not ?
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  22. @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  23. Not = λ b . b False True
    @matyo91
    @matyo91
    25-04-2023
    b False True
    b Not

    View full-size slide

  24. Not = ( λ b . b False True ) ( True )


    = True False True


    = ( λ x . λ y . x ) ( False True )


    = False
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  25. @matyo91
    @matyo91
    25-04-2023
    Not = λ b . b False True

    View full-size slide

  26. Y-Combinator
    @matyo91
    25-04-2023

    View full-size slide

  27. Y ( f ) = f ( Y ( f ) )


    = f ( f ( Y ( f ) ) )


    = f ( f ( f ( f ( ... ) ) ) )
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  28. Fac (n) = if n == 1 then 1


    else n * Fac (n - 1)



    Fac (3) = 3 * Fac (2)


    = 3 * 2 * Fac (1)


    = 3 * 2 * 1


    = 6
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  29. Fac = Y ( ? )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  30. Fac = Y ( ? )


    = Y ( λ f . λ n . ? )


    = Y ( λ f . λ n . max( n , 1 ) * Fac ( n - 1 ) )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  31. Loop = ?
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  32. Loop = Loop
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  33. Loop = ( λ x . x . x ) ( λ x . x . x )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  34. Loop = ( λ x . x . x ) ( λ x . x . x )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  35. Loop = ( λ x . x . x ) ( λ x . x . x )


    Loop = ( λ x . ( λ x . x . x ) . ( λ x . x . x ) )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  36. Loop = ( λ x . x . x ) ( λ x . x . x )


    Loop = ( λ x . ( λ x . x . x ) . ( λ x . x . x ) )


    Loop = ( λ x . x . x ) ( λ x . x . x )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  37. Loop = Y ( ? )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  38. Loop = Y ( λ x . x . x )


    = ( λ x . x . x ) ( Y ( λ x . x . x ) )


    = ( λ x . x . x ) ( λ x . x . x ) ( Y ( λ x . x . x ) )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  39. Y ( f ) = λ f ( λ x . f ( x x ) ) ( λ x . f ( x x ) )
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in Lambda Calculus

    View full-size slide

  40. https://github.com/darkwood-fr/flow
    @matyo91
    @matyo91
    25-04-2023
    YCombinator in PHP

    View full-size slide

  41. https://github.com/loophp/combinator
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  42. https://github.com/igorw/lambda-php
    @matyo91
    @matyo91
    25-04-2023

    View full-size slide

  43. In Real Life
    @matyo91
    25-04-2023 @matyo91

    View full-size slide

  44. Hello!


    I Am Mathieu Ledru
    You can contact me at @matyo91
    25-04-2023 @matyo91
    Thanks!
    Any questions?

    View full-size slide