## LaTeX listings package – Haskell

30 03 2008

As may know, I am a $LaTeX$-fan-boy! To me $LaTeX$, is just the perfect way to get documents well structured and with a professional layout. More, with $LaTeX$ you can write to someone in the other side of the planet mathematic formulas, or whatever because is a standard. It’s also fast, stable, flexible, free, etc.

Well, from the very beginning of my course I have to be able to write documents in $LaTeX$, so after I learn it, I start to do my $LaTeX$ template document. To import code to my document I start to use verbatim package, but it really suck’s!
So, I found listings, great output! And even better, I could make my own style to one language. After reading the manual I did it for Haskell. I love the output.
You can see an example of that here(.pdf).

A simple definition of some colors to highlight our code:

\definecolor{gray_ulisses}{gray}{0.55}
\definecolor{castanho_ulisses}{rgb}{0.71,0.33,0.14}
\definecolor{preto_ulisses}{rgb}{0.41,0.20,0.04}
\definecolor{green_ulises}{rgb}{0.2,0.75,0}


And here it is, the thing that do things 🙂

\lstdefinelanguage{HaskellUlisses} {
basicstyle=\ttfamily\tiny,
sensitive=true,
morecomment=[l][\color{gray_ulisses}\ttfamily\tiny]{--},
morecomment=[s][\color{gray_ulisses}\ttfamily\tiny]{\{-}{-\}},
morestring=[b]",
stringstyle=\color{red},
showstringspaces=false,
numberstyle=\tiny,
numberblanklines=true,
showspaces=false,
breaklines=true,
showtabs=false,
emph=
{[1]
FilePath,IOError,abs,acos,acosh,all,and,any,appendFile,approxRational,asTypeOf,asin,
asinh,atan,atan2,atanh,basicIORun,break,catch,ceiling,chr,compare,concat,concatMap,
const,cos,cosh,curry,cycle,decodeFloat,denominator,digitToInt,div,divMod,drop,
dropWhile,either,elem,encodeFloat,enumFrom,enumFromThen,enumFromThenTo,enumFromTo,
interact,ioError,isAlpha,isAlphaNum,isAscii,isControl,isDenormalized,isDigit,isHexDigit,
isIEEE,isInfinite,isLower,isNaN,isNegativeZero,isOctDigit,isPrint,isSpace,isUpper,iterate,
last,lcm,length,lex,lexDigits,lexLitChar,lines,log,logBase,lookup,map,mapM,mapM_,max,
maxBound,maximum,maybe,min,minBound,minimum,mod,negate,not,notElem,null,numerator,odd,
or,ord,otherwise,pi,pred,primExitWith,print,product,properFraction,putChar,putStr,putStrLn,quot,
reverse,round,scaleFloat,scanl,scanl1,scanr,scanr1,seq,sequence,sequence_,show,showChar,showInt,
showList,showLitChar,showParen,showSigned,showString,shows,showsPrec,significand,signum,sin,
toInt,toInteger,toLower,toRational,toUpper,truncate,uncurry,undefined,unlines,until,unwords,unzip,
unzip3,userError,words,writeFile,zip,zip3,zipWith,zipWith3,listArray,doParse
},
emphstyle={[1]\color{blue}},
emph=
{[2]
Word8,InPacket
},
emphstyle={[2]\color{castanho_ulisses}},
emph=
{[3]
case,class,data,deriving,do,else,if,import,in,infixl,infixr,instance,let,
module,of,primitive,then,type,where
},
emphstyle={[3]\color{preto_ulisses}\textbf},
emph=
{[4]
quot,rem,div,mod,elem,notElem,seq
},
emphstyle={[4]\color{castanho_ulisses}\textbf},
emph=
{[5]
EQ,False,GT,Just,LT,Left,Nothing,Right,True,Show,Eq,Ord,Num
},
emphstyle={[5]\color{preto_ulisses}\textbf}
}


And now, to use it, we define a new environment:

\lstnewenvironment{code}
{\hrulesmallskip}

Now to write Haskell code in our $LaTeX$ document with great output, just write:

\begin{code}
module Main(main) where

main :: IO()
main = putStrLn \$ "LaTeX listings is cool!"
\end{code}

Because I’m not so good at web design I use this blog layout (that I like), but some of the code may not be seen, if you want to see an example of a $LaTeX$ source go here (.lhs).

12 03 2008

## Off-topic

In the past two months I was in exams for my Lic. but now I have the time to continue with my post’s.

Back to my blog I find out that anyone can copy (CTRL+C) the images that are here and what in fact you are copying is the LaTeX code that I write to produce the images. That’s great! Now you can see my Xy-pic skills (lolz)…

## Intro

In the last post of this series I have talk about Products of functions, in this post we will see more about point-free calculation using co-product of functions.

Because of the length of what I intend to explain I talk about these theme in three more post’s. Consider this part II.

• Products (Part I)
• Constants and conditionals (Part III)
• Pointfree – Pointwise conversion & Program calculation (Part IV)

At the time I finish the other posts I will put their link here.

## Part II

In this post I will talk about co-products of functions and some of their rules.

I will use $D_{\huge{E}}F$ that means ‘def’ from ‘definition’. I can’t use ‘def’ in this LaTeX package. And I confess that didn’t investigate a lot about that, in matter of fact I like the output of $D_{\huge{E}}F$.

