next up previous
Next: Step 6: Doing Up: The TkHol Tutorial Previous: Step 4: Defining

Step 5: Making a Recursive Definition

 

By now you should have:

  1. Started TkHol.
  2. Created a theory segment called btree in which to develop a theory of binary trees.
  3. Defined the primitive recursive type btree using the New Recursive Type tool.
We will now define the following functions:
  1. The Size of a binary tree, counting one for each internal node and one for each leaf.
  2. The PasteLeft operation which pastes one binary tree at the leftmost leaf of another, without throwing away the leaf.
Both of these are primitive recursive functions. To make a primitive recursive definition, you need to select New Recursive Definition from the Definitions menu on the Theory Viewer window.

NOTE: The Recursive Definition tool takes a little while to start up the first time you use it, as it needs to search the HOL system for potential primitive recursive datatypes.

The New Recursive Definition Tool

This tool has four components:

 
Figure: Defining Size using Primitive Recursion

Defining Size

Now select btree from the list of types on the right. The cases of the binary tree datatype should be filled in in the Full Specification box.

Now In the Template entry type:

    Size (_:'a btree)
and press Return or the Apply Template button. This indicates we are defining a new constant Size ranging over polymorphic binary trees. We have to specify the 'a type constraint here, otherwise HOL will complain about a type variable being unconstrained. The Full Specification box will be adjusted to match the new template, and should look like:
(Size ((LEAF x):'a btree) = ...) /\
(Size ((NODE b1 b2):'a btree) = ...)
Now edit the specification so it reads:
(Size ((LEAF x):'a btree) = 1) /\
(Size ((NODE b1 b2):'a btree) = 1 + Size b1 + Size b2)
Now press the Execute button. The definition should now be entered in to the HOL system and into the theory. As before, TkHol will ask you if you want the operation added to the script file it is developing. You should press the Yes button here.

Defining PasteLeft

We now want to make a second recursive definition. Edit the template to show

    PasteLeft (_:'a btree) btree2
and press Return or the Apply Template button. Now edit the specification so it reads:
(PasteLeft ((LEAF x):'a btree) b = NODE b (LEAF x)) /\
(PasteLeft ((NODE b1 b2):'a btree) b = NODE (PasteLeft b1 b) b2)
Now press the Execute button as before.



next up previous
Next: Step 6: Doing Up: The TkHol Tutorial Previous: Step 4: Defining



Donald Syme
Fri Sep 1 04:00:07 BST 1995