‘Literate programming (in python)’

4. & 2. Oct 2012, Lukas Prokop

PyGraz Logo

http://lukas-prokop.at/talks/pygraz-lp

D.E.K.

Donald E. Knuth
Fig. 1 Donald E. Knuth

Background

TeX (→ LaTeX) was written by Don Knuth.

TeX and Metafont have been application tests for Web; Knuth's programming system for literate programming.

Thus, the Web system passed its most crucial test when it was first used to produce the programs for TeX and for Metafont.

—[0, p. 185]

>>>> TeX.date
Traceback (most recent call last):
  File "<brain>", line 1, in <module>
KnowledgeBaseError: I am too young for this
>>>>

Timeline of Literate programming in python

Timeline of Literate programming (including Leo for python)
Fig. 2 LP timeline

Table of Contents

  1. Background
  2. The idea of literate programming
  3. The problem of software documentation
  4. Software documentation in practice
  5. Programming language support
  6. Tools

Literate programming

—[0, p. 99]

Literate programming

  1. We understand a complicated system by understanding its simple parts.
  2. Programming is not creating a hierarchical structure. It's neither bottom-up nor top-down.
  3. If we express a program as a web of ideas, we can emphasize its structural properties in a natural and satisfying way.
  4. It's rather a web of ideas which results from your mental representation and gets expressed in source code.
  5. Definitions in programs cannot be put anywhere. Therefore the order of the instructions is merely given.
  6. This order is a problem for us. The order of consciousness is the semantically intelligent order understandable by programmers and gets violated.
  7. We have to provide tools for reordering, formatting and restructuring text in a LP system.

—[0, p. 99]

The WEB system

Usecase: Software documentation.
File extension: *.web, *.w
Technique: Macros

WEB itself is chiefly a combination of two other languages:

  1. a document formatting language
  2. a programming language

—[0, p. 101]

Weave and Tangle in the WEB
Fig. 3 Weave and Tangle

TANGLE output is as ugly as possible to ensure that programmers only deal with WEB files

—[0, p. 140]

Literate programming

A literate programming system is provided iff

Table of Contents

  1. Background
  2. The idea of literate programming
  3. The problem of software documentation
  4. Software documentation in practice
  5. Programming language support
  6. Tools

The problem of software documentation

Contra: The semantics are right there!
Pro: Source code is for the compiler.
Knowledge abstraction conflict:
Program

Knowledge
Writing programs
Program

Knowledge
Extending programs

Visual representation of handling the Knowledge abstraction conflict

Abstraction layers
Fig. 4 Abstraction layers.
Arrows are tasks done by compilers. But we need different tools for the dotted arrow.

Software documentation

What are the variables?

Software documentation

What are the variables?

Example: Abstraction layer

  0 SETUP_LOOP              33 (to 36)
  3 LOAD_FAST                1 (b)
  6 LOAD_CONST               1 (0)
  9 COMPARE_OP               3 (!=)
 12 POP_JUMP_IF_FALSE       35

 15 LOAD_FAST                1 (b)
 18 LOAD_FAST                0 (a)
 21 LOAD_FAST                1 (b)
 24 BINARY_MODULO       
 25 ROT_TWO             
 26 STORE_FAST               0 (a)
 29 STORE_FAST               1 (b)
 32 JUMP_ABSOLUTE            3
 35 POP_BLOCK           

 36 LOAD_FAST                0 (a)
 39 RETURN_VALUE

Example: Abstraction layer

#!/usr/bin/env python
# -*- coding: utf-8 -*-

def euclid(a, b):
    while b != 0:
        a, b = b, a % b
    return a

if __name__ == '__main__':
    print(euclid(21, 35))

Example: Software paradigm

import Data.List
 
seq1 n = snd . foldl sequ' (1, 0) . map (toEnum . fromIntegral) $ unfoldl divs n
    where
        unfoldl f x = case f x of
                Nothing     -> []
                Just (u, v) -> unfoldl f v ++ [u]
 
        divs 0 = Nothing
        divs k = Just (uncurry (flip (,)) (k `divMod` 2))
 
        sequ' (f, g) p
            | p         = (f*(f+2*g), f^2 + g^2)
            | otherwise = (f^2+g^2,   g*(2*f-g))

Example: Software paradigm

fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
def fib():
    a, b = 0, 1
    yield a
    yield a
    while True:
        a, b = b, a + b
        yield b

Example: Web & Order of consciousness

def logout():
    session.clear()
    if request.args.get('signout'):
        <<Delete user from database>>
    return redirect(url_for('main'))
<<Delete user from database>>=
usr = User.query.filter_by(username='meisterluk').first()
usr.delete()
db.session.commit()

anything_else()

Or error handling. Not only functions / classes / modules.

The common sense

Given is a particular documentation. Will programmer X understand the documentation?

The common sense

The real deal-breakers are

We are making 2 assumptions here:

What's wrong? What's right? Docu might help.

