Bartosz Witkowski - Blog.

Intro

Why?

While lounging in #prolog on freenode I stumbled upon mercury and I was instantly blown away. From the casual perspective this seemed the perfect language for me - fixing all things that irked me in prolog (mostly of compile-time type checking) while adding effect tracking, multiple target platforms (native code via C, JVM, CLR and BEAM), and boasting fast execution times.

Getting into mercury was hard. While there exists a body of documentation on the main mercury site, in my opinion it is not approachable by beginners.

So going by the motto “Those who can’t do teach” - I decided to write a tutorial. Not being endowed with imagination I decided to port an existing one and “Write Yourself a Scheme in 48h” seemed to be:

  1. short enough
  2. sufficiently complicated
  3. satisfying in its contents

Because the original tutorial described a haskell implementation for me it was also an exercise in reading haskell and during writing it I could compare the two languages.

Target audience

I wanted this tutorial to be approachable by anyone - though non-programmers will probably feel lost.

Because of the “curse of knowledge”, you will probably get the most out of this tutorial comping from approximately the same background as I did - some experience in logic programming, some experience with languages with a rich type-system and a little knowledge of scheme.

About this tutorial

The long term goal of this tutorial is to write a scheme interpreter while showing basic mercury features. There is no particular emphasis for the scheme interpreter to be feature complete or even useful - it’s just a learning device and the implementation from this tutorial will have numerous limitations.

I wanted this tutorial to be easily approachable and to hold the reader by hand as much possible. I won’t try to challenge you - I’ll explain everything as it happens and I’ll introduce new concepts when it is either really really needed or the concept is pretty simple - some concepts are therefore not introduced when convenient which may present mercury in bad light - don’t fret, have this in mind - this is a tutorial for beginners!

The original tutorial came in 10 parts - because I want to move slowly I suspect that this series will turn out to be longer.

After an minimal introduction to mercury each part will consist of:

  1. Explaining what are the goals
  2. Presenting new concepts that are needed to complete this part
  3. Showing my implementation
  4. Showing the test code.

If you want to challenge yourself you may want to skip #3 and try to implement it yourself - you can use the tests or look at the proposed implementation to verify for yourself that everything is ok.

All of the compilable code in this tutorial is available in this repository:

Caveats

I am neither a guru in haskell (from which the tutorial was ported), scheme - (the language for which we will create an interpreter) nor mercury. I also don’t have a background in education and I’m not the brightest bulb in the box.

Nevertheless, I hope that this tutorial will help someone out - I know I learned a ton while writing it. But because of the above - both critique and pointing out my mistakes are very welcome :)!

What is mercury

Hyping mercury is unfortunately beyond the scope of this article - if you’re not sure if you want to learn mercury check out the official site. From my POV mercury is a very cool language that seems to be overlooked and deserves more recognition and love.

What attracted me to mercury was that it is a compile-time checked programing language in which the primary paradigm is logic programming. The type-system is very expressive allowing:

If you don’t understand these terms - don’t fret they will be explained further along in the series.

Next