YAP User's Manual
Total Page:16
File Type:pdf, Size:1020Kb
YAP User's Manual Version 6.2.2 V´ıtorSantos Costa, Lu´ısDamas, Rog´erioReis, and R´uben Azevedo Copyright c 1989-2000 L. Damas, V. Santos Costa and Universidade do Porto. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the con- ditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another lan- guage, under the above conditions for modified versions. i Table of Contents Introduction :::::::::::::::::::::::::::::::::::::::: 1 1 Installing YAP :::::::::::::::::::::::::::::::::: 3 1.1 Tuning the Functionality of YAP ::::::::::::::::::::::::::::::: 3 1.2 Tuning YAP for a Particular Machine and Compiler :::::::::::: 4 1.3 Tuning YAP for GCC. ::::::::::::::::::::::::::::::::::::::::::: 4 1.3.1 Compiling Under Visual C++ ::::::::::::::::::::::::::::: 6 1.3.2 Compiling Under SGI's cc ::::::::::::::::::::::::::::::::: 7 2 Running YAP ::::::::::::::::::::::::::::::::::: 9 2.1 Running YAP Interactively ::::::::::::::::::::::::::::::::::::: 9 2.2 Running Prolog Files:::::::::::::::::::::::::::::::::::::::::: 10 3 Syntax:::::::::::::::::::::::::::::::::::::::::: 13 3.1 Syntax of Terms :::::::::::::::::::::::::::::::::::::::::::::: 13 3.2 Prolog Tokens::::::::::::::::::::::::::::::::::::::::::::::::: 14 3.2.1 Numbers ::::::::::::::::::::::::::::::::::::::::::::::::: 14 3.2.1.1 Integers ::::::::::::::::::::::::::::::::::::::::::::: 14 3.2.1.2 Floating-point Numbers ::::::::::::::::::::::::::::: 15 3.2.2 Character Strings :::::::::::::::::::::::::::::::::::::::: 15 3.2.3 Atoms ::::::::::::::::::::::::::::::::::::::::::::::::::: 16 3.2.4 Variables::::::::::::::::::::::::::::::::::::::::::::::::: 16 3.2.5 Punctuation Tokens :::::::::::::::::::::::::::::::::::::: 17 3.2.6 Layout ::::::::::::::::::::::::::::::::::::::::::::::::::: 17 3.3 Wide Character Support :::::::::::::::::::::::::::::::::::::: 17 3.3.1 Wide character encodings on streams ::::::::::::::::::::: 17 3.3.2 BOM: Byte Order Mark :::::::::::::::::::::::::::::::::: 19 4 Loading Programs ::::::::::::::::::::::::::::: 21 4.1 Program loading and updating :::::::::::::::::::::::::::::::: 21 4.2 Changing the Compiler's Behavior::::::::::::::::::::::::::::: 22 4.3 Conditional Compilation :::::::::::::::::::::::::::::::::::::: 26 4.4 Saving and Loading Prolog States ::::::::::::::::::::::::::::: 27 5 The Module System ::::::::::::::::::::::::::: 29 5.1 Module Concepts ::::::::::::::::::::::::::::::::::::::::::::: 29 5.2 Defining a New Module ::::::::::::::::::::::::::::::::::::::: 30 5.3 Using Modules :::::::::::::::::::::::::::::::::::::::::::::::: 30 5.4 Meta-Predicates in Modules ::::::::::::::::::::::::::::::::::: 31 5.5 Re-Exporting Predicates From Other Modules ::::::::::::::::: 32 ii YAP Prolog User's Manual 6 Built-In Predicates :::::::::::::::::::::::::::: 33 6.1 Control Predicates :::::::::::::::::::::::::::::::::::::::::::: 33 6.2 Handling Undefined Procedures ::::::::::::::::::::::::::::::: 37 6.3 Message Handling ::::::::::::::::::::::::::::::::::::::::::::: 39 6.4 Predicates on terms ::::::::::::::::::::::::::::::::::::::::::: 40 6.5 Predicates on Atoms :::::::::::::::::::::::::::::::::::::::::: 42 6.6 Predicates on Characters :::::::::::::::::::::::::::::::::::::: 44 6.7 Comparing Terms ::::::::::::::::::::::::::::::::::::::::::::: 46 6.8 Arithmetic :::::::::::::::::::::::::::::::::::::::::::::::::::: 47 6.9 I/O Predicates :::::::::::::::::::::::::::::::::::::::::::::::: 53 6.9.1 Handling Streams and Files :::::::::::::::::::::::::::::: 53 6.9.2 Handling Streams and Files :::::::::::::::::::::::::::::: 58 6.9.3 Handling Input/Output of Terms ::::::::::::::::::::::::: 59 6.9.4 Handling Input/Output of Characters :::::::::::::::::::: 65 6.9.5 Input/Output Predicates applied to Streams:::::::::::::: 66 6.9.6 Compatible C-Prolog predicates for Terminal I/O::::::::: 67 6.9.7 Controlling Input/Output :::::::::::::::::::::::::::::::: 68 6.9.8 Using Sockets From YAP ::::::::::::::::::::::::::::::::: 69 6.10 Using the Clausal Data Base ::::::::::::::::::::::::::::::::: 71 6.10.1 Modification of the Data Base ::::::::::::::::::::::::::: 71 6.10.2 Looking at the Data Base ::::::::::::::::::::::::::::::: 73 6.10.3 Using Data Base References ::::::::::::::::::::::::::::: 74 6.11 Internal Data Base ::::::::::::::::::::::::::::::::::::::::::: 75 6.12 The Blackboard:::::::::::::::::::::::::::::::::::::::::::::: 78 6.13 Collecting Solutions to a Goal :::::::::::::::::::::::::::::::: 78 6.14 Grammar Rules :::::::::::::::::::::::::::::::::::::::::::::: 80 6.15 Access to Operating System Functionality :::::::::::::::::::: 81 6.16 Term Modification ::::::::::::::::::::::::::::::::::::::::::: 84 6.17 Global Variables ::::::::::::::::::::::::::::::::::::::::::::: 84 6.18 Profiling Prolog Programs:::::::::::::::::::::::::::::::::::: 87 6.18.1 The Count Profiler :::::::::::::::::::::::::::::::::::::: 87 6.18.2 Tick Profiler :::::::::::::::::::::::::::::::::::::::::::: 88 6.19 Counting Calls ::::::::::::::::::::::::::::::::::::::::::::::: 89 6.20 Arrays ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 90 6.21 Predicate Information :::::::::::::::::::::::::::::::::::::::: 92 6.22 Miscellaneous :::::::::::::::::::::::::::::::::::::::::::::::: 92 7 Library Predicates ::::::::::::::::::::::::::: 107 7.1 Aggregate ::::::::::::::::::::::::::::::::::::::::::::::::::: 107 7.2 Apply Macros:::::::::::::::::::::::::::::::::::::::::::::::: 109 7.3 Association Lists ::::::::::::::::::::::::::::::::::::::::::::: 109 7.4 AVL Trees ::::::::::::::::::::::::::::::::::::::::::::::::::: 110 7.5 Heaps ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 111 7.6 List Manipulation:::::::::::::::::::::::::::::::::::::::::::: 112 7.7 Line Manipulation Utilities::::::::::::::::::::::::::::::::::: 114 7.8 Maplist :::::::::::::::::::::::::::::::::::::::::::::::::::::: 115 7.9 Matrix Library::::::::::::::::::::::::::::::::::::::::::::::: 117 7.10 MATLAB Package Interface::::::::::::::::::::::::::::::::: 120 iii 7.11 Non-Backtrackable Data Structures ::::::::::::::::::::::::: 122 7.12 Ordered Sets:::::::::::::::::::::::::::::::::::::::::::::::: 123 7.13 Pseudo Random Number Integer Generator ::::::::::::::::: 124 7.14 Queues ::::::::::::::::::::::::::::::::::::::::::::::::::::: 125 7.15 Random Number Generator::::::::::::::::::::::::::::::::: 126 7.16 Read Utilities::::::::::::::::::::::::::::::::::::::::::::::: 126 7.17 Red-Black Trees :::::::::::::::::::::::::::::::::::::::::::: 127 7.18 Regular Expressions :::::::::::::::::::::::::::::::::::::::: 129 7.19 SWI-Prolog's shlib library::::::::::::::::::::::::::::::::::: 131 7.20 Splay Trees ::::::::::::::::::::::::::::::::::::::::::::::::: 132 7.21 Reading From and Writing To Strings ::::::::::::::::::::::: 133 7.22 Calling The Operating System from YAP ::::::::::::::::::: 134 7.23 Utilities On Terms :::::::::::::::::::::::::::::::::::::::::: 138 7.24 Trie DataStructure ::::::::::::::::::::::::::::::::::::::::: 139 7.25 Call Cleanup ::::::::::::::::::::::::::::::::::::::::::::::: 140 7.26 Calls With Timeout :::::::::::::::::::::::::::::::::::::::: 142 7.27 Updatable Binary Trees::::::::::::::::::::::::::::::::::::: 142 7.28 Unweighted Graphs ::::::::::::::::::::::::::::::::::::::::: 143 7.29 Directed Graphs :::::::::::::::::::::::::::::::::::::::::::: 146 7.30 Undirected Graphs:::::::::::::::::::::::::::::::::::::::::: 148 7.31 Lambda Expressions :::::::::::::::::::::::::::::::::::::::: 149 7.32 LAM ::::::::::::::::::::::::::::::::::::::::::::::::::::::: 150 7.33 Block Diagram:::::::::::::::::::::::::::::::::::::::::::::: 152 8 SWI-Prolog Emulation::::::::::::::::::::::: 153 8.1 Invoking Predicates on all Members of a List ::::::::::::::::: 154 8.2 Forall :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 154 9 SWI Global variables :::::::::::::::::::::::: 155 9.1 Compatibility of Global Variables :::::::::::::::::::::::::::: 156 10 Extensions to Prolog ::::::::::::::::::::::: 157 10.1 Rational Trees :::::::::::::::::::::::::::::::::::::::::::::: 157 10.2 Co-routining :::::::::::::::::::::::::::::::::::::::::::::::: 157 11 Attributed Variables:::::::::::::::::::::::: 159 11.1 hProlog and SWI-Prolog style Attribute Declarations :::::::: 159 11.2 SICStus Prolog style Attribute Declarations ::::::::::::::::: 162 11.2.1 Attribute Declarations ::::::::::::::::::::::::::::::::: 162 11.2.2 Attribute Manipulation :::::::::::::::::::::::::::::::: 163 11.2.3 Attributed Unification ::::::::::::::::::::::::::::::::: 163 11.2.4 Displaying Attributes :::::::::::::::::::::::::::::::::: 164 11.2.5 Projecting Attributes :::::::::::::::::::::::::::::::::: 164 11.2.6 Attribute Examples :::::::::::::::::::::::::::::::::::: 164 iv YAP Prolog User's Manual 12 Constraint Logic Programming over Reals ::::::::::::::::::::::::::::::::::::::::::::::: 169 12.1 Solver Predicates ::::::::::::::::::::::::::::::::::::::::::: 169 12.2 Syntax of the predicate arguments :::::::::::::::::::::::::: 170 12.3 Use of unification ::::::::::::::::::::::::::::::::::::::::::: 171 12.4 Non-Linear Constraints ::::::::::::::::::::::::::::::::::::: 171 13 CHR: Constraint Handling Rules ::::::::: 173 13.1 Introduction :::::::::::::::::::::::::::::::::::::::::::::::: 173 13.2 Syntax and Semantics::::::::::::::::::::::::::::::::::::::: 173