The TravelTree Monad is a monad proposed and designed by Paolo Martini (xerox), and coded by David House (davidhouse). It is based on the State monad and is used for navigating around data structures, using the concept of [[Zipper]].

+

The Travel Monad is a generic monad for navigating around arbitrary data structures. It supports movement, mutation and classification of nodes (is this node the top node or a child node?, etc). It was proposed and designed by Paolo Martini (xerox), and coded by David House (davidhouse). It's designed for use with [[Zipper|The Zipper]] but in fact there is no requirement to use such an idiom.

−

As the only zipper currently available is for binary trees, this is what most of the article will be centred around.

+

At the moment there are two specific libraries that use the Travel monad: [[Zipper_monad/TravelTree|TravelTree]] for navigating around binary trees, and [[Zipper_monad/TravelBTree|TravelBTree]] for navigating around "B-Trees", trees where each node has an arbitrary number of branches.

== Definition ==

== Definition ==

<haskell>

<haskell>

−

newtype Travel t a = Travel { unT :: State t a }

+

data Loc c a = Loc { struct :: a,

−

deriving (Functor, Monad, MonadState t)

+

cxt :: c }

−

type TravelTree a = Travel (Loc a) (Tree a) -- for trees

+

deriving (Show, Eq)

−

</haskell>

+

−

+

−

Computations in <hask>TravelTree</hask> are stateful. <hask>Loc a</hask> and <hask>Tree a</hask> are defined as follows:

+

−

+

−

<haskell>

+

−

data Tree a = Leaf a | Branch (Tree a) (Tree a)

+

−

+

−

data Cxt a = Top

+

−

| L (Cxt a) (Tree a)

+

−

| R (Tree a) (Cxt a)

+

−

deriving (Show)

+

−

type Loc a = (Tree a, Cxt a)

+

newtype Travel loc a = Travel { unT :: State loc a }

+

deriving (Functor, Monad, MonadState loc, Eq)

</haskell>

</haskell>

−

See [[Zipper]] for an explanation of the <hask>Cxt</hask> and <hask>Loc</hask> concepts.

+

Computations in <hask>Travel</hask> are stateful. <hask>Loc c a</hask> is a type for storing the location within a structure. <hask>struct</hask> should be the substructure that the <hask>Loc</hask> is refering to, and <hask>cxt</hask> the "context" of the substructure; i.e. the rest of the structure. <hask>Loc</hask> is designed to hold a [[Zipper]] (although it doesn't have to; for example if you wanted to traverse a list it would probably be more natural to hold the entire structure and an index). Indeed, both of the libraries provided with the generic <hask>Travel</hask> monad use a zipper.

== Functions ==

== Functions ==

−

=== Moving around ===

−

There are four main functions for stringing together <hask>TravelTree</hask> computations:

−

<haskell>

+

=== Movement ===

−

left, -- moves down a level, through the left branch

+

At the moment, movement is specific to the structure you are traversing and as such, the movement functions are provided by libraries implementing specific structures. Try the documentation for [[Zipper_monad/TravelTree|TravelTree]] (binary trees) or [[Zipper_monad/TravelBTree|TravelBTree]] (B-Trees; trees where each node has an arbitrary number of branches).

−

right, -- moves down a level, through the right branch

+

−

up, -- moves to the node's parent

+

−

top -- moves to the top node

+

−

:: TravelTree a

+

−

</haskell>

+

−

+

−

All four return the subtree at the new location.

+

=== Mutation ===

=== Mutation ===

−

There are also functions available for changing the tree:

+

There are three generic functions available for changing the structure:

<haskell>

<haskell>

−

getTree :: TravelTree a

+

getStruct :: Travel (Loc c a) a

−

putTree :: Tree a -> TravelTree a

+

putStruct :: a -> Travel (Loc c a) a

−

modifyTree :: (Tree a -> Tree a) -> TravelTree a

+

modifyStruct :: (a -> a) -> Travel (Loc c a) a

</haskell>

</haskell>

−

These are direct front-doors for State's <hask>get</hask>, <hask>put</hask> and <hask>modify</hask>, and all three return the subtree after any applicable modifications.

+

These are direct front-doors for State's <hask>get</hask>, <hask>put</hask> and <hask>modify</hask>, and all three return the substructure after any applicable modifications.

=== Exit points ===

=== Exit points ===

Line 54:

Line 35:

<haskell>

<haskell>

−

traverse :: Tree a -> TravelTree a -> Tree a

+

traverse :: Loc c a -- starting location (initial state)

+

-> Travel (Loc c a) a -- locational computation to use

+

-> a -- resulting substructure

</haskell>

</haskell>

−

Again, this is just a front-door for <hask>evalState</hask>, with an initial state of <hask>(tt, Top)</hask> where <hask>tt</hask> is the <hask>TravelTree</hask> passed in.

+

Again, this is just a front-door for <hask>evalState</hask>. Note that you have to give a <hask>Loc</hask> as a starting state. Both the libraries provided supply a <hask>getTop</hask> function, which takes a tree and returns the <hask>Loc</hask> corresponding to the top of the tree. Thus a typical call to <hask>traverse</hask> might look like:

−

+

−

== Examples ==

+

−

The following examples use as the example tree:

+

<haskell>

<haskell>

−

t = Branch (Branch (Branch (Leaf 1) (Leaf 2))

+

let t = Branch (Leaf 1) (Branch (Leaf 2) (Leaf 3))

−

(Leaf 3))

+

in (getTop t) `traverse` (left >> swap >> right)

−

(Branch (Leaf 4)

+

−

(Leaf 5))

+

</haskell>

</haskell>

−

[[Image:Tree.png|frame|right|The example tree]]

+

== Examples ==

−

+

−

=== A simple path ===

+

−

This is a very simple example showing how to use the movement functions:

+

−

<haskell>

+

−

leftLeftRight :: TravelTree a

+

−

leftLeftRight = do left

+

−

left

+

−

right

+

−

</haskell>

+

−

+

−

Result of evaluation:

+

−

+

−

*Tree> t `traverse` leftLeftRight

+

−

Leaf 2

+

−

+

−

=== Tree reverser ===

+

−

This is a more in-depth example showing <hask>getTree</hask> and <hask>putTree</hask>, but is still rather contrived as it's easily done without the zipper (the zipper-less version is shown below).

+

−

+

−

The algorithm ''reverses'' the tree, in the sense that at every branch, the two subtrees are swapped over.

It turns out this is a fairly powerful combinator. As with <hask>revTree</hask>, it can change the structure of a tree. Here's another example which turns a tree into one where siblings are sorted, i.e. given a <hask>Branch l r</hask>, if <hask>l</hask> and <hask>r</hask> are leaves, then the value of <hask>l</hask> is less than or equal to that of <hask>r</hask>. Also, if one of <hask>l</hask> or <hask>r</hask> is a <hask>Branch</hask> and the other a <hask>Leaf</hask>, then <hask>l</hask> is the <hask>Leaf</hask> and <hask>r</hask> the <hask>Branch</hask>:

+

−

+

−

<haskell>

+

−

sortSiblings :: Ord a => Tree a -> Tree a

+

−

sortSiblings = treeComb Leaf minLeaves where

+

−

minLeaves l@(Branch _ _) r@(Leaf _ ) = Branch r l

+

−

minLeaves l@(Leaf _) r@(Branch _ _ ) = Branch l r

+

−

minLeaves l@(Branch _ _) r@(Branch _ _ ) = Branch l r

+

−

minLeaves l@(Leaf x) r@(Leaf y ) = Branch (Leaf $ min x y)

+

−

(Leaf $ max x y)

+

−

</haskell>

+

−

Result of evaluation:

+

<hask>Travel</hask> is too general to be used in itself, so there are examples given on the documentation pages for the libraries. Here are the links again:

Revision as of 21:21, 19 April 2006

The Travel Monad is a generic monad for navigating around arbitrary data structures. It supports movement, mutation and classification of nodes (is this node the top node or a child node?, etc). It was proposed and designed by Paolo Martini (xerox), and coded by David House (davidhouse). It's designed for use with The Zipper but in fact there is no requirement to use such an idiom.

At the moment there are two specific libraries that use the Travel monad: TravelTree for navigating around binary trees, and TravelBTree for navigating around "B-Trees", trees where each node has an arbitrary number of branches.

is designed to hold a Zipper (although it doesn't have to; for example if you wanted to traverse a list it would probably be more natural to hold the entire structure and an index). Indeed, both of the libraries provided with the generic

Travel

monad use a zipper.

2 Functions

2.1 Movement

At the moment, movement is specific to the structure you are traversing and as such, the movement functions are provided by libraries implementing specific structures. Try the documentation for TravelTree (binary trees) or TravelBTree (B-Trees; trees where each node has an arbitrary number of branches).

2.2 Mutation

There are three generic functions available for changing the structure: