Search topics and sections

In computer science, abstraction means hiding the details of how something works, so that it can be used by knowing only what it does. A programmer who calls a square-root routine needs to know what it returns, not which algorithm (set of steps for solving a problem) runs inside it.[1] It is the computing form of general abstraction, and it shapes how programs are split into parts, how operating systems present hardware, and how tools prove that software is correct.

Abstractions stack into layers. Programs are written in high-level languages, which are designed to be easy for people to read, without thinking about assembly code, the low-level instructions written for a processor. Assembly is written without thinking about logic gates (circuits that do simple logic operations), and processors are built from gates without thinking much about transistors, which are tiny electronic switches.[2]

What versus how

An abstraction keeps the details that matter to its user and hides the rest. In 1974 Barbara Liskov and Stephen Zilles wrote that an abstraction should let programmers express the relevant details and leave out the irrelevant ones. In programming, how something can be used is relevant, and how it is implemented is not.[1]

Two terms name the two sides of this split:

  • The interface is what users see: the operations offered and what each one promises.
  • The implementation is the code behind the interface. Users treat it as a black box and do not need to look inside to use it.[3]

Abstraction does not mean being vague. In his 1972 Turing Award lecture, Edsger Dijkstra wrote that "the purpose of abstracting is not to be vague, but to create a new semantic level in which one can be absolutely precise."[4] A semantic level here means a level with its own clear meaning and precise rules.

Dijkstra also saw abstraction as the way to keep large programs understandable. He suggested that, with good use of abstraction, the effort needed to understand a program need not grow faster than the program's length.[4]

Procedural abstraction as a black box User sees: what it computes interface square x x² Hidden implementation (either one) (* x x) (exp (double (log x))) Hidden: how it computes it
A procedure works as a black box. The user sees only its interface, square, which maps xx to x2x^2. Either of the two hidden implementations, (* x x) or (exp (double (log x))), could sit behind it.

Layers of abstraction

Large computer systems are built as a stack of abstractions. Each layer offers a simpler model and hides the details of the layers below it.[5] David Patterson lists "use abstraction to simplify design" as one of eight great ideas in computer architecture.[5]

From transistors to programs

A common example runs from hardware up to software. Abstraction makes it possible to:[2]

  1. build a processor out of logic gates without thinking too much about transistors;
  2. write code in assembly language without thinking about logic gates;
  3. write in a high-level language such as C without thinking about assembly;
  4. write a large program by dividing it into small, understandable pieces.
Layers of abstraction in a computer Transistors Logic gates Processor Instruction set architecture (ISA) Assembly language High-level language (e.g. C) Large program of small pieces Hardware Software hardware/software interface simpler model more detail
A computer is described as a stack of abstraction layers, from transistors at the bottom to large programs at the top. The instruction set architecture is the interface between the hardware layers below it and the software layers above it.

Interfaces between layers

Computing work always involves at least two layers at once, and usually more: the layer of interest and the one below or above it.[6] A well-defined interface between layers lets users of a component ignore how it is built. It also lets the builder ignore who all the users will be.[6]

An application programming interface (API) is such an interface for a software component.[6]

The instruction set architecture (ISA) is the interface between hardware and software.[7]

A well-designed ISA lets the same software keep running as hardware changes. The ISA of the IBM 360, for example, let the same software run on many computers covering a wide range of cost and performance and decades of technology change.[7]

The general pattern has its own page, abstraction layer.

Operating systems

An operating system turns physical resources, such as the processor, memory, and disk, into more general and easier-to-use virtual forms. This technique is called virtualization.[2]

Programs reach these virtual resources through system calls. A typical operating system offers a few hundred of them.[2]

Hiding the specifics of hardware devices behind a uniform interface is called hardware abstraction.

Networks

The Internet is built in layers. Its Internet Protocol (IP) layer is called a "thin waist": a narrow, shared layer that connects many lower-level network technologies to many applications. It lets new devices and network technology join at the bottom and new, unexpected applications appear at the top.[6]

The Transmission Control Protocol (TCP) is built on top of IP. IP makes no promise that data arrives, and messages may be lost, reordered, or corrupted. TCP uses IP to give the programs above it reliable delivery.[8]

Abstraction in programs

