Tuesday, March 27, 2007

Formating Fortress code with Fortify

A couple of days ago I noticed that a new tool to generate LaTeX code from Fortress source code was available. The tool is called Fortify can be found in the same page where the reference implementation is located.

In this post I'm going to try to create a little example and run Fortify to see the output of the generated LaTeX document.

One of the interesting things about Fortress is that, the language will have an alternative syntax that uses a lot of graphical symbols that will make a Fortress program look like a mathematical document.

For this test I took my old Numerical Analysis book and copied the Newton Raphson algorithm.

The program looks like this:


component Test

export Executable

nr(approx:RR64,f : RR64 -> RR64, f' : RR64 -> RR64,
N : Number,tol : RR64) = do

i:Number := 1
p:RR64 := 0
p0:RR64 := approx
ready:Boolean := false

while(i <= N AND NOT ready) do
p := p0 - f(p0)/f'(p0)
if |p - p0| < tol then
ready := true
end
i := i + 1
p0 := p
end

p
end

myF(x) = cos(x) - x
myF'(x) = - sin(x) - 1

run(a:String...) = print nr(1.5,myF,myF',10,0.0001)

end


Fortify is a tool that works inside Emacs. So I loaded fortify inside Emacs:


(load "..../fortify.el")


Then loaded Fortify by calling M-x fortify.

Selected the code:



And pressed M-&, then LaTeX code is generated.




To the generated code I added the LaTeX document declaration elements and imported a macros file included with Fortify.



\documentclass{article}
\usepackage{url, amssymb, amsmath, amsthm, stmaryrd, xspace}
\usepackage{amsfonts, graphicx, fullpage, times, hyperref}
\input ../fortify/fortify-macros

\begin{document}

....

\end{document}




After running LaTeX the code looks like this: