Programming as applied mathematics

[First published as Python-XO: Programming as applied mathematics on December 17, 2007.]

Python is the programming language used to write most of the applications that come with the XO Laptop.

In Python-XO: The Farmer in the Dell, I said that programming is just a form of writing, and gave a brief example of a program in the Python programming language.

The writing that is programming is scientific in nature. Programs specify how computers are to operate. Computers– including desktops, laptops, or contained within wrist watches, printers, cell phones, and games such as the Sony Playstation and the Microsoft XBox — are the creation of scientists and engineers. The essential elements of programming languages are dictated by the structure of computers and are mathematical in nature. Programming is thus a form of applied mathematics.

However, this view of programming as applied mathematics is not a common one. For example, let’s look at several views of Python.

Guido Rossum, the creator of Python, says, in his Executive Summary:

What is Python? Executive Summary

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.

Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn’t catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python’s introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.

Eric S. Raymond, another well-known programmer, says in his essay, Why Python?:

I had already heard just enough about Python to know that it is what is nowadays called a “scripting language”, an interpretive language with its own built-in memory management and good facilities for calling and cooperating with other programs. So I dived into Programming Python with one question uppermost in my mind: what has this got that Perl does not?

Here is part of the brief summary from the Python wiki:

What Is Python?

Python is a programming/scripting language that can be used on many different computers and operating systems, including Windows, Unix, Macintosh, etc. I’ve heard of it being run on everything from palm computers to Cray super computers. For a number of reasons, it’s the *best* way to learn computer programming in general. The first few that come to mind are:

* The interactive “shell” allows you to instantly see if you did it right. Personally I find the Python shell easier to use than a calculator for everyday math at home and at work.

* The language is elegant and just plain makes sense, which means you or someone else can read your code weeks or months after you wrote it, and it will still make sense!

* Python was designed from the ground up for learning programming.

* You gotta love the awesome Python community of people who really want to help, such as the Python tutor email list.

* It has a short learning curve, which can have you programming successfully within minutes (really).

* Including the one that comes with the free Python download, there plenty of tutorials available for both programmers and people new to programming.

* It’s free and open source.

* It was named after Monty Python, which makes you wonder…..

* It’s a good stepping-stone to languages like Perl, Java, and C++, which make more sense after picking up some Python.

* It’s cross-platform, so the code you write for your Windows PC at home can be run on the linux server at work!

Though all these summaries are accurate, and I agree with most of the points they make, they don’t capture the essence of Python as I see it.

I spent the best years of my professional life — over a decade’s worth as it turned out — working on SETL, a language inspired by a simple observation by Jacob T. “Jack” Schwartz:

It would be an interesting experiment to base a programming language on finite set theory.

Python is based in part on some of the ideas found in SETL, and is the language closest in spirit to SETL that is currently in widespread use. The power of Python comes in large part from the power of set theory itself, for set theory is fundamental to mathematics. Here is an example, in the form of some pictures.

200712 032
On Sets

The page on the left is from Naive Set Theory by Paul R. Halmos. The part just above the miniature terminal says (emphasis added):

A pack of wolves, a bunch of grapes, or a flock of pigeons are all examples of sets of things. The mathematical concept of a set can be used as the foundation for all known mathematics.

The page on the right is from the lecture notes of a course in Mathematical Logic by Professor Martin Davis of NYU’s Courant Institute of Mathematical Sciences (CIMS) that I took in the Fall of 1969 (emphasis added):

Theorem 7.1. N is inconsistent.

This result should serve to emphasize that a theory whose postulates seem plausible enough can turn out to be inconsistent. Because all of contemporary mathematics can be developed in terms of the notion of set theory, N might have been thought of as an entirely reasonable starting point for the derivation of the entire body of mathematics.

Here is photo of the first page of Halmos’s classic work, Measure Theory:

200712 036
Chaper I: Measure Theory

Note that the first word in the first chapter is “sets,” another indication of the fundamental importance of set theory to mathematics.

The Preface to Naive Set Theory begins as follows:

Every mathematician agrees that every mathematician must know some set theory; the disagreement begins in trying to decide how much is some. This book contains my answer to that question. The purpose of the book is to tell the beginning student of advanced mathematics the basic set-theoretic facts of life, and to do so with a minimum of philosophical discourse and logical formalism.

Though one need not know any advanced mathematics, including set theory, to be a programmer, I think that an appreciation for the mathematical foundations of programming will make one a better programmer, and I expect that most skilled programmers know , though they might not first appreciate it, more mathematics than they give themselves credit for.

One Trackback

  1. […] Programming as applied mathematics By daveshields It’sa good stepping-stone to languages like Perl, Java, and C++, which make more sense after picking up some Python. * It’s cross-platform, so the code you write for your Windows PC at home can be run on the linux server at work! … The Wayward Word Press – […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

  • Pages

  • December 2007
    M T W T F S S
  • RSS The Wayward Word Press

  • Recent Comments

    daveshields on SPITBOL for OSX is now av…
    Russ Urquhart on SPITBOL for OSX is now av…
    Sahana’s Respo… on A brief history of Sahana by S…
    Sahana’s Respo… on A brief history of Sahana by S…
    James Murray on On being the maintainer, sole…
  • Archives

  • Blog Stats

  • Top Posts

  • Top Rated

  • Recent Posts

  • Archives

  • Top Rated

  • %d bloggers like this: