PROLOG PROgramming in LOGic

Size: px
Start display at page:

Download "PROLOG PROgramming in LOGic"

Transcription

1 PROLOG PROgramming in LOGic 1 Knowledge-Based Information Systems (Relational) database systems are very efficient in the handling of data. Information is data together with a suitable interpretation. Together with data (facts), knowledge-based IS store interpretation rules. The information system can deduce additional data based on these rules. To express facts and rules, we use logic: Propositional Logic 1 : Propositions with a truth value and boolean operators. Predicate Logic 2 : Extends the propositional logic with variables, quantors, and functions to describe rules. 1 Aussagenlogik 2 Prädikatenlogik c 2004 Jens Teubner, André Seifert, University of Konstanz 1

2 2 Implementation Idea: 1 Feed the information system with facts and rules. Use logic to describe them. 2 Ask questions concerning that knowledge: Can be yes or no questions, or the search for all variable bindings for which a given expression evaluates to true. PROLOG is an implementation of such a system. Prolog is not actually designed to work with large datasets. DATALOG is a variant to handle them. c 2004 Jens Teubner, André Seifert, University of Konstanz 2

3 Example: ➀ Feed facts:?- [user]. : cow(betty). : animal(x) :- cow(x). % betty is a cow (fact) % rule: cows are animals ➁ Pose queries:?- cow(betty). % is betty a cow? (query) Yes?- animal(betty). % is betty an animal? (query) Yes?- animal(x). % give me all animals X = betty ; % betty is a valid binding No % there are no more bindings c 2004 Jens Teubner, André Seifert, University of Konstanz 3

4 3 The Prolog Interpreter In the computer lab, the SWI-Prolog interpreter from the University of Amsterdam is installed. You can start it from the Linux command line: $ pl Welcome to SWI-Prolog (Version 5.0.1) Copyright (c) University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit for details. For help, use?- help(topic). or?- apropos(word).?- You can exit the Prolog interpreter with ^D (Control-D) or by entering halt., followed by the enter key. c 2004 Jens Teubner, André Seifert, University of Konstanz 4

5 3.1 Finding Help in Prolog Prolog has an extensive online help utility. You can access it with the predicate help:?- help(consult). consult(+file) Read File as a Prolog source file. File may be a list of files, in which case all members are consulted in turn. [...] If you want to use Prolog at home, go to You can find sources or binaries for Linux, MacOS X and Windows. I can t give you support for this, however. c 2004 Jens Teubner, André Seifert, University of Konstanz 5

6 3.2 The Prolog Prompt Prolog can be in either one of the two operating modes query mode (Prompt?- or ) You can ask questions interactively in this mode. Prolog tries to give you an instance for the variables you specified that match your question or answers with Yes or No. consult mode (Prompt :) You can teach Prolog facts and rules in this mode. Rules are specified as Horn Clauses. After startup, Prolog is in query mode. c 2004 Jens Teubner, André Seifert, University of Konstanz 6

7 3.2.1 Consult Mode Prolog is in consult mode while reading Prolog source files.?- [foo]. reads the file foo.pl in the current working directory. There is a special source file user. If you advise Prolog to read this file, the file is not read from your hard disk, but you can enter facts and rules on the Prolog prompt.?- [user]. : nice(weather). : ^D % user compiled 0.00 sec, 304 bytes Yes?- c 2004 Jens Teubner, André Seifert, University of Konstanz 7