Programming languages let programmers build their own abstractions. Liskov and Zilles named two main forms, each supported by its own kind of module, a separate part of a program with a clear role. The two forms are procedures for abstract operations, and data types defined by their operations.[1]

Kind What the user relies on What is hidden Example
Procedural abstraction What a procedure computes The algorithm it uses A square or square-root routine
Data abstraction A set of operations on a type of data How the data is stored Rational numbers, stacks

Procedural abstraction

A procedure (also called a function or subroutine) gives a name to a computation so that it can be used as a unit. Its caller cares only about what it computes, not the algorithm inside.[1] Structure and Interpretation of Computer Programs (SICP) calls this a procedural abstraction.[3]

SICP shows two Scheme definitions of the same procedure:[3]

(define (square x) (* x x))

(define (square x) (exp (double (log x))))
(define (double x) (+ x x))

Both return the square of their argument, so a caller that uses only the returned value can use either one. A user should not need to know how a procedure is implemented in order to use it.[3]

Data abstraction

Data abstraction applies the same split to data. Programs use data only through a small set of procedures, and the concrete representation is defined separately.[9] SICP's example is rational numbers. A constructor make-rat builds a fraction from a numerator and a denominator, and selectors numer and denom take it apart.[9]

Any representation works as long as these three procedures meet one condition. For any integer nn and any non-zero integer dd, if xx is (make-rat n d), then:[9]

numer(x)denom(x)=nd\frac{\texttt{numer}(x)}{\texttt{denom}(x)} = \frac{n}{d}

Here nn and dd are the numerator and denominator passed to the constructor, and xx is the rational number it returns.

SICP draws such a system as levels separated by abstraction barriers. Programs that use rational numbers call only add-rat, sub-rat, mul-rat, div-rat, and equal-rat?. These use only make-rat, numer, and denom, which in turn use only pairs (data structures made of two parts).[9]

Abstraction barriers in a rational-number systemuses only the level belowPrograms that use rational numbersadd-ratsub-ratmul-ratdiv-ratequal-rat?make-ratnumerdenomconscarcdrHowever pairs are implemented(pairs)abstraction barrierabstraction barrierabstraction barrierabstraction barrier
Layers of a rational-number system, after SICP section 2.1.2. Each layer uses only the operations of the layer below, so abstraction barriers such as make-rat, numer, and denom keep programs independent of how pairs are implemented.

Abstract data types

An abstract data type is a class of objects defined completely by the operations available on them.[1]

Liskov and Zilles argued in 1974 that procedures alone did not give a rich enough set of abstractions, and this led them to the abstract data type.[1] They paired it with a language feature, the cluster, that implements a type's operations.[1]

The language's type checking (checking that values are used in ways that match their types) kept a cluster's internal representation out of reach of code outside it.[1]

A stack, for example, is defined by operations such as push, pop, and top, not by how its elements are stored.[1]

Liskov led the design of the CLU language, which stressed modular programming and data abstraction.[10] Her Turing Award cited contributions "especially related to data abstraction, fault tolerance, and distributed computing."[10]

Parameterization and specification

Liskov and John Guttag describe two methods that procedures and data types use:[11]

  • Abstraction by parameterization: parameters let a single program text represent a potentially infinite set of different computations. It abstracts from the identity of the data used.
  • Abstraction by specification: a specification states what a module does. Users depend on that behavior, not on how it is implemented.

Their square-root example states its specification in comments:[11]

float sqrt (float coef) {
  // requires: coef > 0
  // effects: Returns an approximation to the square root of coef
  ...
}

If the requires condition held when the procedure was called, the caller may assume the effects condition afterward. The caller may assume only what follows from that effects condition.[11]

Besides procedural and data abstraction, Liskov and Guttag name two further kinds:[11]

  • Iteration abstraction: going through the items in a collection without revealing how the items are obtained.
  • Type hierarchy: abstracting from single data types to families of related types.

Information hiding and modules

David Parnas argued in 1972 that a system should be split into modules by the design decisions each one hides, not by the steps of its processing.[12] He compared two ways of splitting the same program. One made each major processing step a module, as a flowchart would, and the other used information hiding.[12]

In the second design, every module is defined by its knowledge of a design decision that it hides from all others. Its interface was chosen to reveal as little as possible about its inner workings.[12] Parnas advised starting from a list of difficult design decisions, or decisions likely to change, and designing each module to hide one of them.[12]

