Studio Quick Tour

From InterProlog Wiki
Jump to: navigation, search

The following illustrates Prolog Studio with a small Prolog example, [myFarmer.P], which solves an old puzzle: a Farmer/sailor has to carry a Wolf, a Goat and some Cabbage across a river, using a small boat which only carries himself plus one item (without anyone eating anybody/thing). See an animation in the [Math Cats site].

Screen Description
UglyFace.png

Make sure you have XSB Prolog installed and runnable, and have downloaded the file InterPrologStudio.jar. If you have to go to http://xsb.sourceforge.net/downloads/downloads.html. Detailed instructions in Prolog_Studio#Download_and_installation.

The picture depicts XSB Prolog "out of the box" and ready for business; the most powerful logic engine on Earth, with tabling and all the cream of 40 years of world research in logic programming built in, but still in its Unix cave interface.

But we'll fix that in a moment...

PickingProlog.png

After double clicking InterPrologStudio.jar for the first time, you will be asked where the Prolog engine executable is.

Notice that some invisible files (which may not appear depending on your file explorer configuration) were created; "fidji" is a project code name related to InterProlog Prolog Studio. If you happen to cancel this dialog, erase the .fidji.prefs file before you attempt to relaunch Studio.

Listener.png

The Prolog listener will appear, showing output in its top larger panel, and accepting your input below.

The listener window menus are the starting point for most functions in Studio.

ListenerAndEditor.png

Drag the [myFarmer.P] file from your desktop onto any part of the Prolog Studio listener window, and an editor window will open. You can also open it using the File menu.

Or, if you're of the scripting geek kind, perhaps by executing some Prolog code:

ipListenerWindow(_W), 
javaMessage('com.declarativa.fiji.LogicProgramEditor',Editor,makeEditor(string('/YourPath/myFarmer.P'),_W)).

Studio runs over the InterProlog bridge, and its listener window (Java) object is a broker for many common Studio operations; see the above Prolog primitives in Java-Prolog_bridge#Prolog_side_API.

QuerySelection.png

Use the editor File/Load menu to consult the Prolog file.

Then try a goal. You could go to the listener window and type it, but you can also do it using the editor's Edit menu.

QueryWindow.png

The Prolog Query window will appear, showing the goal and its solutions / answers. In this case each answer is a sequence of states (a Prolog list) solving the puzzle.

By the way: the Prolog Query, listener and editor window positions (even over multiple screens) will be remembered between Studio sessions.

BetterQueryWindow.png

The puzzle solution is not very pleasant to read in its current form, as a bunch of Prolog lists.

By using a Prolog goal which extracts elements of the first solution, we can make the Prolog Query answers more clear. It's now easier to see, for each step of the solution, on which margin (East/West) the farmer and his passengers are.

Variable names are used for column names. By the way: the answers can be copied into somewhere else (such an Excel table) by selecting and copying them (with ctrl-C on Windows, Cmd-C on Mac).

PredicatesMenu.png

Let us browse the Prolog code in the example with the Studio editor. Go down to the solve/3 predicate.

You can do it by scrolling... ot you can use the Predicates menu, which shows all predicates in the editor file.

InvokingCallTree.png

To better understand the Prolog code, it helps to realize what predicate depends on what.

Just right-click any clause head or body subgoal in the text to get a popup menu.

CallTree.png

The Call Tree window for path/4 shows the Prolog top-down predicate calling structure.

You can expand and collapse nodes; right-clicking a node pops up a menu to navigate to the predicate.

This is one of Studio's more powerful features, as the full call graph can span multiple Prolog files and modules. Studio incrementally computes that graph, and uses it for the Call Tree and other features.

There is a useful shortcut to navigate along multi file sources: Ctrl-click (CMD-click on Mac) on any clause sub goal and you'll be taken to the definition of its predicate - even if on a different file.

VarHighlighting.png

As Prolog clauses get more complex, Studio helps understand its logic by highlighting logic variables.

Whenever you click on any variable, all its occurrences will be highlighted.

UndefinedPredicate.png

Looking at the already/2 predicate, it seems strikingly similar to the definition of (list) member. So we probably should use that system predicate instead, by replacing the only already/2 call by a call to member.

Let us start by commenting out already/2. At this point, our program is calling an undefined predicate, so Studio warns by highlighting in yellow. Syntax errors get also highlighted as you type - in red, one shown.

By the way, there's unlimited Undo (ctrl-Z) and Redo, see Edit menu.

BadImport.png

After replacing the already/2 call by member/2, and adding a buggy import directive (it should be member/2...), Studio warns us again (member/2 is undefined because it must be explicitly imported from the 'basics' system module)

As you edit Prolog sources, warnings and syntax errors will appear and disappear instantly along the way.

Another aspect of this feature: if you type a new variable it will be highlighted (if not anonymous), as a "singleton variable"; when you type the second occurrence of that variable in the clause the warning will vanish.

GraphSpec.png

Let us wrap up by showing the search mechanics in this example, by displaying it as a graph.

Studio includes the XJ Java Swing layer, one of the most powerful UI generators for Prolog ever made. This includes access to the [JUNG] Graph library. See XJ_Prolog_UI_Library.

The code shown is a full specification for the graph visualization of the farmer example: graph edges are defined as state transitions (moves), and nodes as states. The Prolog terms in the example serve as edge and node identifiers; both nodes and edges are typed according to the example's semantics: start state, safe state, etc. for nodes; and edges are either ordinary moves or moves pertaining to the solution path.

Additionally, there's a printPassengers/1 predicate to dissect a cryptic state transition and show it as a friendlier "passenger manifest".

StateGraph.png

Executing the showGraph goal opens up a graph window. Try the Operations/Layouts menu, and/or drag around the nodes.

Right-clicking graph nodes and edges pops up specific menus with operations calling your Prolog goals or Java messages. Shown (printed to the Prolog listener window) is the passenger list for the first leg in the solution path.