Home
Beware of the Train [entries|archive|friends|userinfo]
pozorvlak

[ website | My Website ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Links
[Links:| My moblog Hypothetical, the place to be My (fairly feeble) website ]

Thesis sitrep [May. 7th, 2008|11:44 pm]
[Tags|, , , ]

I've been collecting some more data on the thesis:
Wed May  7 23:45:49 BST 2008
4741 lines 23246 words 163656 characters
thesis.log:Output written on thesis.dvi (84 pages, 643096 bytes).
33 fixmes
80 definitions
141 term-definitions
24 theorems
21 lemmas
6 corollaries
30 examples
143 bullet-points
A "term-definition" is a call to the macro \defterm, which I use to emphasize the term being defined. The count's not the same as the number of definitions because I often group the definitions of related terms into the same \begin{defn}...\end{defn} block. The lemmas, theorems etc are mostly other people's :-(

I like bullet points. They reflect how I think, and bullet-pointed text takes up more space on the page.
link9 comments|post comment

Template Haskell: baby steps [Mar. 4th, 2008|11:43 am]
[Tags|, , , , , , , ]

My post about design patterns and if-statements in Smalltalk yesterday got a very pleasing reaction, garnering some informative comments from knowledgeable people and making the front page of programming.reddit. I guess I should have checked my facts on Smalltalk semantics more carefully before clicking "Post" :-) One comment that particularly interested me was this one (sadly anonymous), which shows a rather cute trick for using Smalltalk-style keyword methods in Haskell:
data Then = Then
data Else = Else

if' :: Bool -> Then -> a -> Else -> a -> a
if' True Then t Else f = t
if' False Then t Else f = f
Now that's pretty cool, and could even be useful for DSL implementation1. But there's a problem. See the repeated code?
data Then = Then
data Else = Else
Here, let me show it to you the way it looks to me:
data Then = Then
data Else = Else
Literally half the elements that aren't pure syntax on those lines are repeated. What I'd like to do is define a keyword function, and then just write
keyword Then
keyword Else
Or, better,
keywords Then Else
Not a big deal, you might think, but what if you were writing a whole EDSL with many keywords? Or many different EDSLs? And it's not just the repetition: by defining a keywords function, I'd move the code up an abstraction level, making my intent clearer. We could of course write
data Keywords = Then | Else
but then we lose the benefit of compile-time syntax checking of our EDSL. Well, I guess our pseudo-keywords are syntactic sugar anyway, so it doesn't really matter. But still.

A few days ago, someone asked me for an example of code that's repetitive in Haskell but wouldn't be in Perl, and here's one. In Perl, and probably in Python, I could write a keywords function. It would be ugly, and would involve hairy symbol-table hacking or calls to eval, but the user wouldn't need to know that and I'd only need to write it once (never mind the fact that the "pseudo-keyworded functions" pattern wouldn't actually make any sense in Perl...). In Lisp or Ruby, I wouldn't need to write it at all, because I could use symbols instead. But perhaps this kind of thing (and other, more egregious examples) are just the price we pay for Haskell's type system, and are more than balanced out by greater opportunities for concision and abstraction elsewhere?

Nah, bollocks to that. We can do better.

Template Haskell to the rescue! )

The final version of Main.hs is here, and the final version of Keyword.hs is here. Not entirely straightforward, but we pretty much got there in the end. Would it be worth posting these code samples to the TH wiki, and perhaps linking here? Anyway, it's nice to have finally got this simple stuff to work: hopefully I'll be able to get some more difficult things to work soon, and this should reduce the amount of frustration and needlessly duplicated code in my Haskell programming experience.

1 Did you know you can do this in (plain) TeX, too?
\def\fred #1 barney #2 betty{#1#2}

\fred Hello there, barney Wilma betty !
produces "Hello there, Wilma!".
link6 comments|post comment

Non-colliding monic arrows in XYPic [Nov. 26th, 2007|12:53 pm]
[Tags|, , , , , ]

For reasons that are increasingly unclear to me, I've been using the TeX package XY-Pic for the commutative diagrams (of which there are many) in my thesis and papers. If there is one and only one Right Way to do something, you can pretty much guarantee that XY-Pic will:
  1. do something else by default, producing horribly ugly output;
  2. only do the Right Thing if you utter some cryptic and fragile incantation in a language that looks like the bastard offspring of APL and the Black Speech of Mordor1;
  3. hide the details of said incantation away somewhere in the depths of the voluminous, poorly-indexed, verbose and maddeningly unclear manual.
