Thursday, October 6, 2011

HaTeX 3 - First release

Wow! I finally do the first release to Hackage of HaTeX 3:

http://hackage.haskell.org/package/HaTeX-3.0.0

You can try it with the next example!


{-# LANGUAGE OverloadedStrings #-}

import Text.LaTeX.Base.Monad

main :: IO ()
main = do
 l <- execLaTeXT example
 renderFile "Example.tex" l


example :: Monad m => LaTeXT_ m
example = do
 documentclass [] article

 document exampleBody


exampleBody :: Monad m => LaTeXT_ m
exampleBody = do
 "This is an example of how "
 hatex3
 " works, printing a table of "
 "the thirteen first elements of the "
 "Fibonacci sequence."
 bigskip
 center $ underline $ textbf "Fibonacci table"
 center $ tabular Nothing [RightColumn,VerticalLine,LeftColumn] $ do
   textbf "Fibonacci number" & textbf "Value"

   lnbk
   hline
   foldr (\n l -> do fromString (show n) & fromString (show $ fib n)
                     lnbk
                     l ) (return ()) [0..12]


fibs :: [Int]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)


fib :: Int -> Int
fib = (fibs!!)

This example builds a table with the thirteen first elements of the Fibonacci sequence.

I'm writing a manual for the library, but it will take me some time (and time is sometimes hard to find).

Until the next time!
Daniel Díaz.

Saturday, October 1, 2011

HaTeX 3 - Two styles

Since the last post about this topic (previous notes), I worked on HaTeX 3 and it is almost ended. This post will response a question about the "Applicative vs Monadic" section of the last post.

Example over HaTeX 2

Let be the next simple example (it works with HaTeX 2):

{-# LANGUAGE OverloadedStrings #-}

example :: Monad m => LaTeX m
example = do
  documentclass [] article
  author "Daniel Díaz"
  title "Example"
  document $ do maketitle
                "Hello, world!"

Here, expressions like documentclass [] article and title "Example" have type Monad m => LaTeX m, where LaTeX is a monad transformer. Thus, they are actions over a monad and we can put all of them in a do sequence. Each action, appends in the state of the LaTeX monad a piece of LaTeX code. For instance, for title "Example" it appends the code \title{Example}.

Working with HaTeX 3: First style

As I mentioned in the last post, there is a new datatype LaTeX describing the LaTeX syntax. LaTeX combinators (like author an title) still exist, but now you can use them in two different ways. What I meant with applicative style is to use these combinators without the presence of any monad. You create an expression of type LaTeX, and you combine them with the monoid operator (<>), alias of the mappend method (See the Data.Monoid module).

Let's write the example this way:

{-# LANGUAGE OverloadedStrings #-}

example :: LaTeX
example =
    documentclass [] article
 <> author "Daniel Díaz"
 <> title "Example"
 <> document (maketitle
           <> "Hello, world!")

Yes, all these operators seem ugly. Even I was forced to write an extra parentheses. But I like you can do all the work without monads.

Working with HaTeX 3: Second style

Now, we are interested in take back the do notation. But we don't want to do every work twice. Indeed, if we did this work manually, we would have to define all entities (including commenting, type signature and exposing in the export list) twice! That's really bad!

The solution was HaTeX-meta. HaTeX-meta is a program that reads a subset of the HaTeX modules (those that define LaTeX combinators) and generates automatically an analogous module that export the combinators with monadic types. And preserving their documentation! These modules have the same module name, but followed by .Monad, and import their originals no-monadic functions to build the new monadic ones.

So, now you can write LaTeX code with HaTeX using do notation with the new type-system. The example above stay almost equal if you import the monadic modules of HaTeX 3:

{-# LANGUAGE OverloadedStrings #-}

example :: Monad m => LaTeXT_ m
example = do
  documentclass [] article
  author "Daniel Díaz"
  title "Example"
  document $ do maketitle
                "Hello, world!"



Closure

I hope this was understandable enough. For now, this is all about HaTeX 3. Next posts will be about HaTeX-meta and HaTeX 3 release notes. The official release will be soon, but the code will be available before.

Thanks for read,
Daniel Díaz.