Introductory Tour of Sierra

1. Introduction

Sierra is a browser-based interactive development environment (IDE) for Epilog. It allows users to view and edit datasets and rulesets, and it provides tools for programmatically querying and modifying datasets and rulesets. As changes are made, it automatically updates visible datasets in spreadsheet-like fashion in accordance with the user's rules. It also provides tools for analyzing datasets and rulesets, tools for tracing program execution, and tools for saving and loading files.

This document provides an introductory tour of the main features of Sierra. We show how to load Sierra; we show how to create, view, and edit datasets and rulesets; and we show how to save one's work for later use. We suggest performing the steps shown here in one's own browser as we proceed through the tour.

This tour presumes the reader is already familiar with Epilog or another logic programming language, such as Prolog. (See http://logicprogramming.stanford.edu for a course on Logic Programming in general and Epilog in particular.)

2. Getting Started

Since Sierra is browser-based, we start by loading a suitable browser. (Sierra runs in Safari, Chrome, Firefox, and other browsers. In our examples here, we use Safari, though the appearance and interaction are virtually the same in all major browsers.) Once our browser is started, we load Sierra by going to the following URL.


This brings up a page that looks like the following.

The command bar across the top provides access to menus concerning files, tools, and worksheets. We will introduce the items on these menus one by one as we proceed with our tour.

3. Data

The Tools menu contains various tools for manually or programmatically creating, querying, and changing data. Let's start by clicking on Lambda. This creates a separate window, like the one shown on the left below. (Note that the window may appear on top of an existing window. Here, we have manually resized the window and repositioned it to the left of the Sierra window.)

To help newcomers, the database is pre-populated with a small dataset. The facts here describe the initial state of a game of Tic Tac Toe. There are nine cells arranged in three rows and three columns, and all nine cells are blank in this state. There is also a fact stating that it is x's turn to play.

At this point we can modify the data if we wish. For example, we can change the b to x in the first fact to indicate that there is an x in the corresponding cell, and we can replace x with o in the last fact to change control from x to o. The window changes color to indicate that we have made changes but we have not yet committed those changes to the database.

Clicking the Update button stores the data and removes the highlighting, indicating that the window is showing the current data.

Suppose we edit the data and the result is syntactically illegal, as shown below.

If we click Update, we will get an error message like the following, and the database will not be modified.

At this point, we can either repair the problem and try again, or we can click the Revert button to return to the current state of the database, as we have done here.

We can change the data in other ways as well. One frequently useful feature is sorting the data. This can be accomplished by pressing the Sort button.

In this case, there is no change, as the data is already sorted. However, the window is once again highlighted, indicating that the result of the sorting operation has not been saved to the database. Pressing Save once again commits these changes to the database.

Pressing the Browse button switches from editing mode to browsing mode. See below. Note that most of the symbols here are links. Exceptions include numbers, strings, and logical operators.

Clicking on a linked symbol shows all facts in the database that mention that symbol. For example, if we click on cell, the result is the dataset shown below.

To go back to seeing all facts, we can erase the reference to cell in the text box at the top of the page.

To switch back to editing mode, we can click the Modify button on the command bar.

The Tools menu contains various other tools for programmatically viewing and updating data, e.g. evaluating functional terms, computing answers to relational queries, and performing actions that change the data.

4. Evaluate Tool

If we select the Evaluate tool, we get a window like the one shown on the right in the image below.

The Evaluate tool allows us to compute the values of functional terms. To specify a term for evaluation, we enter the term to be evaluated in the Query field. Here, we are asking for the count of all cells such that cell(M,N,b) is true, i.e. all cells that are empty.

Clicking the Show button causes Sierra to evaluate the specified term and place the result in the result field. In this case the answer is 8, since there are 8 cells containing blanks.

Changing the Unifications field changes the maximum number of unifications the system will attempt while evaluating terms. By default, the system performs a maximum of 100,000 unifications. If the Autorefresh check box is checked, the system automatically refreshes the result whenever changes are made to the database.

5. Query Tool

Sierra also provides a means for getting answers to relational queries. If we select the Query tool, we get a window like the one shown on the right below the Evaluate window.

To form a query, we enter an expression for our desired answer in the Pattern field and we enter our query in the Query field. Here, we are asking for all expressions of the form empty(M,N) where cell(M,N,b) is true, i.e. all cells that contain a blank.

Clicking the Show button causes Sierra to evaluate the query and show the results in the query window. In this case, there are eight answers.

In general, queries can have many answers. By default, the Query tool shows only 100 answers, as shown in the Results field. We can change this default by changing the value in this field. In the example, below, we have changed the Results field from 100 to 4, and the system has changed its response to just four answers. In the case of expensive queries, it is often desirable to ask for just one result.

As with the Evaluate tool, changing the Unifications field changes the maximum number of unifications the system will attempt before evaluating queries. By default, the system performs a maximum of 100,000 unifications. It is often useful to increase this to 1,000,000 unifications or more, though this will increase the amount of time needed to evaluate queries.

Also, in keeping with the Evaluate tool, if the Autorefresh check box is checked, the system automatically refreshes the results whenever changes are made to the database (either by manually editing lambda or by executing actions that change lambda).

To see how this works, we can click the Autorefresh checkboxes in our Evaluate and Query windows. Then we go back to lambda and modify our data once again, in this case placing an o in row 1 and column 2. When we press Update, the data is saved and the Evaluate and Query windows are automatically updated, as shown here.

This spreadsheet-style interaction, where results are computed automatically and the results shown immediately, is one of the popular features of Sierra.

6. Transform Tool

While we can edit our data directly in a Lambda window, this is not always the most convenient method for changing data, especially when we have large datasets and/or multiple, similar changes to make. Sierra provides two tools to help in such situations - Execute and Transform. We introduce the Transform tool here, and we discuss the Execute tool in the section on using operations.

Clicking Transform on the Tools menu produces a window like the one shown on the lower right.

To specify a transformation, we enter a pattern into the Condition field and a pattern in the Conclusion field. In executing a transformation, Sierra finds all variable bindings that satisfy the specified condition and, for each, modifies the database in accordance with the instances of the conclusion corresponding to that variable binding. In this case, we want to find all facts of the form cell(M,N,R); and, for each of these, we want it to delete that fact and replace it with a fact of the form cell(N,M,R), i.e. the same fact with the row and column reversed.

The Expand button asks Sierra to display the additions and deletions that would be performed if the transformation were to be executed in the current state. It is extremely useful in debugging to see changes before they are made. (Note that, in the image below, some of the changes have scrolled off the page.)

The Execute button differs from the Expand button in that it actually makes the specified transformation on the database, leading to the situation shown below.

As a result of the transformation, the facts in lambda have been changed as directed. Where there was an o in row 1 and column 2, the o is now in row 2 and column 1. Note that the order of facts has been changed slightly. This is because the old cell facts have been removed from the database, and the new cell facts have been added to the end. Note also that the Evaluate and Query windows have also been updated. In this case, the count of blank cells remains the same, but the list of empty cells has changed.

Clicking Execute a second time will cause the transformation to be repeated, in this case resulting in a reversion to the state before the first transformation.

7. Rules

In addition to viewing and editing data, Sierra allows us to view and edit rules of various sorts - function definitions, view definitions, and operation definitions. These definitions are contained in a single ruleset called the library.

Selecting the Library option on the Tools menu brings up a window like the one show below on the lower left. As with Lambda, the window shows the rules contained in the initial rulebase. To help newcomers, the rulebase is pre-populated with a small ruleset.

The entries here are the rules for the game of Tic Tac Toe. It is legal to mark a cell if the cell contains a blank. Clicking on a cell replaces the blank in a cell with the mark of the player in control and also reverses control. A player gets 100 points if the state contains a line of his marks; he gets 50 points if both players have lies or if neither player has a line; and he gets 0 points if his opponent has a line and he does not. A line is either a row, a column, or a diagonal. The game is over as soon as a player has a line of marks or when there are no blank cells left.

As with Lambda, we can explore the library by pressing the Browse button. This replaces the editable view of the content with a browsing view. See below.

As with the Lambda browser, some of the symbols here are links. Clicking on one of these links replaces the display with a display of all rules defining that concept. For example, if we click on line, the result is the ruleset shown below.

To go back to seeing all rules, we can erase the reference to line in the target box at the top of the page.

If we want to modify the contents of the library, we can click the Modify button on the commandbar. This changes the window from the rule browser back to the rule editor.

In the situation below, we have added a definition for the freecells function to our library editing window. The term freecells(M) denotes the number of free cells in row M. As with the lambda editor, the color of the library editor changes to indicate that we have made changes but we have not yet committed those changes to the database.

Clicking the Update button after a change stores the new rules in the rulebase and removes the highlighting, indicating that the window is showing the current ruleset, as shown below. Clicking Revert leaves the rulebase as is and resets the contents of the editor window to that state before editing began.

Once we have written rules to define functions, views, or operations, we can see the effects using the tools mentioned earlier - Evaluate, Query, and Transform or Execute - as described in the following sections.

8. Using Function Definitions

The Evaluate tool previously introduced evaluates terms containing built-in operators, like plus, times, and countofall. However it also uses function definitions in evaluating terms applying user-functions.

If we have user defined functions, like freecells, we can use the Evaluate window we opened earlier to compute values for these functions as well. In the situation shown below, we have the changed the Query term to freecells(2).

Pressing the carriage return or clicking outside of the query box produces the value of this term. In this case, the answer is 3 which is correct value given our current state and the definition of freecells in the library.

Changing the data in lambda or changing the definition of freecells in the library automatically results in different values being shown for the specified query term.

9. Using View Definitions

Views in Logic Programming are effectively named queries. An important benefit of this is composition. Once we have a name for a view, we can use that name in defining other views. Moreover, we can use the name in defining the view itself, i.e. in defining recursive views. View definitions are expressed via view rules. In Sierra, rulesets are managed using the Library tool.

Once we have a view defined, we can use it in queries. We can, for example, use our query window to request instances for the legal relation. Pressing the Show button produces the answers to this query, based on the facts in lambda and the definition of legal in the library, as shown below. Note that we have reset the number of results to a maximum of 100.

As with simple queries, the Unifications field allows us to specify the number of inference steps performed on an individual query before the system terminates its efforts. Also, if the Autorefresh box is checked, Sierra keeps the display up to date as we make changes.

10. Using Operation Definitions

Operations in Logic Programming are effectively named transformations. As with views, an important benefit of this is composition. Once we have a named operation, we can use that name in defining other operations. Moreover, we can use the name in defining the operation itself, i.e. in defining recursive operations.

Once we have an operation defined, we can use it in transform windows. We could, for example, write mark(3,1) as a conclusion. However, once again, there is a streamlined tool for this purpose, viz. Execute. Here we have clicked Execute on the Tool menu and gotten an empty Execute window.

Using the Execute tool, we can express an action we want to execute. In this case, we have entered mark(3,1).

If we press the Expand button, we see a list of all facts that will be added and those that will be deleted if we execute the mark(3,1) action. Note that, if executed, these facts would be removed in a single step, i.e. operation execution is an "atomic" action.

The Execute button, as before, asks Sierra to make these changes, resulting in the state shown below.

The Expansion Depth field allows us to specify the depth of recursion in expanding operation definitions.

11. Timer

Clicking Timer on the Tools menu brings up a window that allows us to start, pause, and reset a timer that periodically executes the tick action.

In the situation depicted below, we have used the Library editor to define the tick operation to reverse the rows and columns in Tic Tac Toe, as in our previous Transform example.

Pressing the Start button sets the timer going. After one step, we would have the situation shown below. On the next step, the rows and columns would be flipped back again. The state would continue to oscillate back and forth until we press Pause. Once paused, we can press Start again, or we can press Pause to reset the step count.

This example is not especially practical, but updating on clock tick is often useful in simulating dynamic systems. See, for example, the Solar System and the Game of Life.

12. Debugging Tools

Sierra also provides several tools to aid in debugging Epilog programs. The Trace tool is similar to the Query tool except that it prints out a trace of execution so that the programmer can visualize the flow of control. The Errors tool checks the database and library for common errors, such as mismatched arities, unsafe rules, and unstratified rules, and prints warning messages for any problems it finds.

13. File Management

The Load option on the File menu allows us to read datasets and rulesets from the local file system and load them into lambda and library. The Save options allows us to write data to the local file system.

The Save Configuration option allows us to save the complete state of Sierra to the local file system, including data, rules, and tools. The Load configuration option allows us to load a previously saved configuration file. These operations are extremely useful in developing Logic Programming applications. We can stop work and continue right where we left off on another day. And we can exchange demonstrations by sharing config files with others.

14. Worksheets

Worksheets are interactive web pages in which data is stored in the form of Epilog datasets and in which definitions are stored as Epilog rulesets. Developed and debugging these datasets and rulesets separately from worksheets can be tedious. Sierra provides a way for worksheet developers to view and edit the data and rules of running worksheets as Sierra datasets and rulesets, thereby facilitating worksheet creation and debugging.

If we click on the Open with Sierra data item on the Worksheets menu in Sierra, we get a prompt like the one shown below. Here we have entered the URL for a worksheet implementing the game of Tic Tac Toe using data and rules like the ones described earlier.

Clicking the Okay button opens the worksheet in Sierra and harmonizes the Worksheet data with the data in Sierra. The result is shown below. Here we see a graphical rendering of a Tic Tac Toe board with marks placed in accordance with the data in Sierra's dataset.

Once the worksheet is opened, the state of the game can be modified either by interacting with the worksheet directly (e.g. clicking on one of the cells) or by using the tools in Sierra (e.g. the Lambda editor or the Transform or Execute tools), and Sierra will ensure that data in Sierra and on the worksheet are synchronized. For example, in the situation depicted below, we have clicked on the second cell in the first row, leading to an x appearing there, a corresponding change taking place in Lambda, and the Evaluate and Query windows being updated accordingly.

Interestingly, if there are other worksheets that share the same data and rules, they can also be opened in Sierra and we can see the data presented in a different format. Here we have opened another worksheet, this one for a game called Trifecta, in which the goal is to get three of a kind - ranks, suits, suit colors, card colors. Although the appearance is very different, the data for both of these games is identical.

Once both worksheets are opened, it is possible to modify the data by interacting with either worksheet or the tools in Sierra, and the other data visualizations will be updated accordingly.

In its current form, Sierra does not allow one to edit the rules on a worksheet. However there is another tool, called Whitney, that behaves similarly but also allows rule sharing as well as data sharing.

15. Conclusion

In addition to the capabilities described here, Sierra provides tools for manipulating communication channels. These tools enable communication between different incarnations of Sierra running on the same machine or other machines, and they can be used to support the flow of information between Sierra and external data sources. The tools are a bit more complicated than the simple tools discussed here, and so we have skipped over the details in this simple introduction.

Comments and complaints to genesereth@stanford.edu.