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 >>>>
—[0, p. 99]
web of ideaswhich results from your mental representation and gets expressed in source code.
order of consciousnessis the semantically intelligent order understandable by programmers and gets violated.
—[0, p. 99]
Usecase: Software documentation.
File extension: *.web, *.w
Technique: Macros
WEB itself is chiefly a combination of two other languages:
- a document formatting language
- a programming language
—[0, p. 101]
TANGLE output is as ugly as possible to ensure that programmers only deal with WEB files
—[0, p. 140]
A literate programming system is provided iff
Contra: | The semantics are right there! |
---|---|
Pro: | Source code is for the compiler. |
What are the variables?
What are the variables?
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
#!/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))
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))
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
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.
Given is a particular documentation. Will programmer X understand the documentation?
The real deal-breakers are
We are making 2 assumptions here:
int errno = ERROR_NONE; if (errno = delete_item(*prev)) { // deleted. }
Documentation is only needed when program flow isn't obvious.
Guessing means software regression.
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?
One command per line generates often one semantic per line.
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:
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)
(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__' >>>
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?
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")
Switch between different modes
/** * 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
For specification! At least meant to be.
Leo turns literate programmers into reference librarians ;-)
—Edward K. Ream, Author of Leo
Problems include:
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
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
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.