Great cantata from Carl Orff, I think everybody knows that one – Carmina Burana.

## Carl Orff – O Fortuna Imperatrix Mundi

12 04 2009Comments : 1 Comment »

Tags: English, inspiration, music, Português, video

Categories : Uncategorized

## Easter

11 04 2009Comments : Leave a Comment »

Tags: comics, Easter, English, humour, Português

Categories : Uncategorized

## Garrett Lisi: A beautiful new theory of everything

10 04 2009Antony Garrett Lisi is a theoretical physicist best known for “An Exceptionally Simple Theory of Everything”. This TED talk is about that theory. Garrett explains how he find this theory. Garrett Lisi has proposed this new “theory of everything” a grand unified theory that explains all the elementary particles, as well as gravity.

Comments : 4 Comments »

Tags: English, inspiration, physics, TEDtalks, theory, video

Categories : Uncategorized

## More Hylomorphisms in Haskell

9 04 2009If you lost yourself in this post, I advise you to start in catamorphisms, then anamorphisms and then hylomorphisms.

Like I said before (in those posts) when you write an hylomorphism over a particular data type, that means just that the intermediate structure is that data type.

In fact that data will never be stored into that intermediate type or . Because we glue the ana and cata together into a single recursive pattern. and could be some data type your function need. With this post I will try to show you more hylomorphisms over some different data types to show you the power of this field.

## Leaf Tree’s

The data type that we going to discuss here is the . In Haskell we can represent as:

data LTree a = Leaf a | Fork (LTree a, LTree a)

Is just like a binary tree, but the information is just in the leaf’s. Even more: a leaf tree is a tree that only have leaf’s, no information on the nodes. This is an example of a leaf tree:

To represent all the hylomorphisms over we draw the following diagram:

The example I’m going to give is making the fibonacci function using a hylomorphism over this data type. If you remember the method I used before, I’m going to start by the anamorphism . Before that I’m going to specify the strategy to define factorial. I’m going to use the diagram’s again, remember that type is equivalent to Haskell :

As you can see I’m going to use as my intermediate structure, and I’ve already define the names of my gen functions to the catamorphism and to the anamorphism. The strategy I prefer, is do all the hard work in the anamorphism, so here the gen for the anamorphism is:

fibd n | n < 2 = i1 () | otherwise = i2 (n-1,n-2)

This function combined with the anamorphism, going to generate leaf tree’s with leaf’s, being the result of that fib.

Then we just have to write the gen for the catamorphism. This function (combined with the catamorphism) counts the number of leafs that a leaf tree have.

add = either (const 1) plus where plus = uncurry (+)

The final function, the fibonacci function is the hylomorphism of those two defined before:

fib = hyloLTree add fibd

Here is all the auxiliary functions you need to run this example:

inLTree = either Leaf Fork outLTree :: LTree a -> Either a (LTree a,LTree a) outLTree (Leaf a) = i1 a outLTree (Fork (t1,t2)) = i2 (t1,t2) cataLTree a = a . (recLTree (cataLTree a)) . outLTree anaLTree f = inLTree . (recLTree (anaLTree f) ) . f hyloLTree a c = cataLTree a . anaLTree c baseLTree g f = g -|- (f >< f) recLTree f = baseLTree id f

## Lists

The lists that I’m going to talk here, are the Haskell lists, wired into the compiler, but is a definition exist, it will be:

data [a] = [ ] | a : [a]

So, our diagram to represent the hylomorphism over this data type is:

The function I’m going to define as a hylomorphism is the factorial function. So, we know that our domain and co-domain is , so now we can make a more specific diagram to represent our solution:

As you can see I’m going to use to represent my intermediate data, and I’ve already define the names of my gen functions to the catamorphism and to the anamorphism. Another time, that I do all the work with the anamorphism, letting the catamorphism with little things to do (just multiply). I’m start to show you the catamorphism first:

mul = either (const 1) mul' where mul' = uncurry (*)

As you can see the only thing it does is multiply all the elements of a list, and multiply by 1 when reach the empty list.

In the other side, the anamorphism is generating a list of all the elements, starting in (the element we want to calculate the factorial) until 1.

nats = (id -|- (split succ id)) . outNat

And finally we combine this together with our hylo, that defines the factorial function:

fac = hylo mul nats

Here is all the code you need to run this example:

inl = either (const []) (uncurry (:)) out [] = i1 () out (a:x) = i2(a,x) cata g = g . rec (cata g) . out ana h = inl . (rec (ana h) ) . h hylo g h = cata g . ana h rec f = id -|- id >< f

## Binary Tree’s

Here, I’m going to show you the hanoi problem solved with one hylomorphism, first let’s take a look at the structure:

data BTree a = Empty | Node(a, (BTree a, BTree a))

So, our generic diagram representing one hylomorphism over is:

There is a well-known inductive solution to the problem given by the pseudocode below. In this solution we make use of the fact that the given problem is symmetrical with respect to all three poles. Thus it is undesirable to name the individual poles. Instead we visualize the poles as being arranged in a circle; the problem is to move the tower of disks from one pole to the next pole in a speciﬁed direction around the circle. The code deﬁnes to be a sequence of pairs where n is the number of disks, is a disk number and are directions. Disks are numbered from onwards, disk being the smallest. Directions are boolean values, representing a clockwise movement and an anti-clockwise movement. The pair means move the disk numbered from its current position in the direction .

excerpt from R. Backhouse, M. Fokkinga / Information Processing Letters 77 (2001) 71–76

So, here, I will have a diagram like that, type stands for and type for :

I’m going to show all the solution here, because the description of the problem is in this quote, and in the paper:

hanoi = hyloBTree f h f = either (const []) join where join(x,(l,r))=l++[x]++r h(d,0) = Left () h(d,n+1) = Right ((n,d),((not d,n),(not d,n)))

And here it is, all the code you need to run this example:

inBTree :: Either () (b,(BTree b,BTree b)) -> BTree b inBTree = either (const Empty) Node outBTree :: BTree a -> Either () (a,(BTree a,BTree a)) outBTree Empty = Left () outBTree (Node (a,(t1,t2))) = Right(a,(t1,t2)) baseBTree f g = id -|- (f >< g)) cataBTree g = g . (recBTree (cataBTree g)) . outBTree anaBTree g = inBTree . (recBTree (anaBTree g) ) . g hyloBTree h g = cataBTree h . anaBTree g recBTree f = baseBTree id f

## Outroduction

Maybe in the future I will talk more about that subject.

Comments : 3 Comments »

Tags: anamorphism, catamorphism, code, English, functions, hacking, haskell, hylomorphism, mathematics, mp1, pointfree, programming, recursion, types

Categories : Uncategorized

## Hylomorphisms in Haskell

9 04 2009If you miss something in this post, I suggest you to start in Catamorphisms and Anamorphisms.

A Hylomorphism is just the composition of one catamorphism and then one anamorphism.

, replacing that by the proper notation we have:

In this post I will use the structure of a binary tree:

data BTree a = Empty | Node(a, (BTree a, BTree a))

I will use the tuples to don’t have to write uncurry’s. As I will show you, when we say that we are making a hylomorphism on a particular data type , what we are trying to say is that the intermediate structure of our combination of catamorphism and anamorphism is that data type . This is the structure throw our morphism will communicate with each other.

## Anamorphism

So, here I will solve the Quicksort algorithm with a hylomorphism over .

The intermediate structure being a doesn’t mean that my function will receive . My function works over lists. So the first thing to do, is draw the respective anamorphism from to :

My strategy here is to do all the work in the anamorphism, so, I need a function with type:

, or in Haskell

That function is :

qsep :: [a] -> Either () (a, ([a], [a])) qsep [] = Left () qsep (h:t) = Right (h,(s,l)) where (s,l) = part (<h) t part:: (a -> Bool) -> [a] -> ([a], [a]) part p [] = ([],[]) part p (h:t) | p h = let (s,l) = part p t in (h:s,l) | otherwise = let (s,l) = part p t in (s,h:l)

This code is very simple, in I chose a pivotal element (first one), and filter the bigger to one side, and the other ones to the other, just like the algorithm. The function that do all that job is , it process all the list finding the elements that satisfy the condition , to put them in the left side of the tuple, and the others into the right side.

This function by it self don’t do almost anything, it is only a simple part of the algorithm.

## Catamorphism

Next step is to see the diagram for catamorphisms from to :

As I said before, the heavy duty is on the side of the anamorphism, so here, the catamorphism will be very very simple. In fact it is.

inord :: Either a (a, ([a], [a])) -> [a] inord = either (const []) join where join(x,(l,r))=l++[x]++r

That right! The only thing that the catamorphism do is a inorder passage over the structures , which is very simple, as as shown by the code.

## Hylomorphism

The first thing is to draw the diagram, now for the hylomorphism, the composition of the cata with the ana:

Once having made the two most important parts of the function (the ana and cata), the hylo is very simple to do. You just have to make a function :

hyloBTree h g = cataBTree h . anaBTree g

And our function bacame:

qSort :: Ord a => [a] -> [a] qSort = hyloBTree inord qsep

And that’s it, now I’m going to show you the all code that you need to put all the things together and working.

inBTree :: Either () (b,(BTree b,BTree b)) -> BTree b inBTree = either (const Empty) Node outBTree :: BTree a -> Either () (a,(BTree a,BTree a)) outBTree Empty = Left () outBTree (Node (a,(t1,t2))) = Right(a,(t1,t2)) baseBTree f g = id -|- (f >< g)) cataBTree g = g . (recBTree (cataBTree g)) . outBTree anaBTree g = inBTree . (recBTree (anaBTree g) ) . g hyloBTree h g = cataBTree h . anaBTree g recBTree f = baseBTree id f

## Outroduction

If you need more explanations feel free to contact me.

Comments : 2 Comments »

Tags: anamorphism, catamorphism, code, English, functions, haskell, hylomorphism, mathematics, pointfree, programming, recursion, Theoretical fundations, theory

Categories : Uncategorized

## The Umbilical Brothers

9 04 2009The umbilical brothers is a group of humor that combine mime with ordinary dialog and vocal sound effects.

Comments : Leave a Comment »

Tags: English, humour, video

Categories : Uncategorized

## Anamorphisms in Haskell

8 04 2009First I would like to introduce the notation that I use here. The pointfree notation is good to see a program (functions) data flow and as composition of functions, combination of functions, if you prefer. This style is characterized by not using variables in declaration of functions. Haskell allow us to implement that notation natively. The dual of the pointfree notation is the pointwise one.

A simple example of a function in pointwise style:

f n = (n+2)*10 -- pointwise

The dual in pointfree would be:

f = (*10) . (+2) -- pointfree

## Clarifications

First of all to define a function, for example , i can say:

, or .

I will assume that you are familiarized with infix notation, , and composition functions.

## Types

For this post I need to explain the data type we will going to use. In Haskell we define it by:

data Tree a = Node a [Tree a]

Let’s create the same, but more convenient. Consider the following isomorphic type for :

data Tree a = Node (a, [Tree a])

We could see as a the following function:

Node :: (a, [Tree a]) -> Tree a

So typologically we have . We use to define that two things occurs in parallel, like tuples do, so we can redefine it:

Now we can say that is isomorphic to .

This is something to say that and keep the same information without any change. We represent that formally as: .

## Anamorphisms

Let , , , be Inductive data types (sets) and , , functions.

is the anamorphism of if the diagram commute.

We use the notation to say that function in not generic, but only works for data . The same happens with and . We will write using the composition of , and functions. That way we are breaking our problem in small ones. So, in the end we will have the following definition for :

The function that we want is , and that function is over so we have:

ana :: (A -> B) -> A -> Tree c

Type is . Maybe this isn’t clear yet, let’s start with function

### function in

The function is responsible to create the isomorphism between and , so the code could be something like this:

inTree :: Tree a -> (a, [Tree a]) inTree = Node

In Haskell we represent the type as . So, type is . So by now, we already know the following unifications and . So now our graphic is:

### function

The function is also known as *gen*, here is where we said the step that pattern do. This is the only function we need to take care, if this function is good, our problem is solved. Now image that our problem is:

Suppose that the pair of positive integers (v, p) denotes the number of red balls (v) and black (p) that is inside a bag, the balls which are taking randomly, successively, until the bag is empty.

This is the point-wise version of the function we want to convert to pointfree using anamorphisms. This function represent as a tree, all possible states of the bag over these experiences.

state :: (Int,Int) -> Tree (Int,Int) state(0,0) = Node (0,0) [] state(v,0) = Node (v,0) [state(v-1,0)] state(0,p) = Node (0,p) [state(0,p-1)] state(v,p) = Node (v,p) [state(v-1,p),state(v,p-1)]

If we want that “latex state$ became an anamorphism, we have to say that our type unify () with , and became more restrict, and unify with . A consequence of changing the co-domain of is changing the domain of it to . We represent as . Now we can be more specific with our graphic:

### function rec

Here we have to get a function that co-domain is . Probably the best is to pass the first part of the tuple (part with type ) and the rest (part with type ) is just a of the function . So, now our graphic is:

As you can see, the second part of the co-domain of is the type of function :

So our **final** graphic became:

Now, we just have to define the function and apply them to our anamorphism of .

h :: (Int, Int) -> ( (Int, Int), [ (Int, Int) ] ) h(0,0) = ( (0,0), [] ) h(v,0) = ( (v,0), [ (v-1,0) ] ) h(0,p) = ( (0,p) [ (0,p-1) ] ) h(v,p) = ( (v,p), [ (v-1,p), (v,p-1) ] )

And this is it! Now we can say that:

where

## Outroduction

Here is all the code you need to run this example in Haskell:

module AnamorphismExample where infix 5 >< i1 = Left i2 = Right p1 = fst p2 = snd data Tree a = Node (a, [Tree a]) deriving Show split :: (a -> b) -> (a -> c) -> a -> (b,c) split f g x = (f x, g x) (><) :: (a -> b) -> (c -> d) -> (a,c) -> (b,d) f >< g = split (f . p1) (g . p2) inTree :: (a, [Tree a]) -> Tree a inTree = Node anaTree h = inTree . (id >< map (anaTree h)) . h -- our function h_gen :: (Int, Int) -> ( (Int, Int), [ (Int, Int) ] ) h_gen(0,0) = ( (0,0), [] ) h_gen(v,0) = ( (v,0), [ (v-1,0) ] ) h_gen(0,p) = ( (0,p) , [ (0,p-1) ] ) h_gen(v,p) = ( (v,p), [ (v-1,p), (v,p-1) ] ) state = anaTree h_gen

Pass a year since I promised this post. The next will be on hylomorphisms I promise not take too that much.

Comments : 6 Comments »

Tags: anamorphism, code, English, functions, haskell, mathematics, mp1, pointfree, programming, recursion, Theoretical fundations

Categories : Uncategorized