Side Effects (3A) Young Won Lim 1/13/18

Similar documents
Side Effects (3B) Young Won Lim 11/27/17

Side Effects (3B) Young Won Lim 11/23/17

Side Effects (3B) Young Won Lim 11/20/17

Monad Background (3A) Young Won Lim 11/18/17

Monad Background (3A) Young Won Lim 11/8/17

Monad Background (3A) Young Won Lim 10/5/17

Applicatives Comparisons (2C) Young Won Lim 3/6/18

Background Type Classes (1B) Young Won Lim 6/28/18

Polymorphism Overview (1A) Young Won Lim 2/20/18

State Monad (3D) Young Won Lim 9/25/17

State Monad (3D) Young Won Lim 8/31/17

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

Maybe Monad (3B) Young Won Lim 12/21/17

Background Type Classes (1B) Young Won Lim 6/14/18

Pointers (1A) Young Won Lim 11/1/17

IO Monad (3C) Young Won Lim 1/6/18

Monad Background (3A) Young Won Lim 11/20/17

Pointers (1A) Young Won Lim 3/5/18

IO Monad (3D) Young Won Lim 1/18/18

Applications of Pointers (1A) Young Won Lim 12/26/17

Monad Overview (3B) Young Won Lim 1/16/18

Haskell Overview II (2A) Young Won Lim 8/9/16

Logic Haskell Exercises

Function Overview (1A) Young Won Lim 10/23/17

GHCi: Getting started (1A) Young Won Lim 5/26/17

Arrays (1A) Young Won Lim 12/4/17

Arrays (1A) Young Won Lim 1/27/17

Monad (3A) Young Won Lim 8/9/17

Monad (1A) Young Won Lim 6/9/17

CS 11 Haskell track: lecture 1

Maybe Monad (3B) Young Won Lim 1/3/18

Pointers (1A) Young Won Lim 2/6/18

Background Operators (1E) Young Won Lim 7/7/18

Accessibility (1A) Young Won Lim 8/22/13

Pointers (1A) Young Won Lim 1/14/18

Pointers (1A) Young Won Lim 2/10/18

Monad (1A) Young Won Lim 6/26/17

Background Functions (1C) Young Won Lim 4/3/18

IO Monad (3C) Young Won Lim 8/23/17

GHCi: Getting started (1A) Young Won Lim 6/3/17

Introduction to Haskell

Background Constructors (1A) Young Won Lim 7/9/18

CS457/557 Functional Languages

Haskell Overview III (3A) Young Won Lim 10/4/16

Monad (1A) Young Won Lim 6/21/17

Lecture 8: Summary of Haskell course + Type Level Programming

Monad P1 : Several Monad Types (4A) Young Won Lim 2/13/19

Pointers (1A) Young Won Lim 1/22/18

Background Functions (1C) Young Won Lim 5/26/18

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Haskell Overview II (2A) Young Won Lim 8/23/16

State Monad Example (3H) Young Won Lim 2/16/18

Programming in Haskell Aug Nov 2015

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

A Brief Haskell and GHC Refresher

GHCi: Getting started (1A) Young Won Lim 5/31/17

Functional Languages. Hwansoo Han

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Chapter 11 :: Functional Languages

Background Constructors (1A) Young Won Lim 7/5/18

Background Constructors (1A) Young Won Lim 6/30/18

Background Constructors (1A) Young Won Lim 1/22/18

Class (1A) Young Won Lim 9/8/14

Background Expressions (1D) Young Won Lim 7/7/18

CSCE 314 Programming Languages

Functional Programming

State Monad Examples (6C) Young Won Lim 9/26/18

Functional Programming in C++

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Programming Language Pragmatics

State Monad Examples (6C) Young Won Lim 9/22/18

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

G Programming Languages - Fall 2012

Programming Languages Fall 2013

Functions (4A) Young Won Lim 5/8/17

Overview (1A) Young Won Lim 9/14/17

Programming language design and analysis

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Overview (1A) Young Won Lim 9/9/17

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Overview IV (4A) Young Won Lim 10/24/16

Applications of Structures (1A) Young Won Lim 12/8/17

Applications of Pointers (1A) Young Won Lim 4/24/18

JVM ByteCode Interpreter

