The Wild LIFE Handbook
Total Page:16
File Type:pdf, Size:1020Kb
0 The Wild LIFE Handbook d i g i t a l (prepublication edition) PARIS RESEARCH LABORATORY March 1994 Hassan AõÈt-Kaci Bruno Dumant Richard Meyer Andreas Podelski Peter Van Roy 0 The Wild LIFE Handbook (prepublication edition) Hassan AõÈt-Kaci Bruno Dumant Richard Meyer Andreas Podelski Peter Van Roy March 1994 c Digital Equipment Corporation 1994 This work may not be copied or reproduced in whole or in part for any commercial purpose. Permission to copy in whole or in part without payment of fee is granted for non-pro®t educational and research purposes provided that all such whole or partial copies include the following: a notice that such copying is by permission of the Paris Research Laboratory of Digital Equipment Centre Technique Europe, in Rueil-Malmaison, France; an acknowledgement of the authors and individual contributors to the work; and all applicable portions of the copyright notice. Copying, reproducing, or republishing for any other purpose shall require a license with payment of fee to the Paris Research Laboratory. All rights reserved. ii Abstract This handbook provides a tutorial of the LIFE programming language as well as a complete description of the capabilities of the Wild LIFE 1.02 system. Although we have attempted to make the tutorial self-contained, it is preferable that the reader be familiar with Prolog. The tutorial exposes gradually the main components of LIFE in a synthetic approach: its original data structureÐ ¡ -termÐand its use in predicates, functions, and sort (type) de®nitions. Along the way, many useful examples are provided and some common pitfalls are discussed and illustrated. Resum e Ce manuel fournit un cours d'initiation au langage de programmation LIFE, ainsi qu'une description deÂtailleÂe des preÂdicats et fonctions preÂde®nis dans l'interpreÁte Wild LIFE 1.02. C'est la premiereÁ fois que nous tentons d'eÂcrire une introduction pratique aÁ la programmation en LIFE. Bien que nous fassions l'effort de n'utiliser que des notions eÂleÂmentaires ou preÂde®nies, il est preÂfeÂrable que le lecteur ait deÂjaÁ une connaissance de base de Prolog. Le cours expose graduellement et de facËon syntheÂtique les principales composantes de LIFE: sa structure de donneÂes originaleÐle ¡ -termeÐet son emploi dans les clauses, les fonctions et les de®nitions de sortes (types). Beaucoup d'exemples utiles sont donneÂs tout au long du manuel et quelques pieÁges courants sont illustreÂs et discuteÂs. iii Keywords Constraint programming, logic programming, functional programming, object-oriented pro- gramming, rapid prototyping, inheritance. Acknowledgements Hak El Chop Braino Demented Ricardo Pastis Schmodelski Petrovsky The Wild LIFE project has been very much a team effort. The low-level X Window interface was implemented by Jean-Claude Herve. The graphical interface toolkit and accumulator preprocessor were implemented by Bruno Dumant. We thank the other, past and present, members and visitors of the Paradise project at PRL and its follow-on, the Proteus project, for their suffering along with the ®nal debugging of the design and delivery of Wild LIFE. We appreciate their active and continuing participation giving many examples, bug reports, and constructive criticisms. For the ®nal haul on version 1.00 we especially thank Arnaud Venet. iv Contents 1 Introduction 1 2 Road map 1 3 Running Wild LIFE 3 3.1 Getting started ¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 3 3.2 Input syntax ¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 3 3.3 Incremental query extension ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 4 3.4 Loading ®les ¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 6 3.5 Interrupting execution ¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢ 6 4 The basic data structure: ¦ -terms 7 4.1 Sorts ¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢ 8 4.1.1 De®ning sort inheritance ¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢ 8 4.1.2 Built-in sorts ¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 9 4.1.3 Greatest lower bound (glb) ¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 10 4.2 Attributes ¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 11 4.3 Variables and tags ¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 11 4.4 Uni®cation ¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 12 4.4.1 A step-by-step comparison with Prolog uni®cation ¢£¢£¢¤¢£¢¥¢£¢£¢¤¢ 13 5 Predicates 14 5.1 De®ning predicates ¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 14 5.2 Executing predicates ¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢ 14 5.3 Pruning the search tree with cut ¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 15 5.3.1 The scope of cut ¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 15 5.3.2 Disjunctive terms and cut ¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢ 16 5.3.3 Negation-as-failure ¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢ 16 6 Functions 17 6.1 De®ning functions ¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 17 6.2 Executing functions ¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢ 19 6.3 Matching ¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 21 6.4 Currying ¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢ 22 6.5 Quote and eval ¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 26 6.6 Choosing between matching and uni®cation ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 27 7 Constrained sorts 29 7.1 De®ning constrained sorts ¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢ 29 7.1.1 Sort attributes ¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 29 7.1.2 Constrained sorts ¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 30 7.2 Executing constrained sorts ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 31 7.3 Constrained sorts as daemons ¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 33 v 8 Basic built-in routines 33 8.1 Control ¯ow ¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 34 8.2 Ψ-term manipulation ¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 38 8.3 Arithmetic ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 41 8.3.1 Arithmetic calculation ¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 41 8.3.2 Arithmetic comparison ¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 42 8.3.3 Boolean arithmetic ¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 43 8.4 Sorts ¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 43 8.4.1 Sort calculation ¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 43 8.4.2 Sort comparison ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 45 8.5 Strings ¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 46 8.5.1 String calculation ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 46 8.5.2 String comparison ¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 47 8.6 Type checking ¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 47 8.7 Input/output ¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 47 8.7.1 Reading ¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 48 8.7.2 Writing ¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 49 8.7.3 Parsing a string ¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 52 8.7.4 Operator declarations ¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 53 8.7.5 Files and streams ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 54 8.8 System-related built-ins ¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 55 8.8.1 The Wild LIFE system ¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 55 8.8.2 The Unix system ¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 57 8.8.3 Timekeeping ¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 58 8.9 Loading ®les with term expansion ¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 58 8.9.1 De®ning term expansion clauses ¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 59 8.9.2 Using term expansion when loading ®les ¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 59 9 Global variables, persistent terms, and destructive assignment 60 9.1 Global variables ¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 60 9.2 Persistent terms ¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 61 9.3 Persistent variables ¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 61 9.4 Destructive assignment ¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢ 62 9.4.1 Backtrackable destructive assignment ¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 62 9.4.2 Nonbacktrackable destructive assignment ¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 62 9.5 Quoting ¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢ 65 9.6 Summary of built-ins ¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 65 10 Modules 66 10.1 Standard modules ¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 67 10.2 Using features ¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 67 10.3 Overloading ¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢ 68 10.4 Summary of built-ins ¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¥¢ 69 vi 11 Rule-base management 71 11.1 Adding rules ¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 71 11.2 Deleting rules ¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¤¢£¢£¢¥¢ 72 11.3 Inspecting rules ¢¤¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 73 11.4 Function de®nitions ¢£¢¥¢£¢£¢¥¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¤¢ 74 11.5 Summary of built-ins ¢£¢¥¢£¢£¢¤¢£¢¥¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢£¢¤¢£¢£¢¥¢£¢¥¢£¢£¢¤¢ 74 12 Example programs and programming techniques