Assignment 7: Building a Prolog Interpreter in Scala

Description

By the time you have completed this work, you should be able to:

  • Read and understand the semantic rules of Prolog in handout 6.
  • Implement a simple Prolog interpreter in Scala, based on the aforementioned information.

Download the template code below. There is only one file you need to modify in the template code: interpreter.scala. In interpreter.scala, you will need to implement the following components, all of which are described in handout 6:

  • The lookup helper function
  • The addRelation helper function
  • The unifyVals helper function
  • The unifyMultiValsHelper helper function
  • The unifyMultiVals helper function
  • The transition rules for mini-prolog, implemented in the nextState function

If no transition rules apply for a given state, you should throw a StuckException (defined near the top of interpreter.scala).

Running, Compiling, and Testing

You can compile your code like so:

scalac *.scala

The code can be run as follows, where tests.pl is a file containing a valid Prolog program in the subset of Prolog we handle, and myLength([1,2,3], X). is a query to run on tests.pl:

scala miniprolog.interpreter.Interpreter tests.pl "myLength([1,2,3], X)."

For testing your code, you can run the test suite in the provided tester.scala file, like so:

scala -cp scala-parser-combinators.jar:. miniprolog.tester.Tests

The provided test suite is not complete, so you are encouraged to add additional tests.

For ease of debugging, your code should be as close to the small-step transition rules as possible. Code that deviates from the mathematical definition is generally difficult to reason about, since it becomes less clear where bugs are relative to the transition rules.

Downloads

Assignment 7 (zip)

Deliverables

You must use turnin to turn in all of your code. The command below will work for this purpose:

turnin assign7@cs162 interpreter.scala