int errno = ERROR_NONE;
if (errno = delete_item(*prev))
{
    // deleted.
}

Documentation is only needed when program flow isn't obvious.

Guessing means software regression.

Table of Contents

  1. Background
  2. The idea of literate programming
  3. The problem of software documentation
  4. Software documentation in practice
  5. Programming language support
  6. Tools

Established concepts in the software industry

Yeah, but taking over an old project of an unavailable employee is probably the most common usecase for documentation?! Which of those concepts do help with the target audience, conventions & idioms and application domain?

Freedoms of a programmer

Structured programming (Pascal, C)

Freedoms of a programmer

Python

One command per line generates often one semantic per line.

Documentation support

No programming language has full support for literate programming natively ever since 1981; Haskell might have. For instance, no one provides tools to create compilable and readable text separately.

Probably, the WEB system implies to many things:

Table of Contents

  1. Background
  2. The idea of literate programming
  3. The problem of software documentation
  4. Software documentation in practice
  5. Programming language support
  6. Tools

Haskell: Almost literally

Save as rule110.lhs (literate Haskell)

This Haskell file implements Rule 100.  Rule 110 is a cellular automaton rule
for a 1D cellular automaton.  

The state of the cellular automaton is a string that is infinite in both
directions and contains 0s and 1s.  Updating the state of the string consists
of replacing each value with a new value based on its context.  Patterns
"111", "100", and "000" result in the value "0" at the center, and all other
patterns result in the value "1" at their center.

Let's start with a replacement rule that isn't quite right but gets most
of the job done.

> repl110 :: String -> String
> repl110 s | (length s < 3) = s
> repl110 s | take 3 s `elem` ["111","100","000"] = '0' : repl110 (tail s)
> repl110 s = '1' : repl110 (tail s)

Clojure: Documentation strings

(defn func "A doc showcase." [name] (str "Hello World, " name "!"))
(doc func)

Docstrings like python. But besides :doc also eg. :test is available.

>>> def func(name):
...     """A doc showcase."""
...     return "Hello World, {}!".format(name)
... 
>>> func.__doc__
'A doc showcase.'
>>> func.__test__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute '__test__'
>>>

Table of Contents

  1. Background
  2. The idea of literate programming
  3. The problem of software documentation
  4. Software documentation in practice
  5. Programming language support
  6. Tools

XML community

Yeah, there are numerous examples for possible XML formats out there. Inline SVG and MathML rocks and XSLT provide simple tools for the implementation.

But who wants to write XML? :-P

Perl: POD

Documentation? Yes. Literate programming? No.

=pod

=head1 NAME

Date::Manip::Delta - Methods for working with deltas

=head1 SYNOPSIS

   use Date::Manip::Delta;
   $date = new Date::Manip::Delta;

=head1 DESCRIPTION

This module contains functions useful in parsing and manipulating
deltas.  As used in this module, a delta refers only to the amount of
time elapsed.  It includes no information about a starting or ending
time.

There are several concepts involved in understanding the properties
of a delta.

=over 4

=item B<fields>

A delta consists of 7 fields: years, months, weeks, days, hours,
minutes, and seconds, usually expressed as a colon-separated string.
For example:

   1:2:3:4:5:6:7

refers to an elapsed amount of time 1 year, 2 months, 3 weeks, 4 days,
5 hours, 6 minutes, and 7 seconds long.

=cut

print("Hello World")

CWEB

Switch between different modes

javadoc

/**
 * Returns an Image object that can then be painted on the screen. 
 * The url argument must specify an absolute {@link URL}. The name
 * argument is a specifier that is relative to the url argument. 
 * <p>
 * This method always returns immediately, whether or not the 
 * image exists. When this applet attempts to draw the image on
 * the screen, the data will be loaded. The graphics primitives 
 * that draw the image will incrementally paint on the screen. 
 *
 * @param  url  an absolute URL giving the base location of the image
 * @param  name the location of the image, relative to the url argument
 * @return      the image at the specified URL
 * @see         Image
 */
 public Image getImage(URL url, String name) {
        try {
            return getImage(new URL(url, name));
        } catch (MalformedURLException e) {
            return null;
        }
 }

—via oracle.com

Sphinx documentation generator

For specification! At least meant to be.

Screenshot docs.python.org

Leo: a programmer's editor & more

Leo Programming outline editor

Leo turns literate programmers into reference librarians ;-)

—Edward K. Ream, Author of Leo

Summary

Problems include:

Different opinions

I guess, Guido is in favor of Embed documentation in a markup-language inside source code as comments.

Knuth says Embed marked-up source code in documentation

Summary

Literate Programming is difficult. Jon Bentley's observation: "a small percentage of the world's population is good at programming, and a small percentage is good at writing; apparently [Knuth is] asking everybody to be in both subsets."

—S.Lott at StackOverflow

Message of the Day

You should be commenting why you are doing something, not what the code does. Documentation for programmers is the problem.

Documentation in monospace? Take it to the next level!

Think about doc tools! Pick one. Use it.