Monday, November 4, 2013

haskintex: Haskell within LaTeX

I am here today to announce my new package: haskintex. Actually, the package has been around in Hackage for some time now, but I didn't want to announce it. Although everything was ready for this release some days (weeks?) ago, I have been waiting for the Hackage server to update its Cabal version (see #85@hackage-server) to the 1.18 branch. This is because haskintex depends on HaTeX >= 3.9, and the new version was failing to upload to Hackage due to an undefined cabal field (extra-doc-files) warning. This field was defined for the 1.18 version of Cabal and quickly used by HaTeX. HaTeX still builds with previous versions of Cabal, the only difference is that images in the documentation do not appear.

What is haskintex

haskintex is a tool that processes files, usually files that follow the LaTeX syntax. Although it has been programmed with LaTeX in mind, it was later clear that it can be used with other formats as well (with some restrictions). The purpose of haskintex is to include Haskell code within LaTeX files, and evaluate it or display it as desired. Therefore, it is similar in purpose to lhs2tex, but with a different approach. The input file is usually a .htex file, which is a LaTeX file plus some commands and environments like \evalhaskell{2+3}. haskintex would process these declarations running GHC on their argument and substituting the result in the output file. Something like \verb`5`. You can add things to the scope within a \begin{writehaskell}...\end{writehaskell} environment.

Calling HaTeX functions

However, the most interesting way to use Haskell within LaTeX is in combination with HaTeX. HaTeX is library that implements the LaTeX syntax in Haskell. Inside the \hatex{...} command, you can put any Haskell expressions of type LaTeX. When processed by haskintex, it will be type checked, evaluated and rendered as LaTeX code. This brings all the benefits from HaTeX without the need to write all the boilerplate HaTeX code for things that plain LaTeX would do just fine. For example, suppose you want to draw a logarithmic spiral. The code below will do the job for you.

\author{Daniel Díaz}
\title{Embedding HaTeX in \emph{haskintex}}
Below is the \emph{Spira Mirabilis} inserted using the HaTeX
import Text.LaTeX
import Text.LaTeX.Packages.TikZ.Simple

spiral :: Figure
spiral = LineWidth (Pt 2) $
    pathImage 0.01 (0,4) $
      \t -> ( a * exp t * cos (b*t)
            , a * exp t * sin (b*t)
    a = 0.1 ; b = 4
\hatex{center $ tikzpicture $ figuretikz spiral}

See the documentation of the TikZ.Simple module of HaTeX to understand the functions used above. You may want to use different HaTeX modules for other tasks.

More info about haskintex and its usage at the homepage of the project.

Thanks for reading,
Daniel Díaz.

Sunday, October 6, 2013

Vim and me

This post contains some vim commands that I want to remember.

Setting line ending type

Just :set ff=unix, or :set ff=dos, etc.

Thursday, August 22, 2013

LaTeX tricks

In this post I will gather different LaTeX tricks that I want to remember.

Changing the space between paragraphs

Command to change the space between paragraphs globally.

\setlength{\parskip}{desired space}

I think the default value is \baselineskip.

Friday, August 16, 2013

Processing: Optimizations and FireFox

I have done a lot of commits to the processing GitHub repo since the last Hackage release. I have discovered dozens of bugs that are now fixed. I have also implemented some new nice features, like conditionals with custom values or arrays. However, the nicest change has been done in the Substitution Optimization Algorithm. At least, this is the one I am more enthusiast about! It took me a while to get arrays working correctly, but it wasn't so exciting. Well, maybe arrays with custom values are a bit magical! They handle a set a of arrays of potentially different types under the hood, so you think that you are dealing with a single array.

In any case, the optimization process has been improved greatly. Now it looks for common sub-expressions inside a set of optimizable values, which are instances of a class. Instances of this class are automatically generated using Template Haskell. The nice thing is that, an expression of type Float can be inside an expression of type Integer (for example, in round pi), however, the optimizer will find expressions that are nested even in expressions of other types. Furthermore, the optimizer is now able to take greater pieces of code to work with, since it is now able to detect which variables are mutated. Therefore, it is able to stick to the biggest piece of code where variables can be treated as constants. Neat.

Of course, since I am the developer of the library, I find all these words easy to understand, but for the reader they may be quite opaque. In the section below I will try to explain the Optimization by Substitution Algorithm which is fully implemented in the processing package.

Optimization by Substitution Algorithm

A problem that arises when you are automatically generating code for a language you cannot evaluate is that the output code is usually slow and repetitive. Consider the following code example in Haskell:

foo :: Int -> Int
foo x = 2*x + 2*x + 2*x

Apparently (perhaps some cool GHC optimization you should no rely on is avoiding it) we are computing 2*x three times. To avoid it, the general technique is to create a let or where clause and include the repeated computation in it.

foo :: Int -> Int
foo x = let y = 2*x
        in  y + y + y

Now it seems clear that 2*x is computed only once. What we have done is to observe a repeated pattern/computation and gave it a name for its result. We then have substituted all the appearances of the repeated computation with that name. This is an intuitive way to avoid repeat computations, and this is exactly the idea behind the Optimization by Substitution Algorithm. I am pretty sure that this has been done before, but I give it here my own perspective, applied to my library.

In processing.js (JavaScript), we can apply a similar technique to that we applied to Haskell. Consider the following assignment.

v = 2*x + 2*x + 2*x ;

We can create an auxiliar variable, store the result of 2*x in this variable, and then use it to calculate the value of v.

y = 2*x ;
v = y + y + y ;

And this is how we adapt the above technique to JavaScript code.

Unlike Haskell, processing.js variables are mutable. The same variable x may have different values, depending on the last assignment done to that variable. Each time an assignment is done, the value of the variable may change, and we can't know if the new value is going to be the same or not (probably not). For example, consider the following code.

v = 2 ;
v = 2*v ;
v = 2*v + 2*v ;

At first sight, it seems that we are doing the same operation three times. However, the value of v has changed over time. If we replace all the appearances of 2*v we alter the result of the program.

v = 2 ;
y = 2*v ;
v = y ;
v = y + y ;

In the first program, the final value of v is 16. In the second program is 8. We cannot make substitutions freely in this situation. The solution I came out is to keep track of every mutated variable. When we reach an expression that uses one of the mutated variables, we apply substitutions over the code we have traversed so far, without including the expression that uses the mutated variable. Once we are done with the substitutions in that fragment of code, we repeat the process starting from the expression containing the mutated variable. This way, each piece of code where we are applying the Optimization by Substitution is safe.

Is this enough?

After my testings I am very happy with the results. However, in some browsers, like FireFox, it seems that my current test example is not running smoothly. I have made a Pac-Man game (a slightly simplification of the original) and looks like FireFox is not able to run the script smoothly. Either we need further optimizations or FireFox is just not good with processing.js. Perhaps the problem comes from processing.js, which may be not efficient enough. In any case, the game runs perfectly in Chrome/Chromium. I haven't tested with other browsers yet. Try it yourself here.

In the near future

The development of the Haskell processing library is not going to stop yet! More features are to come, and more bugs are to be found (and fixed!). Please, do not hesitate in try it yourself and report any annoyances in the issue tracker.

Thursday, August 8, 2013

Getting started with GHC hacking

After reading this blog post by Jason Dagit with the same title as the one you are reading now, I opened a new terminal session and cloned the GHC repo to my computer. Then, I successfully built GHC. Easy and faster than I thought. Online docs talk about hours of compiling process, when for me it only took some minutes (about 20-30 minutes). But, yeah, I provided the -j3 parameter to compile it in parallel. It works like a charm.

After having GHC built from the source for my first time, I decided to make a modified version of GHC. This version is aimed to really professional Haskell hackers who don't need any help from any source to know which commands are available in GHCi.

This is just my first step to get involved in GHC coding. :)

Monday, August 5, 2013

Processing: Key events

After being thinking and then hacking on processing yesterday, now key events are alive. After implement the feature, and add access to it from the interactiveFigure function, I wrote a code example to see it working. It was really helpful since it showed up some bugs I was not aware of. For example, variable numbers contained in conditionals were wrong. I have uploaded to Hackage a new version fixing this bug (and some other) and with the new feature of key events. Probably, we will find more bugs in the feature. In the meanwhile, please, update to the new version.

I post here the output of the key events demo (code here). Click over the canvas to make it work. Press W to move the black square up, A for left, S for down and D for right.

To interact with keys using the simple interface, use the interactiveFigure function. There is an argument of type [(Key, w -> w)] to handle key events. Each pair included in the list will specify a key, and the transformation over the current state that particular key does. As simple as that.

Saturday, August 3, 2013

From Haskell to Processing

I am happy about my last Haskell project. Its name is processing, and it is a library aimed to generate processing.js code. Actually, the purpose is to create interactive web animations, and processing.js is just the selected backend.

