2011-03-27

Use case for Restart-Bind

It's always a good day when you find a perfect use case for one CommonLisp's less used operators. Lately I had the pleasure to find a goodopportunity for RESTART-BIND.

I've written a network simulator in a couple of hundreds lines ofCommon Lisp (not counting our basic protocol and miscanellous stack)to emulate mobile IP networks. We use it to review that our mobilebroadband accelerator is behaving the way we want it to behave. Wealso use it in a complete virtual setup using UML instances for ourtest suite as well as for the lab in our office.

For determinism purposes I wanted to be able to reseed the*RANDOM-STATE* to its initial value by some means. And it turned outthat RESTART-BIND is just the right thing for that job:

You can reseed manually by interrupting the network simulator andusing the RESEED restart, or you can reseed programmatically (e.g.periodically after a certain time of inactivity) by

(invoke-restart 'reseed)

Can you feel that warm and fuzzy feeling? Just the right thing. :-)

PS.

Other fun note about restarts and RESTART-BIND. If you squint youreyes, you will discover that restarts are essentially nothing elsethan dynamically scoped local functions, and RESTART-BIND is basicallyDYNAMIC-FLET.

23 comments:

"Other fun note about restarts and RESTART-BIND. If you squint youreyes, you will discover that restarts are essentially nothing elsethan dynamically scoped local functions, and RESTART-BIND is basicallyDYNAMIC-FLET."

Exactly! And that is how they're implemented in a lot of Lisps. IMO the Common Lisp condition system is a really simple thing that gets explained in very complicated ways. The best way to learn about it is to look at the implementation (Red Daly has a really nice one for Parenscript: https://github.com/gonzojive/paren-psos/commit/6578ad223515dc2c1ddf49346f4baf7c3bee37c4)

I think this is one of the most interesting articles I’ve read on this subject. You have made your points interesting, unique and I agree with most. I am glad I found your article todayhappy wheelssuper mario brospacmanagario