Docsity

Programming Paradigms: A Comprehensive Overview, Summaries of Programming Paradigms

Toggle sidebar
bg1
Programming Paradigms for
Dummies: What Every
Programmer Should Know
Peter Van Roy
This chapter gives an introduction to all the main programming paradigms, their un-
derlying concepts, and the relationships between them. We give a broad view to help
programmers choose the right concepts they need to solve the problems at hand. We
give a taxonomy of almost 30 useful programming paradigms and how they are related.
Most of them differ only in one or a few concepts, but this can make a world of difference
in programming. We explain briefly how programming paradigms influence language
design, and we show two sweet spots: dual-paradigm languages and a definitive lan-
guage. We introduce the main concepts of programming languages: records, closures,
independence (concurrency), and named state. We explain the main principles of data
abstraction and how it lets us organize large programs. Finally, we conclude by focus-
ing on concurrency, which is widely considered the hardest concept to program with.
We present four little-known but important paradigms that greatly simplify concurrent
programming with respect to mainstream languages: declarative concurrency (both ea-
ger and lazy), functional reactive programming, discrete synchronous programming, and
constraint programming. These paradigms have no race conditions and can be used in
cases where no other paradigm works. We explain why for multi-core processors and we
give several examples from computer music, which often uses these paradigms.
More is not better (or worse) than less, just different.
– The paradigm paradox.
1 Introduction
Programming is a rich discipline and practical programming languages are usually quite
complicated. Fortunately, the important ideas of programming languages are simple.
This chapter is intended to give readers with some programming experience a running
start for these ideas. Although we do not give formal definitions, we give precise intu-
itions and good references so that interested readers can quickly get started using the
concepts and languages that implement them. We mention all important paradigms but
we favor some little-known paradigms that deserve to be more widely used. We have
deliberately left out detailed explanations of some of the more well-known paradigms
9
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download Programming Paradigms: A Comprehensive Overview and more Summaries Programming Paradigms in PDF only on Docsity!

Programming Paradigms for

Dummies: What Every

Programmer Should Know

Peter Van Roy

This chapter gives an introduction to all the main programming paradigms, their un- derlying concepts, and the relationships between them. We give a broad view to help programmers choose the right concepts they need to solve the problems at hand. We give a taxonomy of almost 30 useful programming paradigms and how they are related. Most of them differ only in one or a few concepts, but this can make a world of difference in programming. We explain briefly how programming paradigms influence language design, and we show two sweet spots: dual-paradigm languages and a definitive lan- guage. We introduce the main concepts of programming languages: records, closures, independence (concurrency), and named state. We explain the main principles of data abstraction and how it lets us organize large programs. Finally, we conclude by focus- ing on concurrency, which is widely considered the hardest concept to program with. We present four little-known but important paradigms that greatly simplify concurrent programming with respect to mainstream languages: declarative concurrency (both ea- ger and lazy), functional reactive programming, discrete synchronous programming, and constraint programming. These paradigms have no race conditions and can be used in cases where no other paradigm works. We explain why for multi-core processors and we give several examples from computer music, which often uses these paradigms.

More is not better (or worse) than less, just different.

  • The paradigm paradox.

1 Introduction

Programming is a rich discipline and practical programming languages are usually quite complicated. Fortunately, the important ideas of programming languages are simple. This chapter is intended to give readers with some programming experience a running start for these ideas. Although we do not give formal definitions, we give precise intu- itions and good references so that interested readers can quickly get started using the concepts and languages that implement them. We mention all important paradigms but we favor some little-known paradigms that deserve to be more widely used. We have deliberately left out detailed explanations of some of the more well-known paradigms

Peter Van Roy

(such as functional and object-oriented programming), since they already have a huge literature.

