programming language concepts, cs2104 lecture 01 (2003-08-15)

38
1 2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 1 Programming Language Concepts, cs2104 Lecture 01 (2003-08-15) Seif Haridi Department of Computer Science, NUS 2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 2 Overview Organization Course overview Getting started

Upload: others

Post on 06-Dec-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

1

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 1

Programming Language Concepts, cs2104

Lecture 01 (2003-08-15)

Seif HaridiDepartment of Computer Science,

[email protected]

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 2

Overview� Organization� Course overview� Getting started

Page 2: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

2

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 3

Organization

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 4

Programming Language Concepts cs2401� cs2104 is a 4 credit points module� written final exam 40%� Midterm exam 20%� Lab/tutorial assignments 40%� Module homepage

http://www.comp.nus.edu.sg/~cs2104IVLE� Teaching� lectures� Combined tutorials/lab sessions (labs)

Page 3: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

3

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 5

Team� Course responsible [Lectures]Seif Haridi [email protected]

[email protected]� Teaching assistants [Tutorials/Labs]

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 6

Lectures� Held by me� One special lectures� guest lecture by Joe Armstrong on Concurrency Oriented Programming

Page 4: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

4

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 7

Lecture Structure� Reminder of last lecture� Overview� Content� Summary� Reading suggestions

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 8

Material� Lecture based on bookConcepts, Model, and Techniques of Computer ProgrammingPeter Van Roy, Seif Haridi� Book coming out this fall by MIT-Press� Copies could be made available at the CO-OP� around 580 pages� price � Electronic version on module webpage

Page 5: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

5

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 9

Reading Suggestions� Will be available on webpage (Lectures)� Initially� Browse as you like� Abstract and Preface [casual]� Introduction 1.1 – 1.15 and 1.19 [careful][try examples]� Appendix A.1 for Oz Development Environment[as you need]

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 10

Tutorials/Labs� Purpose� for self-assessment� rehearse material from lectures� answer questions� deepen understanding� prepare labs assignments� save your time!� Simple assignments� done by teaching assistants/students� Good exercises for the exam…

Page 6: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

6

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 11

Tutorial: Example� First/Second tutorial session exercises list processing� Is needed in first assignment� Attend the tutorials, save some work!� You can discuss tutorial/chapters on the IVLE

discussion groups

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 12

Assignments� There will be five lab assignments� One discussion group per assignment� discuss lab assignments� answer questions to assignments� solutions to be submitted through IVLE� there is a deadline for each assignment� Code of conduct� no cheating� pairs are allowed, state your partner in the answer� for details, see webpage

Page 7: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

7

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 13

Assignments � Please submit in time!� Please submit in time!� Please submit in time!� Please submit in time!� Please submit in time!� Please submit in time!� Please submit in time!� Please submit in time!

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 14

Lab Assignments: Overview� First assignment: train shunting� small problem solving puzzle� lists, recursion, problem modeling� see course webpage� Planned (maybe we will have better ideas)� black and white picture compression

Page 8: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

8

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 15

Used Software� http://www.mozart-oz.org/� programming language: Oz� system: Mozart� interactive system� Requires Emacs on your computer� Available from module webpage� Install your self. Ask your friends� First tutorial will help with installation

Bring your computer!Already download Mozart!

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 16

Lab and Tutorial Groups� Assignment done via IVLE� is everybody subscribed to IVLE?

Page 9: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

9

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 17

No Mailing Lists (use IVLE)� Only on exceptional [email protected]� Questions � There is a discussion group for each book chapter/lectures� There is a discussion group for each lab assignment� Tutorials and lab assignments� use lab sessions only� use tutorials only� no email� Submit your assignments using the corresponding Workbin (IVLE)

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 18

Course Board� Please establish as soon as possible� My first course taught @NUS� you know customs better than me� comments and criticism is very welcome� do not wait with comments and criticism until end� Elect/Choose 4 students to form a course committee � Inform me by email� [email protected]

Page 10: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

10

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 19

Feedback in General� Approach course-board� Approach the teaching assistant� Approach me directly, but please arrange for appointment

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 20

Questions and Using Brakes!� Please do ask questions during the lectures� repeat an explanation� give better explanation� for an example?� Please say when things go too fast!� Please say when things go too slow!

Page 11: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

11

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 21

Course Overview

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 22

Aim� Knowledge and skills in� Programming languages Concepts� Corresponding programming techniques� Acquaintance with� central concepts in computer science related to programming (languages)� Focus on concepts and not on a particular language

Page 12: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

12

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 23

Programming Concepts, Models and Techniques� Declarative programming� Concurrent programming� Programming with explicit state� Object-oriented programming

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 24