Overview (1A) Young Won Lim 9/25/17

Intro to Haskell Notes: Part 5

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Monoid (4A) Young Won Lim 8/12/17

Functional Logic Programming Language Curry

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Applications of Arrays (1A) Young Won Lim 3/15/17

Functions (4A) Young Won Lim 3/16/18

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Introduction. chapter Functions

Types and Static Type Checking (Introducing Micro-Haskell)

Transcription:

Side Effects (3A)

Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Based on Haskell in 5 steps https://wiki.haskell.org/haskell_in_5_steps 3

Variables Imperative programming: variables as changeable locations in a computer's memory imperative programs explicitly commands the computer what to do functional programming a way to think in higher-level mathematical terms defining how variables relate to one another leaving the compiler to translate these to the step-by-step instructions that the computer can process. https://en.wikibooks.org/wiki/haskell/variables_and_functions 4

Haskell Language Features Haskell Functional Programming Immutability Recursive Definition No Data Dependency https://en.wikibooks.org/wiki/haskell/variables_and_functions 5

Redefinition : not allowed imperative programming: after setting r = 5 and then changing it to r = 2. r = 5 r = 2 Hakell programming: an error: "multiple declarations of r". Within a given scope, a variable in Haskell gets defined only once and cannot change, like variables in mathematics. r = 5 r = 2 No mutation In Haskell https://en.wikibooks.org/wiki/haskell/variables_and_functions 6

Variables in a file Immutable: they can change only based on the data we enter to run the program. We cannot define r two ways in the same code, but we could change the value by changing the file Vars.hs a = 100 r = 5 pi = 3.14159 e = 2.7818 https://en.wikibooks.org/wiki/haskell/variables_and_functions 7

Loading a variable definition file $ ghci GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help Prelude> :load Var1.hs [1 of 1] Compiling Main ( var.hs, interpreted ) Ok, modules loaded: Main. *Main> r 5 *Main> :t r r :: Integer *Main> *Main> :load Var2.hs [1 of 1] Compiling Main ( var2.hs, interpreted ) Ok, modules loaded: Main. *Main> r 55 :load Var1.hs :load Var1.hs definition with initialization Var1.hs file r = 5 x = 1 y = 3.14 Var2.hs file r = 55 x = 1 y = 3.14 https://en.wikibooks.org/wiki/haskell/variables_and_functions 8

No Mutation *Main> r = 33 <interactive>:12:3: parse error on input = No mutation, Immutable $ ghci GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help Prelude> r = 333 <interactive>:2:3: parse error on input = Prelude> let r = 33 let r = 33 https://en.wikibooks.org/wiki/haskell/variables_and_functions 9

Recursive Definition imperative programming: r = r + 1 incrementing the variable r (updating the value in memory) Hakell programming: a recursive definition of r Side effect, Stateful computation (defining it in terms of itself) if r had been defined with any value beforehand, then r = r + 1 in Haskell would bring an error message. Use a function addone x = x + 1 https://en.wikibooks.org/wiki/haskell/variables_and_functions 10

Simulating imperative codes The most primitive way of x = v is to use a function taking x as argument, and pass v to that function. // sum 0..100 i = s = 0; while (i <= 100) { s = s+i; i++; } return s; sum = f 0 0 -- the initial values where f i s i <=100 = f (i+1) (s+i) // increment i, augment s otherwise = s // return s at the end The above code is not pretty FP code, but it is simulating imperative code https://stackoverflow.com/questions/43525193/how-can-i-re-assign-a-variable-in-a-function-in-haskell 11

No Data Dependence y = x * 2 x = 3 x = 3 y = x * 3 Hakell programming: because the values of variables do not change variables can be defined in any order no mandatory : "x being declared before y" https://en.wikibooks.org/wiki/haskell/variables_and_functions 12

Evaluation area 5 => { replace the LHS area r =... by the RHS... = pi * r^2 } pi * 5 ^ 2 => { replace pi by its value } 3.141592653589793 * 5 ^ 2 => { apply exponentiation (^) } 3.141592653589793 * 25 => { apply multiplication (*) } 78.53981633974483 area r = pi * r^2 pi = 3.141592653589793 5^2 = 25 3.141592653589793 * 25 = 78.53981633974483 https://en.wikibooks.org/wiki/haskell/variables_and_functions 13

Translation to instructions functional programming leaving the compiler to translate these to the step-by-step instructions that the computer can process. replace each function and variable with its definition repeatedly replace the results until a single value remains. to apply or call a function means to replace the LHS of its definition by its RHS. https://en.wikibooks.org/wiki/haskell/variables_and_functions 14

Side Effects Definition a function or expression is said to have a side effect if it modifies some state outside its scope or has an observable interaction with its calling functions or the outside world besides returning a value. a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data from a keyboard or file, or call other side-effecting functions. https://en.wikipedia.org/wiki/side_effect_(computer_science) 15

History, Order, and Context In the presence of side effects, a program's behaviour may depend on history; the order of evaluation matters. the context and histories Imperative programming : frequent utilization of side effects. functional programming : side effects are rarely used. The lack of side effects makes it easier to do formal verifications of a program https://en.wikipedia.org/wiki/side_effect_(computer_science) 16

Side Effects Examples in C int i, j; i = j = 3; i = (j = 3); // j = 3 returns 3, which then gets assigned to i // The assignment function returns 10 // which automatically casts to "true" // so the loop conditional always evaluates to true while (b = 10) { } https://en.wikipedia.org/wiki/side_effect_(computer_science) 17

Pure Languages Haskell is a pure language programs are made of functions that can't change any global state or variables, they can only do some computations and return them results. st1 = 10 s -> (x,s) st1 (v,10) every variable's value does not change in time However, some problems are inherently stateful in that they rely on some state that changes over time. a bit tedious to model Haskell has the state monad features http://learnyouahaskell.com/for-a-few-monads-more 18

Side Effects in Haskell The functional language Haskell expresses side effects such as I/O and other stateful computations using monadic actions state monad https://en.wikipedia.org/wiki/side_effect_(computer_science) 19

Stateful Computation a stateful computation is a function that takes some state and returns a value along with some new state. s -> (a, s) s (a, s) That function would have the following type: s -> (a,s) s is the type of the state and a the result of the stateful computation. a function is an executable data when executed, a result is produced action, execution, result http://learnyouahaskell.com/for-a-few-monads-more 20

Assignment Assignment in an imperative language : will assign the value 5 to the variable x will have the value 5 as an expression x = 529 x = 5 Assignment in a functional language as a function that takes a state and returns a result and a new state s (a, s) w0 (5, w1) http://learnyouahaskell.com/for-a-few-monads-more 21

Assignment as a stateful computation Assignment in a functional language as a function that takes a state and returns a result and a new state an input state : all the variables that have been assigned previously a result : 5 a new state : all the previous variable mappings plus the newly assigned variable. s -> (a, s) s (a, s) all the variables that have been assigned previously x = 5 all the previous variable mappings plus the newly assigned variable a result : 5 http://learnyouahaskell.com/for-a-few-monads-more 22

A value with a context The stateful computation: a function that takes a state and returns a result and a new state can be considered as a value with a context the actual value is the result the context is that we must provide an initial state to get the result and that apart from getting the result, we also get a new state. s -> (a, s) s (a, s) all the current variable mappings context a result : 5 all the previous variable mappings the new variable mapping a value in Haskell http://learnyouahaskell.com/for-a-few-monads-more 23

Side Effects of IO Monad Generally, a monad cannot perform side effects in Haskell. there is one exception: IO monad Suppose there is a type called World, which contains all the state of the external universe World World -> (t, World) (t, World) A way of thinking what IO monad does type IO t = World -> (t, World) type synonym In Haskell, no variable changes a state transition via a function a collection of variables (state) a new collection of variables (updated) In Haskell, a function is a value an action an executable function https://www.cs.hmc.edu/~adavidso/monads.pdf 24

Computations that result in values Monads like IO map types t to a new type IO t that represent "computations that result in values" a function type World -> (t, World) the result type : t type IO t = World -> (t, World) https://wiki.haskell.org/maybe 25

Type Synonym IO t IO t is a parameterized function type input : a World output: a result value of the type t and a new updated World are obtained by modifying the given World in the process of computing the result value of the type t. World -> (t, World) World (t, World) IO t type IO t = World -> (t, World) type synonym World (t, World) cf) type application https://www.cs.hmc.edu/~adavidso/monads.pdf 26

