TweetFollow Us on Twitter

Learning from Leonardo

Volume Number: 15 (1999)
Issue Number: 10
Column Tag: Programming Techniques

Learning from Leonardo

by Joseph J. Strout, La Jolla, CA

Visualizing and understanding algorithms forward and backward

Introduction

Though many readers of this magazine will have forgotten, programming does not come naturally to the human mind. New programmers struggle with burdens at several different levels: learning the syntax of a language, comprehending the step-by-step flow of program code, and grasping abstract algorithms. It's especially hard to learn these skills from a book or chalkboard; we understand better when we see things in action.

A source-level debugger can let a student step through a program, which helps clarify how the computer is interpreting the code. It doesn't help much with following complex algorithms at a more abstract level, though. In addition, a traditional debugger can only step forward; it's like a VCR with no rewind button. This means that if you miss a step or want to see a computation again, you have to restart the program from the beginning. This is where Leonardo comes in.

What Leonardo Does

Leonardo is a C development environment made specifically for teaching and learning. It provides two major improvements over a traditional IDE. First, it provides a mechanism for visualizing computations graphically as they happen; by attaching graphical representations to key variables in a program, it's relatively easy to get a high-level understanding of what the algorithm is doing. Second - and this is the one that really amazes me - its source-level debugger runs both forwards and backwards! In addition to the usual step forward, step down (into a function), step up (out of the function), and run, the Leonardo control panel has buttons to step backwards, step back out of a function, step back into a function, run backwards, and reset the process.

Code written with Leonardo is completely reversible. Variable assignments will be undone, output sent to the console will disappear, graphics drawn will be undrawn, and so on. You have access to the full set standard ANSI functions, and yes, those are reversible too. The number of program steps you can undo is limited by available memory, but in practice, this did not seem to be a serious limitation.

Leonardo does this magic by running your code on a "Virtual CPU" that not only provides reversible execution, but also catches memory errors, invalid parameters to standard function calls, leaks, and so on. You can't produce stand-alone applications with Leonardo, and programs written in it run much slower than they would as native Mac applications. But for illustrating programming concepts, or even debugging a complex algorithm, Leonardo's graphical state display and reversible execution are hard to beat.

Using Leonardo

Leonardo ships as a PowerPC application plus standard C headers and libraries. It also comes with a large and neatly organized set of ready-to-run samples.

To get a feel for Leonardo's animation capabilities, simply launch the program and pick any of the samples from the Program hierarchical menu. Most of these illustrate common data structures and algorithms, like a sorted heap or the mini-max game-playing algorithm, but the collection also includes a few games and utilities. Choosing an item will run the corresponding program, which comes already compiled. Most programs present both a text console interface, and one or more graphical displays to give you a peek at what's going on inside. See Figure 1 for a typical example (from the "RedBlackTrees" sample).


Figure 1.A Leonardo text console and graphics window.

As a consequence of running the already-compiled executable, you have only a limited control palette and no access to the source code. This is fine for observing the graphical illustration of an algorithm, but to dig any deeper, you'll want a full set of controls.

For that, use the Open C Project menu command, or simply double-click one of the project files in the finder (they all end in ".µ" like the old Metrowerks convention). This presents a project window similar to that in other IDEs; it lists the files in the project, and has buttons for running, setting project options, and so on (see Figure 2). The first thing you'll want to do is open the project settings window, by clicking the rightmost button. Project settings in Leonardo are mercifully simple; it's just a matter of setting a couple of memory partitions, and toggling four build options on or off. I recommend you turn all four on - in the pre-built projects, "Debug Mode" is turned off, which is why access to the code was so limited before.


Figure 2.Leonardo's project window (top) and project settings dialog.

With debug mode turned on, running the project presents a full debug console (Figure 3) as well as a source-code display. This will be familiar to anyone used to other source-level debuggers; you can step through your code in various step sizes (e.g., step down into a function, or run until the current function returns). The surprising controls are the buttons at lower left, which allow you to roll a program backwards. While delightful, the use of these buttons is staightforward and little more needs to be said about them.


Figure 3.Leonardo's debug-mode process control panel.

The visualization commands are another matter. The graphical display that accompanies most of the sample projects is created by embedded commands in the source code, in a special declaration language called "Alpha." While you don't need to understand Alpha to make good use of the sample projects, making your own graphical displays will require a bit of study.

Speaking Alpha

Alpha commands are embedded in the C code within block comments. As such, they are ignored by a C compiler, but can be interpreted by a special Alpha preprocessor. This processor sets up graphical elements and inserts calls to update those elements when a relevant variable changes value.

One starts by declaring a window for the graphical display. There can be multiple windows, so each is given an ID number used to refer to it later, as follows:

/**
	View(Out 1);	// declare a window with ID 1
**/

This creates a window entitled "View 1" to appear, as soon as the code containing this block is executed. If it is at global scope, the window will appear as soon as the program runs, before entry into the main() function. When the code block containing the above directive exits, the window will disappear. The Alpha preprocessor acts as if it is converting these directives into C++ object declarations; they can occur anywhere in a code block, and they disappear automatically when that code block is finished.

A blank window is not very informative, so let's add a "Rectangle" declaration, based on the width of a variable, like so:

	// make a rect with ID=0, left=20, top=10,
	// width=i, height=10 in view=1:
	Rectangle(Out 0, Out 20, Out 10, Out H, Out 10, 1) Assign H=i;

The frequent (but not constant) repetition of the keyword "Out" before parameters, as well as the need to use the "Assign" keyword rather than simply specifying "Out i" in the correct position in the parameter list, are mysteries difficult to fathom without a complete manual (see below). However, Leonardo comes with literally dozens of examples of graphical displays using Alpha, so for most purposes you should be able to find a similar example and adapt it to your specific needs. My example here (shown in complete form in Listing 1) was adapted from an example given in the "Read Me" document, with reference to Appendix A of the manual, and was fairly easy to produce. It displays the state of two variables with two different rectangles - a sort of dynamic bar graph, continually updated as the program runs.

Listing 1.

int main(int argc,char* argv[])
{
	long j;
	long i;

/**
	View(Out 1);	// declare a window with ID 1
	// make a rect with ID=0, left=20, top=10,
	// width=i, height=10 in view=1:
	Rectangle(Out 0, Out 20, Out 10, Out H, Out 10, 1) Assign H=i;
	// make a similar one to track j
	Rectangle(Out 0, Out 20, Out 25, Out H, Out 10, 1) Assign H=j;
**/
	
	for (i=0; i<100; i++) {
		j = (i*i) % 100;
	}
}

Documentation & Support

Leonardo is written by two developers in Italy, and is distributed free of charge, so commercial-level technical support is not to be expected. The chief support venue is the Leonardo web site (see URL at the end of this article), which is clean and well designed, and which (fortunately for most American users at least) is in well-written English. It includes an overview; a number of images, including animations; a program library; and an on-line manual. The manual is also included with the distribution.

Unfortunately, the manual is still "under construction" and stops just when it was getting interesting. Only the first two chapters, "Installing Leonardo" and "Let's write a C program" are present; the rest of the manual is outlined but not yet available. Still, these two chapters are an excellent introduction to the system, and provide enough to get you started. There is also an appendix which provides a brief reference for all the Alpha predicates used to provide graphical output.

Sending email to the authors appears to be the only way to get interactive support. A mailing list would have been nice for such a complex and powerful tool. Nonetheless, when I sent some questions and suggestions to the authors, I received a helpful response fairly quickly.

Limitations

Despite the version number (3.4.1 at the time of this writing), Leonardo is clearly not a finished product. The manual is mostly unwritten, and there are menu items which are apparently not yet implemented. For example, the Preferences command is present in the Edit menu but perpetually disabled.

Functionally, Leonardo measures up fairly well. Its most serious shortcoming is that, surprisingly, there is no way to view or change the values of variables in the debugger. That is unfortunate; graphical visualization is very helpful for getting the big picture, but often one needs to inspect or tweak individual variables in order to fully understand the code. The omission of this feature will leave some users having to go back and forth between Leonardo and another IDE. In addition, it's not possible to set or change breakpoints while the program is running; breakpoints can only be set by adding a #pragma to the code and rebuilding. But these were the only functional limitations found; overall it is quite solid.

The interface, on the other hand, has a few more problems. First, it frequently displays black or red text against a thick marbled background, making it nearly illegible. This is true even for the Windows-style bar at the bottom of the screen that displays context-sensitive help - a poor substitute for balloon help. The windows have a very annoying habit of expanding to fill the entire screen whenever you touch anything, regardless of whether they have any actual data to display in all that space. There are interface widgets on each document-style window which look like they may be pop-up menus - and sometimes they are, but sometimes they aren't. Checkbox items can't be toggled by clicking on their text, as is usual in the Mac interface, but only by clicking on the box itself. Also in the "minor quibbles" category, the integrated editor does not support the standard F1-F4 editing keys, and it'd be nice to be able to change the font.

As a piece of software engineering, Leonardo is excellent; as an example of interface design, it's somewhat lacking. Hopefully this will improve as the program matures. In my email to the authors, I complained about some of the worst problems (such as the marbled background), and was assured that future versions would correct at least some of them. It's worth repeating that this is free software, and none of the limitations mentioned above are very serious; overall the quality is superb.

Conclusion

Leonardo is a very remarkable application. While perhaps not as polished as a commercial IDE, it is extremely well polished by the standards of free software, and it was rock solid in my hands. The visualization provided by the Alpha predicates allow one to watch an algorithm at work in a very powerful and intuitive way, and the ability to step ordinary C code forward and backward is nothing short of amazing.

This application would be most useful in computer science and programming courses. It comes with a large library of common data structures and algorithms right out of the box, ready to illustrate their workings in animated color, and more could easily be written from these examples. Any one of these could feature prominently in a class lecture, and is likely to engage the students and foster comprehension much better than abstract discussions or static diagrams. Since Leonardo is free, students can be encouraged to download a copy and play with the programs on their own. When they do, they'll find Leonardo's reversible execution to be an extremely helpful way to explore any algorithm.

Leonardo's only real drawback is that, as yet, it is unfinished - a state especially regrettable in the manual. The authors are doing this work with no financial support from the users, so it's the users' responsibility to support them in other ways. Write to them, let them know what you like and don't like, tell them how you're using it, and ask if there is anything you can do to help. With the concern and support of a strong user base, Leonardo is sure to become an indispensable tool for teaching, learning, and debugging.

Useful URLs

The Leonardo home page:
http://www.dis.uniroma1.it/~demetres/Leonardo/


Joe Strout works as a software developer in a neuroscience lab in southern California. While not helping to unravel the secrets of the brain, he enjoys pursuits ranging from martial arts to 3D modeling. He welcomes your comments at joe@strout.net.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.