760 likes | 976 Views
Advanced Programming Languages. Lecture 1 NCCU CS Dept. Fall 2005 Sept. 19, 2005. Topics. Haskell, Part 1 Lambda Calculus Operational Semantics Denotational Semantics Polymorphism & Type Inference Haskell, Pat 2 (Type classes, …) Subtyping and OO Research Papers. Reading Materials.
E N D
Advanced Programming Languages Lecture 1 NCCU CS Dept. Fall 2005 Sept. 19, 2005
Topics • Haskell, Part 1 • Lambda Calculus • Operational Semantics • Denotational Semantics • Polymorphism & Type Inference • Haskell, Pat 2 (Type classes, …) • Subtyping and OO • Research Papers
Reading Materials • Lecture Notes from Prof. Johan Glimming • Haskell • Tutorial: A Gentle Introduction to Haskell • SML lecture notes • Other lecture notes • Textbooks: • B. Pierce, Type Systems and Programming Languages, MIT Press, 2002 • Nielson’s, Semantics with Applications, Draft, 2005 • Papers
Haskell-based Textbooks • Simon Thompson. Haskell: The Craft of Functional Programming, Addison Wesley, 1999. • Richard Bird. Introduction to Functional Programming Using Haskell, second edition, Prentice Hall Europe, 1998. • Paul Hudak. The Haskell School of Expression, Cambridge University Press, 2000. • H. C. Cunningham. Notes on Functional Programming with Gofer, Technical Report UMCIS-1995-01, University of Mississippi, Department of Computer and Information Science, Revised January 1997. http://www.cs.olemiss.edu/~hcc/reports/gofer_notes.pdf
Other FP Textbooks Of Interest • Fethi Rabhi and Guy Lapalme. Algorithms: A Functional Approach, Addison Wesley, 1999. • Chris Okasaki. Purely Functional Data Structures, Cambridge University Press, 1998.
Programming Language Paradigms • Imperative languages • have implicit states • use commands to modify state • express how something is computed • include C, Pascal, Ada, … • Declarative languages • have no implicit states • use expressions that are evaluated • express what is to be computed • have different underlying models • functions: Lisp (Pure), ML, Haskell, …spreadsheets? SQL? • relations (logic): Prolog (Pure) , Parlog, …
Orderly Expressions andDisorderly Statements Values ofxandydepend upon order of execution of statements x represents different values in different contexts
Summary: Why Use Functional Programming? • Referential transparency • symbol always represents the same value • Equational reasoning (equals can be substituted by equals) • easy mathematical manipulation, parallel execution, etc. • Expressive and concise notation • Higher-order functions • take/return functions • powerful abstraction mechanisms • Lazy evaluation • defer evaluation until result needed • new algorithmic approaches • Polymorphic Type systems
Why Teach/Learn FP and Haskell? • Introduces new problem solving techniques • Improves ability to build and use higher-level procedural and data abstractions • Helps instill a desire for elegance in design and implementation • Increases comfort and skill in use of recursive programs and data structures • Develops understanding of programming languages features such as type systems • Introduces programs as mathematical objects in a natural way
Haskell: http://haskell.org/ • Haskell is a general purpose, purely functional programming language. • Started in 1987; current version Haskell 98 (2002 revised) • Yale Univ. & Glasgow Univ. • Chalmers Univ. (Sweden) • Many free implementations: • Hugs 98, a popular Haskell interpreter (written in C) • Derived from Gopher, by Mark Jones • GHC, the Glasgow Haskell Compiler • … • Good course websites: • http://csit.nottingham.edu.my/~cmichael/Teaching/Feb05/G51FUN/fun.html • http://www.cs.chalmers.se/Cs/Grundutb/Kurser/d1pt/d1pta/external.html
Haskell Timeline Sept 87: kick off Apr 90: Haskell 1.0 Aug 91: Haskell 1.1 (153pp) May 92: Haskell 1.2 (SIGPLAN Notices) (164pp) May 96: Haskell 1.3. Monadic I/O, separate library report Apr 97: Haskell 1.4 (213pp) The Book! Feb 99: Haskell 98 (240pp) Dec 02: Haskell 98 revised (260pp)
Definitions • Definitions name :: type e.g.: size :: Int size = 12 - 3 • Function definitions name :: t1 -> t2 -> … -> tk -> t function name types of type of result arguments e.g.: exOr :: Bool -> Bool -> Bool exOr x y = (x || y) && not (x && y)
Variable binders: x and y get their values from the argument
Currying and Partial Evaluation add :: (Int,Int) -> Int add (x,y) = x + y ? add(3,4) => 7 ? add (3, ) => error add’ takes one argument and returns a function Takes advantage of Currying add' :: Int->(Int->Int) add' x y = x + y ? add’ 3 4 => 7 ? add’ 3 (add’ 3) :: Int -> Int (add’ 3) x = 3 + x ((+) 3)
FoldRight Abstract different binary operators to be applied foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z -- binary op, identity, list foldr f z (x:xs) = f x (foldr f z xs) sumlist :: [Int] -> Int sumlist xs = foldr (+) 0 xs concat' :: [[a]] -> [a] concat' xss = foldr (++) [] xss
Using Partial Evaluation doublePos :: [Int] -> [Int] doublePos xs = map ((*) 2)(filter ((<) 0) xs) • Using operator section notation doublePos xs = map (*2) (filter (0<) xs) • Using list comprehension notation doublePos xs = [ 2*x | x <- xs, 0< x ]