Docsity

Visual prolog - Beginner Guide, Notas de estudo de Informática

Pré-visualização parcial do texto

Baixe Visual prolog - Beginner Guide e outras Notas de estudo em PDF para Informática, somente na Docsity!

A Beginners Guide

to

Visual Prolog

Version 7.

Thomas W. de Boer

Version of this book: 1.

Preface

There is a beautiful programming language out there. It is called Prolog.

Originally the language Prolog was created by Calmeraur it was popularized by famous books by Clocksin & Mellish and by Ivan Bratko and brought to the masses by software producer Borland who sold Turbo Prolog for MsDos systems. But the masses didn’t buy it and Borland stopped selling Turbo Prolog.

That was long ago. After a few years it became apparent that the makers of Turbo Prolog had returned to Denmark and started the company PDC: the Prolog Development Company. They put years of effort into their dialect of the language and now there is Visual Prolog. It is object-oriented, it has a Graphical User Interface, it has a Integrated Development Environment.

And it still has the charm and elegance of the language they call Prolog.

Formerly the great problem in learning Prolog was that you had to master the very different thinking of a declarative programming language. Once you’ve mastered it, it is a great way to write computer programs, so the effort was and is worthwhile.

But nowadays it is not only mastering a declarative language, there are these other aspects of a modern programming language: object-orientation, graphical user interface, et cetera. This book is an effort to tell you all about it.

This book is an introduction. It is meant for people who know little about programming. It is not for people that know nothing. You should know the basics about computers and that it is possible to program them and that for a program you use a programming language. But nowadays these things seem to bee widely known - even among computer nitwits. What when you know more? Then you should look for other sources.

  • When you are programmer and know about other languages like Visual Basic or C##, read the book Prolog for Tyros by Eduardo Costa.
  • When you know about other languages and are curious about Prolog, read the articles by Thomas Linder Puls and Sabu Francis at the Visual Prolog web site at www.pdc.dk.
  • When you have experience in Prolog programming, look for details about Visual Prolog, object orientation in the advanced tutorials at the same web site. You will find the publications at the website: www.pdc.dk -> Solutions -> Visual Prolog -> Tutorials. At that site you will also find a Wiki on programming in Visual Prolog

There is a lot available about Visual Prolog. But for a beginner it is very hard to find his way through all the material. This book tries to bring structure in the apparent chaos for the beginner. I edited this book by borrowing and rewriting texts from other people (with their consent) and by filling in some gaps with old material that I wrote years ago. It all comes together in this introductory text. I have tried to keep the content as low profile as possible, so even beginners can use it.

But if you think that some things are not understandable, please let me know. You can reach me at [email protected].

Groningen, Autumn 2008. Thomas W. De Boer

Contents

Introduction

This is a book on how to write computer programs in Visual Prolog. Visual Prolog, or VIP for short, is an Object-Oriented Programming Language (OOPL for short) that can be used to create programs that run under MsWindows. This means that the programs that you write should support a GUI, a Graphical User Interface.

Writing this book gives immediately a funny didactic problem: you will need to learn about these three subjects (programming in VIP, programming the GUI and object-oriented programming) at the same time. But it is good practice to write about one thing at a time. I shall try to solve this problem by writing incrementally. I introduce one subject a little, just enough to understand the basics. Then I switch to another subject, again to give you just enough understanding for the chapters that follow.

This sounds rather vague and it is. Let me elaborate on what I am going to do.

  1. I start with explaining what you see when you start Visual Prolog. What you see is the Integrated Development Environment, the IDE. This is the place where you will be programming in Visual Prolog. In the first few chapters you will get to grips with the basics of the IDE. You will do a lot of things that will make you feel like Harry Potter: you enter some magic formulae and suddenly it appears that you wrote a working program.
  2. Then I should like to extend your knowledge to the basics of the Graphical User Interface (GUI) as that is important in programming under MsWindows. But when you program the GUI, it is unavoidable that you will need some knowledge of Prolog. So first I pay attention to explaining the basics of Programming in Prolog.
  3. Then we go back to Visual Prolog as an Object Oriented Programming language. In the next chapters I tell you the basics of OO programming and how it is implemented in Visual Prolog. In this chapter you will find stuff about class declaration, class interface and class implementation.
  4. Different from other Prolog dialects, Visual Prolog is a strongly typed language. In chapter 9 we take a look at the declarative side of VIP.
  5. The last few chapters are on special topics that are important in Prolog like recursion, and lists. Also we take a look at well-known data structures as stacks, queues and trees. Finally there is a chapter on how to decently handle input and output.

By then you are ready to explore Prolog and VIP by yourself. There are a few very good books available, there are several articles on various subjects on the PDC site. Besides, as this book is for the very beginners, by then you are no longer a “beginner”. So it is time to close the book.

I wish you a lot of fun in reading and trying this book. Please let me know when you encounter trouble, I like to improve this book as much as I can. You can reach me at [email protected]. Please indicate in the subject line of your message “Visual Prolog”.

Acknowledgments I have to thank Eduardo Costa, Sabu Francis and Thomas Linder Puls for their generosity to allow me to freely use texts they have written. I extensively used (and maybe abused) their writings. I tried to be precise and I indicate in every chapter where the text is originally from. When I missed something, please let me know. I hope it will be clear that any mistakes are purely mine.

Chapter 1 The Integrated Development Environment^1

In this chapter I introduce the Integrated Development Environment of Visual Prolog. Programming nowadays is not only done by typing code in an editor. Programming has become very complex - that’s why the developers of programming languages offer more than a programming language. They provide you with programming aids. The Integrated Development Environment (IDE for short) is such an aid. When you create a Visual Prolog computer program, you will use the IDE. The purpose of this chapter is to make you feel at home in the Integrated Development Environment (IDE) of Visual Prolog (VIP).

1.1 The Integrated Development Environment IDE

When you start Visual Prolog, what is really started is the Integrated Development Environment, the IDE. It looks more or less like figure 1.1^2

Figure 1.1 The Integrated Development Environment of Visual Prolog

(^1) This chapter is a slightly rewritten version of the first chapter of Prolog For Tyros by Eduardo Costa

(^2) The screenshots are from Visual Prolog Commercial Edition Version 7.

You only have to fill in the Project Name as most of the fields are already filled by the IDE, but it is wise to take a closer look at the content.

  • The first field will contain the project name. For the Project Name I chose “ch01p01". Of course you can choose another name, but I suggest that you use the same name.
  • The next field is the UI Strategy. It indicates what kind of user interface you will use. The IDE suggests that you will use an Object-oriented Graphical User Interface (GUI). It means that you will use the “standard” windows interfacing. That’s OK.
  • The Target Type is the kind of program the IDE should produce. “Exe” is fine, as it means that the IDE wil produce a stand-alone Windows Program. The other option is DLL. If that means nothing to you, forget about it for now.
  • The Base Directory is the folder where you want your programs to reside. The base directory is not the definite folder where this program “ch01p01" will reside; that folder is specified in the next field. The Base Directory is the folder where your program folders are located.
  • In the Sub-Directory you give the name of the folder where this program “ch01p01" should be stored. This folder will be created, if necessary, in the Base Directory. When you typed the name of the project, the IDE assumed that the name of this sub-directory would be the same. But you can change this, if you want. For now we suggest that you keep the names of project and sub-directory the same. By the way, you understand by now that we use the words “folder” and “directory” interchangeably.

There are more project settings. You see them when you click the other Tabs (, et cetera) but in this chapter we only need to fill in the fields in the tab. When you are satisfied with the project settings, press the button and you will get the Project Tree. The Project Tree is an overview of the files of your programming project. It is set up like the directories in Windows Explorer, in fact it shows shows a part of the directory structure on your hard disk.

Figure 1.3 Project Settings

The Project Tree is also called the Project Directory. It shows the set of folders and modules in which the IDE keeps track of your program. When you created the new project ch01p01, a directory was created somewhere on your hard disk that is resembled in the Project Tree. The root name of the Project Tree is “ch01p01". It is the name of the project that you entered in the project settings. It is not the name of the sub-directory that you indicated in the project settings. In fact you don’t see the name of the directory in which the program is stored on your hard disk. The IDE only copes with the name of the project. In this case it is “ch01p01". In the root directory of the Project Tree are two folders, named “$(ProDir)\Lib” and “TaskWindow”. They were created by the IDE, you can skip these for the moment, we will come back to them later. In the project directory there is also a file, named “main.pack". This file contains the basic information about your project.

You should be aware that there is not yet something like a program. To create the program of this project, we use the generating power of the IDE. Based on what you declared about the project, the IDE generates the Prolog code that is necessary. Although you only declared the name of the project and maybe the directory where it should reside, the IDE knows that it is a Windows program so it generates the code to produce a Windows program with a Main Window (that is called the Task Window) and the menu items File, Edit, et cetera that you know from other Windows applications.

So now it is time for step 2: Build, compile and execute the program

Figure 1.4 The Project Tree

Figure 1.5 Build the project

You see that the IDE created a “normal” Windows-looking program with the usual menu-options File, Edit, Window and Help. When you click one of these, nothing happens. That is because you did not specify what should happen. Only a few options really work in this program. The Minimize and Maximize buttons in the upper right corner and the menu option File\Exit. These options work because they are standard options that are the same in every Windows application. For the IDE it was not hard to guess what code you wanted here. One last remark concerns the Messages window. It is typically for Visual Prolog. You don’t have to bother about it yet. By the Way, your program ch01p01 is a separate program from the IDE. When you close ch01p01, the IDE is still open. When you close the IDE, the program ch01p01 is still running.

Congratulations. You made your first Visual Prolog 7 program. Or maybe better said, you arranged things and the IDE of Visual Prolog generated the code for the program so you could execute it. This is the way you will learn to use the IDE. You specify what you want, the code is generated, occasionally you change the code or add something to it. This is the way to create computer programs with an advanced tool like Visual Prolog and its Integrated Development Environment. This way of programming seems excessive and laborious. The reason for it is, that programming in an object oriented programming language environment using a graphic user interface is quite complex. It is very difficult to keep track of every detail. That’s why the IDE does a lot of bookkeeping for you. It makes life easier.

It may be interesting to inspect at this moment the files on your hard disk with Windows Explorer. When you inspect the directory where you put ch01p01, you will see that the IDE created not only the files that you see in the Project Tree, but a lot more. Especially note the two folders “Exe” and “Bin”. In “Exe” you will find the executable files (the programs that you can start independent from VIP) and in “Bin” you will find other files that the IDE needs to generate your program. It gives you some idea of the efforts that are to be made to create a Windows program. Also you can see that your program project consists of several files. This is typical for an Object Oriented Language like Visual Prolog.

Chapter 2 Forms^3

The program you created in chapter 1 is a simple program. You can start it, minimize and maximize the task windows and you can exit it. That’s all. A real program should at least be able to communicate a little bit more with the user. In a Windows environment communication is, among others, done in special windows. There are several kinds of communication windows. The ones most used are called Forms and Dialogs. Because in VIP Forms and Dialogs look and are very much the same, I will not make a difference between them. In this chapter, you will add a form to the empty project that you have created in chapter 1. So please, when project “ch01p01" is not open, open it. To open a project use the option Project/Open from the Tassk Menu or choose Ch01p01 from the Project Overview that is shown when you start Visual Prolog.

2.1 Create a form

A project may become very complex and will consist of a lot of files. Therefore it is wise to think beforehand about the folders in the Project Tree where you want to put the various parts or modules of your program. In this case we will not bother too much about this, we will do that in later chapters. Now we simply place the new form in the root of your project. Go to the Project Tree and highlight “ch01p01" by clicking on it. With “ch01p01" highlighted you are ready to create a form and it will be placed in the root directory of “ch01p01". To create a form, choose the option File/New in Existing Package from the task menu, as in figure 2.1.

The IDE opens a window that offers the possibility to create several project items, see figure 2.2. The name of the window is “Create Project Item” (that is what you want to do) and it is a form with two panes. The left pane shows the items that you can create. Take a moment to review the possible items.

Figure 2.1 File/New in Existing Package

(^3) This chapter is a slightly rewritten version the second chapter of Prolog For Tyros by Eduardo Costa

shown in the window with title “Controls”. You are free to place a control anywhere on a form. To do that, you need a prototype of the new form. That prototype is the window with title “form1". You use this window to design and edit the new form, I call it the edit window. In creating the form you can use the options in the window “Layout” to change the layout, e.g. to justify a group of buttons. Every control in a form has a series of properties. E.g. a button has a place on the form and certain dimensions. The properties of a control are shown in the window “properties”. Try it! Click on the button in the edit window and watch the content of the properties window change. Click anywhere on the form in the edit window (but not on a button!) and you see the general properties of the new form. Change the size of the edit window by pulling a side and watch the dimensions in the properties window change. The four windows work together, but you are free to place them anywhere on your screen.

Let’s take a closer look at the four windows.

The window titled “Controls” (figure 2.3) is in fact a toolbar. It shows icons for every control that can be put on the form. On the top row you see from left to right icons for a button, a check box, a radio button, a static text field and an edit field. When you move the cursor over the icons, little text boxes show their meaning. To add a control to the form, you click the icon in the Controls Toolbar and then click in the forms editor at the place where you want the control to be.

The window titled “Layout” (figure 2.4) is also a toolbar. The icons represent different ways to adjust the layout of the controls on the form. On the top row you see icons for left, center and right justifying. When you select a group of controls on the form and then click one of these icons, the positions of the controls are justified. When you move the cursor over an icon, its meaning is displayed. Especial handy are the icons in the bottom row: they are for equalizing the spacing between controls and for equalizing the dimensions.

The window titled “form1" (figure 2.5) is the form edit window. It is a prototype in which already three controls are put by the IDE. .

Figure 2-3 Controls Toolbar

Figure 2.4 Layout Toolbar

Figure 2.5 Form Edit W indow

The fourth window titled “Properties” contains the properties of the selected item on the form. When there is no item selected, the properties of the form are shown. When you don’t see the properties of the form, click anywhere in the form (but not on the buttons) and the properties will look like they are in figure 2.6. The properties tell you the title that is shown in the title bar, the coordinates X and Y where the form will be placed on the screen, the Width and Height of the form and many more characteristics of this form. Also you can set properties by changing the value True into False. E.g. for (not) showing the title bar, a closebox, et cetera. Feel free to experiment a little with controls and properties, you cannot do much harm. Please note that the name in the Title field is the name that appears in the Title Bar of the form. It is not the name of the form, that is “form1". Feel free to change the title of the form.

The IDE has already placed three buttons (, , ) as these are very common in Windows-windows. When you want to change the size of the form, click and hold the mouse at lower right corner and drag it, as you do when you resize any Windows-window. We call this a prototype as it shows the general characteristics of a form. It has a name on top, room to work in and three often- used buttons. This is the way the IDE works. Whenever you create something in the IDE, the IDE comes up with a kind of prototype that you can use to modify. At this moment the form only contains three buttons. Normally you would enter other controls, but for now we are satisfied with the form as it is, so you can save it by clicking the disk icon on the Task Menu toolbar and close the form prototype by clicking the cross button in the upper right corner of the form prototype. As an alternative you can click the cross button without saving. The IDE will then ask you if you want to save. Please do save the form.

When you now look at the project tree, you see that there is a file added, named “form1.frm”. See figure 2.7. This is where the IDE has put the characteristics of the form you created. When you double click on the name “form1.frm”, the prototype opens again.

Figure 2.6 Properties window

choose the event that the user clicks with the left mouse button on the menu option File/New in your program.

When you ran the empty application in chapter 1, you certainly took notice of the fact that the File/New option was disabled. It was grey, that means in Windows that the option cannot be used. If we want to use it, it must be enabled. To find the place to enable File/New, go to the Project Tree. In the Project Tree you see a folder icon labeled “Taskwindow”. This folder contains everything that is needed to create the Task Window (or Main Window) when you start the program. Please open the folder TaskWindow. You open it by clicking on the plus-sign in front of the name or by double clicking on the name. In the folder you find typical parts of the Task Window of the program, that is also called the Main Window. See figure 2.8. There are several files, and maybe you already see a familiar pattern. E.g. there are four files with the name “AboutDialog”, but with different extensions. The file “AboutDialog.dlg” contains the design of the window that pops up when you click the option Help/About when you run your program. Double click on “AboutDialog.dlg” and the IDE opens a dialog editor that looks very similar to the one that we used to create “form1". The other files contain the Prolog code for the AboutDialog window.

For now we are interested in the menu bar of the program because that is where the option File/New resides. So you click on “TaskMenu.mnu”. This is a kind of prototype of the Task Menu. You can compare it to “form1.frm” that is a prototype of a form. If you want to change something in the main menu, use the file “TaskMenu.mnu”.

You want to enable an option in the menu, so you double click on the name TaskMenu.mnu. The IDE opens the TaskMenu Editor. It is shown in figure 2.10. Here you can enable, disable, append and remove menu options, define short cuts et cetera. As you are interested in the option File/New of the menu, you open the &File option in the lower part of the form by clicking on the plus-sign. Subsequently single click on &New/tF7 to select it. The IDE shows the characteristics of this menu option in the upper part of the form. There you find the checkbox “Disabled”. Uncheck this box and the menu option will be enabled. Save the new menu options and close the TaskMenu Dialog.

Figure 2.9 Editor for the TaskMenu

2.3 In CodeExpert, add code to Project Tree item

The menu option File/New is enabled now. When you run the program you will see that it is no longer grayed. But when you click on it, nothing happens. That is bcause we have not yet added code to the menu option. Now is the time to add the necessary Prolog code to this option. To add code to the File/New option, go back to the project tree and look for the file “TaskWindow.win”. In this file you manage and control the Prolog code that is linked to the various parts of the Task Window. The code is eventually placed in the file “TaskWindow.pro” You could go directly to TaskWindow.pro and enter code. But for now it is better not to do so. Do not place the code directly. It is better to use the part of the IDE that is called the Code Expert. The Code Expert keeps track of the places where code is inserted. When the program grows, this Code Expert will guide you. Trust him.

Click on TaskWindow.win in the Project Tree with the right button of the mouse. That opens a floating menu. In this menu choose the option Code Expert (figure 2.10)

The IDE now opens the file TaskWindow.win in the “Dialog and Window Expert” and you see a list of the many options that you have in the Task Window of your program. Here you can specify many things. You see the Dialog and Window Expert in figure 2.11.

To add code to the menu option File/New , you first have to find it. It is hidden in the folder “Menu”. Open it and you see the subfolder TaskMenu. Open that one too and you see the standard menus that the IDE placed in your program. The options File, Edit and Help are represented here by id_file, id_edit and id_help. Open the id_file folder and single click id_file_new.

Figure 2.10 Open the Code Expert