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 Slide

  2. ⾠ I’m not an expert ⾠


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

    View 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 Slide

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

    View 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 Slide

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

    View Slide

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

    @matyo91
    25-04-2023

    View Slide

  8. Recursive


    Iterative


    @matyo91
    @matyo91
    25-04-2023

    View Slide

  9. Pipe


    @matyo91
    @matyo91
    25-04-2023

    View 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 Slide

  11. Lambda Calculus
    @matyo91
    25-04-2023

    View Slide

  12. Alonzo Church


    ( 1903 - 1995 )


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

    View Slide

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

    View Slide

  14. λ x . x + 1


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

    View 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 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 Slide

  17. True ?


    False ?
    @matyo91
    @matyo91
    25-04-2023

    View Slide

  18. @matyo91
    @matyo91
    25-04-2023

    View Slide

  19. True = λ x . λ y . x


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

    View Slide

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

    View Slide

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

    View Slide

  22. @matyo91
    @matyo91
    25-04-2023

    View Slide

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

    View Slide

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


    = True False True


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


    = False
    @matyo91
    @matyo91
    25-04-2023

    View Slide

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

    View Slide

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

    View Slide

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


    = f ( f ( Y ( f ) ) )


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

    View 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 Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 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 Slide

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

    View 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  44. Hello!


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

    View Slide