As should be clear, I'm not a huge fan.

One of its more annoying characteristics is the way it handles tails of arrows, which (by default) start after the end of the arrow, so the tail invariably collides with whatever it was the arrow is pointing away from. Like this:
That was produced by the code \xymatrix{ A \ar@{>->}[r] & B }, which, while not terribly clear, is the obvious thing to try, and the only thing you'll know how to do unless you've wasted days reading the manual.

Fortunately, there is a fix )

1 Thinking about it, XY-Pic is actually kinda agglutinative, much like the Black Speech...
link9 comments|post comment

Originality considered difficult [Apr. 17th, 2007|01:31 pm]
[Tags|, , , , , , , ]

About a month ago, [info]wormwood_pearl and I went to see the fantastic stand-up comedian Daniel Kitson (who you should all go and see if the slightest opportunity presents itself). One thing that he said really struck me:
If you ever think you've had an original thought, put it in quotation marks and type it into Google. Now that's a humbling experience.
So I really shouldn't have been surprised when I found out that someone else had already gone ahead with my idea to extend TeX with OCaml. You can get OCamlTeX here. Somewhat to my surprise, it's based on PerlTeX, which allows you to extend TeX using Perl, writing the bodies of your macros in Perl code. For example, \perlnewcommand{\reversewords}[1]{join " ", reverse split " ", $_[0]} defines a macro that reverses the words in its arguments (not at all easy in raw TeX). I'm not entirely sure (the Perl's less than entirely clear, and the TeX is completely beyond me), but it seems to work by setting up intermediate files to communicate between a perl process and a latex process: latex writes out requests to one file, perl reads them, writes the results out to another file, which latex reads and incorporates in the document. Thus there's no need to reimplement the hairy TeX lexer or macro-expander (as there would be with a preprocessor-based approach) or to rewrite the whole of TeX in Perl/OCaml (as I originally envisaged - but come on, it could be fun :-) ).

Another one for the originality-is-hard file is this article, which says a lot of the things I've been thinking about the differences between static typing and unit testing, and how they each influence development methodology. Only the author, mwh, said it back in 2004 :-(. Mwh mentions some things I hadn't thought of, like unit testing tending to make your designs more loosely coupled (because it's such a pain setting up dozens of helper objects for each test). I was particularly struck by this paragraph:
As to which "side" is better, well, I'm not going to attempt an answer to that question :-) One thing to consider, though, is the side benefits of a given methodology such as the unit testing driving loose coupling. There are probably side benefits to the powerful static type system approach, too. I will notice that for sub-genius programmers, the unit test approach is probably just plain easier.
This resonated with something else that I've been thinking. Kevin Barnes draws a distinction between "freedom languages" and "safety languages" - freedom languages are languages like Lisp or Perl or C, which strive to give you as much freedom as possible. The rationale for this was expressed wonderfully by chromatic: bad programmers will shoot themselves in the foot anyway, and good programmers will use the features to do things that would otherwise be impossible. Safety languages are languages that go to the opposite extreme, and make it very hard to do unsafe things, like Java or Ada. The rationale being roughly a) fewer bugs (even good programmers make mistakes), b) better automated tools. Compare this with the distinction drawn here between Languages for the Masses and Languages for Smart People. At first I thought he was talking about the same thing, but then it struck me: Haskell (and ML, etc), are Languages for Smart People that are also Safety Languages. Which makes them interesting, as most other LFSPs are Freedom Languages. This suggests the question: are there any LFMs that are Freedom Languages? Possibly Python: it's certainly an LFM (it's closely entwined with a project called Computer Programming for Everybody, will be the main programming language used on the One Laptop Per Child laptops, etc - note that I'm not saying that being an LFM is a bad thing!), and yet you can (if you're prepared to ignore the scary DEPRECATED! warnings) do most of the run-time metaprogramming and dark hackery that are more common in Ruby, Perl etc.

Re-reading the LFSP article, I notice that he's mainly concerned with abstractive power: maybe this is the distinction. Safety Languages for Smart People employ powerful abstractions, but restrict dark hackery; Freedom Languages for Smart People allow for both. Maybe the distinction is only visible above a certain level of power, like the weak and electromagnetic forces are only distinct at certain energies...

Of course, if all my "thoughts" are formed by stitching together things I read on the web, it's hardly surprising that they're not original :-)
link13 comments|post comment

Emacs question [Nov. 17th, 2006|12:02 pm]
[Tags|, ]
[music |Mr Scruff, Keep it Unreal]

Emacs users: how would you do this in Emacs? I've included a vi solution under the cut, but I suspect (based on the kind of thing we've been talking about recently) that, while the vi solution would work within Emacs, the most natural Emacs solution would look rather different.

