Functional Programming
Mayer Goldberg \ Ben-Gurion University June 2, 2019
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 1 / 24
Functional Programming June 2, 2019 Functional Programming June 2, - - PowerPoint PPT Presentation
Functional Programming June 2, 2019 Functional Programming June 2, 2019 1 / 24 Mayer Goldberg \ Ben-Gurion University Mayer Goldberg \ Ben-Gurion University Road Map Functional Programming June 2, 2019 2 / 24 Introduction to Functional
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 1 / 24
▶ Possibly additional topics, depending on time
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 2 / 24
▶ Monads are part of a programming technique that is common in
▶ Alternately, you may think of monads as design patterns.
▶ This technique allows us to thread a computation in an abstract
▶ Threading computation abstractly allows us to implement many
▶ We can thread computation with an environment, to provide for
▶ We can thread computation with continuations, to provide for
▶ Monads have been known, if not by name, since the 1960’s
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 3 / 24
▶ Computer Scientist Eugenio Moggi connected this particular
▶ Monads allow us to enjoy both worlds, functional & imperative,
▶ Monads are native to Haskell, a purely functional programming
▶ The term “monad” comes from the Greek μονάς, meaning
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 4 / 24
▶ Leibniz wrote a treatise on metaphysics titled Monadology
▶ The universe, according to Leibniz, is comprised of simple,
▶ Monads can be composed and combined to give larger monads ▶ Monads have limited perception of the rest of the universe ▶ Got, though the simplest of all monads, has full, unrestricted,
▶ Monads interact & coexist harmoniously with other monads,
▶ And this was thought up by the same Leibniz who co-invented
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 5 / 24
▶ The term monad was adopted for use in Category Theory
▶ Computer scientist Eugenio Moggi was the fjrst to connect
▶ Because monads can be so many things and have so many
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 6 / 24
▶ We considered a simplifjed version of the printf function:
▶ We mentioned that we had a problem with the type of this
▶ The type didn’t really capture all the arguments to the function ▶ The full type of printf must include a screen:
▶ Once the type includes the screen, there is no side-efgect!
▶ The problem with this type is that it’s inconvenient to have to
▶ In particular, it makes sequences of calls to printf into
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 7 / 24
▶ The standard way of handling such an annoying type as
▶ If the type of printf is string → screen → int × screen,
▶ What kind of a function is this? ▶ Nevermind! Let’s move on! Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 8 / 24
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 9 / 24
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 10 / 24
▶ This function screen → int × screen can be composed with
▶ It’s closed from the outside, its interface to the outside is very
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 11 / 24
▶ Each and every monad is a function of some argument of type τ ▶ A value of type τ appears in a cross-product of the return type
▶ Composing two monads that both abstract over and return τ
▶ Given two monads M1, M2 : τ → ⟨σ, τ⟩, they represent
▶ This composition is a sequence of monadic operations, and is
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 12 / 24
▶ We wish to thread our computation with a store or environment,
▶ Monad : Store →
▶ An action is an abstact computational object that maps a value
▶ printf is an action that maps to an IO-monad ▶ set! is an action that maps to a store-monad ▶ Action : Value → Store →
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 13 / 24
▶ The simplest action does nothing with either the value of the
▶ It is useful to defjne the Lift operator, which maps functions to
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 14 / 24
▶ Actions can be added to the end of a monad, resulting in a new,
▶ The Pipe operation is how sequencing is performed using
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 15 / 24
▶ Later, when we implement monads in some formalism or
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 16 / 24
▶ Store-monads are abstract computational objects that are
▶ A computation is encoded starting from a very simple monad,
▶ And actions that have been added onto it, in a pipeline fashion,
▶ The Pipe operation is designed so that the store that threads
▶ Defjning monads in the λ-calculus (and in any programming
▶ We then need to defjne actions that are specifjc to the
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 17 / 24
▶ Defjning the Unit is straightforward, and follows from the
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 18 / 24
▶ The defjnition of Pipe is trickier. Pipe takes a monad and an
▶ m is a monad ▶ a is an action ▶ s is a store Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 19 / 24
▶ The new monad should apply the old monad to the store,
▶ Applying the monad to the store is given by (m s) ▶ Since the result is a pair, we apply it to (λv′s′. · · · ), where v′
▶ What shall we do with v′, s′ ? Looking at the abstract defjnition
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 20 / 24
▶ Putting all this together, we get a defjnition for Pipe as follows:
▶ For completeness, we now defjne the Lift operator
▶ This is a straightforward encoding of the abstract defjnition
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 21 / 24
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 22 / 24
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 23 / 24
▶ Possibly additional topics, depending on time
Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 24 / 24