Com S 541 Lecture -*- Outline -*- * Introduction to Programming Concepts (Chapter 1) ** Working with Mozart/Oz (1.1, Appendix A) *** Browse {Browse 3} {Browse true} {Browse fiddle} *** declare **** variables declare X=20 {Browse X + X} Note that variable names start with an upper case letter! **** functions declare fun {Inc N} N+1 end ------------------------------------------ FOR YOU TO DO Code the Fibonacci function defined by: Fib(0) = 1 Fib(1) = 1 Fib(n) = Fib(n-1) + Fib(n-2), if n >= 2 ------------------------------------------ Q: What's the complexity of your solution? Use lazy to make it faster need to do some demanding operation to see the result. ** Data (Appendix B) See appendix B for details. ------------------------------------------ ATOMIC DATA Numbers: ~5 is minus 5 Character Literals &c is the literal for 'c' == 99 &\n is newline &\012 is also a newline Atoms (like Lisp symbols) an_atom anotherOne ':=' ------------------------------------------ Note that the names of atoms start with a lower case letter. ------------------------------------------ STRUCTURED DATA Records tree(key:I value:Y left:LT right:RT) labeled "tree" with 4 features, whose values are the values of the variables I, Y, LT, and RT Tuples pair(3 4) '#'(a b c) a#b#c Lists nil a|b|nil == '|'(a '|'(b nil)) == [a b] Strings "strings are lists of characters" ------------------------------------------ Q: Does order matter in records? In tuples? Q: How would you define a list recursively? ** Pattern Matching ------------------------------------------ PATTERN MATCHING Syntax: case of then else end Example: Write ListLength, so that {ListLength nil} = 0 {ListLength [a b c d]} = 4 ------------------------------------------ What is a related simplier example? {ListLength [b c d]} = 3 How can we use that to solve the problem? declare ListLength fun {ListLength L} case L of _|T then 1 + {ListLength T} else 0 end end Note that the pattern variables all have to start with capital letters. If you don't use a name, use _ instead of a normal variable name % Some tests of ListLength {Browse {ListLength [a b c]}} declare L L = [a b c d e f] {Browse {ListLength L}} ------------------------------------------ FOR YOU TO DO Write a function Assoc that takes a list of key-value pairs and a key K, and returns the element in the list associated with K, if any. Examples {Assoc [c#3] c} = 3 {Assoc [a#1 b#2 c#3] b} = 2 {Assoc [] z} = nil {Assoc [a#1 b#2 c#3] z} = nil ------------------------------------------ Q: What is the induction over? Lists Q: What's a related simplier example to the second one? ... and how does that help? ** Higher Order Functions ------------------------------------------ HIGHER-ORDER FUNCTIONS Take functions as arguments, or return them. declare Add1 Add2 fun {Add1 X} X+1 end fun {Add2 X} X+2 end declare CAdd fun {CAdd N} fun {$ M} N+M end end {Browse {{CAdd 3} 4}} ------------------------------------------ ------------------------------------------ FOR YOU TO DO Define the function Compose, such that: {{Compose Add1 Add2} 10} = 13 {{Compose {CAdd 100} Add2} 10} = 112 ------------------------------------------ declare Compose fun {Compose F G} fun {$ X} {F {G X}} end end {Browse {{Compose Add1 Add2} 10}} {Browse {{Compose {CAdd 100} Add2} 10}} declare Compose ------------------------------------------ FOR YOU TO DO Write a function Map that takes a function F and a list Lst and returns the list of the results of applying F to each element of Lst, in order. {Map Add2 nil} = nil {Map Add1 [1 2 3]} = [2 3 4] ------------------------------------------ ** threads and dataflow execution *** threads (1.10) ------------------------------------------ THREADS (1.10) Models concurrency in "real world" % thread 1 thread P in P = {Pascal 30} {Browse P} end % thread 2 {Browse 99*99} ------------------------------------------ The declared variable is shorthand *** dataflow execution (1.11) ------------------------------------------ DATAFLOW EXECUTION What should happen if we do: declare A B C C = A+B {Browse C} Waits (suspends) % then later feeding... A=10 B=20 Makes the browser show it ------------------------------------------ "Programming errors often result in dataflow suspensions." p. 89 ------------------------------------------ DATAFLOW AND THREADS % The following suspends, % if fed all at once declare L1 {Browse {ListLength L1}} L1 = [a b] % But with threads this works... declare L2 thread {Browse {ListLength L2}} end L2 = [a b c d e] % What's going on here? declare L3 thread {Browse {ListLength L3}} end thread L3 = a|b|c|L4 end L4 = d|nil ------------------------------------------ ** Explicit State (1.12) Q: Why have explicit state? have memory of what happened previously. Q: How does the book model state? Memory cells with {NewCell X} and access via @ and assignment via := ------------------------------------------ CELLS declare Toggle = {NewCell true} Toggle := {Not @Toggle} {Browse @Toggle} ------------------------------------------ ** Objects and Classes *** Objects (1.13) ------------------------------------------ OBJECTS Object = functions + shared state declare local V in V = {NewCell true} fun {Flip} V := {Not @V} @V end fun {Value} @V end end ------------------------------------------ Q: Is the state necessarily encapsulated? usually, but doesn't have to be to be considered an object :-( *** Classes (1.14) ------------------------------------------ CLASSES Classes are factories that make objects declare fun {NewToggle} V Flip Value in ------------------------------------------ ... V = {NewCell true} fun {Flip} V := {Not @V} @V end fun {Value} @V end toggle(flip:Flip value:Value) end declare T1 = {NewToggle} T2 = {NewToggle} _ = if {T1.value} then {T2.flip} else {T1.flip} end {Browse {T2.value}} {Browse {T1.value}} ------------------------------------------ FOR YOU TO DO Write a class Point that implements 2D points with operations Move, GetX, GetY. ------------------------------------------ Q: This is object-based programming. What other feature(s) is needed for OOP? inheritance. ** Concurrency Problems *** Nondeterminism and time (1.15) Q: What happens if you combine concurrency and state? ------------------------------------------ RACE CONDITIONS declare C = {NewCell 0} thread C := 1 end thread C := 2 end What happens? ------------------------------------------ Similarly for: declare C = {NewCell 0} thread I in I = @C C := I+1 end thread J in J = @C C := J+1 end *** Atomicity (1.16) Q: How can we prevent these problems? Use locks