Kathleen Fisher Question: Interesting Answer in Algol
Total Page:16
File Type:pdf, Size:1020Kb
10/21/08 cs242! Lisp! Algol 60! Algol 68! Pascal! Kathleen Fisher! ML! Modula! Reading: “Concepts in Programming Languages” Chapter 5 except 5.4.5! Haskell! “Real World Haskell”, Chapter 0 and Chapter 1! (http://book.realworldhaskell.org/)! Many other languages:! Algol 58, Algol W, Euclid, EL1, Mesa (PARC), …! Thanks to John Mitchell and Simon Peyton Jones for some of these slides. ! Modula-2, Oberon, Modula-3 (DEC)! Basic Language of 1960! Simple imperative language + functions! Successful syntax, BNF -- used by many successors! real procedure average(A,n); statement oriented! real array A; integer n; No array bounds.! begin … end blocks (like C { … } )! begin if … then … else ! real sum; sum := 0; Recursive functions and stack storage allocation! for i = 1 step 1 until n Fewer ad hoc restrictions than Fortran! do General array references: A[ x + B[3] * y ] sum := sum + A[i]; Type discipline was improved by later languages! average := sum/n No “;” here.! Very influential but not widely used in US! end; Tony Hoare: “Here is a language so far ahead of its time that it was not only an improvement on its predecessors Set procedure return value by assignment.! but also on nearly all of its successors.”! Question:! Holes in type discipline! Is x := x equivalent to doing nothing?! Parameter types can be arrays, but! No array bounds! Interesting answer in Algol:! Parameter types can be procedures, but! No argument or return types for procedure parameters! integer procedure p; begin Problems with parameter passing mechanisms! …. Pass-by-name “Copy rule” duplicates code, p := p …. interacting badly with side effects! end; Pass-by-value expensive for arrays! Some awkward control issues! Assignment here is actually a recursive call!! goto out of block requires memory management! 1 10/21/08 Substitute text of actual parameter Considered difficult to understand! Unpredictable with side effects! Idiosyncratic terminology! Types were called “modes”! Example Arrays were called “multiple values”! Used vW grammars instead of BNF! procedure inc2(i, j); Context-sensitive grammar invented by van Wijngaarden! integer i, j; begin begin Elaborate type system! i := i+1; k := k+1; Complicated type conversions! j := j+1 A[k] := A[k] +1 end; end; Fixed some problems of Algol 60! inc2 (k, A[k]); Eliminated pass-by-name! Not widely adopted! Is this what you expected?! Adriaan van Wijngaarden! Primitive modes! . Compound modes! Storage management! int! . arrays! real! . structures! Local storage on stack! char! . procedures! Heap storage, explicit alloc, and garbage collection! bool! . sets! string! . pointers! Parameter passing! compl (complex)! Pass-by-value! bits! bytes! Use pointer types to obtain pass-by-reference! sema (semaphore)! format (I/O)! Rich, structured, and Assignable procedure variables! file! orthogonal type system is Follow “orthogonality” principle rigorously! a major contribution of Algol 68. A Tutorial on Algol 68 by Andrew S. Tanenbaum! Designed by Niklaus Wirth (Turing Award)! Array bounds part of type! illegal! Revised the type system of Algol! procedure p(a : array [1..10] of integer) Good data-structuring concepts! procedure p(n: integer, a : array [1..n] of integer) records, variants, subranges! Attempt at orthogonal design backfires! More restrictive than Algol 60/68! – Parameter must be given a type! Procedure parameters cannot have procedure – Type cannot contain variables! parameters! How could this have happened? Emphasis on teaching?! Popular teaching language! . Not successful for “industrial-strength” projects! Simple one-pass compiler! . Kernighan: “Why Pascal is not my favorite language”! . Left niche for C; niche has expanded!!! Niklaus Wirth! 2 10/21/08 ML Statically typed, general-purpose programming language! Type safe!! Designed by Dennis Ritchie, Turing Award winner, for writing Unix! Intended for interactive use! Evolved from B, which was based on BCPL! Combination of Lisp and Algol-like features! Expression-oriented! B was an untyped language; C adds some checking! Higher-order functions! Garbage collection! Relationship between arrays and pointers! Abstract data types! An array is treated as a pointer to first element! Module system! E1[E2] is equivalent to ptr dereference: *((E1)+(E2)) Exceptions! Pointer arithmetic is not common in other languages! Designed by Turing-Award winner Robin Milner for LCF Theorem Prover! Ritchie quote! Used in textbook as example language! “C is quirky, flawed, and a tremendous success.”! Haskell Haskell is a programming language that is! Similar to ML: general-purpose, strongly typed, higher-order, Good vehicle for studying language concepts! functional, supports type inference, supports interactive and Types and type checking! compiled use! General issues in static and dynamic typing! Different from ML: lazy evaluation, purely functional, rapidly evolving type system.! Type inference! Parametric polymorphism! Designed by committee in 80’s and 90’s to unify Ad hoc polymorphism! research efforts in lazy languages.! Control! Haskell 1.0 in 1990, Haskell ‘98, Haskell’ ongoing. ! Lazy vs. eager evaluation! “A History of Haskell: Being Lazy with Class” HOPL 3! Tail recursion and continuations! Paul Hudak! Simon Precise management of effects! Peyton Jones! John Hughes! Phil Wadler! ! Functional programming will make you think 1,000,000 differently about programming.! Mainstream languages are all about state! 10,000 Functional programming is all about values! Practitioners Ideas will make you a better programmer in 100 ! whatever language you regularly use.! The quick death! 1 Haskell is “cutting edge.” A lot of current Geeks research is done in the context of Haskell.! 1yr 5yr 10yr 15yr 3 10/21/08 ! ! Threshold of immortality! 1,000,000 1,000,000 10,000 10,000 Practitioners Practitioners The complete 100 100 absence of death! ! The slow death! ! 1 1 Geeks Geeks 1yr 5yr 10yr 15yr 1yr 5yr 10yr 15yr “Learning Haskell is a great way of training yourself to think functionally so you are ready to take full advantage of ! “I'm already looking at coding C# 3.0 when it comes out” ! problems and my mental (blog Apr 2007)! In Haskell, f :: A → B means for every x ∈ A,! perspective is now shifting 1,000,000 back and forth between purely OO and more FP styled solutions” ! f(x) = some element y = f(x) ∈ B! (blog Mar 2007)! 10,000 run forever! Practitioners 100 ! The second life?! In words, “if f(x) terminates, then f(x) ∈ B.”! In ML, functions with type A → B can throw an 1 Geeks exception, but not in Haskell.! 1990 1995 2000 2005 2010 Functions that take other functions as arguments or return as a result are higher-order functions.! Interactive Interpretor (ghci): read-eval-print! ghci infers type before compiling or executing! Common Examples:! Type system does not allow casts or other loopholes!! Map: applies argument function to each element in a collection.! Reduce: takes a collection, an initial value, and a function, and Examples! combines the elements in the collection according to the function.! Prelude> (5+3)-2 6 list = [1,2,3] it :: Integer r = foldl (\accumulator i -> i + accumulator) 0 list Prelude> if 5>3 then “Harry” else “Hermione” “Harry” Google uses Map/Reduce to parallelize and distribute it :: [Char] -- String is equivalent to [Char] massive data processing tasks. Prelude> 5==4 False (Dean & Ghemawat, OSDI 2004)! it :: Bool 4 10/21/08 Booleans! Tuples! True, False :: Bool if … then … else … --types must match (4, 5, “Griffendor”) :: (Integer, Integer, String) Integers! Lists! 0, 1, 2, … :: Integer [] :: [a] -- polymorphic type +, * , … :: Integer -> Integer -> Integer 1 : [2, 3, 4] :: [Integer] -- infix cons notation Strings! Records! “Ron ! Weasley” data Person = Person {firstName :: String, lastName :: String} Floats! hg = Person { firstName = “Hermione”, lastName = “Granger”} 1.0, 2, 3.14159, … --type classes to disambiguate Haskell Libraries! Patterns can be used in place of variables! <pat> ::= <var> | <tuple> | <cons> | <record> …! Anonymous function! \x -> x+1 --like Lisp lambda, function (…) in JS Value declarations! General form ! Declaration form! <pat> = <exp>! <name> ! <pat > = <exp >! Examples! 1 1 <name> <pat > = <exp > …! myTuple = (“Flitwick”, “Snape”) 2 2 (x,y) = myTuple <name> <patn> = <expn> …! myList = [1, 2, 3, 4] z:zs = myList Examples! Local declarations! f !(x,y) = x+y --actual parameter must match pattern (x,y) length [] = 0 let (x,y) = (2, “Snape”) in x * 4 length (x:s) = 1 + length(s) Apply function to every element of list! Append lists! map f [] = [] append ([], ys) = ys map f (x:xs) = f x : map f xs append (x:xs, ys) = x : append (xs, ys) map (\x -> x+1) [1,2,3] [2,3,4] Reverse a list! Compare to Lisp reverse [] = [] reverse (x:xs) = (reverse xs) ++ [x] (define map (lambda (f xs) (if (eq? xs ()) () Questions! (cons (f (car xs)) (map f (cdr xs))) ))) How efficient is reverse?! Can it be done with only one pass through list?! 5 10/21/08 Examples! data ! Color = Red | Yellow | Blue reverse xs = elements are Red, Yellow, Blue let rev ( [], accum ) = accum rev ( y:ys, accum ) = rev ( ys, y:accum ) data Atom = Atom String | Number Int in rev ( xs, [] ) elements are Atom “A”, Atom “B”, …, Number 0, ...! data List = Nil | Cons (Atom, List) elements are Nil, Cons(Atom “A”, Nil), …! # Cons(Number 2, Cons(Atom(“Bill”), Nil)), ...! General form! 1 3 data <name> = <clause> | … | <clause>! 2 2 2 2 <clause> ::= <constructor>