You want to create a table of the Greek alphabet, in TeX, with lowercase and uppercase forms of the letters, their names, and similar symbols which they might be confused with. So you want a line of code to start the table and another to end it, and in the middle, you want lines like
$\alpha$ & A & Alpha & $\infinity$ \\
$\beta$ & $\Beta$ & Beta & \\
...
$\epsilon, \varepsilon$ & E & $\in$ \\
There are, as you can see, some irregularities - some Greek letters don't have uppercase equivalents in TeX (so there's no \Alpha or \Eta), some have alternate forms, and the "easily confused with" column has to be done by hand. But there's still a lot of redundancy in the table. What's the easiest way of creating this in Emacs?

How I would have done it in vi )

Unfortunately, when I was actually doing this, I had access to neither vi nor Emacs - due to various annoyances, I had to create the document under Windows, using a ghastly excuse for an editor called WinEdt. If you are ever tempted to try using this "editor", resist. It will only cause pain.
link11 comments|post comment

More TeX [Nov. 3rd, 2006|05:16 pm]
[Tags|, , , , ]

This morning I wrote a TeX macro using \expandafter (ie, I had to muck about with the order in which the macro engine expands things). Possibly this makes me some sort of l337 TeX h4x0r, but mainly it makes me unhappy with (a) TeX and (b) XY-Pic for forcing me to do such an ugly thing.

[The macro in question defines an XY-Pic arrow with certain decorations. The code to produce the decoration is (since it's XY-Pic) hairy and unreadable, so I've packaged it into a macro. But this needs to be expanded before the rest of the code for drawing the arrow, or it gets swallowed up by the label on the arrow. Aaargh.]

In other mathematical news, today I sent this email to two of my students who have yet to show their faces at my tutorials:
Hi [students' names]

Are you planning to turn up to our tutorial this week? I've got some work from you both that I've marked and would like to hand back. In case you've forgotten, it's in room [number] on the [n'th] floor of the maths building (the really ugly one next to the Boyd Orr and opposite the QM), at 12 noon on Wednesday. It would be good to see you both.

I hope your class tests went well!

[My name]
I think that strikes a nice balance between friendliness and menace :-)
link3 comments|post comment

Rewriting TeX [Apr. 28th, 2006|05:23 pm]
[Tags|, , , ]

It seems someone's already done something similar to what I mentioned a while ago, namely rewriting TEX (or something like it) as an embedded DSL in Caml or some such, so that extensions could be written in a Real Programming Language (and so that other DSLs could be embedded more easily). It's troff rather than TEX, and Scheme rather than Caml, but the principle is much the same. I haven't tried it yet: I wonder if it's any good? I've never used troff, but I like the way it handles minilanguages for embedded pictures, equations and so on.

I've almost finished reading Leslie Lamport's LATEX manual (I really don't know why it's taken me so long) and I'm about to start on The TEXBook. My rationale is that if I'm going to be an academic I should know how to use TEX properly; this will involve reading the TEXBook through at some point, and I might as well do it now. I look forward to finding out what, if anything, LATEX actually brings to the TEX experience...
link15 comments|post comment

Software design done right [Apr. 18th, 2006|03:51 pm]
[Tags|, , ]

Periodically, I have another go at using LaTeX to see if it's still as ghastly as I'd remembered. So far, my current attempt has gone on for longer than any of the others, and I haven't yet run screaming back to plain TeX. Rant about the evils of LaTeX )

Right, rant over. Now I'm going to talk, as promised, about software design done right, as exemplified by BiBTeX and the Unix command units. Fulsome praise for the aforementioned utilities, in which six cubic metres of punch are mixed )

I think the boat was probably overkill...
link7 comments|post comment

navigation
[ viewing | most recent entries ]