Functional Programming June 2, 2019 Functional Programming June 2, - - PowerPoint PPT Presentation

functional programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Functional Programming

Mayer Goldberg \ Ben-Gurion University June 2, 2019

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 1 / 24

slide-2
SLIDE 2

Road Map

🗹 Introduction to Functional Programming 🗹 λ-Defjnability 🗹 Tuples & RTTI 🗹 Bases 🗹 Fixed-Point Theory 🗵 Functions as Data 🗹 Streams 🗵 Maps & Folds 🗹 CPS & Threading Code ☞ Monads

▶ Possibly additional topics, depending on time

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 2 / 24

slide-3
SLIDE 3

Monads

▶ Monads are part of a programming technique that is common in

functional programming

▶ Alternately, you may think of monads as design patterns.

▶ This technique allows us to thread a computation in an abstract

way:

▶ Threading computation abstractly allows us to implement many

imperative features in a purely functional way, while abstracting away the implementation details

▶ We can thread computation with an environment, to provide for

side-efgects & IO

▶ We can thread computation with continuations, to provide for

non-determinism, asynchronous computing, and other control metaphors

▶ Monads have been known, if not by name, since the 1960’s

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 3 / 24

slide-4
SLIDE 4

Monads (continued)

▶ Computer Scientist Eugenio Moggi connected this particular

design pattern with the Category-theoretic concept of a monad, and thus the name stuck…

▶ Monads allow us to enjoy both worlds, functional & imperative,

while remaining grounded in the functional theory of computation

▶ Monads are native to Haskell, a purely functional programming

language, and can be implemented in any language that supports higher-order abstraction

▶ The term “monad” comes from the Greek μονάς, meaning

singularity

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 4 / 24

slide-5
SLIDE 5

Monads (continued)

Even though the term monad was used in Greek philosophy, it was Gottfried Leibniz who resurrected the term:

▶ Leibniz wrote a treatise on metaphysics titled Monadology

(1714)

▶ The universe, according to Leibniz, is comprised of simple,

atomic structures known as monads

▶ 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,

and perfect perception of all monads

▶ Monads interact & coexist harmoniously with other monads,

through the grace of God

▶ And this was thought up by the same Leibniz who co-invented

difgerential calculus…

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 5 / 24

slide-6
SLIDE 6

Monads (continued)

▶ The term monad was adopted for use in Category Theory

(Samuel Eilenberg & Saunders Maclane, 1942), which is a theory for the abstract study of mathematical structures

▶ Computer scientist Eugenio Moggi was the fjrst to connect

monads in Category Theory to the specifjc design pattern that was used in functional programming

▶ Because monads can be so many things and have so many

meanings and uses and applications, it might be best to start with an example of a monad…

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 6 / 24

slide-7
SLIDE 7

Monads (continued)

Recall our introduction to functional programming:

▶ We considered a simplifjed version of the printf function:

string → int

▶ We mentioned that we had a problem with the type of this

function:

▶ The type didn’t really capture all the arguments to the function ▶ The full type of printf must include a screen:

string × screen → int × 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

pass the screen explicitly with each and every call to printf

▶ In particular, it makes sequences of calls to printf into

deeply-nested expressions…

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 7 / 24

slide-8
SLIDE 8

Monads (continued)

▶ The standard way of handling such an annoying type as

string × screen → int × screen is to Curry over the last argument, which is the environment: string → screen → int × screen

▶ If the type of printf is string → screen → int × screen,

then calling printf with some string- argument returns a function

  • f type: 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

slide-9
SLIDE 9

Monads (continued)

Suppose we want to compute the sequence: { printf("A"); printf("B"); printf("C"); } Each of these calls to printf results in a function of type: screen → int × screen.

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 9 / 24

slide-10
SLIDE 10

Monads (continued)

To get the expected output ABC, parts of the output of these functions need to be composed in a peculiar way: screen → int × screen screen → int × screen screen → int × screen

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 10 / 24

slide-11
SLIDE 11

Monads (continued)

Suppose we were to discover and defjne this peculiar composition, and we used it to compose these three functions, what would we have? — Something that also took a screen and returned int × screen: screen → int × screen screen → int × screen screen → int × screen

▶ This function screen → int × screen can be composed with

similar functions

▶ It’s closed from the outside, its interface to the outside is very

rigid.

☞ This is an example of the IO-monad

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 11 / 24

slide-12
SLIDE 12

Monads (continued)

What does it mean to thread a computation

▶ 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

  • f the monad

▶ Composing two monads that both abstract over and return τ

threads the computation by τ

▶ Given two monads M1, M2 : τ → ⟨σ, τ⟩, they represent

computation threaded by τ, and the value of τ returned by M1 is then passed onto to M2.

☞ The composition of M1, M2 is also a monad, and can be

composed with other monads

▶ This composition is a sequence of monadic operations, and is

the semantics of begin ... end

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 12 / 24

slide-13
SLIDE 13

Monads (continued)

The store-monad

▶ We wish to thread our computation with a store or environment,

so as to implement variables that can be changed via side-efgects

▶ Monad : Store →

⟨ Value, Store′⟩

▶ An action is an abstact computational object that maps a value

to a monad.