Solving a programming problem requires choosing the right concepts. All but the smallest toy problems require different sets of concepts for different parts. This is why programming languages should support many paradigms. A programming paradigm is an approach to programming a computer based on a mathematical theory or a coherent set of principles. Each paradigm supports a set of concepts that makes it the best for a certain kind of problem. For example, object-oriented programming is best for problems with a large number of related data abstractions organized in a hierarchy. Logic programming is best for transforming or navigating complex symbolic structures according to logical rules. Discrete synchronous programming is best for reactive problems, i.e., problems that consist of reactions to sequences of external events. Languages that support these three paradigms are respectively Java, Prolog, and Esterel.

Popular mainstream languages such as Java or C++ support just one or two separate paradigms. This is unfortunate, since different programming problems need different programming concepts to solve them cleanly, and those one or two paradigms often do not contain the right concepts. A language should ideally support many concepts in a well-factored way, so that the programmer can choose the right concepts whenever they are needed without being encumbered by the others. This style of programming is sometimes called multiparadigm programming, implying that it is something exotic and out of the ordinary. On the contrary, in our experience it is clear that it should be the normal way of programming. Mainstream languages are far from supporting this. Nevertheless, understanding the right concepts can help improve programming style even in languages that do not directly support them, just as object-oriented programming is possible in C with the right programmer attitude.

This chapter is partly based on the book [50], familiarly known as CTM, which gives much more information on many of the paradigms and concepts presented here. But this chapter goes further and presents ideas and paradigms not covered in CTM. The code examples in this chapter are written in the Oz language, which is also used in CTM. Oz has the advantage that it supports multiple paradigms well, so that we do not have to introduce more than one notation. The examples should be fairly self-explanatory; whenever anything out of the ordinary occurs we explain it in the text.

Contents of this chapter

Languages, paradigms, and concepts Section 2 explains what programming paradigms are and gives a taxonomy of the main paradigms. If your experience is limited to one or just a few programming languages or paradigms (e.g., object-oriented program- ming in Java), then you will find a much broader viewpoint here. We also explain how we organize the paradigms to show how they are related. We find that it is certainly not true that there is one “best” paradigm, and a fortiori this is not object-oriented programming! On the contrary, there are many useful paradigms. Each paradigm has its place: each has problems for which it gives the best solution (simplest, easiest to reason about, or most efficient). Since most programs have to solve more than one problem, it follows that they are best written in different paradigms.

Peter Van Roy

Figure 1. Languages, paradigms, and concepts

origins in the 1970s. It uses sophisticated algorithms to find solutions that satisfy global conditions. This means that it genuinely delivers on its ambitious claims.

Conclusions and suggestions for going further Section 8 concludes by reiterating why programming languages should support several paradigms. To understand the “soul” of each paradigm and to gain experience programming with different paradigms, we recommend the use of a multiparadigm language. A multiparadigm language permits programming in each paradigm without interference from other paradigms. The two most extensive multiparadigm languages are the dynamically typed language Oz [50] and the statically typed language Alice [38].

2 Languages, paradigms, and concepts

This section gives the big picture of programming paradigms, the languages that realize them, and the concepts they contain. There are many fewer programming paradigms than programming languages. That is why it is interesting to focus on paradigms rather than languages. From this viewpoint, such languages as Java, Javascript, C#, Ruby, and Python are all virtually identical: they all implement the object-oriented paradigm with only minor differences, at least from the vantage point of paradigms.

Figure 1 shows the path from languages to paradigms and concepts. Each program- ming language realizes one or more paradigms. Each paradigm is defined by a set of programming concepts, organized into a simple core language called the paradigm’s ker- nel language. There are a huge number of programming languages, but many fewer paradigms. But there are still a lot of paradigms. This chapter mentions 27 different paradigms that are actually used. All have good implementations and practical applica- tions. Fortunately, paradigms are not islands: they have a lot in common. We present a taxonomy that shows how paradigms are related.

Programming Paradigms for Dummies

No

Unnamed state (seq. or conc.) Named state More Expressiveness of state

Less

nondeterminism?Observable Yes programming^ functional

Descriptivedeclarative programming