inCircles n =
  let t = intToFloat n * 0.03
      r = 100
  in  Circle (r * sin t, r * cos t) 20

I found the process of creating the library both funny and exciting. Phantom types, Template Haskell, Monads, ... I have used all the Haskell artillery to make the library nicer for the user, with some extra work from the developer (me). A lot of work still needs to be done. However, I am going to make it public right now, since it is already capable of working reasonably well.

Some days ago I published one of the output scripts in reddit to make sure that these animations are compatible with different browsers and settings. And it seems that it worked for everyone. Here is the animation I posted (try to move your mouse inside).

The reason I started working on this library is that I was looking for a library to write web animation scripts, without the need of running a server for them. Mainly, because I do not own a web server (I only have a web hosting, where I can upload these scripts). I looked through several choices, and processing.js suited my purpose very nicely.

Levels of abstraction

The library is built by levels of abstraction, and I have allowed the user to use any of the levels, hiding selected entities to make sure the user builds correct processing code. For example, you won't be able to use a variable before it is defined, create a variable twice, or calling a function where it does not make much sense. The compiler will stop you. The library levels are classified in the following form. Each one is built on top of the previous one.

  • Primal. This level is actually hidden in an internal module. It defines the abstract syntax tree of a processing script, how to print it, and other pretty basic stuff. However, it is one of the largest modules. It is the core of the library.

  • Basic. This level exports a writer monad which produces processing code. Imperative feeling. It is not really convenient, but it served me as starting point to work in the next level of abstraction. I exported it since it does not do any damage, and also because it has the property that the generated processing code is predictable.

  • Mid. Full-featured like the basic level, but more convenient. Still imperative feeling, but it makes sense given the nature of processing. It works using events, with things like

    on Draw $ do
    on MouseClicked do $

    The output processing code produced by this level is optimized using a common-subexpression recognition. It searches for commonly done operations, and create variables for them where they are only done once.

  • Simple. The most convenient level. Haskell-ish feeling. Create values of the Figure datatype (defined in the same module) and let the library decide how to write the processing code for you. It includes several options, like static image displaying, time-dependent animations or even interactive animations (very experimental yet). It is inspired by the gloss library, which I think is the perfect example of easy-to-use animation library.

Using the Simple interface I have created this recursive animation.

You can see the complete code here. More examples are to be added in the future.

Future plans

In the near future some of my planned additions are: arrays, conditional values, pre-made figures, images (the type is there but there is no function to create them) and more examples. I also have to do more testing and check if the library documentation is clear enough for a new user. Any contribution in this regard would be really appreciated. The library code lives in GitHub.

Thanks for reading,
Daniel Díaz.

Tuesday, June 25, 2013

HaTeX 3.6: Texy class, Babel, Fontenc, TikZ and more

It has been a long time since the last release of HaTeX. However, the development of the Haskell LaTeX library has not stopped in all this time. In fact, this release comes full of new features and bug fixes. Below a short description of the changes.

Bug fixes

Let's start having a look to the bugs that have been fixed in HaTeX 3.6:

  • The family of autoBraces functions has been fixed. It didn't create correct code (credits to leftaroundabout).
  • The verbatim function now expects a Text value instead of LaTeX code (credits to leftaroundabout).
  • Size modifiers functions are now protected adequately (reported by jvilar).
  • Fixed Color Render instance.

Some of these bugs may have been annoying some people for a long time now. My apologies to this people for the late release.

Texy class

A new class has been defined. Its name: Texy. The definition is as follows:

class Texy t where
  texy :: LaTeXC l => t -> l

This class provides a function to render different types to a LaTeX value. Basically, it defines a pretty-printer which output is LaTeX code. Useful to render vectors, matrices or trees. More details are given in the proposal of this feature.


Babel is a well-known LaTeX package useful to deal with documents written in languages other than US English. It is a basic feature but it has not been added until now. The module exports a Language type and some functions that use values of this type. See the Text.LaTeX.Packages.Babel module.


A short package to select the desired font encoding of your document. Pretty basic feature now available in HaTeX.