▶ printf is an action that maps to an IO-monad ▶ set! is an action that maps to a store-monad ▶ Action : Value → Store →

⟨ Value′, Store′⟩

  • some monad

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 13 / 24

slide-14
SLIDE 14

Monads (continued)

The store-monad (continued)

▶ The simplest action does nothing with either the value of the

store, returning them exactly as they were received. This

  • peration corresponds to the identity function, and is called
  • Unit. Faithful to the above formualtion of an action, we

characterize Unit as follows: Value → Store → ⟨Value, Store⟩

☞ Notice that Value remains unchanged

▶ It is useful to defjne the Lift operator, which maps functions to

actions: (Value → Value′)

  • some

function → Value → Store → ⟨Value′, Store⟩

  • monad
  • action that computes the given function

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 14 / 24

slide-15
SLIDE 15

Monads (continued)

The store-monad (continued)

▶ Actions can be added to the end of a monad, resulting in a new,

larger monad. If you think of a monad as a piping system, then an action becomes yet-another pipe that can be attached onto the end of the piping system. The result is a new, longer piping

  • system. The algebraic operation of attaching a pipe onto a

piping system is known as pipe or bind: ⟨Monad, Action⟩ → Monad′

▶ The Pipe operation is how sequencing is performed using

monads, and therefore, it corresponds to, and can be used to provide a semantics for begin · · · end blocks in structured programming languages

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 15 / 24

slide-16
SLIDE 16

Monads (continued)

The store-monad (continued) ☞ In this section, and in the context of abstract monadic

  • perations, when we mention pairs, we do not have in mind a

specifjc implementation, either in some set-theoretic or computational formalism, or in some programming language. Rather, these pairs are to be understood as an abstract, ordered ”joining” of two things, an abstract cross-product, Plato’s CONS, if you will

▶ Later, when we implement monads in some formalism or

programming language, we shall have the freedom to do so in a way that is natural for that domain.

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 16 / 24

slide-17
SLIDE 17

Monads (continued)

The store-monad (continued)

▶ Store-monads are abstract computational objects that are

parameterized by a store.

▶ A computation is encoded starting from a very simple monad,

which serves to initialize the store

▶ And actions that have been added onto it, in a pipeline fashion,

to form larger monads

▶ The Pipe operation is designed so that the store that threads

each action shall thread the entire combined monad

▶ Defjning monads in the λ-calculus (and in any programming

language) requires that we defjne the Pipe, Unit, and Lift

  • perations, which are general, and can be used for many

purposes

▶ We then need to defjne actions that are specifjc to the

computation we wish to perform

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 17 / 24

slide-18
SLIDE 18

Monads (continued)

The store-monad (continued)

▶ Defjning the Unit is straightforward, and follows from the

commonly-used representation of ordered-pairs in the λ-calculus, and the abstract defjnition of Unit in the previous section: Unit = λvsx.(x v s) ≡ ⟨␣, ␣⟩

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 18 / 24

slide-19
SLIDE 19

Monads (continued)

The store-monad (continued)

▶ The defjnition of Pipe is trickier. Pipe takes a monad and an

action, and returns another monad, which means that it returns a function of a store. Our defjnition shall therefore start with λmas. · · · , where

▶ m is a monad ▶ a is an action ▶ s is a store Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 19 / 24

slide-20
SLIDE 20

Monads (continued)

The store-monad (continued)

▶ The new monad should apply the old monad to the store,

getting a pair of a value and a new store. The value should be passed onto the action, followed by the store, and the resulting monad is the result of the computation

▶ 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′

stands for the resulting value, and s′ stands for the resulting store

▶ What shall we do with v′, s′ ? Looking at the abstract defjnition

for Action, in the previous section, we see that it expects a value and a store, so what we want to return is (a v′ s′)

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 20 / 24

slide-21
SLIDE 21

Monads (continued)

The store-monad (continued)

▶ Putting all this together, we get a defjnition for Pipe as follows:

λmas.(m s (λv′s′.(a v′ s′))). Notice that we have two

  • pportunities for η-reduction, giving us the fjnal form of:

λmas.(m s a)

🤕 Notice that Pipe is none other than the C-combinator

▶ For completeness, we now defjne the Lift operator

▶ This is a straightforward encoding of the abstract defjnition

given in the previous section: Lift = λfvsx.(x (f v) s)

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 21 / 24

slide-22
SLIDE 22

Monads (continued)

The store-monad — Summary

We summarize the monad operators: Unit ≡ λvsx.(x v s) Pipe ≡ λmas.(m s a) ≡ C Lift ≡ λfvsx.(x (f v) s)

☞ AFAIK, this is the simplest & shortest implementation of the

monadic operators

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 22 / 24

slide-23
SLIDE 23

Monads (continued)

The store-monad — Examples

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 23 / 24

slide-24
SLIDE 24

Road Map

🗹 Introduction to Functional Programming 🗹 λ-Defjnability 🗹 Tuples & RTTI 🗹 Bases 🗹 Fixed-Point Theory 🗵 Functions as Data 🗹 Streams 🗵 Maps & Folds 🗹 CPS & Threading Code 🗹 Monads

▶ Possibly additional topics, depending on time

Mayer Goldberg \ Ben-Gurion University Functional Programming June 2, 2019 24 / 24