Imperative programmingsearch

Event−loop programming

Multi−agent programming Message−passing programmingconcurrent

Data structures only

  • unification

Dataflow and

Oz, Alice, Curry Oz, Alice, Curry

CLU, OCaml, Oz E in one vat

Continuation programming

constraints^ Logic and message passing Message passing^ Shared state

+ nondeterministic (channel)

Oz, Alice, Curry, Excel,AKL, FGHC, FCP + synch. on partial termination

FrTime, Yampa Discrete synchronousprogramming Esterel, Lustre, Signal

Functional reactive programming (FRP) Continuous synchronous programming

Pipes, MapReduce

Nondet. state

Erlang, AKL

CSP, Occam,E, Oz, Alice, publish/subscribe, tuple space (Linda)

+ clocked computation

Dijkstra’s GCL

_+ cell (state)

  • nondet. choice_

programming Imperative Pascal, C programming

Guarded command

choice Nonmonotonic programmingdataflow Concurrent logicprogramming Oz, Alice, AKL

+ port Multi−agent programmingdataflow

+ local cell Active object programming Object−capability programming

Turing complete

Java, OCaml

+ closure

embeddings + solver

LIFE, AKL

CLP, ILOG Solver _+ thread

  • single assignment_

+ thread

Smalltalk, Oz,

+ thread

Java, Alice + log

(state)+ cell

Functional

SQL embeddings

Prolog, SQL

+ search

record

XML, S−expression

Haskell, ML, E

_(unforgeable constant)

  • cell_

Scheme, ML

+ procedure

+ closure

SNOBOL, Icon, Prolog

+ search

+ port(channel) Scheme, ML

(equality) + name

+ by−need synchronization

+ by−need synchronization

+ thread

+ continuation

Lazy concurrent

object−orientedConcurrent programming Shared−state programmingconcurrent

transactionalSoftware memory (STM)

object−orientedSequential programming Stateful programmingfunctional

declarativeLazy programmingconcurrent

programming

Lazy dataflow

Concurrentconstraint programming

programmingconstraint

Constraint (logic) programming

Relational & logicprogramming

logic programmingDeterministic

+ by−need synchron. + thread+ single assign.

Haskell

functional^ Lazy programming

Monotonicdataflow programming Declarative programmingconcurrent

functionalADT programming imperativeADT programming

programmingFunctional

First−order

Figure 2. Taxonomy of programming paradigms

2.1 Taxonomy of programming paradigms

Figure 2 gives a taxonomy of all major programming paradigms, organized in a graph that shows how they are related [55]. This figure contains a lot of information and re- wards careful examination. There are 27 boxes, each representing a paradigm as a set of programming concepts. Of these 27 boxes, eight contain two paradigms with different names but the same set of concepts. An arrow between two boxes represents the concept or concepts that have to be added to go from one paradigm to the next. The concepts are the basic primitive elements used to construct the paradigms. Often two paradigms that seem quite different (for example, functional programming and object-oriented pro- gramming) differ by just one concept. In this chapter we focus on the programming concepts and how the paradigms emerge from them. With n concepts, it is theoretically possible to construct 2n^ paradigms. Of course, many of these paradigms are useless in practice, such as the empty paradigm (no concepts)^1 or paradigms with only one concept. A paradigm almost always has to be Turing complete to be practical. This explains why functional programming is so important: it is based on the concept of first-class function,

(^1) Similar reasoning explains why Baskin-Robbins has exactly 31 flavors of ice cream. We postulate that they have only 5 flavors, which gives 2^5 − 1 = 31 combinations with at least one flavor. The 32nd combination is the empty flavor. The taste of the empty flavor is an open research question.

Programming Paradigms for Dummies

unnamed, deterministic, sequential!

named , deterministic, sequential! unnamed, deterministic, concurrent!

named , nondeterministic , sequential!^ unnamed,^ nondeterministic ,^ concurrent!

named , nondeterministic , concurrent!

