06.11.2014 Views

Introduction to SWI-Prolog & Its Graphical Tracer Programming ...

Introduction to SWI-Prolog & Its Graphical Tracer Programming ...

Introduction to SWI-Prolog & Its Graphical Tracer Programming ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Introduction</strong> <strong>to</strong> <strong>SWI</strong>-<strong>Prolog</strong><br />

&<br />

<strong>Its</strong> <strong>Graphical</strong> <strong>Tracer</strong><br />

<strong>Programming</strong> Languages Midterm Project<br />

Phuong T. Tran<br />

March 8, 2011


What is <strong>Prolog</strong>?<br />

●<br />

●<br />

<strong>Prolog</strong> is:<br />

– A logic programming language<br />

– A declarative language<br />

– Comprised of:<br />

●<br />

●<br />

●<br />

Facts<br />

Rules<br />

Queries<br />

Useful in specific problems like AI and<br />

scheduling.


Facts<br />

●<br />

●<br />

●<br />

has(bicycle,wheel,2).<br />

This fact states that a bicycle has wheel, two of<br />

them.<br />

Other examples of facts:<br />

– has(wheel,spoke,32).<br />

– has(bicycle, frame,1).


Rules<br />

●<br />

●<br />

abs(X,Y) :- X >= 0, Y is X<br />

This rule states that for the predicate<br />

”abs(X,Y)” where X is the parameter inputed,<br />

and Y is the result.<br />

● If X >=0, then Y is X.<br />

●<br />

This abs predicate is part of defining the<br />

absolute value of X. The other part is:<br />

● abs(X,Y) :- X < 0, Y is X * -1


Queries<br />

●<br />

Using the Facts from the earlier slide, here is<br />

an example of a query:<br />

– par<strong>to</strong>f(wheel,spoke).<br />

– Is spoke part of wheel?<br />

●<br />

Using the abs predicate rule:<br />

– abs(-1,Y).<br />

– What is the absolute value of -1?


Is an IDE Necessary for <strong>Prolog</strong>?<br />

●<br />

●<br />

●<br />

Starting off with <strong>Prolog</strong> only needs two <strong>to</strong>ols:<br />

– The <strong>Prolog</strong> compiler/implementation<br />

– Text Edi<strong>to</strong>r<br />

IDEs typically provide the following <strong>to</strong>ols:<br />

– A ”project/explorer” view for multiple files &<br />

dependencies<br />

– <strong>Graphical</strong> debugger<br />

– GUI development <strong>to</strong>ols<br />

Of the above, a graphical debugger may be the most<br />

useful part of an IDE for teaching and learning.


Launching <strong>SWI</strong>-<strong>Prolog</strong>


Let's Use the abs(X,Y) Rules<br />

abs(X,Y) :- X >= 0, Y is X<br />

abs(X,Y) :- X < 0, Y is X * -1<br />

And compile...


Result of a Query<br />

● The following is the of the query that inputs -1<br />

and asks for the absolute value of -1.<br />

● The result is Y = 1.


Using trace.<br />

●<br />

Typing in ”trace.” at the <strong>Prolog</strong> prompt allows<br />

you <strong>to</strong> trace the step of execution. This is very<br />

useful as a debugging <strong>to</strong>ol, especially for more<br />

complex programs.


Tracing abs(-1,Y)


<strong>Graphical</strong> <strong>Tracer</strong><br />

●<br />

Type in the following <strong>to</strong> launch the graphical<br />

tracer and trace the abs(X,Y) predicate.


Parts of the <strong>Graphical</strong> <strong>Tracer</strong><br />

●<br />

The graphical tracer has three main parts:<br />

– Bindings<br />

– Call Stack<br />

– Program steps<br />

●<br />

Everything is color coded.


Step 1<br />

●<br />

The green highlights the first step.


Step 2.<br />

●<br />

The next step is <strong>to</strong> consider the first condition:<br />

X > = 0.


Step 3.<br />

●<br />

Condition is false, as highlighted in red.


Step 4.<br />

●<br />

The debugger continues on <strong>to</strong> the next<br />

condition: X < 0.


Step 5 & 6<br />

●<br />

Green indicates that the condition is true.<br />

Evaluates Y = X * -1. Program then exits.


Other Components of <strong>Graphical</strong><br />

<strong>Tracer</strong><br />

●<br />

Thread viewing for more advanced programs


Other Components (cont.).<br />

●<br />

●<br />

●<br />

A Naviga<strong>to</strong>r <strong>to</strong> view all components<br />

of a program<br />

Useful for larger programs that is<br />

comprised of many files.<br />

Query Insert:


Console vs. <strong>Graphical</strong> <strong>Tracer</strong><br />

●<br />

●<br />

The Console and <strong>Graphical</strong> <strong>Tracer</strong> providers<br />

the same information.<br />

The <strong>Graphical</strong> <strong>Tracer</strong> advantages<br />

– Color coding debugger and text edi<strong>to</strong>r<br />

– Easier <strong>to</strong> view longer, more complex predicates<br />

– Other Functions:<br />

●<br />

●<br />

●<br />

Edit program within debugger<br />

Set breakpoints<br />

Run queries within debugger.


<strong>Graphical</strong> <strong>Tracer</strong> as a Learning<br />

Tool<br />

●<br />

●<br />

●<br />

●<br />

Dividing the tracing in<strong>to</strong> three sub windows allow for<br />

easier viewing of the steps and stack calls.<br />

Color coding helps <strong>to</strong> track steps, especially with<br />

complex predicates, such as recursions.<br />

Ability <strong>to</strong> edit, query, and compile in debugger saves<br />

time from editing in text edi<strong>to</strong>r and then compiling in<br />

different window.<br />

Better for visual learners <strong>to</strong> see the background of<br />

stack calls and step throughs.


Another IDE – Visual <strong>Prolog</strong><br />

●<br />

●<br />

Other IDEs exist, some more complex than the<br />

<strong>to</strong>ols that come with <strong>SWI</strong>-<strong>Prolog</strong>.<br />

Visual <strong>Prolog</strong> is an IDE for a Windows OS.<br />

– Text Edi<strong>to</strong>r<br />

– Compiler, Linker, and Debugger<br />

– Additional <strong>to</strong>ols for developing GUIs, such as a<br />

graphics edi<strong>to</strong>r, <strong>to</strong>olbar edi<strong>to</strong>r.


Summary<br />

●<br />

●<br />

A graphical debugger, such as provided with<br />

<strong>SWI</strong>-<strong>Prolog</strong> can be a very useful<br />

teaching/learning <strong>to</strong>ol for <strong>Prolog</strong>, especially for<br />

visual learners.<br />

A more complex IDE, such as Visual <strong>Prolog</strong>,<br />

probably is not necessary for teaching because<br />

it has more functions not needed for just<br />

learning.


Further Research<br />

●<br />

●<br />

●<br />

Analyze a more complex predicate in the<br />

graphical debugger, such as recursion.<br />

Compare the different graphical debuggers,<br />

such as <strong>SWI</strong>-<strong>Prolog</strong> and Visual <strong>Prolog</strong>.<br />

Use the more advanced functions of the<br />

debugger, such as breakpoints.


References<br />

●<br />

●<br />

●<br />

●<br />

My implementation of <strong>Prolog</strong> Assignment for<br />

abs and par<strong>to</strong>f code.<br />

Textbook ”7 Languages in 7 Weeks”<br />

Http://www.swi-prolog.org/<br />

http://www.visual-prolog.com/


Hours Spent on This Project<br />

● Researching <strong>SWI</strong> <strong>Prolog</strong> and gtrace and Visual <strong>Prolog</strong>: 1.5<br />

hours<br />

●<br />

●<br />

●<br />

●<br />

●<br />

●<br />

●<br />

Slide Planning: 1 hour.<br />

Learning how <strong>to</strong> use gtrace: 45 minutes<br />

Running programs through trace and gtrace with screenshots:<br />

45 minutes.<br />

Compiling and editing presentation: 1.5 hour.<br />

Total time: 5.5 hours.<br />

Average time I study for a midterm: 3 hours.<br />

Time taking a midterm: 2 hours or less.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!