experchange > lisp

jurgen_defurne (05-30-07, 07:55 PM)
Hello,

As a little counter-weight against certain people whom regularly troll
this list with strange essays and weird how-to-get-rich-quickly
questions, I like to describe what I built in the past 10 months using
Common Lisp.

I have mostly programmed in imperative languages, with a main emphasis
on databases and business programming. In this domain I have used
FoxPro, Clipper, WANG PACE and COBOL. Currently, I support
configuration and change management, using Perl (already 7 years),
with fairly recently the addition of mySQL into the mix.

Except for a course AutoLisp as part of a course in industrial
programming, I haven't used Lisp until very recently, when I got
myself together, and decided to finally learn Lisp. That was in 2005.

When looking for resources, there are many and they are a little bit
confusing when one knows the name Lisp, but doesn't know about the
meaning of the expressions 'Common Lisp' and 'Scheme'. So I think it
took me a few weeks looking and searching for online introductions to
any kind of Lisp. It is inevitable that one finds the R5RS and ClTl2,
but the confusion only gets bigger when the differences in size are
seen, fifty pages vs. one thousand pages.

Then I found How To Design Programs. DrScheme exists as a package on
Debian, so I had it quickly installed and I started to read the book
and try the examples and exercises. Especially the later chapters gave
me some new insights in how to design programs, insights which I
started to use in my Perl programs.

In the beginning of 2006, I had found out and read enough to
appreciate the differences between Scheme and Common Lisp, so I
decided to get to grips with Common Lisp. I do not remember fairly
well how I really started with it. I think I started by reading parts
of ClTl2, and trying out small examples, learning about the different
datastructures available in CL, and then about the control structures,
then discovering the CLHS, and trying to write small programs.

I gained new insights in CL, when I discovered that it is not only an
interpretive language, but also a compiled one, and that SBCL and
CMUCL optimize very aggressively. I spent a couple of weeks writing
small benchmark programs in C and CL, and compiling them with GCC, G++
and SBCL and CMUCL respectively. In some case the results where faster
through CMUCL than through C, and in most cases of aggressive
optimization, they matched C. However, these are difficult exercises
because it is difficult finding small benchmark programs, which can be
written quickly in both languages.

In August 2006 then, I decided to take up a project that I had already
started in C, but I wanted to write it in Common Lisp, so I had a good
exercise in it.

The project is a simulation of computers which are built by means of
MSI components.My goal is to have a library of simulated components
which can be interconnected to form a computer architecture, and use
this as a validation of the design. This library is entirely written
in Common Lisp and has also test harnesses for each of the components
separately. This took me about 4 months to mostly finish (this is a
hobby project).

After the components, I started to look into the creation of a CPU
architecture with few components and the definition of an instruction
set. Since this got nothing to do anymore with CL, let's say I
finished this in about 2 months, but that was overlapping with the
build of the components, so end December I had an architecture and an
ISA which I could use to create a simulated computer system, using the
components library.

It was especially in this part of the project that I started to use
the advanced features of CL.

When I started to build the system by creating and interconnecting the
components, I noticed repeating patterns and I had a go at using
macros.

What I obtained with macros was this : instead of manually creating
components and their connections, I got to a system where I was able
to create a system for simulation by defining two tables. The first
table is one of aliases, in which I give every connection of every
component a unique name. These aliases are nothing more than inline
functions for accessing quickly structure members. The second table
then contains a list of these aliases, which is used to connect the
components together.

When I had chosen C, I would probably have to use m4 or write a
separate compiler to obtain the same functionality.

I think I did not spend more than three weeks implementing this. This
is a hobby project, so it is worked on only in the evenings, and even
then not every evening.

The next step was to create a micro-assembler. The controller of the
system is microprogrammed (the fastest way in this simulation to
implement a control path). This also took me a couple of weeks. The
microassembler uses s-expressions and lists.

The current step, which I am still testing, is the implementation of
the microprogram.

Integrating everything together, components, system, micro-assembler
and microprogram, went fairly well. I discovered some bugs, but
through the power of the above tools, these where repaired very
quickly.

So, what does the future hold ?

I would like to write an assembler for the system, also using s-
expressions. My current problem is to find way for expressing variants
of the same instruction, eg.

LD DR, constant
LD DR, @direct address
LD DR, M[@indirect address]

I would like to go further and wider with the simulation, building a
more complex system. One of the possibilities that I like to
investigate is to use a variant of Lisp as a system language (yes, I
know about the history of Lisp Machines etc...). I was wondering in
this context if it wouldn't be an
advantage to implement the CLISP instruction set here, as it would
make available a complete CL implementation at once.

The other possibility is to write a GCC backend for the architecture
and take advantage of all the free/open software that is available for
C.

I haven't decided on the issue yet, since I haven't even defined IO
yet in the system, this issue is for somewhere in the future.

So, to summarize, I love CL, especially since my daily work is Perl
(it's the syntax I hate, Perl is almost as powerful as CL, but it has
more libraries and better OS interaction), and CL really IS very
powerful. Just use it, and ask for support on comp.lang.lisp.

Regards,

Jurgen
Mark Hoemmen (05-30-07, 11:34 PM)
jurgen_defurne wrote [cool stuff he did with CL...]:
> ...


Yay, that's a bunch of cool stuff!!! *grins*

> I would like to go further and wider with the simulation, building a
> more complex system. One of the possibilities that I like to
> investigate is to use a variant of Lisp as a system language (yes, I
> know about the history of Lisp Machines etc...). I was wondering in
> this context if it wouldn't be an
> advantage to implement the CLISP instruction set here, as it would
> make available a complete CL implementation at once.


You may want to look at the Movitz project, which seems to have similar
goals.

> The other possibility is to write a GCC backend for the architecture
> and take advantage of all the free/open software that is available for
> C.


My personal favorite is to help port ECL to whatever system I'm using.
Then, I get to use the system's native C compiler, and also write CL.

mfh
rlb3 (05-31-07, 06:26 PM)
> So, to summarize, I love CL, especially since my daily work is Perl
> (it's the syntax I hate, Perl is almost as powerful as CL, but it has
> more libraries and better OS interaction), and CL really IS very
> powerful. Just use it, and ask for support on comp.lang.lisp.
> Regards,
> Jurgen


As a perl programmer learning lisp. I find your story inspirational.

Thanks,

Robert
Similar Threads