Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Turbo_Prolog_Owners_Handbook_1987

.pdf
Скачиваний:
2
Добавлен:
28.06.2024
Размер:
8.44 Mб
Скачать

Turbo Prolog

Owner's Handbook

Copyright 1986 by

Borland International, Inc.

4585 Scotts Valley Drive Scotts Valley. CA 95066 USA

Table ofContents

Introduction

 

I

How to Use This Book

I

The Distribution Disks

2

Minimum System Requirements

2

Acknowledgments . . . . .

2

Chapter I

About Prolog

3

What Can Turbo Prolog be Used For?

.4

How Does Turbo Prolog Differ From Other Languages?

.4

Chapter 2 A Short Introduction to the Turbo Prolog System

.7

The Main Menu . . . . . . . . . . .

. 7

Entering Your First Turbo Prolog Program

. 8

Editor Survival Kit .

II

Basic Operation .

II

Block Operations

II

Search and Replace

12

Tracing .....

14

Altering the Default Window Setup

15

Temporary Changes to Windows

15

Saving a Window Layout

15

Chapter 3 Tutorial I: Five Simple Programs

17

The Structure of A Turbo Prolog Program

17

Variables

.....

19

Objects and Relations

20

Domains and Predicates

20

Compound Goals

22

Anonymous Variables

22

Finding Solutions in Compound Goals-Backtracking

23

Turbo Prolog the Matchmaker: Using Not

24

Comments .......... .

27

A More Substantial Program Example

27

Summary

.......... .

28

Chapter 4

Tutorial II: A Closer Look at Domains, Objects and Lists

33

Free and Bound Variables . . . . . . . . .

33

Turbo Prolog's Standard Domain Types

34

Compound Objects Can Simplify Your Clauses!

38

Domain Declaration of Compound Objects

38

Going Down a Level

 

 

40

Recursion .............

42

Recursive Objects . . . . . . . . .

44

The Fascinating Worlds of Lists and Recursion

45

Using Lists . . . . . . .

 

46

List Membership

......

47

Writing Elements of a List . . .

48

Appending One List to Another:

 

Declarative and Procedural Programming

48

One Predicate Can Have Several Applications

49

Chapter 5

Tutorial III: Turbo Prolog'sRelentless Search for Solutions

51

Matching Things Up: The Unification of Terms

51

Controlling the Search for Solutions . . . .

54

Use of Fail . . . . . . . . . . . . .

57

Preventing Backtracking: The Cut Element

58

Using the Cut to Prevent Backtracking to a

 

Previous Subgoal in a Rule

.....

58

Using the Cut to Prevent Backtracking to the Next Clause

59

Determinism and the Cut . . . . . . . . . .

60

Chapter 6

Tutorial IV:

 

 

 

Arithmetic, Simple Input and Output, and Debugging

63

Prolog can do Arithmetic Too! . . . . . . . . .

63

The Order of Evaluation of Arithmetic Expressions

64

Comparisons . . . . . . . .

64

Special Conditions for Equality

 

66

Arithmetic Functions and Predicates

68

Simple Input and Output

 

 

69

Writing .....

 

 

69

Reading .... . .

 

72

Debugging and Tracing

 

 

74

Some Predicates are Special

 

75

An Exercise in Tracing

 

 

75

Chapter 7

Tutorial V: Seeing Through Turbo Prolog'sWindows

77

Setting the Screen Display Attributes

77

Windows in Your Programs . . .

78

Read and Write With Windows

 

80

Screen-Based Input and Output

 

82

A Simple Arcade Game . . . . .

83

A Word Guessing Game Using Windows

86

A Window To DOS

 

 

87

Date and Ti me .........

88

ii

Turbo Prolog Owner's Handbook

Chapter 8 Tutorial VI: Graphics and Sound

91

Turbo Prolog's Graphics

91

Turtle Graphics Commands

93

Let's Hear Turbo Prolog

96

Chapter 9

Tutorial VII: Files and Strings

99

The Turbo Prolog File System

99

String Processing ........ .

104

Type Conversion Standard Predicates

106

Findall and Random ....... .

107

Chapter 10

Tutorial VIII: Spreading Your Wings

109

Building A Small Expert System . . . .

09

Prototyping: A Simple Routing Problem

12

Adventures in a Dangerous Cave

14

Hardware Simulation . . . . .

16

Towers of Hanoi

..... .

17

Division of Words Into Syllables

18

The N Queens Problem

21

Using The Keyboard . . . . .

24

Chapter I I

Programmer'sGuide

27

An Overview of the Turbo Prolog System

27

Basic Language Elements

28

Names

.....

28

Reserved Names

29

Restricted Names

29

Program Sections

 

29

Domain Declarations

30

Shortening Domains Declarations

31

Predicate Declarations

32

Clauses .....

32

Simple Constants

32

Variables

 

33

Compound Terms or Structures

33

Turbo Prolog Memory Management

34

Compiler Directives

 

35

check_cmpio

 

35

check_determ

 

36

code

 

 

36

diagnostics

 

36

include

 

 

37

nobreak

 

 

38

nowarnings

 

38

project

 

 

38

trace and shorttrace

38

trail . . . . . . .

39

Table of Contents

iii

Dynamic Databases in Turbo Prolog

 

140

Declaration of the Database

 

140

Handling Facts ...... .

 

141

Extending the Database onto Files

 

142

Control of Input and Output Parameters: Flow Patterns

144

Programmi ng Style . . . . . . . . . . . . . . .

145

Stack Considerations and Eliminating Tail Recursion

145

Use of the Fail Predicate .......... .

148

Determinism, Non-determinism and How to Set the Cut

149

Domains Containing References ....

149

Generating Executable Stand-Alone Programs

151

Modular Programming ...... .

 

152

Projects . . . . . . . . . . . .

 

152

Global Domains and Global Predicates

 

153

Compiling and Linking the Modules

 

154

An Example ......... .

 

154

Interfacing Procedures Written in Other Languages

155

Declaring External Predicates

 

156

Calling Conventions and Parameters

... .

56

Naming Conventions . . . . ..

... .

56

An Assembler Routine Called from Turbo Prolog

57

Calling C. Pascal and FORTRAN Procedures from Turbo Prolog

59

Low-Level Support .............. .

59

Accessing the Editor From Within a Turbo Prolog Program

61

edit ..

 

 

61

display ......... .

 

61

editmsg ......... .

 

61

Directory and Formatting Facilities

 

62

dir

....... .

 

62

writef . . . . . . . . .

 

62

Chapter 12

Reference Guide

 

63

Files on the Distribution Disk

 

63

Files Needed When Using Turbo Prolog

 

64

Installation . . . . .

 

64

The Main Menu

 

164

The Run Command

 

164

The Compile Command

 

165

The Options Menu

 

166

The Edit Command

 

166

The Files Menu

 

166

Load

 

 

166

Save

 

 

167

Directory

 

167

File Name

 

167

Zap File in Editor

 

168

Print .....

 

168

iv

Turbo Prolog Owner's Handbook

Erase .....

68

Rename

68

Operating System

68

Setup Menu

68

Defining Directories

68

Librarian . . . .

69

Window Definition

69

Color Setting . .

70

Miscellaneous . .

71

Load Configuration

71

Save Configuration

72

Quit Command . .

72

The Turbo Prolog Editor

72

Cursor Movement Commands

73

Insert and Delete Commands

74

Block Commands .....

75

Miscellaneous Commands . .

76

The Calculation of Screen Attributes

77

Monochrome Display Adapter

78

Color/Graphics Adapter

78

Arithmetic Functions and Predicates

79

Classified Index of Standard Predicates

79

Alphabetical Directory of Standard Predicates

81

asserta

82

assertz

82

attribute

82

back

82

beep

82

bios

82

bound

83

char_int

83

clearwindow

83

closefile

83

consult

83

cursor .

83

cursorform

83

date

84

deletefile

84

dir

84

disk .

84

display

84

dot .

84

edit .

84

editmsg

85

eof ..

85

existfile

85

exit ..

85

Table of Contents

v

fail

85

field_attr

85

field_str

85

filepos

86

file_str

86

findall

86

flush

87

forward

87

free . .

87

frontchar

87

frontstr

87

fronttoken

87

graphics

88

is name

88

left

88

line ..

88

makewindow

88

membyte

89

memword

89

nl

89

not ...

89

openappend

89

openmodify

89

openread

90

openwrite

90

pencolor

90

pendown

90

penup .

90

portbyte

90

ptr_dword

90

readchar .

91

read device

91

readint

91

readln .

91

readreal

91

readterm

91

removewindow

91

renamefile

91

retract

92

right

92

save

92

scr_attr

92

scr_char

92

shiftwindow

92

sound .

92

storage

93

str_char

93

vi

Turbo Prolog Owner's Handbook

str_int

 

193

str_len

 

193

str_real

 

193

system

 

194

text .

 

194

time

 

194

trace

 

194

upper_lower

194

window_attr

194

window_str

 

195

write

 

195

writedevice

 

195

writef . .

 

195

BNF Syntax for Turbo Prolog

196

Names

 

196

Program Section

197

Directives

 

197

Domains Section

197

Predicate and Database Section

198

Clause Section

198

Goal Section

199

Terms ..

199

Comparisons

199

Compiler Directives

196

System Limits . . .

· 200

Appendix A

ASCII Character Codes

.201

Appendix B

Error Messages

· 205

Appendix C

PLINK

 

Use of the File PLlNK.BAT Contents of the File PLlNK.BAT

Appendix D PROLOG.SYS

Appendix E Using Turbo Prolog with Turbo Pascal Appendix F Glossary . . . . . . . . . . . .

·209

·210

·211

·213

·215

List of Figures

2-1

The Logon Display .......... .

· 7

2-2

The Main Menu and the Four System Windows

· 8

2-3

Using the Editor

· 9

2-4

Executing a Program

10

3-1

Backtracking...

25

4-1

Evaluation of Factorial( 4,Answer)

44

10-1

Prototype Map . . . . .

113

10-2

Fundamental XOR Circuit

116

10-3

The Towers of Hanoi

117

10-4

The N Queens Chessboard

121

I I-I

Memory Partitioning in Turbo Prolog

134

Table of Contents

vii