Abstraction in verification

In program verification, abstraction means approximation. A tool replaces the exact set of a program's possible behaviors with a simpler model it can analyze. The model must cover every real behavior, so that a proof about the model also holds for the program.[13]

Abstract interpretation

Abstract interpretation was presented by Patrick Cousot and Radhia Cousot in 1977 as a single, general model for the static analysis of programs, which means checking a program without running it.[14] It works with an abstract semantics, which is a superset of the program's real (concrete) behaviors: a larger set that contains every real behavior.[13] Two results follow:

  • Soundness: if the abstract semantics never reaches a forbidden zone of errors, the real program cannot reach it either.[13]
  • False alarms: the abstract semantics may reach the forbidden zone while the real program never does. The tool then reports an error that cannot happen.[13]

This makes abstraction-based methods the mirror image of testing:[13]

Method Executions checked Possible result
Testing A subset of real executions May miss errors
Abstraction-based methods A superset of real executions May report errors that do not exist

An abstract domain is a kind of simplified description of values that a tool can store and compute with. A classic one, the interval domain, approximates a set of values by its minimum and maximum, and static analysis tools compute such approximations automatically.[13]

Sound over-approximation in abstract interpretation Proof of safety False alarm Forbidden zone (errors) Forbidden zone (errors) Real behaviors Real behaviors Abstract model (superset) Abstract model (superset) No alarm: real program is safe Alarm, but no real error alarm
Sound over-approximation in abstract interpretation. On the left the abstract model stays clear of the forbidden zone, so the program is proved safe. On the right the abstract model overlaps the forbidden zone while the real behaviors do not, which gives a false alarm.

Abstract model checking

Model checking tests a property against the states a system can reach. Its main obstacle is the state explosion problem (the number of states becomes too large), and abstraction is the most important technique for handling it.[15]

Abstract model checking maps the system's data values onto a small set of abstract values. Extending this map to states and transitions gives a much smaller abstract model.[16] Edmund Clarke, Orna Grumberg, and David Long developed this approach in the paper "Model checking and abstraction", first presented at POPL in 1992 (journal version 1994).[16]

The abstract model is a larger model that includes every real behavior. Let hh be an abstraction function and φ\varphi a specification in ACTL*. ACTL* is a temporal logic, a notation for statements about how a system behaves over time, that only makes claims about all paths.[15]

Suppose the basic statements in φ\varphi respect hh, meaning each has the same truth value in all concrete states that hh maps to the same abstract state. Then:[15]

M^⊨φ  ⇒  M⊨φ\widehat{M} \models \varphi \;\Rightarrow\; M \models \varphi

Here MM is the concrete model, M^\widehat{M} is the abstract model built with hh, and ⊨\models means "satisfies". The reverse does not hold: if M^\widehat{M} fails φ\varphi, the real model may still satisfy it.[15] A counterexample is a behavior that shows a property is false, and one that exists in the abstract model but not in the real model is called spurious.[15]

Refining the abstraction

Counterexample-guided abstraction refinement (CEGAR), described by Clarke and colleagues in 2000, automates the search for a good abstraction:[15]

  1. Build an initial abstraction from the program.
  2. Model check the abstract model. If it satisfies the property, so does the real system, and the loop stops.
  3. Otherwise, check whether the counterexample exists in the real system. If it does, report it to the user.
  4. If it is spurious, split an abstract state into less abstract states to remove it, then return to step 2.

The best refinement is the least detailed one, because it keeps the abstract model smallest. Finding it is NP-hard, meaning no method is known that finds it quickly for large models.[15]

The authors therefore used a faster method that gives a good but not always the least detailed refinement. Its running time grows at a rate bounded by a fixed power of the model size.[15]

Counterexample-guided abstraction refinement loop Build initial abstraction Model check abstract model Property holds? Counterexample real? Real system satisfies property Report bug to user Refine: split an abstract state yes yes no no (spurious)
Counterexample-guided abstraction refinement checks a coarse abstract model and stops when the property holds or a real counterexample is found. When the counterexample is spurious, it splits an abstract state and checks again.

Costs and limits

Leaky abstractions

Joel Spolsky wrote in 2002 that "all non-trivial abstractions, to some degree, are leaky." A leaky abstraction is one where details it was meant to hide show through to its users.[8]

His example was TCP. If the network cable is cut, TCP cannot deliver the message. On an overloaded network TCP still works but becomes very slow, so the unreliable network below shows through.[8]

Edge cases

Computing abstractions must run within the limits of the physical world. Jeannette Wing notes that designers must therefore plan for edge cases and failures, such as a full disk or a server that does not respond.[6]

Overhead and indirection

Each layer has a cost. In operating systems, abstractions add overhead in two forms:[2]

  • extra time (more instructions);
  • extra space (in memory or on disk).

A well-known saying holds that all problems in computer science can be solved by another level of indirection, meaning one more intermediate step between a user and the thing used. Butler Lampson attributes it to David Wheeler, who added, "But that usually will create another problem."[17] Andrew Koenig has called the idea the fundamental theorem of software engineering.[18]

Diomidis Spinellis notes that indirection and layering add space and time overhead and can make code harder to understand.[17]

History

The main ideas of abstraction in computing took shape in the 1970s and were extended to verification over the following decades.

Year Work Contribution
1972 Dijkstra, "The Humble Programmer" Called abstraction the only mental tool by which a finite piece of reasoning can cover a myriad of cases[4]
1972 Parnas, "On the criteria to be used in decomposing systems into modules" Information hiding as the rule for dividing a system into modules[12]
1974 Liskov and Zilles, "Programming with abstract data types" Abstract data types and the cluster feature[1]
1977 Cousot and Cousot Abstract interpretation for static analysis[14]
1992 Clarke, Grumberg, and Long, "Model checking and abstraction" (POPL; journal version 1994) Abstract models for model checking[16]
2000 Clarke and colleagues Counterexample-guided abstraction refinement[15]
2002 Spolsky, "The Law of Leaky Abstractions" Named the problem of hidden details showing through[8]
2008 Wing, "Computational thinking and thinking about computing" Called abstraction "the essence of computational thinking"[6]

References

  1. Programming with Abstract Data Types, Barbara Liskov and Stephen Zilles, ACM SIGPLAN Notices, 1974.
  2. Introduction to Operating Systems, Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, Arpaci-Dusseau Books, 2018.
  3. Structure and Interpretation of Computer Programs, Section 1.1, Harold Abelson and Gerald Jay Sussman, MIT Press, 1996.
  4. The Humble Programmer (EWD 340), Edsger W. Dijkstra, ACM Turing Lecture, 1972.
  5. 8 great ideas in computer architecture, David A. Patterson, Elsevier, 2014.
  6. Computational thinking and thinking about computing, Jeannette M. Wing, Philosophical Transactions of the Royal Society A, 2008.
  7. Instruction Set Architectures, Computation Structures course notes, MIT, undated.
  8. The Law of Leaky Abstractions, Joel Spolsky, Joel on Software, 2002.
  9. Structure and Interpretation of Computer Programs, Section 2.1, Harold Abelson and Gerald Jay Sussman, MIT Press, 1996.
  10. Barbara Liskov, A.M. Turing Award Laureate, ACM.
  11. Program Development in Java: Abstraction, Specification, and Object-Oriented Design, Chapter 1 (sample), Barbara Liskov and John Guttag, Addison-Wesley, 2000.
  12. On the Criteria To Be Used in Decomposing Systems into Modules, D. L. Parnas, Communications of the ACM, 1972.
  13. Abstract Interpretation in a Nutshell, Patrick Cousot, École normale supérieure.
  14. Abstract interpretation: a unified lattice model for static analysis of programs by construction or approximation of fixpoints, Patrick Cousot and Radhia Cousot, POPL, 1977.
  15. Counterexample-guided Abstraction Refinement, Edmund Clarke, Orna Grumberg, Somesh Jha, Yuan Lu, and Helmut Veith, CAV, 2000.
  16. The Birth of Model Checking, Edmund M. Clarke, Carnegie Mellon University, 2007.
  17. Another Level of Indirection, Diomidis Spinellis, Beautiful Code, O'Reilly, 2007.
  18. The Fundamental Theorem of Project Management, Michael Feathers, Artima Weblogs, 2005.

Figure