next up previous
Next: Step 5: Making Up: The TkHol Tutorial Previous: Step 3: Creating

Step 4: Defining a Recursive Type of Binary Trees

 

We are now ready to introduce the notion of binary trees into the system. We do this by defining a new recursive type. These recursive types are very much like the concrete data types found in Standard ML and other functional languages.

Specifying the type

To define a new recursive type, go to the Types menu and select the New Recursive Type... option. A new window will now appear. At the top is a window for entering the grammar of a type. The following grammar is automatically entered for you (as an example):

btree = LEAF of 'tag
      | NODE of 'tag => btree => btree
By curious coincidence, this happens to be exactly the type we want to define for the purposes of this tutorial.

 
Figure: Defining a New Primitve Recursive Type

The type specification says that elements of the new type are either ß LEAFs or ß NODEs. The symbol 'tag is a type variable, indicating that the type is polymorphic over a type being used to tag leaf nodes. The (rather strange) syntax

        NODE of btree => btree}
indicates that if a binary tree is of kind ß NODE, then it contains two other binary trees.

To the bottom left are a series of options regarding extra facts we may want to automatically prove about this type. They should all be selected by default - there should be no reason to change this. For more information on the different theorems that get proved, see the HOL User Manual, or view the theorems after the end of this session.

Defining the type

You are now ready to proceed. Hit the Execute button. A second dialog box should now appear. This will be asking you for parsing and pretty-printing information about the constructors ß NODE and ß LEAF. We want both to have ``prefix'' status, so just press Ok here.

TkHol will now proceed to define the type and prove important theorems about it. It will let you know what it is doing in the feedback line at the bottom of the window. After the type is successfully defined, TkHol will ask you if you want to add the operation to your script file. You should say Yes to this. You will then be asked if you want to define another recursive type. As we have now introduced our recursive type, answer No to this.

Viewing the results

Your type has now been defined, but where is it? Go to the theory viewer window, select the btree theory, and open the Theorems window by clicking on the triangle next to the label ``Theorems:''. You will see the following list of theorems:

btree_11
btree_Axiom
btree_Induct
btree_cases
btree_distinct
These are the theorems that the recursive types package has proved automatically for you. Select these theorems with the left mouse button, and they will be displayed in the window next door. take the time to understand btree_11 and btree_cases - they are the easiest to comprehend. In general you do not need to understand the content of the induction theorem or the axiom, but you should feel confidet that they do, in fact, characterise the type of labelled binary trees.

You should also reexamine the script browser for the btree theory. As you requested, the operation of defining the new recursive type will have been added to the script file. This has been saved to disk, as the premanent record of your work.



next up previous
Next: Step 5: Making Up: The TkHol Tutorial Previous: Step 3: Creating



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