functional programming
play

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


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

  2. Road Map Functional Programming June 2, 2019 2 / 24 🗹 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

  3. Monads away the implementation details June 2, 2019 Functional Programming metaphors non-determinism, asynchronous computing, and other control side-efgects & IO 3 / 24 imperative features in a purely functional way, while abstracting way: functional programming ▶ 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

  4. Monads ( continued ) and thus the name stuck… while remaining grounded in the functional theory of computation language, and can be implemented in any language that supports higher-order abstraction singularity Functional Programming June 2, 2019 4 / 24 ▶ Computer Scientist Eugenio Moggi connected this particular design pattern with the Category-theoretic concept of a monad, ▶ 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

  5. Monads ( continued ) Even though the term monad was used in Greek philosophy, it was June 2, 2019 Functional Programming difgerential calculus… through the grace of God and perfect perception of all monads 5 / 24 (1714) Gottfried Leibniz who resurrected the term: ▶ Leibniz wrote a treatise on metaphysics titled Monadology ▶ 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, ▶ Monads interact & coexist harmoniously with other monads, ▶ And this was thought up by the same Leibniz who co-invented Mayer Goldberg \ Ben-Gurion University

  6. Monads ( continued ) (Samuel Eilenberg & Saunders Maclane, 1942), which is a theory for the abstract study of mathematical structures monads in Category Theory to the specifjc design pattern that was used in functional programming meanings and uses and applications, it might be best to start with an example of a monad… Functional Programming June 2, 2019 6 / 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

  7. Monads ( continued ) Recall our introduction to functional programming: June 2, 2019 Functional Programming deeply-nested expressions… pass the screen explicitly with each and every call to printf 7 / 24 function: ▶ We considered a simplifjed version of the printf function: string → int ▶ 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: 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 ▶ In particular, it makes sequences of calls to printf into Mayer Goldberg \ Ben-Gurion University

  8. Monads ( continued ) argument, which is the environment: then calling printf with some string- argument returns a function Functional Programming June 2, 2019 8 / 24 ▶ The standard way of handling such an annoying type as string × screen → int × screen is to Curry over the last string → screen → int × screen ▶ If the type of printf is string → screen → int × screen , of type: screen → int × screen ▶ What kind of a function is this? ▶ Nevermind! Let’s move on! Mayer Goldberg \ Ben-Gurion University

  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: Functional Programming June 2, 2019 9 / 24 screen → int × screen . Mayer Goldberg \ Ben-Gurion University

  10. Monads ( continued ) To get the expected output ABC , parts of the output of these functions need to be composed in a peculiar way: Functional Programming June 2, 2019 10 / 24 screen → int × screen screen → int × screen screen → int × screen Mayer Goldberg \ Ben-Gurion University

  11. Monads ( continued ) Suppose we were to discover and defjne this peculiar composition, June 2, 2019 Functional Programming rigid. similar functions 11 / 24 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 ▶ It’s closed from the outside, its interface to the outside is very ☞ This is an example of the IO-monad Mayer Goldberg \ Ben-Gurion University

  12. Monads ( continued ) What does it mean to thread a computation June 2, 2019 Functional Programming the semantics of begin ... end composed with other monads 12 / 24 of the monad ▶ 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 τ threads the computation by τ ▶ Given two monads M 1 , M 2 : τ → ⟨ σ, τ ⟩ , they represent computation threaded by τ , and the value of τ returned by M 1 is then passed onto to M 2 . ☞ The composition of M 1 , M 2 is also a monad, and can be ▶ This composition is a sequence of monadic operations, and is Mayer Goldberg \ Ben-Gurion University

  13. Monads ( continued ) to a monad. June 2, 2019 Functional Programming some monad The store-monad 13 / 24 so as to implement variables that can be changed via side-efgects ▶ We wish to thread our computation with a store or environment, ⟨ Value , Store ′ ⟩ ▶ 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 ⟨ Value ′ , Store ′ ⟩ ▶ Action : Value → Store → � �� � Mayer Goldberg \ Ben-Gurion University

  14. Monads ( continued ) actions: June 2, 2019 Functional Programming action that computes the given function monad function The store-monad ( continued ) some 14 / 24 Unit. Faithful to the above formualtion of an action, we store, returning them exactly as they were received. This ▶ The simplest action does nothing with either the value of the operation corresponds to the identity function, and is called 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 ( Value → Value ′ ) → Value → Store → ⟨ Value ′ , Store ⟩ � �� � � �� � � �� � Mayer Goldberg \ Ben-Gurion University

  15. Monads ( continued ) The store-monad ( continued ) June 2, 2019 Functional Programming programming languages monads, and therefore, it corresponds to, and can be used to 15 / 24 piping system is known as pipe or bind : system. The algebraic operation of attaching a pipe onto a the end of the piping system. The result is a new, longer piping an action becomes yet-another pipe that can be attached onto larger monad. If you think of a monad as a piping system, then ▶ Actions can be added to the end of a monad, resulting in a new, ⟨ Monad , Action ⟩ → Monad ′ ▶ The Pipe operation is how sequencing is performed using provide a semantics for begin · · · end blocks in structured Mayer Goldberg \ Ben-Gurion University

  16. Monads ( continued ) The store-monad ( continued ) specifjc implementation, either in some set-theoretic or computational formalism, or in some programming language. ”joining” of two things, an abstract cross-product, Plato’s CONS , if you will programming language, we shall have the freedom to do so in a way that is natural for that domain. Functional Programming June 2, 2019 16 / 24 ☞ In this section, and in the context of abstract monadic operations, when we mention pairs, we do not have in mind a Rather, these pairs are to be understood as an abstract, ordered ▶ Later, when we implement monads in some formalism or Mayer Goldberg \ Ben-Gurion University

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend