1 of 13

Programming Paradigms

the understanding of different types of programming languages by looking at Java and Prolog and the reasons for their development.

9.4.1 Option 1

2 of 13

Learning Outcomes

This final unit of the NSW HSC SDD course at AISM offers students the opportunity to look at different types of programming languages. By focusing on different paradigms, students should gain an insight into how effective each approach has been, together with an understanding of the specific areas where the use of a particular paradigm could be particularly appropriate. This understanding will broaden the students’ experience of different paradigms and will also offer them a wider choice from which to select an appropriate language to solve a specific problem.

Learning Outcomes

A student:

H1.2 differentiates between various methods used to construct software solutions

H2.1 explains the implications of the development of different languages

H2.2 explains the interrelationship between emerging technologies and software development

H4.1 identifies needs to which software solutions are appropriate

H4.2 applies appropriate development methods to solve software problems

H5.3 selects and applies appropriate software to facilitate the design and development of software solutions.

3 of 13

What is a programming paradigm?

A programming paradigm is a style of computer programming. It serves as a way of building the structure and elements of computer programs.

Programming paradigms that are often distinguished include:

  1. imperative paradigm
    • includes procedural programming languages such as C, Fortran, Pascal and BASIC.
    • Object-Oriented Programming (OOP) languages such as Python, C++, Objective-C, Delphi, Java, C#, Perl, Ruby and PHP.
  2. declarative paradigm
    • includes logic programming languages such as Lisp and Prolog which are used for building artificial intelligence and expert systems.

Each of these paradigm was developed in an attempt to improve programmer productivity. It can be shown that anything solvable using one of these paradigms can be solved using the others; however, certain types of problems lend themselves more naturally to specific paradigms.

In this unit you will explore the logic paradigm with Prolog and the OOP paradigm with Java.

4 of 13

Contrasting different paradigms

  1. Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. Imperative programming (necessary programming) is focused on describing how a program operates. In imperative programming we use statements like If, Then, Else, Do, While etc. These statements control the flow of the program and describe how it operates. The 'first do this, next do that' is a short phrase which really in a nutshell describes the spirit of the imperative paradigm. The basic idea is the command, which has a measurable effect on the program state.
  2. Procedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions). In procedural programming we make use of localised variables, arguments and returns. Because procedural programming supports modular and top down design, it is also known as structured programming. Procedural programming provides a modular approach which helps maintainability and improves the quality of solutions by providing extended library routines and assists teams of programmers working on large projects.
  3. From the 1960s onwards, structured programming and modular programming in general have been promoted as techniques to improve the maintainability and overall quality of imperative programs. Object-oriented programming extends this approach and Java is OOP.
  4. Declarative programming contrasts with imperative and procedural programming. Declarative programming is a non-imperative style of programming in which programs describe their desired results without explicitly listing commands or steps that must be performed. To better understand this, procedural programming could be considered a step towards declarative programming although it is not actually declarative. For example, a programmer can often tell, simply by looking at the names, arguments and return types of procedures, what a particular procedure is supposed to do, without necessarily looking at the details of how it achieves its result. However, a procedural program is imperative since it 'fixes' the statements and order of execution within each procedure. Prolog is declarative: the program logic is expressed in terms of relations, represented as facts and rules.

5 of 13

Understanding the imperative paradigm

Literacy: low level languages, high level languages, imperative programming, assembly language, machine code, variables, assignment statements, arithmetic operations, function evaluations, looping statements, conditional branching statements

The hardware implementation of almost all computers is imperative; nearly all computer hardware is designed to execute machine code (a low level language) , which is native to the computer, written in the imperative style. Most computer languages are in the imperative style. Many imperative programming languages (such as Java, Pascal, BASIC and C++) are abstractions of low level languages such as assembly language and machine code.

Imperative programming is based on commands. This is different than declarations which are the staple of declarative languages. Rather than making claims about the universe, programmers only issue instructions for what a program should do. Almost all computers follow an imperative paradigm at a hardware level - the CPU follows instructions from machine code line-by-line. Therefore it is unsurprising that most computer languages are imperative in nature.

Higher-level imperative languages like Pascal, Basic, C++ and Java use variables and more complex statements. Assignment statements, in imperative paradigm, perform an operation on information located in memory and store the results in memory for later use. High-level imperative languages, in addition, permit the evaluation of complex expressions, which may consist of a combination of arithmetic operations and function evaluations, and the assignment of the resulting value to memory. Looping statements (such as in while loops, do while loops and for loops) allow a sequence of statements to be executed multiple times. Loops can either execute the statements they contain a predefined number of times, or they can execute them repeatedly until some condition changes. Conditional branching statements allow a sequence of statements to be executed only if some condition is met. Otherwise, the statements are skipped and the execution sequence continues from the statement following them. Unconditional branching statements allow the execution sequence to be transferred to some other part of the program. These include the switch and the subprogram, or procedure, call (which usually returns to the next statement after the call).

6 of 13

Development of the imperative paradigm

The earliest imperative languages were the machine languages of the original computers. In these languages, instructions were very simple, which made hardware implementation easier, but hindered the creation of complex programs.

FORTRAN, developed by John Backus at IBM starting in 1954, was the first major programming language to remove the obstacles presented by machine code in the creation of complex programs. FORTRAN was a compiled language that allowed named variables, complex expressions, subprograms, and many other features now common in imperative languages. The next two decades saw the development of a number of other major high-level imperative programming languages. In the late 1950s and 1960s, ALGOL was developed in order to allow mathematical algorithms to be more easily expressed, and even served as the operating system's target language for some computers.

COBOL (1960) and BASIC (1964) were both attempts to make programming syntax look more like English. In the 1970s, Pascal was developed by Niklaus Wirth, and C was created by Dennis Ritchie while he was working at Bell Laboratories. The 1980s saw a rapid growth in interest in object-oriented programming. These languages were imperative in style, but added features to support objects. The last two decades of the 20th century saw the development of a considerable number of such programming languages.

C++ was first implemented in 1985. In the late 1980s and 1990s, the notable imperative languages drawing on object-oriented concepts were Perl, released by Larry Wall in 1987; Python, released by Guido van Rossum in 1990; Visual Basic and Visual C++, released by Microsoft in 1991 and 1993 respectively; PHP, released by Rasmus Lerdorf in 1994; Java, first released by Sun Microsystems in 1994 and Ruby, released in 1995 by Yukihiro “matz” Matsumoto. Microsoft's .NET (2002) is imperative at its core, as are its primary target languages, VB.NET and C#.

7 of 13

Issues with imperative paradigm

Literacy: Structured programming, Modularization, Object-oriented programming

Most programming projects today use the imperative style of programming as it has many strengths. Developers write sequences of operations in a language, such as C++, Java, Visual Basic, etc., that implement an algorithm, or recipe, for performing tasks. The algorithm for the task mixes logical, or relational, statements about the task to be solved and control statements about how to calculate the solution. The logical statements describe "what-to" calculate while the control statements describe "how-to" calculate. Debugging the algorithm consists of verifying the accuracy of the logical statements and fixing the control statements, if necessary.

There are many problems with the imperative approach. The sequence of operations critically determines the correctness of the algorithm. Unexpected execution sequences through an algorithm caused by user input actions or real-time events in a multitasking environment may result in subtle or catastrophic algorithm failure. Writing the control logic is the programmer's responsibility and, therefore, subject to implementation errors. Understanding a program's algorithm is often difficult for other developers without extensive comments on the code and tracing of the program's execution with sample data. Verifying program correctness consumes a significant portion of the development effort, but also usually fails to discover a significant number of defects.

To address the problems associated with imperative programming, the computer industry has developed and advocated many approaches. Structured programming and campaigns against "go-to" statements address some of the problems discovered with ad hoc control structures and statements. Modularization initiatives stress decomposition techniques on the premise that humans can better comprehend, reason about, and maintain smaller pieces of code. Object-oriented programming advocates program constructions using reusable components, libraries, and frameworks.

8 of 13

limitations of the imperative paradigm

Advantages

  1. efficient.
  2. close to the machine.
  3. popular.
  4. familiar.

Disadvantages

  1. difficulty with solving certain types of problems. Writing the control logic is the programmer's responsibility and, therefore, subject to implementation errors.
  2. the sequence of operations critically determines the correctness of the algorithm.
  3. unexpected execution sequences through an algorithm caused by user input actions or real-time events in a multitasking environment may result in subtle or catastrophic algorithm failure.
  4. understanding a program's algorithm is often difficult for other developers without extensive comments on the code and tracing of the program's execution with sample data.
  5. verifying program correctness consumes a significant portion of the development effort, but also usually fails to discover a significant number of defects.
  6. the need to specify code for every individual process.
  7. difficulty of coding for variability.

LEARN ALL THESE

REMEMBER

9 of 13

Development of the declarative paradigm

Declarative programming is a style of building the structure and elements of computer programs, that expresses the logic of a computation without describing its control flow. Declarative programming separates the logic, or what, of an algorithm from the control, or how, of an algorithm. The programmer still specifies the logic or equations specifying the problem's relations, but the programming system is responsible for control, or how the logic is evaluated. The most familiar examples are spreadsheets and query languages for relational databases.

Many languages applying this style attempt to minimize or eliminate side effects by describing what the program should accomplish rather than describing how to go about accomplishing it as a sequence of steps. This is in contrast with imperative programming, in which algorithms are implemented in terms of explicit steps.

The declarative family of programming languages has a long history in the academic computer science community and specialized areas of commercial application, such as compiler construction, expert systems, and databases. Despite the theoretical advantages of declarative programming languages, they do not have widespread use in commercial programming practice.

Common declarative languages include those of database query languages such as SQL, Prolog and Lisp. Prolog is a logical language which is the major example of a fourth generation programming language. The name itself, Prolog, is short for PROgramming in LOGic. Prolog consists of a series of rules and facts. A program is run by presenting some query and seeing if this can be proved against these known rules and facts.

Prolog's heritage includes the research of automated deduction systems developed in the 1960s and 1970s. The inference mechanism of Prolog is based upon Robinson's resolution principle (1965) together with mechanisms for extracting answers proposed by Green (1968). The "first" Prolog was " based on work by Colmerauer (1970). The major influence on the nature of this first Prolog was that it was designed to facilitate natural language processing.

Outline each generation of languages

10 of 13

Writing Task: Chat with eviebot at https://www.eviebot.com/en/

  1. Collect evidence of conversation which identifies Eviebot as an automated system.
  2. Identify and describe a real life example of an online chat support system.
  3. Assess if a chat bot could automate this system.

11 of 13

Applications of declarative programming languages

  • Expert Systems. An expert system is a computer program that represents and reasons with knowledge of some specialist subject with a view to solving problems or giving advice. In order to accomplish feats of apparent intelligence, an expert system relies on two components: a knowledge base and an inference engine. A knowledge base is an organized collection of facts about the system’s or world. An Inference Engine is a tool from artificial intelligence (AI). An inference engine interprets and evaluates the facts in the knowledge base in order to provide an answer. An inference engine enables the expert system to draw deductions from rules. Typical tasks for expert systems involve:
    • health diagnosis
    • pattern matching and predicting the future such as weather, share prices and exchange rates
    • planning sequences of actions such as might be performed by robots,
  • Compiler construction.
  • Databases.

Task

EXSYS has customers worldwide who have developed and deployed thousands of Knowledge Automation Expert Systems. Explain how Corvid can be used for a wide range of problem types and with various end user interfaces.

Click here to run the example systems.

12 of 13

Heuristics (computer science)

In computer science and artificial intelligence, a heuristic (I find.I discover) is a technique for finding an approximate solution when standard methods fail to find any exact solution.

A heuristic method() is able to rank alternative pathways when searching for information.

Example Virus scanning

Many virus scanners use heuristic rules for detecting viruses and other forms of malware. Heuristic scanning looks for code and/or behavioral patterns indicative of a class or family of viruses, with different sets of rules for different viruses.

If a file or process is observed to contain matching patterns and behaviour, then the scanner infers that the file is infected. It does not definately know that a file is infected, it simply infers that it, based upon observed patterns.

Heuristic scanning has the advantage to detect future viruses without requiring the virus to be submitted to the virus scanner developer and a detection update for the scanner provided to the scanner's users.

Writing Task

Explain the relationship between heuristics and inference in AI.

13 of 13

NSW EXAM: PROGRAMMING PARADIGMS EXERCISE

  1. Different programming paradigms enable software developers to solve different types of problems and achieve greater productivity. Do you agree with this statement? Provide reasons for your answer. (2011, 3 Marks)
  2. Describe an advantage of using the logic paradigm instead of an imperative approach. (2010, 2 Marks)
  3. Identify TWO limitations of the imperative paradigm, and describe how each is addressed by either the object oriented paradigm or the logic paradigm. (2012, 3 Marks).
  4. Describe the strengths of the logic paradigm, with reference to a specific type of problem. (2014, 3 Marks)
  5. Describe the strengths of the object-oriented paradigm, with reference to a specific type of problem. (2014, 3 Marks)
  1. A museum has a database which identifies dinosaurs based on their features. The software associated with the database was developed using the imperative paradigm.

The details on the left show part of the information in the database.

What are the limitations of using the imperative approach for developing this database software? (2013, 3 Marks)