Variable Mappings : Context x :: a s -> (a, s) s (a, s) w0 IO a func (x, w1) all the current variable mappings all the previous variable mappings the new variable mapping a = 1 b = 2 a = 1 b = 2 x = 5 a result : 5 w0 :: s w1 :: s http://learnyouahaskell.com/for-a-few-monads-more 27

Types involved in stateful computations type IO a = s -> (a, s) type IO Int = s -> (Int, s) type IO Char = s -> (Char, s) type IO Int = Int -> (Int, Int) func :: IO Int w0 a = 1 b = 2 w0 :: s x :: a IO a func (x, w1) a = 1 b = 2 x = 5 w1 :: s func :: IO a func :: IO World http://learnyouahaskell.com/for-a-few-monads-more 28

func :: IO Int type type IO Int = Int -> (Int, Int) w0 func (x, w1) func :: IO Int func w0 :: IO Int Int the function type the function return type x w0 :: Int :: Int the result type the function input type initial state type let (x, w1) = func w0 the bindings of x and w1 http://learnyouahaskell.com/for-a-few-monads-more 29

IO t is a function type not a function type IO t = World -> (t, World) type synonym World -> (t, World) World (t, World) IO t World (t, World) w0 IO t (x, w1) https://www.cs.hmc.edu/~adavidso/monads.pdf x :: t w1 :: World (x, w1) :: IO t World 30

IO t World the return type of the function type IO t = World -> (t, World) type synonym World -> (t, World) World (t, World) IO t x :: t w0 :: World w1 :: World (x, w1) :: IO t World World (t, World) w0 func (x, w1) func :: IO t https://www.cs.hmc.edu/~adavidso/monads.pdf func w0 (x, w1) 31

Implementation of IO t It is impossible to store the extra copies of the contents of your hard drive that each of the Worlds contains given World updated World https://www.cs.hmc.edu/~adavidso/monads.pdf 32

IO Monad in GHC Which World was given initially? Which World was updated? The modification of the World In GHC, a main must be defined somewhere with type IO () a program execution starts from the main the initial World is contained in the main to start everything off the main passes the updated World from each IO to the next IO as its initial World an IO that is not reachable from main will never be executed an initial / updated World is not passed to such an IO initial World 1 st IO updated World 2 nd IO updated World 3 rd IO updated World https://www.cs.hmc.edu/~adavidso/monads.pdf 33

IO Monad in GHCI when using GHCI, everything is wrapped in an implicit IO, since the results get printed out to the screen. the implementation of bind there s only 1 World in existence at any given moment. Each IO takes that one and only World, consumes it, and gives back a single new World. Consequently, there s no way to accidentally run out of Worlds, or have multiple ones running around. current World IO only 1 World updated World https://www.cs.hmc.edu/~adavidso/monads.pdf 34

GHCI Every time a new command is given to GHCI, GHCI passes the current World to IO, GHCI gets the result of the command back, GHCI request to display the result (executing actions) the implementation of bind (which updates the World by modifying the contents of the screen or the list of defined variables or the list of loaded modules or whatever), GHCI saves the new World to process the next command. https://www.cs.hmc.edu/~adavidso/monads.pdf 35

IO Actions in main IO action is invoked, after the Haskell program has run We can never execute an IO action inside the program once created, an IO action keeps percolating up until it ends up in main and is executed by the runtime. can also discard an IO action, but that means it will never be evaluated https://www.schoolofhaskell.com/school/starting-with-haskell/basics-of-haskell/3-pure-functions-laziness-io 36

Monadic Effect class Monad m where return :: a -> m a (>>=) :: m a -> (a -> m b) -> m b https://en.wikibooks.org/wiki/haskell/understanding_monads/io https://stackoverflow.com/questions/2488646/why-are-side-effects-modeled-as-monads-in-haskell https://stackoverflow.com/questions/7840126/why-monads-how-does-it-resolve-side-effects https://stackoverflow.com/questions/2488646/why-are-side-effects-modeled-as-monads-in-haskell https://www.cs.hmc.edu/~adavidso/monads.pdf 37

References [1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf [2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf