axiom-developer
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Literate Executables


From: Tim Daly
Subject: Re: Literate Executables
Date: Sun, 4 Dec 2022 03:14:43 -0500

In fact, making your program 'literate' (aka, a book ) is easy.

The first steps (1)-(6) are only done once to get your program in book form.
Note that this works with any programming language. I normally use Lisp
but I'll illustrate it with C code.

The last step (7) will create a PDF and also execute your code.
You run step (7) which just requires typing 'make' any time you
want to see the book and run the code.

So now you're in a "hot loop", writing explanations and changing your
literate code, typing 'make' to remake the book and rerun the code while
continuing to create a literate program in a PDF.

To explain in detail, assume you have "hello.c" and want it literate.

(1) convert your program to a latex file. Copy hello.c to hello.tex.

Starting with the your hello.c:

====================================
#include <stdio.h>
int main() {
  printf("hello\n");
  return 0;
}
=====================================

At the command line type:
=====================================
cp hello.c hello.tex
=====================================

(2) wrap your code into latex using macros (attached below)

There is a latex macro pair (attached in a latex style file below called
chunk.sty) which defines \begin{chunk} and \end{chunk} that wrap anything
in a verbatim-style block. The \begin{chunk} takes an argument which is any
word or sentence, as in \begin{chunk}{int main}. So you walk your source file(s)
inserting these macros around every function / struct / define, etc. So, for hello.c
you create a copy, call it hello.tex which contains your C code with latex blocks.
This is tedious but trivial.

Your hello.tex file looks like (the chunk name can be anything)
======================================================
\begin{chunk}{includes}
#include stdio.h
\end{chunk}

\begin{chunk}{main}
int main() {
  printf("hello\n"};
  return 0;
}
\end{chunk}
========================================================

(3) automate the extraction from your hello.tex to hello.c

Now you'd like to make it so your program can be extracted easily.
Make a new chunk using any name that collects your chunks: The
third macro, called getchunk, will insert the named chunk inline.

at the end of hello.tex add a new chunk:
===================================================
\begin{chunk}{all}
\getchunk{includes}
\getchunk{main}
\end{chunk}
====================================================

(4) check that you can recreate the original hello.c from hello.tex

The second too is a program called 'tanglec' which, given the name of a
chunk will extract it to stdout. So we ask for the whole program and put it into
extracted.c with and check that it is byte compatible with hello.c:
(Note: tanglec is attached below. gcc -o tanglec tanglec.c)

at the command line type:
===========================================
./tanglec hello.tex all >extracted.c
diff -Naur extracted.c hello.c
===========================================

(5) make hello.tex into a PDF. Just add some trivial latex header/footer.
For example add a header: (Note: chunk.sty is attached below)

So now your hello.tex looks like:
============================================

\documentclass[dvipdfm]{book}
\usepackage{chunk}
\begin{document}
\title{A Literate Hello}
\author{Timothy Daly}
\maketitle

\chapter{We need includes}
\section{This is the usual one for printf}
\begin{chunk}{includes}
#include <stdio.h>
\end{chunk}

\chapter{This is where the magic happens}
\begin{chunk}{main}
int main() {
  printf("hello\n");
  return 0;
}
\end{chunk}
\begin{chunk}{all}
\getchunk{includes}
\getchunk{main}
\end{chunk}

\end{document}

=================================================

(6) create a trivial makefile (note that indented lines require TABS, not spaces)

in a file called 'makefile'
===================================================
doit:
    latex hello.tex
    dvipdfm hello.dvi
    evince hello.pdf &
    tanglec hello.tex all >hello.c
    gcc -o hello hello.c
    ./hello
==================================================

(7)

At the command line
===============================================
make
================================================

WIN! Now you write all your explanation and your code in the latex file.
Then type 'make' which recreates the book and runs the code. The
explanation and the code are always in sync. Now your code is a book.

Now you can add pictures, URLs, a table of contents, an index,
cross references, a bibliography, and even a spiffy new cover!
Oh, and someone can actually read your explanation of your code.

Make Knuth happy! Win a Pulitzer prize!

Tim



On Sat, Dec 3, 2022 at 8:20 AM Tim Daly <axiomcas@gmail.com> wrote:
Java code has the pseudo-ability to re-generate the original source
through decompilation. Unfortunately given the size of any executable
it would be years worth of work to reverse-engineer the understanding
without explanation. I struggle to even understand the traceback of
any Java failure :-)

Open source, by definition, already has the source available.
Only the URL of the github repository and the hash number corresponding
to the current executable is needed.

I haven't worked for IBM since 1995, the year IBM Research eliminated
the math department, including me. My ground-breaking work in Artificial
Intelligence has yet to be cited by anyone so their decision was probably wise.

Tim


On Sat, Dec 3, 2022 at 1:01 AM Terence Kelly <tpkelly@eecs.umich.edu> wrote:

Hi Tim,

Your observations seem sound.  Keep in mind, however, that we're not
confronted with an either/or choice.  The chicken/egg aspect of literate
executables means that, in your context, we can arrange for the PDF to
generate code *and* for the code to generate PDF.  Cyclic dependency
graphs take some getting used to, but one can learn to love them.

If literate execution isn't right for Axiom, perhaps it can benefit other
IBM open source projects.  If you circulate the paper among your
colleagues I'd be interested to see if they find useful applications.

_Queue_ readers are remarkably creative and routinely find uses for Drill
Bits ideas that I never anticipated.  I wonder what your colleagues will
come up with.

Thanks.

-- Terence


On Fri, 2 Dec 2022, Tim Daly wrote:

> ...
>
> The above considerations leads me to the conclusion that the PDF is the
> thing that generates code rather than the code generating the PDF.

Attachment: tanglec.c
Description: Text Data

Attachment: chunk.sty
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]