I will also use Commutative diagrams to explain the rules, Because I think that is the easy way to get it.

## Combinators

I will use $\noindent (\odot)$ combinator defined in Part I.

### either combinator

Like we sow for the structs (split combinator, Part I), we have also a combinator for unions (eithers).

The $(\nabla)$ is often called ’either’. This is the combinator responsible to take care of union’s (eithers in Haskell).

$\noindent \{D_{\huge{E}}F-\nabla\}:$
$(\nabla)~:~(a~\rightarrow~c)~\rightarrow~(b~\rightarrow~c)~\rightarrow~(a~+~b~\rightarrow~c)$
$(f~\nabla~g)~(Left~x)~=~f~x$
$(f~\nabla~g)~(Right~y)~=~g~y$

either :: (a -> c) -> (b -> c) -> Either a b -> c
either f g (Left x) = f x
either f g (Right y) = g y


As we will see with this combinator we can combine functions with the same counterdomain.

‘Left‘ and ‘Right‘ are constructors of data type Either (in Haskell) and we can see them as a functions with the folowing type:

$\noindent \{type-D_{\huge{E}}F-Left\}:$
$Left~:a~\rightarrow~a~+~b$

$\noindent \{type-D_{\huge{E}}F-Right\}:$
$Right~:b~\rightarrow~a~+~b$

As you can see with both functions we can inject information in our functions, in the oposite side we have ´fst´ and ´snd´ functions that we use to project information (Part I).

Let A, B, C, D be Types and f : A -> C, g : B -> C and we want the function either : A + B -> C



Like diagram is commutative we can infer two rules:

$\noindent \{univ-+\}:$
$either = f \nabla g \Leftrightarrow either \odot Left = f \wedge either \odot Right = g$

$\noindent \{cancel-+\}:$
$(f \nabla g) \odot Left = f$
$(f \nabla g) \odot Right = g$

Now, if type C = A + B, we will have $f \nabla g : A + B \rightarrow A + B$

To have a commutative diagram we must have $f = Left$ and $g = Right$



And we can infer the rule:

$\noindent \{reflex-+\}:$
$Left \nabla Right = id_{A + B}$

Let A, B, C, D be Types and g : A -> C, h : B -> C and f : C -> D.



$\noindent \{fusion-+\}:$
$f \odot (g \nabla h) = f \odot g \nabla f \odot h$

### $\bigoplus$ combinator

When we want to defin a function that takes a co-product and goes to a co-product we may use the function (\bigoplus)

$\noindent \{D_{\huge{E}}F-\bigoplus\}:$
$\noindent f \bigoplus g~=~Left \odot f \nabla Right \odot g$

infixr 4 -|-

(-|-) :: (a -> b) -> (c -> d) -> Either a c -> Either b d
f -|- g = either (Left . f) (Right . g)


here is the diagram of it definition:





$\noindent \{abssoc-+\}:$
$(f \nabla g) \odot (h \bigoplus i) = f \odot h \nabla g \odot i$



$\noindent \{functor-+\}:$
$(f \bigoplus g) \odot (h \bigoplus i) = f \odot h \bigoplus g \odot i$

We also have the equalty of eithers;

$\noindent \{eq-\bigoplus\}:$
$(f \nabla g) = (h \nabla k) \Leftrightarrow f = h \wedge g = k$

And, to conclude, we have a rule that relationate ‘eithers’ with ‘splits’,

$\noindent \{change-law\}:$
$(f \Delta g) \nabla (h \Delta k) = (f \nabla h) \Delta (g \nabla k)$

### examples

To show you some of the power of program calculation here is the prove of $\noindent \{change-law\}:$

(please open the image to see it in full size)

$\noindent(f \Delta g) \nabla (h \Delta k) = (f \nabla h) \Delta (g \nabla k)\\\Leftrightarrow \{nat-id, reflex-\bigoplus\}\\(f \Delta g) \nabla (h \Delta k) = (f \nabla h)\Delta (g \nabla k) \odot (Left \nabla Right)\\\Leftrightarrow \{fusion-\bigoplus\}\\(f \Delta g) \nabla (h \Delta k) = ((f \nabla h) \Delta (g \nabla k)) \odot Left \nabla ((f \nabla h) \Delta (g \nabla k)) \odot Right\\\Leftrightarrow \{eq-\bigoplus\}\\f \Delta g = (f \nabla h) \Delta (g \nabla k) \odot Left\\\wedge\\h \Delta k = (f \nabla h) \Delta (g \nabla k) \odot Right\\\Leftrightarrow \{fusion-\bigotimes\}\\f \Delta g = ((f \nabla h) \odot Left ) \Delta ((g \nabla k) \odot Left)\\\wedge\\h \Delta k = ((f \nabla h) \odot Right) \Delta ((g \nabla k) \odot Right)\\\Leftrightarrow \{cancel-\bigoplus\}\\f \Delta g = f \Delta g\\\wedge\\h \Delta k = h \Delta k\\\Leftrightarrow {TRUE}\\TRUE$

## Notes

The $\odot$ combinator have more precedence than the $\nabla$ combinator.
So, $((f \Delta h) \nabla (g \Delta k)) \odot Left = (f \Delta h) \nabla (g \Delta k) \odot Left$

## Aknowledgements

I would like to thank dysfunctor to the corrections made to this post.