490 likes | 661 Views
Programming. Prof.Dr. Bruce W. Watson Software Construction Research Group b.w.watson@tue.nl www.win.tue.nl/~watson/TM/. Presentation style. Language: English, Nederlands, Afrikaans. Class interaction! Presentations (by you) on the board. Breaks of 15 minutes. Introduction. Who am I?
E N D
Programming Prof.Dr. Bruce W. Watson Software Construction Research Group b.w.watson@tue.nl www.win.tue.nl/~watson/TM/
Presentation style • Language: English, Nederlands, Afrikaans. • Class interaction! • Presentations (by you) on the board. • Breaks of 15 minutes.
Introduction Who am I? • Professor of Software Construction, head of the research group. • TU/e and University of Pretoria. • PhD Eindhoven, Bachelors Waterloo. • Work experience: • Watcom (Canadian company producing programming-language products).
Microsoft (again, working on programming-language products). • Ribbit (startup company, produced hard-core software technologies). • Current activities: • Still very interested in languages, and ways of expressing program ideas. • Software engineering issues, and optimizing and managing software teams.
Course aims (be able to…) • Recognize the essential details of different programming languages. • Critically evaluate information from software engineers/programmers working for you. • Assist in choosing a programming paradigm or style for a project.
Additional material • Programming Languages: concepts & constructs by Ravi Sethi, Addison-Wesley, 1996. • The Practice of Programming by Brian Kernighan and Rob Pike, Addison-Wesley, 1999.
Time-line Over 5 weeks, you get three of 2 hrs lectures/week. At least an additional 2 hrs own research/week • Procedural programming. • Functional programming. • Object-oriented style. • Scripting. • Domain-specific programming. • End assignment (opdracht).
Mini-assignment (10% mark) (Use the WWW.) Choose 3 procedural languages. With all three, identify (examples!) and contrast their versions of the concepts presented today. Some languages: C, Pascal, Modula-2, Modula-3, Fortran, COBOL, Ada, … At least one full A4 page, by email.
Procedural programming What is it? • Closest to the actual operation of a computer. • Sometimes called ‘imperative’ programming. • Focus on the ‘actions’ which take place. • Sequencing the actions. • Not focused on what the actions actually operate on.
Control-flow Flow charts: • Nice visual way to express sequences of actions. • Can be found in many other application areas. • Express three main ideas: • Sequencing. • Choices. • Loops, repetitions, re-doing something. • (Maybe) separation of common applications.
Critique (of flow charts): • Cumbersome (especially when large). • Difficult to reason about. • Difficult to understand when large (complexity and layouts). • Not good for typing-in.
Text notations: • Sequencing: semicolons, new-lines, … • Choices: • If-then statements. • Else clauses. • Problems with them. • Loops: • While. • Repeat/until. • Definite iterations. • Breaks and continues. • Freak: goto.
Expressions What happens in an ‘action’? • Computing an expression using constants or the current values of some variables. • Update of at least one variable with the resulting expression value. Other uses? • Decision points in choices and loops use Boolean expressions.
Types Values are grouped into types: • Safety (when do assignments make sense?). • Values versus types. Variables are declared: • Memory can be allocated. • Checking (for safety) can be done. Type-checking can be static or dynamic: • Efficiency. • When are errors discovered.
Basic types: • Enumerations. • Integers. • Reals. • Subranges. Conversions: • Subtyping. • Automatic conversions: coercions.
Constructed types: • Why? • Large amounts of data need/have internal structure. • Arrays: • Sequence of values of one type. • Layout. • Uses. • Initialization. • Multiple dimensions.
Records: • Grouping (aggregation) of values of various types. • Layout. • Uses. • Initialization. • Pointers: • Notion of indirection. • Others: sets, trees, …
Procedures (and functions) • Programs usually contain parts which look similar. • Similar parts can be split out into a separate piece, called a procedure. • Procedures should at least have: • A name. • A body. • Using a procedure is conceptually the same as simple replacement.
Most interesting procedures have some special variables (parameters): • A baking-oven operator needs to know: • What to bake. • The temperature. • The duration.
Variables: • Global. • Local. Parameter passing mechanisms: • By value. • By reference. (How are these specified?)
Related issues: • Scope of a variable. • Nested scopes. • Lifetime of a variable. • Dangling pointers. • Recursive procedures: • Which variable is accessed?
Advantages: • Abstraction: the meaning of the procedure can be thought-of at a different level. • Implementation hiding: the implementation (body) can be changed (within reason). • Libraries: complete functionality pieces can be put into libraries.
Pros and cons • Well understood. • Large body of programmers. • Lots of choice of languages. • Good tool support. • In some sense, corresponds to people’s ‘recipe’ thinking.
Functional programming Notion of purity. • No variables which ‘hold’ values, or get assigned-to. • The value of an expression depends only on the subexpressions and the operator. • Expressions cannot have side-effects. • (There are some exceptions to this.)
Expressions in ML Local declarations: • A declaration can be done as close as possible to its use. • This can be done for functions and values. e.g. let val x = 2 in x+x end or let fun f(x)=x-1 in f(8) end
Types in ML • ML is very strongly typed. • Basic types: boolean, int, real, string. • Tuples (pairing, products, etc.). This is the same idea as records. • Operations on pairs: extraction/projection.
More types in ML Lists (of anything): • null • hd • tl • a::x (concatenation). This is right associative. E.g. int list
Still more types in ML Since functions are so important, defining their types is too: int->bool Type declarations: type intpair = int*int;
Defining a function (No procedures!) In principle: fun <name> <parameters> = <body>; E.g. fun plusone n = n + 1; (Parentheses around the ‘n’ also possible.)
More functions in ML Try the following examples (some recursive): • Absolute value. • Length of a list. • Factorial. • Even.
Type inferencing • ML is (usually) smart enough to figure out the types of things. (Type inferencing.) • Polymorphism is also possible. • What is the type of hd? • How about fun I(x)=x;? • Sometimes overloading confuses it: fun add(x,y) = x+y;
Function patterns fun first(x, _) = x; or fun len([]) = 0 | len(a::y) = 1 + len(y); Cover all possibilities: fun head(a::y) = a; hint: what happens with head([])?
First class functions • Functions can be passed as arguments too. • How would you apply some operation to every element of a list? E.g. fun map(f, []) = [] | map(f, (a::y)) = f(a)::map(f, y);
Using first class functions… Doing absolute value on a list. map(abs,l) Or, if you don’t have absolute value yet: let abs x=if x<0 then –x else x end in map(abs, l) end
Longer example of functions(mini-opdracht) Consider how to make a list of prime numbers • Make a list of numbers up to n. • Remove the ones which are not prime. • Use some kind of filtering (sieve). • Walk the list, sieving out the non-prime ones.
Pros and cons • Very pure; easy to debug. • Relatively few programmers available. • Lots of programming languages. • Tool support is quite poor. • Corresponds closely to mathematical notions, but sometimes not to intuition.
Object-orientation Is this really a new paradigm in programming? It can be built on top of: • Procedural (imperative) programming, or • Functional programming, or • Scripting, …
Driving factors • Software is very expensive to build. • You would like to be able to reuse parts of your programs. • The operations (procedures/functions, etc) are usually very closely wrapped-up with the data (and the way the data is structured).
Modules • These are a powerful way to separate the operations from the data. • More importantly: separate behaviour from implementation. • Examples: • Representations of patients, … • Methods for sorting records, adding more information to records, …
Modules… Information hiding: • From a piece of software, only expose the minimum required (the interface) by other pieces of software. • Keep the details of how the implementation works hidden.
Example: Object Pascal (Delphi) unit card; interface type suits = (Heart, Club, Diamond, Spade); colours = (Red, Black); … implementation … end.
Correctness of modules If you have objects everywhere in a program, what guarantees that they’re arranged correctly? • (Imagine a complex relationship between objects.) • Structural invariants are used to express when something is correct. • They can be checked, and an error given if bad.
C++ modules (classes) • Class header. • Public members. • Constructors and destructors. • Member functions (aka methods). • Private members.
What more to object-oriented? Consider a set of geometric shapes. If you have a collection of shapes, how would you draw them? Typically, you need case statements in some big procedure.
You can introduce subtyping instead. • Make one type for each geometric shape. • Arrange for their subtyping. • How can we arrange the procedure now? • Place the relevant program-parts close to where they are used.
Subtyping in C++ • Subtyping is usually known as inheritance. • A class B can inherit from class A: • B gets all the same data-fields as A. • B perhaps adds more data-fields. • B gets all the same operations. • B can add its own versions of some operations. • The correct version of an operation is selected based on the type of the object. This is known as virtuality.
Pros and cons • Thinking corresponds very closely to what you do in the real-world (you can model real things easily). • Can be done with underlying procedural or functional programming. • Good support of tools. • Many programmers have a good grasp of it.
Final assignment • Write an essay of 5 to 10 pages, in 10pt type, single-spaced, with reasonable margins. • Dutch or English. • Write an essay contrasting the three main programming paradigms and domain-specific languages, including: advantages of one over another, advantages in practice, …
Final assignment… • Make sure that you do not simply repeat aspects presented in class; there is more than enough information on the web for this. • Submit before 15 December, electronically to b.w.watson@tue.nl