Programming Models� Combine� data types together with operations on them� language to write programs� Each model supports different techniques� Sometimes also “Programming Paradigm”� buzzword-kind of style

Page 13: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

13

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 25

Programming Languages� Different programming languages support different programming models� We want to study many models!� Do we have to study many languages?� Learn syntax…� Learn semantics…� Learn the software…

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 26

Pragmatic Way Out � Just one programming language� which supports several programming models� sometimes “multi-paradigm” programming language� Our choice here is Oz� supports computation models of our interest� The focus is on� the programming models!� techniques and concepts!� not the particular language!

Page 14: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

14

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 27

Presenting Computation Models� Based on kernel language� simple language� small number of significant language constructs� goal: simple, minimal� Richer language on top of kernel language� expressed in terms of kernel language� goal: support common programming tasks

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 28

Our Incremental Approach� Start with one kernel language� declarative programming model� Add constructs� yields the other programming models� very few constructs!� very little to understand!

Page 15: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

15

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 29

Models You Know (CS1102)� Java supports� programming with state� object-oriented programming� Clearly, these two models matter!

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 30

Why the Other Models?� Models new to you� declarative programming model� concurrent programming model� Do they matter?� yes, of course� relevant to complex systems� will become clear during course� guest lecture will discuss practical relevance

Page 16: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

16

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 31

Our First Model� Declarative programming model� Approach� informal introduction to important concepts and techniques� introducing the underlying kernel language� formal semantics based on abstract machine� in depth study of programming techniques

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 32

Declarative Programming Model� Ideal of declarative programming� say what you want to compute� let computer find how to compute it� More pragmatically� let the computer provide more support� free the programmer from some burden

Page 17: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

17

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 33

Properties of Declarative Model� Computations are evaluating functions on

data structures� Widely used� functional languages: LISP, Scheme, ML, Haskell, …� logic languages: Prolog, Mercury, …� representation languages: XML, XSL, …� Stateless programming� no update of data structures� yields simplicity

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 34

Getting Started

Page 18: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

18

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 35

Programming Language� Implements a programming model� Describes programs� composed of statements� compute with values� Let’s have a first look� statements� values

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 36

Interactive System� Mark program fragment in Emacs buffer� Feed marked region� program fragment is compiled� compiled program fragment is executed� Interactive system: use like a calculator� Start practice after the lecture

declareX = 1234 * 5678{Browse X}

Page 19: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

19

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 37

Interactive System: What Happens?� Declares variable X� Assigns the variable the value 7006652� obtained by computing 1234*5678� Applies procedure Browse to value stored

under X� pops up a window that shows 7006652declareX = 1234 * 5678{Browse X}

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 38

Variables� Short-cuts for values� Can be assigned at most once� Are dynamically typed as opposed to Java� Two aspects� variable identifier: what you typea string starting with capital letterVar, A, X123, OnlyIfFirstIsCapital� store variable: part of memory systeminitially, an empty box

Page 20: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

20

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 39

Variable Declaration� declare statement� creates new store variable� links identifier X in environment to store variable� Environment� maps identifiers to variables (values)

declareX = …

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 40

Assignment� Assignment takes store variable and replaces with value� Environment will map X to value 42 now

declareX = 42

Page 21: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

21

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 41

Single Assignment� Variable can only be assigned at most once� also: single assignment variable� Incompatible assignment: raise errorX = 43� Compatible assignment: do nothingX = 42

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 42

Redeclaring Variables� Variables can be redeclared� Environment will always map variable identifier to store variable introduced last� here X will refer to 11

declareX = 42declareX = 11

Page 22: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

22

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 43

Data Structures (Values)� Simple data structures� integers 42, ~1, 0~ means unary minus� floating point 1.01, 3.14� atoms atom, ‘Atom’, nil� Compound data structures� lists� tuples, records

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 44

Functions� Defining a function� give a statement that defines what to compute� Applying a function� use the function to compute according to its definition� also: calling a function

Page 23: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

23

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 45

Our First Function� Computes the negative value of an integer� takes one argument: the integer� returns a value: the negated integer� In mathematical notation:Integer → Integer

minus: n |→ ~n

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 46

Function Definition Minus� Declares the variable Minus� Assigns the function to Minusdeclarefun {Minus X}~Xend

Page 24: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

24

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 47

Function Application Minus� Y is assigned value computed by application

of Minus to ~42declareY = {Minus ~42}{Browse Y}

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 48

Syntax� Function definitionfun {Identifier Arguments}body of functionend� Function applicationX = {Identifier Arguments}

Page 25: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

25

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 49

Maximum Function� Computes maximum of two integers� takes two argument: integers� returns a value: the larger integer� In mathematical notation:Integer × Integer → Integer

max: n, m |→ n, n>m

m, otherwise

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 50

Function Definition Maxdeclarefun {Max X Y}if X>Y then Xelse Yendend• New construct: conditional (if-then-else)

Page 26: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

26

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 51

Function Application MaxdeclareX = {Max 42 11}Y = {Max X 102}{Browse Y}

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 52

Now the Minimum � Possible: cut and paste� just repeat what we did for Max� Better: compose from other functions� reuse what you did before� good, when complicated functions can be reused� For minimum of two numbersmin(n,m) = ~max(~n,~m)

Page 27: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

27

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 53

Function Definition Mindeclarefun {Min X Y}{Minus {Max {Minus X}{Minus Y}}}end

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 54

Function Definition Min(With ~)declarefun {Min X Y}~{Max ~X ~Y}end

Page 28: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

28

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 55

Inductive Function Definition� Factorial function n!� inductively defined as0! = 1n! = n * ((n-1)!) � program as function Fact� How to compute?� by recursive application of Fact

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 56

Function Definition Factfun {Fact N}if N==0 then % Equality test1elseN * {Fact N-1}endend

Page 29: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

29

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 57

Recursion� General structure� base case� recursive case� For natural number n often� base case: n is zero� recursive case: n is different from zeron is greater than zero� Much more: next lecture

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 58

Functions Are Special Case� General concept� procedure� plus variable to return computed value� Again: next lecture…

Page 30: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

30

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 59

Summary� Variable� variable declaration� store variables� assignment� Data structures (Simple values)� numbers and atoms� Functions� definition� call (application)� Recursion

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 60

Preview of Next� Interesting data structures� lists, tuples, records� More on variables� bound and unbound variables� partial values� dataflow variables and dataflow synchronization� More on computing� pattern matching� Recursion� recursion over lists� recursion over integers� Computation model: what, why, how?

Page 31: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

31

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 61

Data Structures� Already seen:� number: integers 1, 2, ~1, 0floating point (floats) 1.0, ~1.21� atom: a, ‘Atom’, v123� Now we address: compound data structures� tuple combining several values� list special case of tuple� record generalization of tuple

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 62

Tuples

Page 32: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

32

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 63

Tuples

� Combine several values (variables)� here: 1, a, 2� position is significant!� Have a label� here: stateX=state(1 a 2) state1 a 2X

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 64

Tuple Operations� {Label X} returns label of tuple X� here: state� is an atom� {Width X} returns the width (number of fields)� here: 3� is a positive integer

X=state(1 a 2) state1 a 2X

Page 33: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

33

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 65

Feature Access(Dot Access)� Fields are numbered from 1 to {Width X}� X.N returns N-th field of tuple� here, X.1 returns 1� here, X.3 returns 2� In X.N, N is called feature

X=state(1 a 2) state1 a 2X

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 66

Tuples for Trees

� Trees can be constructed with tuples:declareY=l(1 2) Z=r(3 4)X=m(Y Z)l1 2 r3 4mXY Z

Page 34: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

34

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 67

Equality Operator (==)� Testing equality with an atom or number� simple, must be the same number or atom� okay to use� we will see pattern matching as something much nicer in many cases� Testing equality among trees� not so straightforward� don’t do it, we don’t need it (yet)

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 68

Summary: Tuples� Tuple� label� width� field� feature

Page 35: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

35

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 69

Records

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 70

Records� Records are generalizations of tuples� features can be atoms� features can be arbitrary integers� not restricted to start with 1� not restricted to be consecutive� Records also have label and width� Needed for lab 01, will be discussed again

Page 36: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

36

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 71

Records

� Position is insignificant� Field access is as with tuplesX.a is 1X=state(a:1 2:a b:2) state1 a 2Xa 2 b

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 72

Tuples are Records� ConstructingdeclareX = state(1:a 2:b 3:c)is equivalent toX = state(a b c)

Page 37: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

37

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 73

What we have done� Interesting data structures� lists, tuples, records� More on variables� bound and unbound variables� partial values� dataflow variables and dataflow synchronization� More on computing� pattern matching� Recursion� recursion over lists� recursion over integers� Computation model: what, why, how?

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 74

What you should do?� Chapter 1: Introduction to Programming Concepts� Reading suggestions:� Browse as you like Abstract and Preface [casual],� Introduction 1.1-1.11 [careful], [try examples]

Appendix A.1 for Oz Development Environment [as you need] and Appendix B.1 to B.3� TO DO: install Emacs, Mozart on your PC

Page 38: Programming Language Concepts, cs2104 Lecture 01 (2003-08-15)

38

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 75

What you should do?� Start working on Tutorial 1� Look at Assignment 1

2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 76

See you next week!