## GAWK (GNU AWK) for AI?

I've been doing a lot of (g)awk lately and recently stumbled upon an interesting little paper called GAWK for AI?.

This resonates with my recent experiences/revelations regarding how feature impoverished languages (like gawk) can often help you focus more on the domain and task at hand.

## Comment viewing options

### Nice

Cute article. Not very deep, but worth reading anyway.

### Non-obvious choices of PL for teaching

I'll be teaching a course on the Curry-Howard correspondence next semester, and I'm leaning towards using Perl as the course language.

### Wow!

You go girl! ;-)

Let me know when I can post a link to the lecture notes...

### Any news?

Did you use perl?

### awk as superfoldl

Over at "Towards the best collection traversal interface", it has been proposed that a superfoldl, a left-fold with possible early exit, is a suitable basic element for traversing collections -- and AWK allows one to script cheap and cheerful left-folds (including their possible exits).

I find AWK to be at its best for lists of sum-of-product style datatypes, but a little string manipulation will go a long ways towards handling trees almost as simply.

### the author's site is at http:

the author's site is at http://www.cs.wustl.edu/~loui/ and there's a draft of a book on cgi programming with awk at http://www.cs.wustl.edu/~loui/160f00/Draft-091700.doc

that book has example code in awk near the end.

i was curious to see what awk code looked like because all i remember from using awk 15 or 20 years ago was a syntax that matched regular expressions with actions. so i was wondering how that extended to ai code (i was imagining complex state machines based on repeated matching...).

anyway, that must be some kind of special case (i guess i could read the man page) - they use it as a simple imperative language (define functions, call them, print stuff). so in the end there's not much interesting here (if, like me, you somehow thought there was some exciting new paradigm involved). it really is just "awk is simple". pity.

### Not much new added to awk

Awk/gawk hasn't changed much over the years. Nothing special to support AI or any other paradigms has been added. It's still basically about pattern->action pairs. If you can reduce or re-cast your problem domain into the manipulation of simple arrays or streams of characters, then awk offers a very easy to learn language.

I think that it was its lack of baggage that may have made it attactive for teaching AI principles. How Loui managed to recast AI into manipulation of strings is the mystery to me (then again, I don't know what AI is these days). But, I can see how, once that decision was made, Perl and other more complex languages could get in the way (with the student having to master a complex language instead of concentrating on the problems at hand).

Awk is/was interesting because of its focus on implicit iteration, pattern->action pairs, automatic splitting of data and generalized use of associative arrays.

### i think maybe you missed my p

i think maybe you missed my point. he's not using pattern action pairs. he's using awk as a simplified perl. function blocks and calls. so i don't think he's "recast ai into the manipulation of strings" (which is what i was looking for, too).

here's some example code from the cgi book:

function genimage(x,y,  i,j,command,width,mult) {
# generates a bar graph in text

# we have to scale this to 50 characters wide
mult = int(50/(numyes+numno))
x = mult * numyes
y = mult * numno

print "Content-type: text/plain\n"

width = 10

for (j=1; j

### That's kinda like what Jpn Be

That's kinda like what Jon Bentley does in the pearls books.

While it's not patter-action pairs, this does make use of the programming facilities provided by AWK, especially associative arrays. Not very exciting, in that regard, sure.

### i still haven't read those bo

i still haven't read those books.

i was re-reading turing's paper ("on...") the other day, after remembering/reading chaitin (has there been a post here recently about him? - all his books are on his web page if you click enough). so i leapt to the conclusion that somehow they were using strings as the tape and awk statements as the different states...

although how a turing machine helps with ai is something my subconscious didn't get round to explaining.

### and not too wise (anymore)...

...That's kinda like what Jpn Bentley does in the pearls books...

Back then, there weren't that many choices for the working programmer. Awk was a breathe of fresh air (to me as a Unix/C coder). My Bentley books were quite well worn.

But, these days choosing awk for general purpose programming is probably not too wise. Even with the short-ish scripts I do in awk, I am often tripped up by the clumsy way awk declares local variables in functions.

However, I am still tickled by how far you can take stuff like Gawkinet

### ah... bummer

...he's not using pattern action pairs. he's using awk as a simplified perl...

Okay, I see. If that is what he is doing w/ his AI approach (where you can just as easily substitute another syntatically simple language like Lua, Tcl etc), then it isn't that interesting. I'll have to fire off an email to him and see if he will respond with what he was doing with AI and awk.

### I agree that the interesting

I agree that the interesting part of awk is the catamorphic fragment; the anamorphic side is fairly vanilla algoly. However, that leads to two points in favor of awk; one theoretical and one practical:

- from a theoretical viewpoint, even though awk is an eager language, it is a good component with which to build lazy computations. As an intermediate stage in a pipeline, an awk program can partially reduce fragments of state, to be fully evaluated only by later (perhaps awk, perhaps even the user) stages. If one limits oneself to the pattern-matching fragment of awk, it, unlike sed, does not lend itself to fixed-point convergences, and hence encourages "online" implementations of functions.

- from a practical viewpoint, the design of awk, which was meant to run on machines whose total memory was probably comparable to the close caches on modern architectures, means that it is very lightweight from the iron's perspective. As an example, I had some quick-and-dirty 3D model processors written in awk which I recently converted to python/numpy, with the result that performance suffered: it turned out that while the python program was still thinking about loading and linking its interpreter, the awk program had almost finished streaming through its input buffer to produce results. (I expect this distinction extends to other syntactically simple languages like Lua, Tcl etc. but do not necessarily expect it to extend to GNU awk...) This difference is not so important in batch, but if one is firing up processes in the middle of interactive loops, it becomes noticeable.

:: :: ::

Almost as interesting as the pattern-action pairs is awk's sugar around popen(). Judicious use of close() can easily turn tree processing into list processing; much of the time, however, one has no call for the control breaks. One need not "deforest" if one never constructs trees in the first place.
 #!/usr/bin/awk -f {c="awk 'BEGIN{n=1}{n*=$1}END{print n}'";while($1){print \$1--|c}close(c)} 

### is there a simple definition

is there a simple definition of catamorphic and anamorphic anywhere? i just failed to google a simple, isolated definition. thanks.

### Fold/unfold

Roughly, catamorphic means related to folds (recursive reductions of a more complex structure to a simpler one) and anamorphic means related to unfolds (recursive construction of a more complex structure from a simpler one).

Hope that helps.

### No roughly about it

Catamorphisms are folds, anamorphisms are unfolds. Nowadays fold/unfold are used more often.

thanks

### Rough week

Catamorphisms are folds, anamorphisms are unfolds. Nowadays fold/unfold are used more often.

Given that most people here interpret fold and unfold as list operations in a PL, "roughly" still applies: catamorphism/anamorphism has a more general meaning in CT.

Tough crowd at LtU this week. Perhaps I should have picked another week to post again...

### GNU awk

I'm reminded of a joke by Philip Greenspun - the power of Gnu AWK.

Funny!

### joke, not anecdote

Read some of the other messages in that mailing list archive. Eg, the following week's message states that henceforth Satan will be a member of every thesis committee.

(The mailing list in question, "gsb", is (was?) used for sending out reminders of the weekly AI Lab "Girl Scout Benefit", although there are rumors that the Girl Scouts are just a cover story, and that "GSB" actually stands for "Graduate Student Beer". :-)

thanks

### you wouldn't be the first to

you wouldn't be the first to not get the joke... (wrt this)

### forward xref

A forward cross-reference: In Praise of Scripting: Real Programming Pragmatism, by Ronald Loui.