Declarative paradigms (relational and functional)!

Deterministic concurrency!

Guarded command programming! Concurrent logic programming!

Imperative programming!

Message-passing and shared-state concurrency!

Less!

Expressiveness of state!

More!

Figure 3. Different levels of support for state

adjacent boxes differ in one coordinate.^2 One intriguing box shown is Dijkstra’s guarded command language (GCL) [14]. It has named state and nondeterministic choice in a sequential language. It uses nondeterministic choice to avoid overspecifying algorithms (saying too much about how they should execute). The paradigms in Figure 2 are classified on a horizontal axis according to how strongly they support state. This horizontal axis corresponds to the bold line in Figure 3. Let us follow the line from top to bottom. The least expressive combination is functional pro- gramming (threaded state, e.g., DCGs in Prolog and monads in functional programming: unnamed, deterministic, and sequential). Adding concurrency gives declarative concur- rent programming (e.g., synchrocells: unnamed, deterministic, and concurrent). Adding nondeterministic choice gives concurrent logic programming (which uses stream mergers: unnamed, nondeterministic, and concurrent). Adding ports or cells, respectively, gives message passing or shared state (both are named, nondeterministic, and concurrent). Nondeterminism is important for real-world interaction (e.g., client/server). Named state is important for modularity (see Section 4.4). Both observable nondeterminism and named state are cases where it is important to choose a paradigm that is expressive enough, but not too expressive (see epigram at the head of the chapter). Each of these two concepts is sometimes needed but should be left out if not needed. The point is to pick a paradigm with just the right concepts. Too few and programs become complicated. Too many and reasoning becomes complicated. We will give many examples of this principle throughout this chapter.

2.2 Computer programming and system design

Figure 4 gives a view of computer programming in the context of general system design. This figure adds computer programming to a diagram taken from Weinberg [56]. The two axes represent the main properties of systems: complexity (the number of basic interacting components) and randomness (how nondeterministic the system’s behavior is). There are two kinds of systems that are understood by science: aggregates (e.g., gas

(^2) Two of the eight possible combinations are not shown in the figure. We leave it to the reader to discover them and find out if they make any sense!

Peter Van Roy

Computer programming

Computer programming

Figure 4. Computer programming and system design (adapted from Weinberg [56])

molecules in a box, understood by statistical mechanics) and machines (e.g., clocks and washing machines, a small number of components interacting in mostly deterministic fashion). The large white area in the middle is mostly not understood. The science of computer programming is pushing inwards the two frontiers of system science: computer programs can act as highly complex machines and also as aggregates through simulation. Computer programming permits the construction of the most complex systems. Modern programming languages have evolved over more than five decades of expe- rience in constructing programmed solutions to complex, real-world problems. Modern programs can be quite complex, reaching sizes measured in millions of lines of source code, written by large teams of programs over many years. In our view, languages that scale to this level of complexity are successful in part because they model some essential factors of how to construct complex systems. In this sense, these languages are not just arbitrary constructions of the human mind. They explore the limits of complexity in a more objective way. We would therefore like to understand them in a scientific way, i.e., by understanding the basic concepts that compose the underlying paradigms and how these concepts are designed and combined. This is the deep justification of the creative extension principle explained below.

2.3 Creative extension principle

Concepts are not combined arbitrarily to form paradigms. They can be organized ac- cording to the creative extension principle. This principle was first defined by Felleisen [18] and independently rediscovered in [50]. It gives us a guide for finding order in the vast set of possible paradigms. In a given paradigm, it can happen that programs be- come complicated for technical reasons that have no direct relationship to the specific problem that is being solved. This is a sign that there is a new concept waiting to be discovered. To show how the principle works, assume we have a simple sequential func- tional programming paradigm. Then here are three scenarios of how new concepts can be discovered and added to form new paradigms:

Peter Van Roy

language. We will look at two interesting cases: languages that support two paradigms (Section 3.1) and layered languages (Section 3.2). The layered language we present is a particularly interesting one because almost the same layered structure appears in four different areas.

3.1 Languages that support two paradigms

Many languages support two paradigms, typically one for programming in the small and another for programming in the large. The first paradigm is chosen for the kind of problem most frequently targeted by the language. The second paradigm is chosen to support abstraction and modularity and is used when writing large programs. Here are a few examples:

  • Prolog: The first paradigm is a logic programming engine based on unification and depth-first search. The second paradigm is imperative: the assert and retract op- erations which allow a program to add and remove program clauses. Prolog dates from 1972, which makes it an old language. Recent developments in modeling lan- guages based on advanced search algorithms advance both the logic programming and imperative programming sides. Modern Prolog implementations have added some of these advances, e.g., support for constraint programming and a module system.
  • Modeling languages (e.g., Comet, Numerica [48]): The first paradigm is a solver: constraint programming (see Section 7), local search (see the chapter by Philippe Codognet [8]), satisfiability (SAT solvers), and so forth. The second paradigm is object-oriented programming.
  • Solving libraries (e.g., Gecode): The first paradigm is a solver library based on advanced search algorithms, such as Gecode [43, 47]. The second paradigm is added by the host language, e.g., C++ and Java support object-oriented programming.
  • Language embedding (e.g., SQL): SQL already supports two paradigms: a relational programming engine for logical queries of a database and a transactional interface for concurrent updates of the database. The host language complements this by supporting object-oriented programming, for organization of large programs. This example goes beyond two paradigms to show a design with three complementary paradigms.

3.2 A definitive programming language

At some point in time, language research will give solutions that are good enough that researchers will move on to work at higher levels of abstraction. This has already arrived for many subareas of language design, such as assembly languages and parsing algo- rithms. In the 1970s, compiler courses were built around a study of parsing algorithms. Today, parsing is well understood for most practical purposes and compiler design has moved on. Today’s compiler courses are built around higher level topics such as dataflow analysis, type systems, and language concepts. We postulate that this kind of evolution is happening with language design as well.

Programming Paradigms for Dummies

Layer Language project Erlang [6, 5] E [32, 31] Distrib. Oz [10] Didactic Oz [50] Functional programming (see Section 4.2)

A process is a re- cursive function in its own thread, employing clo- sures for hot code update

An object is a recursive func- tion with a local state

Functions, pro- cedures, classes, and components are closures with efficient distrib. protocols

Closures are the foundation of all paradigms

Deterministic concurrency (see Section 6)

(not supported) Deterministic execution of all objects in one vat (process)

Dataflow concur- rency with effi- cient protocol for dataflow variables

Concurrency is as easy as functional programming, no race conditions Message-passing concurrency (see Section 4.3)

Fault tolerance by isolation, fault detection with messages

Security by iso- lation, messages between objects in different vats

Asynchronous message pro- tocols to hide latency

Multi-agent pro- gramming is ex- pressive and easy to program Shared-state concurrency (see Section 4.4)

Global database (Mnesia) keeps consistent states

(not supported) Coherent global state protocols; transactions for latency and fault tolerance

Named state for modularity

Table 1. Layered structure of a definitive programming language

This section presents the structure of one possible definitive language [52]. We study four research projects that were undertaken to solve four very different problems. The solutions achieved by all four projects are significant contributions to their respective areas. All four projects considered language design as a key factor to achieve success. The surprise is that all four projects ended up using languages with very similar structures. Table 1 shows the common properties of the programming language invented in each of the four projects. The common language has a layered structure with four layers: a strict functional core, followed by declarative concurrency, then asynchronous message passing, and finally global named state. This layered structure naturally supports four paradigms. We briefly summarize the four projects:

  1. Erlang Programming highly available embedded systems for telecommunications. This project was undertaken by Joe Armstrong and his colleagues at the Ericsson Computer Science Laboratory starting in 1986. The Erlang language was designed and a first efficient and stable implementation was completed in 1991 [5, 6]. An Erlang program consists of isolated named lightweight processes that send each other messages. Because of the isolation, Erlang programs can be run almost unchanged on distributed systems and multi-core processors. The Erlang system has a replicated database, Mnesia, to keep global coherent states. Erlang and its programming platform, the OTP (Open Telecom Platform) system, are being used successfully in commercial systems by Ericsson and other companies [57, 17].
  2. E Programming secure distributed systems with multiple users and multiple se- curity domains. This project was undertaken over many years by different insti- tutions. It started with Dennis and Van Horn’s capability model in 1965 [13] and Carl Hewitt’s Actor model in 1973 [24] and it led via concurrent logic program- ming to the E language designed by Doug Barnes, Mark Miller, and their colleagues [32, 31]. Predecessors of E have been used to implement various multiuser virtual

Programming Paradigms for Dummies

Subsystem

Actuating agent Monitoring agent

Calculate corrective action

Figure 6. A single feedback loop

themselves to survive in extremely hostile environments by doing reversible phase transi- tions [44, 54]. Let us leave aside for now the artificial intelligence required to build such a system, and investigate just the language mechanisms it needs. The system may ask for human assistance in some cases, but in principle it should contain all the mechanisms it needs to accomplish its tasks. What is a reasonable architecture for designing self-sufficient systems? From the conclusions of the previous section and our experience in building distributed systems, we can propose an architecture. In terms of programming paradigms, what we need first is components as first-class entities (specified by closures) that can be manipulated through higher-order programming. Above this level, the components behave as isolated concurrent agents that communicate through message passing. Finally, we need named state and transactions for system reconfiguration and system maintenance. Named state allows us to manage the content of components and change their interconnections. This gives us a language that has a layered structure similar to the previous section. With this language we can program our system. To allow the program to adapt itself to its environment, we take inspiration from biological systems and organize its components as feedback loops. The system then consists of a set of interacting feedback loops. A single feedback loop consists of three concurrent components that interact with a subsystem (see Figure 6): a monitoring agent, a correcting agent, and an actuating agent. Realistic systems consist of many feedback loops. Since each subsystem must be as self-sufficient as possible, there must be feedback loops at all levels. These feedback loops can interact in two fundamental ways:

  • Stigmergy: Two loops share one subsystem.
  • Management: One loop controls another loop directly.

Figure 8 gives a real-world example from biology: the human respiratory system [49]. This system contains four loops. Three loops form a tower connected by management. The fourth loop interacts with the others through stigmergy. The style of system design illustrated by the human respiratory system can be applied to programming. A program then consists of a set of feedback loops interacting through stigmergy and management. Figure 7 shows part of the Transmission Control Protocol as a feedback loop structure [49]. The inner loop implements reliable transfer of a byte stream using a sliding window protocol. The outer loop does congestion control: if too many packets are lost, it reduces the transfer rate of the inner loop by reducing the window size. In our view, the large-scale structure of software will more and more be done in this self-sufficient style. If it is not done in this way, the software will simply be too fragile and collapse with any random error or problem.

Peter Van Roy

Send

Inner loop (reliable transfer)

Outer loop (congestion control) Calculate policy modification

Actuator (send packet)

Monitor Monitor throughput

Calculate bytes to send

(modify throughput)

(sliding window protocol)

destination and receives ack)

(network that sends packet to

Subsystem

(receive ack)

Send stream acknowledgement

Figure 7. TCP as a feedback loop structure

Render unconscious

reflex

Breathing Laryngospasm (seal air tube) (^) in bloodCO

Measure in blood O

Measure

Trigger breathing reflex when CO2 increases to threshold

Increase or decrease breathing rate

Conscious control

when O2 falls to threshold

Trigger unconsciousness

(and reduce CO2 threshold to base level)

(maximum is breath!hold breakpoint)

of body and breathing and change CO2 threshold

Trigger laryngospasm temporarily

in airways

Detect obstruction

when sufficient obstruction in airways

breathing

Monitor

Other inputs

in human body

Breathing apparatus

Actuating agents Monitoring agents

Figure 8. The human respiratory system as a feedback loop structure

Peter Van Roy

central paradigm (see Figure 2). From an implementation viewpoint, a closure combines a procedure with its external references (the references it uses at its definition). From the programmer’s viewpoint, a closure is a “packet of work”: a program can transform any instructions into a closure at one point in the program, pass it to another point, and decide to execute it at that point. The result of its execution is the same as if the instructions were executed at the point the closure was created. Figure 9 shows schematically what happens when a closure is defined and when it is called. The procedure P is implemented by a closure. At the definition (context D), P stores the references from the definition context. For example, it keeps the reference x to some named state. We say that the environment (set of references) of P is closed over its definition context. At the call (context C), P uses the references from context D. Figure 10 shows one possible use for a closure: creating a control structure. At the left, we execute the instruction . At the right, instead of executing , we place it inside a procedure (closure) referenced by P (the example uses Oz syntax). Any time later on in the program, we can decide to call P. We have separated the definition of from its execution. With this ability we can define control structures such as an if statement or while loop.

… … … … …

…

P= proc {$} end … … … {P}

Instruction

Define procedure referenced by P (context D)

Call the procedure (context C)

Figure 10. Example: modifying a program to separate creation and execution

The example of Figures 9 and 10 can easily be generalized to procedures with ar- guments. The closed environment exists as before. The arguments are passed during each call. The closure therefore has two sets of references: a closed environment (from the definition) and the arguments (from each call). Almost all programming languages (except for a few venerable ancestors such as Pascal and C) use this kind of closure:

  • functions are closures;
  • procedures are closures;
  • objects are closures;
  • classes are closures;
  • software components are closures.

Many abilities normally associated with specific paradigms are based on closures:

  • Instantiation and genericity, normally associated with object-oriented program- ming, can be done easily by writing functions that return other functions. In object-oriented programming the first function is called a “class” and the second is called an “object”.

Programming Paradigms for Dummies

  • Separation of concerns, normally associated with aspect-oriented programming, can be done easily by writing functions that take other functions as arguments. For ex- ample, Erlang has a function that implements a generic fault-tolerant client/server. It is called with a function argument that defines the server’s behavior. Aspect- oriented programming in object-oriented languages is explained in the chapter by Pierre Cointe [9]. It is usually done by syntactic transformations (called “weaving”) that add aspect code to the original source. The AspectJ language is a good exam- ple of this approach. Weaving is difficult to use because it is fragile: it is easy to introduce errors in the program (changing the source code changes the semantics of the program). Using closures instead makes it easier to preserve correctness because the source code is not changed.
  • Component-based programming is a style of programming in which programs are organized as components, where each component may depend on other components. A component is a building block that specifies part of a program. An instance of a component is called a module, which is a record containing closures. A new module is created by a function that takes its dependent modules as inputs. The component is the function.

The Erlang language implements all these abilities directly with closures. This is practical and scalable: successful commercial products with more than one million lines of Erlang code have been developed (e.g., the AXD-301 ATM switch [57]). In most other languages, though, the use of closures is hidden inside the language’s implementation and is not available directly to the programmer. If done carefully this can be an advantage, since the implementation can guarantee that the closures are used correctly.

4.3 Independence (concurrency)

Another key concept is independence: constructing a program as independent parts. This is not as simple as it may seem. For example, consider a program that consists of instruc- tions executing one after the other. The instructions are not independent since they are ordered in time. To implement independence we need a new programming concept called concurrency. When two parts do not interact at all, we say they are concurrent.^3 (When the order of execution of two parts is given, we say they are sequential.) Concurrent parts can be extended to have some well-defined interaction, which is called communication. Concurrency should not be confused with parallelism. Concurrency is a language concept and parallelism is a hardware concept. Two parts are parallel if they execute simultaneously on multiple processors. Concurrency and parallelism are orthogonal: it is possible to run concurrent programs on a single processor (using preemptive scheduling and time slices) and to run sequential programs on multiple processors (by parallelizing the calculations). Parallel execution on multi-core processors is explained on page 38.

The real world is concurrent: it consists of activities that evolve independently. The computing world is concurrent as well. It has three levels of concurrency:

(^3) Technically, a program’s execution consists of a partial order of state transition events and two events are concurrent if there is no order between them.

Programming Paradigms for Dummies

of the program. To do this, we add an abstract notion of time to the program. This abstract time is simply a sequence of values in time that has a single name. We call this sequence a named state. Unnamed state is also possible (monads and DCGs, see Section 2.1), but it does not have the modularity properties of named state.

Figure 11 shows two components, A and B, where component A has an internal named state (memory) and component B does not. Component B always has the same behavior: whenever it is called with the same arguments, it gives the same result. Component A can have different behaviors each time it is called, if it contains a different value in its named state. Having named state is both a blessing and a curse. It is a blessing because it allows the component to adapt to its environment. It can grow and learn. It is a curse because a component with named state can develop erratic behavior if the content of the named state is unknown or incorrect. A component without named state, once proved correct, always stays correct. Correctness is not so simple to maintain for a component with named state. A good rule is that named state should never be invisible: there should always be some way to access it from the outside.

Component A

Memory

Component B

No memory

Figure 11. A component with named state and a component without named state

Named state and modularity

Named state is important for a system’s modularity. We say that a system (function, procedure, component, etc.) is modular if updates can be done to part of the system without changing the rest of the system. We give a scenario to show how we can design a modular system by using named state. Without named state, this is not possible.

Assume that we have three developers, P, U1, and U2. P has developed a module M that contains two functions F and G. U1 and U2 are users of M: their own programs used module M. Here is one possible definition of M:

fun {ModuleMaker} fun {F ...} ... % Definition of F end fun {G ...} ... % Definition of G end in themodule(f:F g:G) end M={ModuleMaker} % Creation of M

Peter Van Roy

The function ModuleMaker is a software component, i.e., it defines the behavior of part of a system. We create instances of this component by calling ModuleMaker. One such instance is the module M. Note that a module’s interface is simply a record, where each field is one of the module’s operations. The module M has two operations F and G.

Now assume that developer U2 has an application that consumes a huge amount of calculation time. U2 would like to investigate where all this time is being spent, so that he can rewrite his application to be less costly. U2 suspects that F is being called too many times and he would like to verify this. U2 would like a new version of M that counts the number of times F is called. So U2 contacts P and asks him to create a new version of M that does this, but without changing the interface (that defines the operations of M and how they are called) since otherwise U2 would have to change all of his program (not to mention U1!).

Surprise! This is not possible without named state. If F does not have named state then it cannot change its behavior. In particular, it cannot keep a counter of how many times it is called. The only solution in a program without named state is to change F’s interface (its arguments):

fun {F ... Fin Fout} Fout=Fin+ ... end

We add two arguments to F, namely Fin and Fout. When calling F, Fin gives the count of how many times F was called, and F calculates the new count in Fout by adding one to Fin. When calling F, we have to link all these new arguments together. For example, three successive calls to F would look like this:

A={F ... F1 F2} B={F ... F2 F3} C={F ... F3 F4}

F1 is the initial count. The first call calculates F2, which is passed to the second call, and so forth. The final call returns the count F4. We see that this is a very bad solution, since U2 has to change his program wherever F is called. It gets worse: U1 also has to change his program, even though U1 never asked for any change. All users of M, even U1, have to change their programs, and they are very unhappy for this extra bureaucratic overhead.

The solution to this problem is to use named state. We give an internal memory to the module M. In Oz, this internal memory is called a cell or a variable cell. This corresponds simply to what many languages call a variable. Here is the solution: