Visual Prolog Classes
Total Page:16
File Type:pdf, Size:1020Kb
Visual Prolog Version 5.x Language Tutorial (c) Copyright 1986-2001 Prolog Development Center A/S H.J. Holst Vej 3-5C, DK - 2605 Broendby, Copenhagen Denmark Copyright The documentation for this software is copyrighted, and all rights are reserved. It may not be reproduced, transmitted, stored in a retrieval system, or translated, either by electronic, mechanical or any other means, without the prior written consent of Prolog Development Center A/S. The software products described in these manuals are also copyrighted, and are licensed to the End User only for use in accordance with the End User License Agreement, which is printed on the diskette packaging. The prospective user should read this agreement carefully prior to use of the software. Visual Prolog is a registered trademark of Prolog Development Center A/S. Other brand and product names are trademarks or registered trademarks of their respective holders. Table of Contents Part 1 Introduction to Visual Prolog Chapter 1, Using Visual Prolog's Visual Development Environment What Needs to be Installed for This Book? ....................................................... 6 Starting the Visual Prolog's Visual Development Environment........................ 7 Creating the TestGoal Project for Running the Examples................................. 8 Opening an Editor Window ............................................................................. 11 Running and Testing a Program....................................................................... 11 Testing Language Tutorial Examples .............................................................. 12 Testing Examples in Test Goal .................................................................... 13 Remarks About Properties of the Test Goal Utility............................ 13 Testing Examples as Standalone Executables.............................................. 14 Handling Errors................................................................................................ 15 Part 2 Tutorial Chapters 2 – 11: Learning Visual Prolog Chapter 2, Prolog Fundamentals PROgramming in LOGic ................................................................................. 18 Sentences: Facts and Rules .......................................................................... 19 Facts: What Is Known ........................................................................ 19 Rules: What You Can Infer from Given Facts ................................... 20 Queries......................................................................................................... 21 Putting Facts, Rules, and Queries Together........................................ 22 Variables: General Sentences....................................................................... 24 Overview...................................................................................................... 25 Exercises............................................................................................. 26 From Natural Language to Prolog Programs ................................................... 26 Clauses (Facts and Rules) ............................................................................ 26 More About Facts............................................................................... 26 More About Rules .............................................................................. 27 Predicates (Relations) .................................................................................. 30 Variables (General Clauses) ........................................................................ 31 How Variables Get Their Values........................................................ 32 Anonymous Variables ........................................................................ 33 Goals (Queries) ............................................................................................ 35 Compound Goals: Conjunctions and Disjunctions............................. 36 Comments .................................................................................................... 38 What Is a Match? ............................................................................................. 39 Summary.......................................................................................................... 40 i Chapter 3, Visual Prolog Programs Visual Prolog's Basic Program Sections.......................................................... 43 The Clauses Section..................................................................................... 44 The Predicates Section................................................................................. 44 How to Declare User-Defined Predicates........................................... 45 The Domains Section................................................................................... 47 The Goal Section ......................................................................................... 51 A Closer Look at Declarations and Rules........................................................ 51 Typing Arguments in Predicate Declarations..................................... 55 Multiple Arity..................................................................................... 58 Rule Syntax ........................................................................................ 59 Automatic Type Conversions............................................................. 60 Other Program Sections................................................................................... 60 The Facts Section......................................................................................... 61 The Constants Section ................................................................................. 61 The Global Sections..................................................................................... 63 The Compiler Directives.............................................................................. 63 The include Directive ......................................................................... 63 Summary.......................................................................................................... 64 Chapter 4, Unification and Backtracking Matching Things Up: Unification.................................................................... 67 Backtracking .................................................................................................... 70 Visual Prolog's Relentless Search for Solutions .......................................... 73 A Detailed Look at Backtracking ....................................................... 76 Backtracking in Standalone Executables............................................ 80 Controlling the Search for Solutions............................................................ 85 Using the fail Predicate................................................................................ 85 Preventing Backtracking: The Cut............................................................... 87 How to Use the Cut ............................................................................ 88 Determinism and the Cut.................................................................... 91 The not Predicate................................................................................ 92 Prolog from a Procedural Perspective.............................................................. 97 How Rules and Facts Are Like Procedures ................................................. 97 Using Rules Like Case Statements..................................................... 98 Performing Tests within the Rule....................................................... 99 The cut as a GoTo............................................................................... 99 Returning Computed Values ............................................................ 101 Summary........................................................................................................ 103 Chapter 5, Simple and Compound Objects Simple Data Objects ...................................................................................... 105 Variables as Data Objects .......................................................................... 105 Constants as Data Objects.......................................................................... 105 ii Characters......................................................................................... 106 Numbers ........................................................................................... 106 Atoms ............................................................................................... 107 Compound Data Objects and Functors .......................................................... 108 Unification of Compound Objects ............................................................. 109 Using the Equal Sign to Unify Compound Objects.......................... 109 Treating Several Items as One ................................................................... 110 An Example Using Compound Objects............................................ 111 Declaring Domains of Compound Objects ................................................ 115 Writing Domain Declarations: a Summary ...................................... 117 Multi-Level Compound Objects....................................................... 118 Compound Mixed-Domain Declarations................................................... 120 Multiple-Type Arguments ...............................................................