Notes on MP3

University of Alabama

Spring 2004


The prolog program works in two phases--first it builds an abstract syntax tree (AST), then it evaluates it. The section of the program from "exp(literal(L)) ->" to "num(9)" are the rules that you must fill in to parse the program and build an AST. The section of the program from "prog_eval(exp(E), Result) :-" to the end of the file evaluates the AST "exp(E)" and binds the result as "Result"

For the language as defined, the middle section of the program, labeled ``SEMANTIC ALGEBRA'', is not necessary. If we had extended the language to include (set var exp), then we would need it. However, I will explain it here anyway. These two rules use Prolog's database to represent the store of the program. The access rule says that if there is a rule ``store(Var, Val)'' then bind Val. For example, if there is a rules in the database store(x,2)., then access(x,Result). would result in Result = 5. The update rule is a little more complicated. Let us examine the second goal first. The assert predicate places its argument into Prolog's database. In the update rule, if update(x,3) is executed, then the rule store(x,3) is placed in the database. The first goal handles the case where the rule that will be inserted by the second goal already exists. The retractall predicate removes its argument from the database. For example, if update(x,5) is entered, then any store predicate with a first argument of x would be removed.

The first thing to do is finish the code for building the AST. You can test this separately. Here is an example execution.

$ swipl
Welcome to SWI-Prolog (Multi-threaded, Version 5.2.0)
Copyright (c) 1990-2003 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- [''].
% compiled 0.01 sec, 6,356 bytes

?- num(Result,[0],[]).

Result = 0 

?- literal(Result, ['#t'],[]).

Result = bool(sc_true) 

?- prim(Result,[+],[]).

Result = + 

?- literal(Result, [0], []).

Result = num(0) 

?- exp(Result, [5], []).

Result = literal(num(5)) 

?- exp(Result, ['(', if, '#t', 5, 6, ')'], []).

Result = if(literal(bool(sc_true)), literal(num(5)), literal(num(6))) 

The second thing to do is to finish the code for evaluating the AST. Here is an example execution.
?- exp(P,['(', if, '#t', 5, 6, ')'], []), prog_eval(exp(P), Result).

P = if(literal(bool(sc_true)), literal(num(5)), literal(num(6)))
Result = 5 


About this document ...

Notes on MP3

This document was generated using the LaTeX2HTML translator Version 2K.1beta (1.61)

Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The command line arguments were:
latex2html -split 0 -nonav mp3notes.tex

The translation was initiated by on 2004-04-22