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

Mundane Utility: A Functional Shell

Mundane Utility: A Functional Shell

Functional programming is a useful technique. We spend a lot of time discussing it in the context of hard, challenging or interesting problems, but no where nearly enough in the context of mundane problems.

The UNIX shell, or command line interpreter. A simple, but useful program too often mistaken as complex, or mysterious. Almost every programmer is exposed to shells from a users perspective, but far fewer have ever implemented one, or even know where to start.

This talk aims to be a fun look at using functional programming in and around a traditional, some would say mundane, system utility. We will work through the concepts involved with implementing your own shell, and live code our way to a basic functional shell implemented in Haskell.

From this talk, attendees will walk a way with a better understanding of a program they use every day, as well as ideas and inspiration around using functional programming to solve mundane programming problems.

NOTE: this is the lead in to a workshop https://github.com/markhibberd/xsh

Mark Hibberd

May 08, 2017
Tweet

More Decks by Mark Hibberd

Other Decks in Programming

Transcript

  1. Mundane
    Utility
    by Mark Hibberd

    View Slide

  2. “Awesomeness put to mundane use”
    TV Tropes on Mundane Utility

    View Slide

  3. Unix Shell Userland
    Kernel
    Hardware

    View Slide

  4. How did we get here…

    View Slide

  5. How did we get here…
    firmware

    View Slide

  6. How did we get here…
    firmware
    boot loader

    View Slide

  7. How did we get here…
    firmware
    boot loader
    kernel

    View Slide

  8. How did we get here…
    firmware
    boot loader
    kernel
    init

    View Slide

  9. View Slide

  10. How did we get here…
    firmware
    boot loader
    kernel
    init

    View Slide

  11. How did we get here…
    firmware
    boot loader
    kernel
    init
    getty
    note this is just one
    example sequence at
    this point onwards

    View Slide

  12. How did we get here…
    firmware
    boot loader
    kernel
    init
    getty
    login

    View Slide

  13. How did we get here…
    firmware
    boot loader
    kernel
    init
    getty
    login
    shell

    View Slide

  14. How did we get here…
    firmware
    boot loader
    kernel
    init
    getty
    login
    shell
    ls

    View Slide

  15. A shell…

    View Slide

  16. why?
    A shell…

    View Slide

  17. View Slide

  18. A shell…
    Line Editing
    Command Parsing
    Command Expansion
    Process Management
    Builtins

    View Slide

  19. line editing…
    q w e r t y

    View Slide

  20. command parsing…

    View Slide

  21. command parsing…
    ::= lists EOF
    | EOF
    ::= list
    | list list
    ::= list
    | list pipeline
    | list pipeline
    ::= pipeline
    | pipeline command
    ::= word
    | command word
    ::= ;
    ::= &&
    ::= ||
    ::= |
    ::= …

    View Slide

  22. command parsing…
    character input

    View Slide

  23. command parsing…
    lexer

    View Slide

  24. command parsing…
    token stream

    View Slide

  25. command parsing…
    parser

    View Slide

  26. command parsing…
    program

    View Slide

  27. lexing words…
    word
    soft hard unquoted
    text variable text
    variable

    View Slide

  28. process management…

    View Slide

  29. ls | sort | uniq
    process management…

    View Slide

  30. process management…
    fork()
    ls | sort | uniq
    uniq
    sort
    ls
    xsh
    xsh
    xsh
    xsh

    View Slide

  31. fork() fork()
    ls | sort | uniq
    uniq
    sort
    ls
    xsh
    xsh
    xsh
    xsh
    process management…

    View Slide

  32. fork() fork() exec()
    ls | sort | uniq
    uniq
    sort
    ls
    xsh
    xsh
    xsh
    xsh
    process management…

    View Slide

  33. fork() fork()
    fork()
    exec()
    ls | sort | uniq
    uniq
    sort
    ls
    xsh
    xsh
    xsh
    xsh
    process management…

    View Slide

  34. fork() fork()
    fork()
    exec()
    exec()
    ls | sort | uniq
    uniq
    sort
    ls
    xsh
    xsh
    xsh
    xsh
    process management…

    View Slide

  35. fork() fork()
    fork()
    exec()
    exec()
    exec()
    ls | sort | uniq
    uniq
    sort
    ls
    xsh
    xsh
    xsh
    xsh
    process management…

    View Slide

  36. ls | sort | uniq
    xsh xsh xsh xsh
    uniq sort ls
    fork()
    process management…

    View Slide

  37. ls | sort | uniq
    xsh xsh xsh xsh
    uniq sort ls
    fork() fork()
    process management…

    View Slide

  38. ls | sort | uniq
    xsh xsh xsh xsh
    uniq sort ls
    fork() fork() fork()
    process management…

    View Slide

  39. ls | sort | uniq
    xsh xsh xsh xsh
    uniq sort ls
    fork() fork() fork()
    exec()
    process management…

    View Slide

  40. ls | sort | uniq
    xsh xsh xsh xsh
    uniq sort ls
    fork() fork() fork()
    exec() exec()
    process management…

    View Slide

  41. ls | sort | uniq
    xsh xsh xsh xsh
    uniq sort ls
    fork() fork() fork()
    exec() exec() exec()
    process management…

    View Slide

  42. ls | sort | uniq
    fork()
    xsh
    xsh xsh xsh
    ls
    sort
    uniq
    process management…

    View Slide

  43. ls | sort | uniq
    fork()
    exec()
    xsh
    xsh xsh xsh
    ls
    sort
    uniq
    process management…

    View Slide

  44. ls | sort | uniq
    fork()
    fork()
    exec()
    xsh
    xsh xsh xsh
    ls
    sort
    uniq
    process management…

    View Slide

  45. ls | sort | uniq
    fork()
    fork()
    exec() exec()
    xsh
    xsh xsh xsh
    ls
    sort
    uniq
    process management…

    View Slide

  46. ls | sort | uniq
    fork() fork()
    fork()
    exec() exec()
    xsh
    xsh xsh xsh
    ls
    sort
    uniq
    process management…

    View Slide

  47. ls | sort | uniq
    fork() fork()
    fork()
    exec() exec() exec()
    xsh
    xsh xsh xsh
    ls
    sort
    uniq
    process management…

    View Slide

  48. pushing limits

    View Slide

  49. workshop @ 1:15
    https://github.com/markhibberd/xsh

    View Slide

  50. Mundane
    Utility
    by Mark Hibberd

    View Slide