8 4 Propositional Logic in Prolog 4.1 Facts Facts are represented in Prolog by identifiers that start with a lowercase letter, e. g. x. it_is_winter. i_like_prolog. Facts can be structured: is_bad(weather). owns(john, computer). research_group(professor(scholl), researchers(grust, seifert, teubner)). Don t put a space in front of the (. The structure can be arbitrarily deep. Predicates of this type can be interpreted as relations. Note that relations are distinguished by their name and their arity; relations with the same name but different number of parameters are completely distinct. In Prolog the closed world assumption holds: All facts you enter are true, everything else is considered false. c 2004 Jens Teubner, André Seifert, University of Konstanz 8

9 4.2 Rules Rules are facts that only hold under a certain prerequisite. In Prolog they are specified as h(x) :- g(x,y). which corresponds to the horn clause g(x,y) h(x) g(x,y) h(x) All variables are implicitly quantified and quantifiers are omitted. Examples: z :- x. it_is_cold :- it_is_winter. is_nice(weather) :- not(is_bad(weather)). go_hiking :- is_nice(weather), have_time. will_lose(stuttgart) :- is_ill(hildebrand). Conjunctions are written as, in Prolog. Disjunctions are written as ;. c 2004 Jens Teubner, André Seifert, University of Konstanz 9

10 4.3 Queries After providing Prolog with a fact base, you can formulate queries on the knowledge represented by the given facts and rules. A query is formulated the same way as a fact. The system answers either Yes or No, depending on whether the query statement can be reduced to the facts and rules in the fact base. Prolog uses resolution to make its decision.?- it_is_cold. Yes.?- is_nice(weather). No. c 2004 Jens Teubner, André Seifert, University of Konstanz 10

11 4.4 Data Types Prolog does not employ data types in the usual way. We may rather speak about Prolog lexical elements or data objects instead of data types. We can differentiate between two types of data objects: Simple data objects Atoms, Numbers, and Variables. Complex or constructed data objects Terms and Lists. c 2004 Jens Teubner, André Seifert, University of Konstanz 11

12 4.4.1 Atoms Syntax: An atom is a sequence consisting of letters, numbers and underscores, which begins with a lower-case letter (e.g., ab3_, _5C). Some sequences of special characters are already defined (e.g., :-, +,... ). Strings are usually written as a sequence of characters surrounded by quotes (e.g., PrettyWoman ). They are often internally represented as lists of ASCII codes Numbers Many Prolog implementations don t distinguish integers (e.g., 3, -15) from real numbers (e.g., ). SWI Prolog, however, distinguishes between both types. c 2004 Jens Teubner, André Seifert, University of Konstanz 12

13 4.4.3 Variables Syntax: Variables are denoted by a string consisting of letters, numbers and underscore characters, and begin with an upper-case letter (e.g., X). Variables: are not typed, are not declared, are bound (i.e., have a value) either explicitly via matching or implicitly via goal matching or unbound. Variables used only once in a clause do not need a name. Unnamed variables are called anonymous variables. Syntax: An anonymous variable is written as a single underscore ( ). c 2004 Jens Teubner, André Seifert, University of Konstanz 13

14 Values are not reported for anonymous variables appearing in queries.?- [user]. : weather( , cold). Yes?- weather(a,_). A = ; No? Variables in Queries Parts of a query can be replaced by a variable. The interpreter tries to find variable bindings that make the query statement true and prints them to the screen.?- animal(x). X = betty c 2004 Jens Teubner, André Seifert, University of Konstanz 14

15 By typing a semicolon (;) you can ask the system for more solutions. If no more are found, the system prints No. In queries the anonymous variable ( ) only asks for existence of a solution. If you use the anonymous variable more than once, they are distinct variables Variables in Rules Variables can be necessary in facts and rules to state correlations, either within a fact or between left and right side of the :-: : equal(x,x). : animal(x) :- cow(x). : sister(x,y) :- father(x,father), father(y,father), female(x), female(y). : likes(_, mary). c 2004 Jens Teubner, André Seifert, University of Konstanz 15

16 The anonymous variable can be used as an every or don t care variable.?- [user]. : nice(weather(monday)). : not_nice(weather(tuesday)). : go_hiking(day) :- nice(weather(day)). : ^D % user compiled 0.00 sec, 828 bytes Yes?- go_hiking(monday). Yes?- go_hiking(tuesday). No? Terms Terms are the only way Prolog can represent complex data. A term consists of a head, also called functor and parameters (e.g., parentof(hans, michael), orphan(bernd)). c 2004 Jens Teubner, André Seifert, University of Konstanz 16

17 The functor must be an atom and parameters can be structures (e.g. person(name(teubner, jens), ). The number of parameters, so called arity of term, is significant. A term is identified by its head and arity, usually written as functor/arity (e.g., parentof/2). Terms can be pictured as trees. person name jens.teubner@uni-konstanz.de teubner jens c 2004 Jens Teubner, André Seifert, University of Konstanz 17

18 4.4.5 Lists A list is a collection in which the elements come in order there is a first, second, third element, etc., repetition is allowed, i.e., the same element can occur more than once. Syntax: Lists are enclosed in square brackets ([]) and entries are separated by comma which can be any Prolog structure whatsoever. An empty list is denoted by []. Every non-empty list has a head H and a tail T. H is the first element of the list and may or may not be a list. T is what s left from the list after H has been removed and T is always a list. c 2004 Jens Teubner, André Seifert, University of Konstanz 18

19 List Head Tail Number of List Elements [1,2,3] 1 [2,3] 3 [1,[2,3]] 1 [[2,3]] 2 [[[]]] [[]] [] 1 [[[1,2],[3],2,3]] [[1,2],[3],2,3] [] 1 Lists are recursive constructions. The list [1,2,3] is internally represented as.(1,.(2,.(3,[]))). A syntactic shortcut is [H T], which is mostly used to construct rules. Lists can be processed by processing the first element, and then the rest of the list in a recursive manner Useful List Predicates The goal member(x,l) succeeds if X is one of the elements of the list L. c 2004 Jens Teubner, André Seifert, University of Konstanz 19

20 ?- member(x,[x _]). % X is a member if it is the head X = _G154 ; X = _G154 ;... : member(x,[_,t] :- member(x,t). % X is a member if it is in the tail.?- member(x,[_ T]). X = _G154 T = [_G154 _G226] ; X = _G154 T = [_G225, _G154 _G229];... The goal append(l1, L2, L3) succeeds if the list L3 is constructed from lists L1 and L2 by joining them together with L1 first and L2 second.?- append([1,2],[2,3],x). X = [1, 2, 2, 3]; No?- c 2004 Jens Teubner, André Seifert, University of Konstanz 20

21 The goal reverse(l1,l2) reverses the order of the elements in list L1 and appends the result upon the list L2. : reverse([],[]). : reverse([h T], RevList) :- reverse(t, RevTail), append(revtail, [H], RevList). The first fact says what happens to []. The second clause handles non-empty lists and the body is a conjunction with: the first literal being a recursive call using the original list s tail and the second literal being a goal with constructs the final answer from H and the result of the reversed tail of the original list.?- reverse([1,2,3],a). A = [3, 2, 1] c 2004 Jens Teubner, André Seifert, University of Konstanz 21

22 4.4.6 Matching Most important operation on data objects is matching. Matching is invoked in two different ways: Explicitly via the built-in predicate = (e.g., X = 10). Implicitly when Prolog tries to match a goal against the head of a clause in the knowledge base (e.g. matching the query parentof(x,jens) against the knowledge base parentof(bernd,jens), parentof(sabine,jens) succeeds since X can be bound to bernd or sabine). When studying matching in general, i.e., Prolog tries to unify term1 = term2, the following cases can be differentiated: If term1 and term2 are atoms or number values then they will match when they are of the same atom or same number and type of number. c 2004 Jens Teubner, André Seifert, University of Konstanz 22

23 ?- 34 = 34. % 34 matches 34 Yes?- 34 = 34.0 % 34 does not match 34.0 No?- jens = jens. % jens matches jens Yes?- jens = jens. % jens does not match jens No If term1 is some arbitrary term (e.g., atom, number, functor, etc.) and term2 is an unbound variable then term2 will become bound to that value.?- X = parentof(bernd,jens). X = parentof(bernd, jens) If term1 and term2 are variables and both variables are unbound then they share their eventual binding.?- X = Y. X = _G154 Y = _G154 c 2004 Jens Teubner, André Seifert, University of Konstanz 23

24 If term1 and term2 are variables and one of them is bound then the other unbound variable will be bound to the term of the bound variable.?- X = Y, X = X = 4711 Y = 4711 If term1 and term2 are structures then they will match when they have the same functor name, the same arity, and each of the arguments match.?- abc(a,d,e(x,y)) = abc(d,d,e(f,g)). A = d X = f Y = g?- 5+3 = +(5,3). Yes?- abc(a,d,e(x,y)) = abc(d,d,e(f,g,h)). No c 2004 Jens Teubner, André Seifert, University of Konstanz 24

25 4.4.7 Operators In Prolog and for programmers convenience some built-in functors can (but don t have to) be written in infix notation. You can write 5+3 instead of +(5,3). In Prolog each operator belongs to a priority class. When you write a myop1 b myop2 c, the system knows whether it is: myop1(a,myop2(b,c)) or myop2(myop1(a,b),c). If operators belong to the same priority class, its associativity determines how expressions are evaluated. Assume that operator myop is left-associative. When you write a myop b myop c, the system treats this expression as: myop(myop(a, b), c). c 2004 Jens Teubner, André Seifert, University of Konstanz 25

26 Operators in Prolog can be classified into: built-in operators and user-defined operators. Some built-in operators and predicates are: Operator / Predicate term1 = term2 term1 \= term2 term1 == term2 term1 \== term2 term1 =@= term2 term1 \=@= term2 term2 term2 term2 term2 Description unification of term1 and term2; succeeds if the unification succeeds. term1 and term2 must not match term1 and term2 reference the same object negation of term1 == term2 term1 is structurally equal to term2; structural equivalence is weaker than equivalence (==), but stronger than unification (=) negation of term1 =@= term2 term1 is before term2 in the standard order of terms both terms are equal (==) or term1 is before term2 in the standard order of terms term1 is after term2 in the standard order of terms both terms are equal (==) or term1 is after term2 in the standard order of terms c 2004 Jens Teubner, André Seifert, University of Konstanz 26

27 Operators cont d Operator / Predicate Description R := expr R is the result of the computation of expr R is expr the same meaning as R := expr expr1 =:= expr2 succeeds when expr1 evaluates to a number equal to expr2 expr1 =\= expr2 succeeds when expr1 evaluates to a number non-equal to expr2 expr1 < expr2 the value of expr1 is to be smaller than the value of expr2 expr1 > expr2 the value of expr1 is to be larger than the value of expr2 expr1 =< expr2 the value of expr1 is to be equal to or smaller than the value of expr2 expr1 >= expr2 the value of expr1 is to be larger than or equal to the value of expr addition subtraction... *... multiplication... /... division X mod Y X modulo Y -X unary minus +X unary plus c 2004 Jens Teubner, André Seifert, University of Konstanz 27

28 4.4.8 Arithmetic in Prolog { } { } expr1 := Syntax: variable is expr2.?- X is X = 8?- 7 is 5 + X. [Error in arithmetic expression]?- 8 is yes?- rest(x,y,r) :- integer(x), integer(y), R is X mod Y.?- rest(10,7,r). R = 3?- c 2004 Jens Teubner, André Seifert, University of Konstanz 28

29 5 Prolog and Relational Databases When we told you about the Tuple Relational Calculus (TRC), you already heard something like the TRC is the 1st order predicate calculus. Prolog is an implementation of this calculus. Relations are predicates with as many parameters as the relation s attribute number. The database content is stored as facts. actor(1, Damon, Matt, male). actor(2, Potente, Franka, female). actor(3, Cruise, Tom, male). actor(4, Grant, Hugh, male). act_film(1, 3, Jason Bourne ). act_film(2, 3, Marie Kreutz ). The names of all actors? {t a : actor(a) t(name) = a(name)} actor(, Name, ). c 2004 Jens Teubner, André Seifert, University of Konstanz 29

30 All actors with actor id smaller than 3? {t a : actor(a) t(actor id) < 3 t(actor id) = a(actor id) t(name) = a(name) t(sex) = a(sex)} actor(actor id, Name, Sex), Actor id < Joins and Views Joins are expressed like in the TRC: {t a, b : actor(a) act film(b) a(actor id) = b(actor id) t(name) = a(name) t(role) = b(role)} actor(id, Name, ), act film(id,, Role). Views in SQL correspond to rules in Prolog: CREATE VIEW actor_names AS SELECT Name From actor actor_names(name) :- actor(_, Name, _). c 2004 Jens Teubner, André Seifert, University of Konstanz 30

31 5.2 Recursive Queries With Prolog, it is also possible to express recursive queries. : son(otto, franz). : son(franz, heinz). : son(heinz, hugo). : descendant(d, A) :- son(d, A). : descendant(d, A) :- son(d, X), descendant(x, A).?- descendant(x, heinz). X = otto ; X = franz ; No?- c 2004 Jens Teubner, André Seifert, University of Konstanz 31

PROLOG PROgramming in LOGic

PROLOG PROgramming in LOGic PROLOG PROgramming in LOGic 1 Knowledge Based Information Systems (Relational) database systems are very efficient in the handling of data. Information is data together with a suitable interpretation.

More information

Logic Languages. Hwansoo Han

Logic Languages. Hwansoo Han Logic Languages Hwansoo Han Logic Programming Based on first-order predicate calculus Operators Conjunction, disjunction, negation, implication Universal and existential quantifiers E A x for all x...

More information

Prolog Introduction. Gunnar Gotshalks PI-1

Prolog Introduction. Gunnar Gotshalks PI-1 Prolog Introduction PI-1 Physical Symbol System Hypothesis A physical symbol system has the necessary and sufficient means for general intelligent action. Allen Newell and Herbert A. Simon PI-2 Physical

More information

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 2.1-2.7 p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation Topics Chapter 16 Logic Programming Proving Theorems Resolution Instantiation and Unification Prolog Terms Clauses Inference Process Backtracking 2 Predicate Calculus and Proving Theorems A use of propositions

More information

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses Prolog Programming Derived from PROgramming in LOGic (1972) Good at expressing logical relationships between concepts Prolog and LISP - two most popular AI languages Execution of a Prolog program is a

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy Topics Chapter 16 Logic Programming Summary (resolution, unification, Prolog search strategy ) Disjoint goals The cut operator Negative goals Predicate fail Debugger / tracer Lists 2 Resolution Resolution

More information

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3. 1 Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3. Satisfying goals 2 Prolog A standard free Prolog can be downloaded from http://www.swi-prolog.org

More information

Brief Introduction to Prolog

Brief Introduction to Prolog CSC384: Intro to Artificial Intelligence Brief Introduction to Prolog Prolog Programming for Artificial Intelligence by Ivan Bratko. Prolog is a language that is useful for doing symbolic and logic based

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

Prolog Programming. Lecture Module 8

Prolog Programming. Lecture Module 8 Prolog Programming Lecture Module 8 Prolog Language Prolog is unique in its ability to infer facts from the given facts and rules. In Prolog, an order of clauses in the program and goals in the body of

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

More information

SMURF Language Reference Manual Serial MUsic Represented as Functions

SMURF Language Reference Manual Serial MUsic Represented as Functions SMURF Language Reference Manual Serial MUsic Represented as Functions Richard Townsend, Lianne Lairmore, Lindsay Neubauer, Van Bui, Kuangya Zhai {rt2515, lel2143, lan2135, vb2363, kz2219}@columbia.edu

More information

Introduction to Prolog

Introduction to Prolog Introduction to Prolog David Woods dwoods@scss.tcd.ie Week 3 - HT Declarative Logic The Prolog programming language is, at its theoretical core, a declarative language. This is unlike more commonly used

More information

Part I Logic programming paradigm

Part I Logic programming paradigm Part I Logic programming paradigm 1 Logic programming and pure Prolog 1.1 Introduction 3 1.2 Syntax 4 1.3 The meaning of a program 7 1.4 Computing with equations 9 1.5 Prolog: the first steps 15 1.6 Two

More information

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

INTRODUCTION TO PROLOG

INTRODUCTION TO PROLOG INTRODUCTION TO PROLOG PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/44 STRUCTURE OF A PROLOG PROGRAM Where, declaratively, Haskell expresses a computation as a system

More information

What is Prolog? - 1. A Prolog Tutorial. What is Prolog? - 2. Prolog Programming. » Declaring some facts about objects and their relationships

What is Prolog? - 1. A Prolog Tutorial. What is Prolog? - 2. Prolog Programming. » Declaring some facts about objects and their relationships What is Prolog? - 1 Prolog is an example of a logic programming language Invented by Alain Colmeraurer in 1972 A Prolog Tutorial Based on Clocksin and Mellish Chapter 1 The version implemented at the University

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

This book is licensed under a Creative Commons Attribution 3.0 License

This book is licensed under a Creative Commons Attribution 3.0 License 6. Syntax Learning objectives: syntax and semantics syntax diagrams and EBNF describe context-free grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars

More information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

JME Language Reference Manual

JME Language Reference Manual JME Language Reference Manual 1 Introduction JME (pronounced jay+me) is a lightweight language that allows programmers to easily perform statistic computations on tabular data as part of data analysis.

More information

Chapter 16. Logic Programming Languages

Chapter 16. Logic Programming Languages Chapter 16 Logic Programming Languages Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming The Origins of

More information

Prolog. Intro to Logic Programming

Prolog. Intro to Logic Programming Prolog Logic programming (declarative) Goals and subgoals Prolog Syntax Database example rule order, subgoal order, argument invertibility, backtracking model of execution, negation by failure, variables

More information

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E Intelligent Systems and HCI D703E Lecture : More Prolog Paweł Pietrzak Prolog (cont d) 1 Remark The recent version of SWI- Prolog displays true and false rather than and no Using multiple clauses Different

More information

Fundamentals of Prolog

Fundamentals of Prolog Fundamentals of Prolog Prof. Geraint A. Wiggins Centre for Cognition, Computation and Culture Goldsmiths College, University of London Contents Summary of Lecture 1 What makes a good Prolog program? What

More information

Logic Programming Languages

Logic Programming Languages Logic Programming Languages Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical inferencing process to

More information

First-Order Logic (FOL)

First-Order Logic (FOL) First-Order Logic (FOL) FOL consists of the following parts: Objects/terms Quantified variables Predicates Logical connectives Implication Objects/Terms FOL is a formal system that allows us to reason

More information

Part I CHR tutorial. Cambridge University Press Constraint Handling Rules Thom Fruhwirth Excerpt More information

Part I CHR tutorial. Cambridge University Press Constraint Handling Rules Thom Fruhwirth Excerpt More information Part I CHR tutorial We present the essentials of the Constraint Handling Rules (CHR) programming language by the use of examples in this Tutorial part. The first chapter Getting started is a step-by-step

More information

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN GBIL: Generic Binary Instrumentation Language Language Reference Manual By: Andrew Calvano COMS W4115 Fall 2015 CVN Table of Contents 1) Introduction 2) Lexical Conventions 1. Tokens 2. Whitespace 3. Comments

More information

And Parallelism. Parallelism in Prolog. OR Parallelism

And Parallelism. Parallelism in Prolog. OR Parallelism Parallelism in Prolog And Parallelism One reason that Prolog is of interest to computer scientists is that its search mechanism lends itself to parallel evaluation. In fact, it supports two different kinds

More information

Sprite an animation manipulation language Language Reference Manual

Sprite an animation manipulation language Language Reference Manual Sprite an animation manipulation language Language Reference Manual Team Leader Dave Smith Team Members Dan Benamy John Morales Monica Ranadive Table of Contents A. Introduction...3 B. Lexical Conventions...3

More information

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012.

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012. PROLOG Prolog is a programming language for symbolic, n-numeric computations. It is specially well suited for solving problems that involve objects and relation between objects. First simple Prolog Program

More information

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Decaf Language Reference Manual

Decaf Language Reference Manual Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented

More information

LING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong

LING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong Today's Topics SWI-Prolog installed? We will start to write grammars today Quick Homework 8 SWI Prolog Cheatsheet At the prompt?- 1. halt. 2. listing. listing(name).

More information

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript Chapter 17 Fundamental Concepts Expressed in JavaScript Learning Objectives Tell the difference between name, value, and variable List three basic data types and the rules for specifying them in a program

More information

Declarative Programming Prolog CS360

Declarative Programming Prolog CS360 Declaratie Programming Prolog CS360 Terms Numerical literals String literals Ø By default, any string that starts is all lowercase Ø Use single quotes for anything else Atoms Ø Essentially, strings Ø Also,

More information

L-System Fractal Generator: Language Reference Manual

L-System Fractal Generator: Language Reference Manual L-System Fractal Generator: Language Reference Manual Michael Eng, Jervis Muindi, Timothy Sun Contents 1 Program Definition 3 2 Lexical Conventions 3 2.1 Comments...............................................

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

CA4003 Compiler Construction Assignment Language Definition

CA4003 Compiler Construction Assignment Language Definition CA4003 Compiler Construction Assignment Language Definition David Sinclair 2017-2018 1 Overview The language is not case sensitive. A nonterminal, X, is represented by enclosing it in angle brackets, e.g.

More information

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

15 Unification and Embedded Languages in Lisp

15 Unification and Embedded Languages in Lisp 15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

Introduction to Prolog

Introduction to Prolog Introduction to Prolog York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 03_Prolog 1 Overview Introduction & Preliminaries Syntax Characters Constants

More information

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the

More information

Operators (2A) Young Won Lim 10/5/13

Operators (2A) Young Won Lim 10/5/13 Operators (2A) Copyright (c) 2013 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Scheme: Expressions & Procedures

Scheme: Expressions & Procedures Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University

More information

Software Paradigms (Lesson 6) Logic Programming

Software Paradigms (Lesson 6) Logic Programming Software Paradigms (Lesson 6) Logic Programming Table of Contents 1 Introduction... 2 2 Facts... 3 3 Predicates (Structured Terms)... 4 3.1 General Structures... 4 3.2 Predicates (Syntax)... 4 3.3 Simple

More information

Introduction to predicate calculus

Introduction to predicate calculus Logic Programming Languages Logic programming systems allow the programmer to state a collection of axioms from which theorems can be proven. Express programs in a form of symbolic logic Use a logical

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

DaMPL. Language Reference Manual. Henrique Grando

DaMPL. Language Reference Manual. Henrique Grando DaMPL Language Reference Manual Bernardo Abreu Felipe Rocha Henrique Grando Hugo Sousa bd2440 flt2107 hp2409 ha2398 Contents 1. Getting Started... 4 2. Syntax Notations... 4 3. Lexical Conventions... 4

More information

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution Foundations of AI 9. Predicate Logic Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution Wolfram Burgard, Andreas Karwath, Bernhard Nebel, and Martin Riedmiller 09/1 Contents Motivation

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

LCSL Reference Manual

LCSL Reference Manual LCSL Reference Manual Sachin Nene, Chaue Shen, Bogdan Caprita, Julian Maller 1.1 Lexical Conventions 1.1.1 Comments Comments begin with (* and end with *) 1.1.2 Variables Variables consist solely of alphanumeric

More information

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Contents 1 Introduction...2 2 Lexical Conventions...2 3 Types...3 4 Syntax...3 5 Expressions...4 6 Declarations...8 7 Statements...9

More information

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation CS 91.406/534 Compiler Construction University of Massachusetts Lowell Professor Li Xu Fall 2004 NOTHING: A Language for Practice Implementation 1 Introduction NOTHING is a programming language designed

More information

Chapter 16. Logic Programming Languages ISBN

Chapter 16. Logic Programming Languages ISBN Chapter 16 Logic Programming Languages ISBN 0-321-49362-1 Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming

More information

Introduction to Logic Programming in Prolog 1 / 39

Introduction to Logic Programming in Prolog 1 / 39 Introduction to Logic Programming in Prolog 1 / 39 Outline Programming paradigms Logic programming basics Introduction to Prolog Predicates, queries, and rules Understanding the query engine Goal search

More information

Artificial Intelligence Course Sharif University of Technology

Artificial Intelligence Course Sharif University of Technology Artificial Intelligence Course Sharif University of Technology Outline Data objects Matching Declarative meaning of Prolog programs Procedural meaning Example: monkey and banana Order of clauses and goals

More information

FRAC: Language Reference Manual

FRAC: Language Reference Manual FRAC: Language Reference Manual Justin Chiang jc4127 Kunal Kamath kak2211 Calvin Li ctl2124 Anne Zhang az2350 1. Introduction FRAC is a domain-specific programming language that enables the programmer

More information

3 Lists. List Operations (I)

3 Lists. List Operations (I) 3 Lists. List Operations (I) The list is the simplest yet the most useful Prolog structure. A list is a sequence of any number of objects. Example 3.1: L = [1, 2, 3], R = [a, b, c], T = [john, marry, tim,

More information

Operators (2A) Young Won Lim 10/2/13

Operators (2A) Young Won Lim 10/2/13 Operators (2A) Copyright (c) 2013 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Z Notation. June 21, 2018

Z Notation. June 21, 2018 Z Notation June 21, 2018 1 Definitions There are many different ways to introduce an object in a Z specification: declarations, abbreviations, axiomatic definitions, and free types. Keep in mind that the

More information

An introduction to logic programming with Prolog

An introduction to logic programming with Prolog An introduction to logic programming with Prolog Dr. Constantinos Constantinides Department of Computer Science and Software Engineering Concordia University A running example: A family genealogy tree

More information

Typescript on LLVM Language Reference Manual

Typescript on LLVM Language Reference Manual Typescript on LLVM Language Reference Manual Ratheet Pandya UNI: rp2707 COMS 4115 H01 (CVN) 1. Introduction 2. Lexical Conventions 2.1 Tokens 2.2 Comments 2.3 Identifiers 2.4 Reserved Keywords 2.5 String

More information

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming The Idea Underlying CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Department of Computer Science and Engineering University of Minnesota Lectures in Fall

More information

Logical reasoning systems

Logical reasoning systems Logical reasoning systems Theorem provers and logic programming languages Production systems Frame systems and semantic networks Description logic systems CS 561, Session 19 1 Logical reasoning systems

More information

Topic B: Backtracking and Lists

Topic B: Backtracking and Lists Topic B: Backtracking and Lists 1 Recommended Exercises and Readings From Programming in Prolog (5 th Ed.) Readings: Chapter 3 2 Searching for the Answer In order for a Prolog program to report the correct

More information

CS 381: Programming Language Fundamentals

CS 381: Programming Language Fundamentals CS 381: Programming Language Fundamentals Summer 2017 Introduction to Logic Programming in Prolog Aug 2, 2017 Outline Programming paradigms Logic programming basics Introduction to Prolog Predicates, queries

More information

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROgramming in LOGic PROLOG Recursion, Lists & Predicates PROgramming in LOGic PROLOG Recursion, Lists & Predicates CSC9Y4 1 Recursion Recursion in Prolog means placing in the body of a rule a call to the predicate which occurs in the head of the rule. Here is

More information

Implementação de Linguagens 2016/2017

Implementação de Linguagens 2016/2017 Implementação de Linguagens Ricardo Rocha DCC-FCUP, Universidade do Porto ricroc @ dcc.fc.up.pt Ricardo Rocha DCC-FCUP 1 Logic Programming Logic programming languages, together with functional programming

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Maciej Sobieraj. Lecture 1

Maciej Sobieraj. Lecture 1 Maciej Sobieraj Lecture 1 Outline 1. Introduction to computer programming 2. Advanced flow control and data aggregates Your first program First we need to define our expectations for the program. They

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

ARG! Language Reference Manual

ARG! Language Reference Manual ARG! Language Reference Manual Ryan Eagan, Mike Goldin, River Keefer, Shivangi Saxena 1. Introduction ARG is a language to be used to make programming a less frustrating experience. It is similar to C

More information

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Introduction to Erlang. Franck Petit / Sebastien Tixeuil Introduction to Erlang Franck Petit / Sebastien Tixeuil Firstname.Lastname@lip6.fr Hello World % starts a comment. ends a declaration Every function must be in a module one module per source file source

More information

Prolog. Logic Programming vs Prolog

Prolog. Logic Programming vs Prolog Language constructs Prolog Facts, rules, queries through examples Horn clauses Goal-oriented semantics Procedural semantics How computation is performed? Comparison to logic programming 1 Logic Programming

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

will take you everywhere.

will take you everywhere. Prolog COMP360 Logic will get you from A to B. Imagination will take you everywhere. Albert Einstein Prolog Assignment A programming assignment in Prolog has been posted on Blackboard Upload your.pl file

More information

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3) Declarative Computation Model Single assignment store (VRH 2.2) Kernel language syntax (VRH 2.3) Carlos Varela RPI October 6, 2009 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL On Academic

More information

CS 321 Programming Languages and Compilers. Prolog

CS 321 Programming Languages and Compilers. Prolog CS 321 Programming Languages and Compilers Prolog Prolog PROgramming LOGic Algorithm = Logic + Control Logic programming deals with computing relations rather than functions. To understand Prolog one must

More information

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION EDIABAS Electronic Diagnostic Basic System BEST/2 LANGUAGE DESCRIPTION VERSION 6b Copyright BMW AG, created by Softing AG BEST2SPC.DOC CONTENTS CONTENTS...2 1. INTRODUCTION TO BEST/2...5 2. TEXT CONVENTIONS...6

More information

COMP 9416, session 1, 2006 p. 1. Datalog. COMP 9416, session 1, 2006

COMP 9416, session 1, 2006 p. 1. Datalog. COMP 9416, session 1, 2006 OMP 9416, session 1, 2006 p. 1 Datalog OMP 9416, session 1, 2006 OMP 9416, session 1, 2006 p. 2 Atoms, integers and variables The fact "person(socrates).", the fact "sum(2,3,y)." and the rule "mortal(x)

More information

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator 1 2 This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator (total) and a counter (i); it works by assigning

More information

RSL Reference Manual

RSL Reference Manual RSL Reference Manual Part No.: Date: April 6, 1990 Original Authors: Klaus Havelund, Anne Haxthausen Copyright c 1990 Computer Resources International A/S This document is issued on a restricted basis

More information

Programming Paradigms

Programming Paradigms PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 1/31 Programming Paradigms Unit 16 Erlang Modules, Functions and Control Structures J. Gamper Free University of Bozen-Bolzano Faculty

More information

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and

Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 6 Outline. Unary Relational Operations: SELECT and Chapter 6 The Relational Algebra and Relational Calculus Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 Outline Unary Relational Operations: SELECT and PROJECT Relational

More information

IN112 Mathematical Logic

IN112 Mathematical Logic Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA

More information

IN112 Mathematical Logic

IN112 Mathematical Logic Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA

More information

SPARK-PL: Introduction

SPARK-PL: Introduction Alexey Solovyev Abstract All basic elements of SPARK-PL are introduced. Table of Contents 1. Introduction to SPARK-PL... 1 2. Alphabet of SPARK-PL... 3 3. Types and variables... 3 4. SPARK-PL basic commands...

More information

Programming Language Concepts, cs2104 Lecture 04 ( )

Programming Language Concepts, cs2104 Lecture 04 ( ) Programming Language Concepts, cs2104 Lecture 04 (2003-08-29) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-09-05 S. Haridi, CS2104, L04 (slides: C. Schulte, S. Haridi) 1

More information