Doubtless, the most exciting new feature of this release. TikZ is a frontend for PGF (Portable Graphics Format), a package for creating graphics using scripts embedded in a LaTeX document. Using the TikZ package, writing some simple scripts gives you high quality results. The HaTeX TikZ module exports an interface to create this scripts and embed them in the rest of the HaTeX code, so you can program the graphics that appears in the LaTeX document from Haskell. Two layers of abstraction are provided. First, the module Text.LaTeX.Packages.TikZ exports an interface closer to the original TikZ. With this interface, you have to create paths and then use them to draw, fill, clip, etc. In addition, a PathBuilder monad is provided to create these paths. The alternative interface is much more attractive. Figures are created in a similar way to gloss. A simple, recursive and intuitive datatype describes how the figure looks like. Then, applying a certain function to the figure will generate the TikZ script, that you can insert in the HaTeX code right away. An example is worth a thousand words.

Definition of the figure.

myFigure :: Figure
myFigure = Scale 2 $ Figures
  [ RectangleFilled (0,0) 1 1
  , Colored Green $ RectangleFilled (-1,1) 1 1
  , Colored Red   $ RectangleFilled ( 0,2) 1 1
  , Colored Blue  $ RectangleFilled ( 1,1) 1 1

Output image.

Learn more about this in the Text.LaTeX.Packages.TikZ.Simple module.


I will probably be extending the documentation and testing different things. There are a lot of improvements to do in TikZ yet, but I thought that it is a good moment for a release. And, certainly, those suffering from the bugs described above will be thankful. Unfortunately, the HaTeX User's Guide keeps getting outdated.

Tuesday, May 28, 2013

My Linux Manual

Hi there!

This page is intended for personal use, but published publicly so anyone who find this information useful can use it. I am collecting data about different topics. This is for Debian with Gnome by the way.


OK, so you have a compressed file and you want to decompress it. Tarballs are usually compressed using te gz extension. Fine, use the following command:

$ tar zxvf foo.tar.gz

Why zxvf? Basically, z for the gzip compression; x to extract the files from the tarball; v makes the output more verbose; and f indicates that the next argument is the file. If the tarball is compressed using the bz2 extension (this is how GHC is usually shipped), replace z with j.

$ tar jxvf foo.tar.bz2

Removing launchers

If you added a launcher in your desktop bar, you may want to remove it later. To do so, instead of just right clicking (which only will allow you to launch it or modify it), press ALT + right click. This will open the options "Move" and "Remove From Panel". Press the second option and you are done.

Running things at start

Open the "Run application" window (with ALT+F2) and run gnome-session-properties. There you can manage your commands and applications that run at start.

How to kill a process that owns a particular port

Just run this:

sudo netstat -ap | grep :

Editing your network connections

To edit network connections with the network-manager, run the following command:

sudo nm-connection-editor

Once you have edited it, you can restart the network-manager with

/etc/init.d/network-manage restart

Wednesday, April 17, 2013

HaTeX 3.6: Proposal for Texy class


This is a proposal for a new feature I would like to see (and implement) in HaTeX for its next version. It consists in a new type class, with tentative name Texy (tex-like or tex-ify shortened). Other names are to be proposed, if any other better is found. The class would contain every type whose values can be pretty-printed in LaTeX form. Therefore, the definition of the class would be as follows:

class Texy t where
  texy :: LaTeXC l => t -> l

We can also make Texy a subclass of Render, so we can have a default implementation using rendertex.

class Render t => Texy t where
  texy :: LaTeXC l => t -> l
  -- Default implementation
  texy = rendertex

But I am not sure if this is suitable. The purpose of Texy is to build, from Haskell values, more complex LaTeX expressions than just rendering to Text, which, in the other hand, it is no more than a Show instance after all. It may work with numbers but not with more complex values like fractions or matrices.


A first application could be Rational pretty-printing as fractions using the frac function. However, we also have a problem here: the constructor % for rational numbers is already in use in our library. Perhaps we should rename the comment operator to %:? I have not seen an extensive use of this operator yet.

We would also be able to create LaTeX values for tuples, resizing the parenthesis in the tuple appropriately using autoParens.

Another application, perhaps more sofisticated, is pretty-printing matrices. Since the elements of the matrix would be a Texy instance as well, we can use texy to pretty-print to LaTeX matrices which may contain fractions or any other user-supplied value. This is an interface much more flexible than the current one (using a plain Render instance for the elements).

When rendering trees (see Text.LaTeX.Packages.Trees.Qtree), we are already using a LaTeXC l => (a -> l) parameter in order to pretty-print the elements of the tree. This is a more free version of the Texy class, which allow the user to supply different ways to generate LaTeX values from a single type. However, this is not a point against the current proposal, since this functionality can be kept as it is and added wherever is needed.

I honestly think this would be a good improvement.

Saturday, April 13, 2013

Haskell Platform from source in Linux

So I spent my day installing Haskell in my new Linux (Ubuntu-12.10) machine. Also, I have cooked a delicious cake. :)

In brief, these are the minimum packages I needed to install before starting with Haskell:

  • libgmp3c2. GHC needs this to perform arbitrary precision arithmetic. You may also need libgmp-dev.
  • zlib1g-dev. Needed for the zlib library in the Haskell Platform.
  • freeglut3-dev. Nedeed for the Open GL bindings in the Haskell Platform.

If the Haskell Platform configure still complains about the Open GL C library, did you try to install libgl1-mesa-dev?

Stop looking further! This is all you need to get the Platform installed from the source. Well, this, and the source by itself. Once you have this just follow the instructions in GHC and the Haskell Platform to finish your installation. Also note that linking of executables requires tons of memory. This is what killed my PC. If this happens to you, you should set swappiness to 10. That may solve the problem. If not, you can always try with a lighter version of Linux.

Good luck, haskellers!

Sunday, March 24, 2013

Benchmarks on matrix multiplication

I have been tuning for performance the matrix library, specially matrix multiplication. I have been using the well-known Strassen's algorithm to achieve sub-cubic complexity, but extending with zeroes the matrix to the next power of two order to match the hypothesis of the algorithm. Obviously, this method didn't work very well, but I took it as a starting point. From this point, my idea was to mix the standard multiplication with Strassen's idea, and to not expand the matrix unnecessarily.

After some work, I ended up extending the matrix to a square matrix of the next even order. Then apply one iteration of Strassen's algorithm. This process is iterated until certain order. Smaller matrices to this fixed order are multiplied using the definition. I have tried different switcher orders and benchmarking to see what's the best choice. These are benchmarks using switcher order k = 150. To understand it, note that multN means multiplication of square matrices of order N and that Definition and Strassen mixed mean that the multiplication has been done by definition or using my mixed algorithm respectively.

This benchmark table is hosted here.

Both take a very similar time for small entries, but, as the matrix grows, the difference gets bigger and bigger in favor of the mixed algorithm. This is a nice result that will be applied in the next release of matrix (0.2).

Tuesday, March 19, 2013

HaTeX 3.5

It's time for a new release of HaTeX!

The following changes have been made since the last release:

  • Fixed some minor bugs.
  • Remake of the parser, now using attoparsec instead of parsec. Also more correct and tested. Thanks to Tobias Schoofs for his contributions.
  • Extension of the AMSMath module. Thanks to leftaroundabout for his contributions.
  • Applicative instance for LaTeXT.
  • New functions for rendering matrices. This includes a new dependency in the matrix package.
  • And some other minor changes.

A complete list of changes can be found at the commit history. The new version is up in Hackage so update yours!

Sunday, March 17, 2013

Writing a new library: Wavy

I have two proposals for this Spring Break.
  • Release the version 3.5 of HaTeX.
  • End up the first layer of my new library: Wavy.

HaTeX 3.5

HaTeX has been in stand-by for a while now and I want to upload a version of HaTeX that at least contains the changes I have made so far. Hopefully, I will add some new features. I am thinking about a matrix writer, since is a pain to write matrices right now. If you take a look to the last changes on github, better support for math typesetting has been added thanks to leftaroundabout. He is (or has been) also writing an extension for HaTeX to make math typesetting more sophisticated. Also, toschoo has improved the parser and now it uses attoparsec. These are good news for HaTeX.


In the other hand, I am writing a new library. Its name is Wavy. It is a super kool library that read, writes and manipulates sounds very nicely. Yes, I know, lot of libraries have been written doing exactly the same. Exactly the same? Well, I think they are all different in some sense, and this is just another new approach. If it is going to be a better or worse approach is something I don't care, as long as is useful to somebody. And I already find it useful for myself! Although, of course, it would be great if somebody else find it interesting, so I will do my best to make of Wavy a nice library. To begin with, I started writing a user manual. I think is looking pretty good, but that is something that Haskell users should decide in my place. As an extension, I wrote a library that writes sound waves in PDFs. So yes, I am having a lot of fun! And it's everything written in Haskell!

I hope to have something more mature to show after the Spring Break, but I think is moment to start sharing this thoughts with the community. Below is a code example that shows Wavy in action writing a sine wave in a .wav file.

import Data.Sound
import Data.Sound.WAVE

main :: IO ()
main = encodeFile "sine.wav" $ fromSound 16 s
  s = sine 5 1 100 0