Logic Programming. Adrian Crăciun. November 22, 2013

Size: px
Start display at page:

Download "Logic Programming. Adrian Crăciun. November 22, 2013"

Transcription

1 Logic Programming Adrian Crăciun November 22,

2 Contents I An Introduction to Prolog 5 1 Introduction Traditional programming paradigms Logic programming Prolog: Informal Introduction Overview Facts Queries (Goals) Rules Summary Further Reading and Exercises Syntax and Data Structures Terms Constants Variables Structures Operators Unification Arithmetic in Prolog Summary Further Reading and Exercises Using Data Structures Structures as Trees Lists Recursion Introducing Recursion Recursive Mapping Recursive Comparison Joining Structures Accumulators Difference Structures Further Reading and Exercises Backtracking and the Cut (!) Backtracking behaviour The cut (!) Common uses of the cut (!) Reading and Exercises

3 7 Efficient Prolog Declarative vs. Procedural Thinking Narrow the search Let Unification do the Work Understand Tokenization Tail recursion Let Indexing Help How to Document Prolog Code Reading and Further Exercises I/O with Prolog Edinburgh style I/O ISO I/O Reading and Further Exercises Defining New Operators Reading and Further Exercises II The Theoretical Basis of Logic Programming Logical Background Predicate logic Herbrand s Theorem Clausal Form of Formulae Reading and Further Exercises Resolution Ground Resolution Substitution Unification Resolution Reading and Further Exercises Logic Programming Formulas as programs Horn Clauses SLD Resolution Reading and Further Exercises

4 Overview of the Lecture Contents of this lecture Part 1: An introduction in the logic programming language Prolog. largely on [Clocksin and Mellish, 2003]. Based Part 2: A review of the theoretical basis of logic programming. Based on corresponding topics in [Ben-Ari, 2001] and [Nilsson and Maluszynski, 2000]. Part 3: Advanced topics in logic programming/prolog. Based on corresponding topics in [Ben-Ari, 2001] and [Nilsson and Maluszynski, 2000]. 4

5 Part I An Introduction to Prolog 1 Introduction 1.1 Traditional programming paradigms Recalling the Von Neumann machine ˆ The von Neumann machine (architecture) is characterized by: large uniform store of memory, processing unit with registers. ˆ A program for the von Neumann machine: a sequence of instructions for moving data between memory and registers, carrying out arithmetical-logical operations between registers, control, etc. ˆ Most programming languages (like C, C++, Java, etc.) are influenced by and were designed for the von Neumann architecture. In fact, such programming languages take into account the architecture of the machine they address and can be used to write efficient programs. The above point is by no means trivial, and it leads to a separation of work ( the software crisis ): finding solutions of problems (using reasoning), implementation of the solutions (mundane and tedious). Alternatives to the von Neumann approach ˆ How about making programming part of problem solving? ˆ i.e. write programs as you solve problems? ˆ rapid prototyping? ˆ Logic programming is derived from an abstract model (not a reorganization/abstraction of a von Neumann machine). ˆ In logic programming program = set of axioms, computation = constructive proof of a goal statement. 5

6 1.2 Logic programming Logic programming: some history ˆ David Hilbert s program (early 20th century): formalize all mathematics using a finite, complete, consistent set of axioms. ˆ Kurt Gödel s incompleteness theorem (1931): any theory containing arithmetic cannot prove its own consistency. ˆ Alonzo Church and Alan Turing (independently, 1936): undecidability - no mechanical method to decide truth (in general). ˆ Alan Robinson (1965): the resolution method for first order logic (i.e. machine reasoning in first order logic). ˆ Robert Kowalski (1971): procedural interpretation of Horn clauses, i.e. computation in logic. ˆ Alan Colmerauer (1972): Prolog (PROgrammation en LOGique).prover. ˆ David H.D. Warren (mid-late 1970 s): efficient implementation of Prolog. ˆ 1981 Japanese Fifth Generation Computer project: project to build the next generation computers with advanced AI capabilities (using a concurrent Prolog as the programming language). Applications of logic programming ˆ Symbolic computation: relational databases, mathematical logic, abstract problem solving, natural language understanding, symbolic equation solving, design automation, artificial intelligence, biochemical structure analysis, etc. ˆ Industrial applications: aviation: * SCORE - a longterm aiport capacity management system for coordinated airports (20% of air traffic worldwide, according to 6

7 * FleetWatch - operational control, used by 21 international air companies. personnel planning: StaffPlan (airports in Barcelona, Madrid; Hovedstaden region in Denmark). information management for disasters: ARGOS - crisis management in CBRN (chemical, biological, radiological and nuclear) incidents used by Australia, Brasil, Canada, Ireland, Denmark, Sweden, Norway, Poland, Estonia, Lithuania and Montenegro. 2 Prolog: Informal Introduction 2.1 Overview Problem solving with Prolog ˆ Programming in Prolog: rather than prescribe a sequence of steps to solve a problem, describe known facts and relations, then ask questions. ˆ Use Prolog to solve problems that involve objects and relations between objects. ˆ Examples: Objects: John, book, jewel, etc. Relations: John owns the book, The jewel is valuable. Rules: Two people are sisters if they are both females and they have the same parents. ˆ Attention!!! Problem solving requires modelling of the problem (with its respective limitations). ˆ Problem solving with Prolog: declare facts about objects and their relations, define rules about objects and their relations, ask questions about objects and their relations. ˆ Programming in Prolog: a conversation with the Prolog interpreter. 7

8 2.2 Facts ˆ Stating a fact in Prolog: l i k e s ( johnny, mary ). Names of relations (predicates) and objects are written in lower case letter. Prolog uses (mostly) prefix notation (but there are exceptions). Facts end with. (full stop). ˆ A model is built in Prolog, and facts describe the model. ˆ The user has to be aware of the interpretation: l i k e s ( john, mary ). l i k e s ( mary, john ). are not the same thing (unless explicitly specified). ˆ Arbitrary numbers of arguments are allowed. ˆ Notation: likes /2 indicates a binary predicate. ˆ Facts are part of the Prolog database (knowledge base). 2.3 Queries (Goals) ˆ A query in Prolog:? owns ( mary, book ). Prolog searches in the knowledge base for facts that match the question: ˆ Prolog answers true if : the predicates are the same, arguments are the same, ˆ Ortherwise the answer is false : only what is known is true ( closed world assumption ), Attention: false may not mean that the answer is false (but more like not derivable from the knowledge ). 8

9 Variables ˆ Think variables in predicate logic. ˆ Instead of:? l i k e s ( john, mary ).? l i k e s ( john, a p p l e s ).? l i k e s ( john, candy ). ask something like What does John like? (i.e. give everything that John likes). ˆ Variables stand for objects to be determined by Prolog. ˆ Variables can be: instantiated - there is an object the variable stands for, uninstantiated - it is not (yet) known what the variable stands for. ˆ In Prolog variables start with CAPITAL LETTERS:? l i k e s ( john, X). Prolog computation: example ˆ Consider the following facts in a Prolog knowledge base: ˆ To the query Prolog will answer... l i k e s ( john, f l o w e r s ). l i k e s ( john, mary ). l i k e s ( paul, mary )....? l i k e s ( john, X). X = f l o w e r s and wait for further instructions. Prolog answer computation ˆ Prolog searches the knowledge base for a fact that matches the query, ˆ when a match is found, it is marked. ˆ if the user presses Enter, the search is over, 9

10 ˆ if the user presses ; then Enter, Prolog looks for a new match, from the previously marked place, and with the variable(s) in the query uninstantiated. ˆ In the example above, two more ; Enter will determine Prolog to answer: X = mary. f a l s e ˆ When no (more) matching facts are found in the knowledge base, Prolog answers false. Conjunctions: more complex queries ˆ Consider the following facts: ˆ And the query: l i k e s ( mary, food ). l i k e s ( mary, wine ). l i k e s ( john, wine ). l i k e s ( john, mary ).? l i k e s ( john, mary ), l i k e s ( mary, john ). the query reads does john like mary and does mary like john? Prolog will answer false : it searches for each goal in turn (all goals have to be satisfied, if not, it will fail, i.e. answer false ). Conjunctions: more complex queries (cont d) ˆ For the query:? l i k e s ( mary, X), l i k e s ( john, X). Prolog: try to satisfy the first goal (if it is satisfied put a placemarker), then try to satisfy the second goal (if yes, put a placemarker). If at any point there is a failure, backtrack to the last placemarker and try alternatives. Example: conjunction, backtracking The way Prolog computes the answer to the above query is represented: ˆ In Figure 1, the first goal is satisfied, Prolog attempts to find a match for the second goal (with the variable instantiated). ˆ The failure to find a match in the knowledge base causes backtracking, see Figure 2. ˆ The new alternative tried is successful for both goals, see Figure 3. 10

11 Figure 1: Success for the first goal. Figure 2: Second goal failure causes backtracking. Figure 3: Success with alternative instantiation. 11

12 2.4 Rules ˆ John likes all people can be represented as: but this is tedious!!! l i k e s ( john, a l f r e d ). l i k e s ( john, bertrand ). l i k e s ( john, c h a r l e s ). l i k e s ( john, david ).... l i k e s ( john, X). but this should be only for people!!! ˆ Enter rules: John likes any object, but only that which is a person is a rule about what (who) John likes. ˆ Rules express that a fact depends on other facts. Rules as definitions ˆ Rules can be used to express definitions. ˆ Example: ˆ Example: X is a bird if X is an animal and X has feathers. X is a sister of Y if X is female and X and Y have the same parents. ˆ Attention! The above notion of definition is not the same as the notion of definition in logic: such definitions allow detection of the predicates in the head of the rule, but there may be other ways (i.e. other rules with the same head) to detect such predicates, in order to have full definitions iff is needed instead of if. ˆ Rules are general statements about objects and their relationships (in general variables occur in rules, but not always). 12

13 Rules in Prolog ˆ Rules in Prolog have a head and a body. ˆ The body of the rule describes the goals that have to be satisfied for the head to be true. ˆ Example: l i k e s ( john, X) : l i k e s (X, wine ). l i k e s ( john, X) : l i k e s (X, wine ), l i k e s (X, food ). l i k e s ( john, X) : female (X), l i k e s (X, wine ). ˆ Attention! The scope of the variables that occur in a rule is the rule itself (rules do not share variables). Example (royals) ˆ Knowledge base: male ( a l b e r t ). male ( edward ). female ( a l i c e ). female ( v i c t o r i a ). p arents ( a l i c e, a l b e r t, v i c t o r i a ). p arents ( edward, a l b e r t, v i c t o r i a ). s i s t e r o f (X, Y): female (X), parents (X, M, F ). parents (Y, M, F ). ˆ Goals:? s i s t e r o f ( a l i c e, edward ).? s i s t e r o f ( a l i c e, X). Exercise (thieves) ˆ Consider the following: /*1*/ t h i e f ( john ). /*2*/ l i k e s ( mary, food ). /*3*/ l i k e s ( mary, wine ). /*4*/ l i k e s ( john, X): l i k e s (X, wine ). /*5*/ may steal (X, Y) : t h i e f (X), l i k e s (X, Y). 13

14 ˆ Explain how the query? may steal ( john, X). is executed by Prolog. 2.5 Summary In this introduction to Prolog, the following were discussed: asserting facts about objects, asking questions about facts, using variables, scopes of variables, conjunctions, an introduction to backtracking (in examples). 2.6 Further Reading and Exercises ˆ All things SWIProlog can be found at ˆ Install SWI-Prolog and try out the examples in the lecture. ˆ Read: Chapter 1 (including exercises section) of [Clocksin and Mellish, 2003]. 14

15 3 Syntax and Data Structures 3.1 Terms ˆ Prolog programs are built from terms (written as strings of characters). ˆ The following are terms: constants, variables, structures Constants ˆ Constants are simple (basic) terms. ˆ They name specific things or predicates (no functions in Prolog). ˆ Constants are of 2 types: atoms, numbers: integers, rationals (with special libraries), reals (floating point representation). Examples of atoms ˆ atoms: likes, a (lowercase letters), =, >, Void (anything between single quotes), george smith (constants may contain underscore), ˆ not atoms: 314a5 (cannot start with a number), george smith (cannot contain a hyphen), George (cannot start with a capital letter), something (cannot start with underscore). 15

16 3.1.2 Variables ˆ Variables are simple (basic) terms, ˆ written in uppercase or starting with underscore, ˆ Examples: X, Input, something, (the last one called anonymous variable). ˆ Anonymous variables need not have consistent interpretations (they need not be bound to the same value):? l i k e s (, john ). % does anybody l i k e John?? l i k e s (, ). % does anybody l i k e anybody? Structures ˆ Structure are compound terms, single objects consisting of collections of objects (terms), ˆ they are used to organize the data. ˆ A structure is specified by its functor (name) and its components owns ( john, book ( w u t h e r i n g h e i g h t s, bronte ) ). book ( w u t h e r i n g h e i g h t s, author ( emily, bronte ) ).? owns ( john, book (X, author (Y, bronte ) ) ). % does John own a book (X) by Bronte (Y, b r onte )? Characters in Prolog ˆ Characters: A-Z a-z * / \ ˆ < > # $ & ˆ Characters are ASCII (printing) characters with codes greater than 32. ˆ Remark: allows the use of any character. 16

17 3.2 Operators Arithmetic operators ˆ Arithmetic operators: +,, *, /, ˆ +(x, (y, z)) is equivalent with x + (y z) ˆ Operators do not cause evaluation in Prolog. ˆ Example: 3+4 (structure) does not have the same meaning with 7 (term). ˆ X is 3+4 causes evaluation (is represents the evaluator in Prolog). ˆ The result of the evaluation is that X is assigned the value 7. Parsing arithmetic expressions ˆ To parse an arithmetic expression you need to know: The position: * infix: x + y, x y * prefix x * postfix x! Precedence: x + y z? Associativity: What is x + y + z? x + (y + z) or (x + y) + z? ˆ Each operator has a precedence class: 1 - highest 2 - lower... lowest ˆ / have higher precedence than + ˆ 8/2/2 evaluates to: 8 (8/(2/2)) - right associative? or 2 ((8/2)/2) - left associative? ˆ Arithmetic operators are left associative. 17

18 3.3 Unification The unification predicate = ˆ = - infix built-in predicate.? X = Y. Prolog will try to match(unify) X and Y, and will answer true if successful. ˆ In general, we try to unify 2 terms (which can be any of constants, variables, structures):? T1 = T2. ˆ Remark on terminology: while in some Prolog sources the term matching is used, note that in the (logic) literature matching is used for the situation where one of the terms is ground (i.e. contains no variables). What = does is unification. The unification procedure Summary of the unification procedure? T1 = T2: ˆ If T1 and T2 are identical constants, success (Prolog answers true); ˆ If T1 and T2 are uninstantiated variable, success (variable renaming); ˆ If T1 is an uninstantiated variable and T2 is a constant or a structure, success, and T1 is instantiated with T2; ˆ If T1 and T2 are instantiated variables, then decide according to their value (they unify - if they have the same value, otherwise not); ˆ If T1 is a structure: f(x 1, X 2,..., X n ) and T2 has the same functor (name): f(y 1, Y 2,..., Y n ) and the same number of arguments, then unify these arguments recursively (X 1 = Y 1, X 2 = Y 2, etc.). If all the arguments unify, then the answer is true, otherwise the answer is false (unification fails); ˆ In any other case, unification fails. Occurs check ˆ Consider the following unification problem:? X = f (X). ˆ Answer of Prolog: X = f ( * * ). X = f (X). 18

19 ˆ In fact this is due to the fact that according to the unification procedure, the result is X = f(x) = f(f(x)) =...= f(f (...( f(x )...))) - an infinite loop would be generated. ˆ Unification should fail in such situations. ˆ To avoid them, perform an occurs check: If T1 is a variable and T2 a structure, in an expression like T1 = T2 make sure that T1 does not occur in T2. ˆ Occurence check is deactivated by default in most Prolog implementations (is computationally very expensive) - Prolog trades correctness for speed. ˆ A predicate complementary to unification: \= succeeds only when = fails, i.e. T1 \= T2 cannot be unified. 3.4 Arithmetic in Prolog Built-in predicates for arithmetic ˆ Prolog has built-in numbers. ˆ Built-in predicates on numbers include: X = Y, X \= Y, X < Y, X > Y, X =< Y, X >= Y, with the expected behaviour. ˆ Note that variables have to be instantiated in most cases (with the exception of the first two above, where unification is performed in the case of uninstantiation). The arithmetic evaluator is ˆ Prolog also provides arithmetic operators (functions), e.g.: +,, *, /, mod, rem, abs, max, min, random, round, floor, ceiling etc, but these cannot be used directly for computation(2+3 means 2+3, not 5) - expressions involving operators are not evaluated by default. 19

20 ˆ The Prolog evaluator is has the form: X i s Expr. where X is an uninstantiated variable, and Expr is an arithmetic expression, where all variables must be instantiated (Prolog has no equation solver). Example (with arithmetic(1)) r e i g n s ( rhondri, 844, ). r e i g n s ( anarawd, 878, ). r e i g n s ( hywel dda, 916, ). r e i g n s ( l a g o a p i d w a l, 950, ). r e i g n s ( h y w e l a p i e u a f, 979, ). r e i g n s ( cadwallon, 985, ). r e i g n s ( maredudd, 986, ). p r i n c e (X, Y): r e i g n s (X, A, B), Y >= A, Y =< B.? p r i n c e ( cadwallon, ). true? p r i n c e (X, ). X = l a g o a p i d w a l ; X = h y w e l a p i e u a f Example (with arithmetic(2)) pop ( place1, ). pop ( place2, ). pop ( place3, ). pop ( place4, ). area ( place1, 3 ). area ( place2, 1 ). area ( place3, 4 ). area ( place4, 3 ). d e n s i t y (X, Y): pop (X, P), area (X, A), Y i s P/A.? d e n s i t y ( place3, X). X = 200 true 20

21 3.5 Summary ˆ The notions covered in this section: Prolog syntax: terms (constants, variables, structures). Arithmetic in Prolog. Unification procedure. Subtle point: occurs check. 3.6 Further Reading and Exercises ˆ Read: Chapter 2 of [Clocksin and Mellish, 2003]. ˆ Try out all the examples in these notes, and in the above mentioned Chapter 2 of [Clocksin and Mellish, 2003]. from [?]. 4 Using Data Structures 4.1 Structures as Trees Structures as trees ˆ Consider the following structure: parent ( c h a r l e s, e l i s a b e t h, p h i l i p ). this can be represented as a tree: parent ˆ Other examples: a + b* c charles elisabeth philip + a * b c book ( moby dick, author ( herman, m e l v i l l e ) ). 21

22 book moby dick author herman melville s e n t e n c e ( noun (X), v e r b p h r a s e ( verb (Y), noun (Z ) ) ). sentence noun verb phrase X verb noun Y Z john likes mary f (X, g (X, a ) ). f ˆ g X a In the above, the variable X is shared between nodes in the tree representation. 4.2 Lists Introducing lists ˆ Lists are a common data structure in symbolic computation. ˆ Lists contain elements that are ordered. 22

23 ˆ Elements of lists are terms (any type, including other lists). ˆ Lists are the only data type in LISP. ˆ They are a data structure in Prolog. ˆ Lists can represent practically any structure. Lists (inductive domain) ˆ Base case : [ ] the empty list. ˆ General case :.(h, t) the nonempty list, where: h - the head, can be any term, t - the tail, must be a list. List representations ˆ.(a, [ ]) is represented as ˆ or ˆ [ ] a [ ] a tree representation vine representation ˆ.(a,.(b, [ ])) is ˆ ˆ [ ] a b ˆ.(a, b) is not a list, but it is a legal Prolog structure, represented as ˆ b a ˆ.(.( a, []),.(a,.(x, [ ]))) is represented as 23

24 ˆ ˆ ˆ [ ] ˆ [ ] a X a Syntactic sugar for lists ˆ To simplify the notation,, can be used to separate the elements. ˆ The lists introduced above are now: [a], [a, b], [[ a ], a, X]. List manipulation ˆ Lists are naturally split between the head and the tail. ˆ Prolog offers a construct to take advantage of this: [H T]. ˆ Consider the following example: ˆ Prolog will give: p ( [ 1, 2, 3 ] ). p ( [ the, cat, sat, [ on, the, mat ] ] ).? p ( [H T ] ). H = 1, T = [ 2, 3 ] ; H = the T = [ cat, sat, [ on, the, mat ] ] ; no ˆ Attention! [a b] is not a list, but it is a valid Prolog expression, corresponding to.( a, b) Unifying lists: examples [X, Y, Z ] = [ john, l i k e s, f i s h ] X = john Y = l i k e s Z = f i s h 24

25 [ cat ] = [X Y] X = cat Y = [ ] [X, Y Z ] = [ mary, l i k e s, wine ] X = mary Y = l i k e s Z = [ wine ] [ [ the, Y] Z ] = [ [ X, hare ], [ is, here ] ] X = the Y = hare Z = [ [ is, here ] ] [ golden T] = [ golden, n o r f o l k ] T = [ n o r f o l k ] [ vale, horse ] = [ horse, X] f a l s e [ white Q] = [P horse ] P = white Q = horse Strings ˆ In Prolog, strings are written inside double quotation marks. ˆ Example: a string. ˆ Internally, a string is a list of the corresponding ASCII codes for the characters in the string. ˆ? X = a s t r i n g. X = [ 9 7, 32, 115, 116, 114, 105, 110, ]. Summary ˆ Items of interest: the anatomy of a list in Prolog.(h, t) graphic representations of lists: tree representation, vine representation, syntactic sugar for lists [...], list manipulation: head-tail notation [H T], strings as lists, unifying lists. 25

26 Reading ˆ Read the corresponding Chapter 3, Section 3.2, from [Clocksin and Mellish, 2003]. 26

27 5 Recursion 5.1 Introducing Recursion Induction/Recursion ˆ Inductive domain: A domain composed of objects constructed in a manageable way, i.e.: there are some simplest (atomic) objects, that cannot be decomposed, there are complex objects that can be decomposed into finitely many simpler objects, and this decomposition process can be performed finitely many times before one reaches the simplest objects. In such domains, one can use induction as an inference rule. ˆ Recursion is the dual of induction, i.e.: recursion describes computation in inductive domains, recursive procedures (functions, predicates) call themselves, but the recursive call has to be done on a simpler object. As a result, a recursive procedure will have to describe the behaviour for: (a) The simplest objects, and/or the objects/situations for which the computation stops, i.e. the boundary conditions, and (b) the general case, which describes the recursive call. Example: lists as an inductive domain ˆ simplest object: the empty list [ ]. ˆ any other list is made of a head and a tail (the tail should be a list): [H T]. Example: member ˆ Implement in Prolog the predicate member/2, such that member(x, Y) is true when X is a member of the list Y. % The boundary c o n d i t i o n. member(x, [X ] ). % The r e c u r s i v e c o n d i t i o n. member(x, [ Y]): member(x, Y). 27

28 ˆ The boundary condition is, in this case, the condition for which the computation stops (not necessarily for the simplest list, which is [ ]). ˆ For [ ] the predicate is false, therefore it will be omitted. ˆ Note that the recursive call is on a smaller list (second argument). The elements in the recursive call are getting smaller in such a way that eventually the computation will succeed, or reach the empty list and fail. predicate for the empty list (where it fails). When to use the recursion? ˆ Avoid circular definitions: parent (X, Y): c h i l d (Y, X). c h i l d (X, Y): parent (Y, X). ˆ Careful with left recursion: In this case, person (X): person (Y), mother (X, Y). person (adam ).? person (X). will loop (no chance to backtrack). Prolog tries to satisfy the rule and this leads to the loop. ˆ Order of facts, rules in the database: i s l i s t ( [A B]): i s l i s t (B ). i s l i s t ( [ ] ). The following query will loop:? i s l i s t (X) ˆ The order in which the rules and facts are given matters. In general, place facts before rules. 5.2 Recursive Mapping ˆ Mapping: given 2 similar structures, change the first into the second, according to some rules. ˆ Example: you are a computer maps to i am not a computer, do you speak french maps to i do not speak german. 28

29 ˆ Mapping procedure: 1. accept a sentence, 2. change you to i, 3. change are to am not, 4. change french to german, 5. change do to no, 6. leave everything else unchanged. ˆ The program: change ( you, i ). change ( are, [ am, not ] ). change ( french, german ). change ( do, no ). change (X, X). a l t e r ( [ ], [ ] ). a l t e r ( [H T], [X Y]): change (H, X), a l t e r (T, Y). ˆ Note that this program is limited: it would change i do like you into i no like i, new rules would have to be added to the program to deal with such situations. 5.3 Recursive Comparison ˆ Dictionary comparison (lexicographic comparison) of atoms: aless/2 1. aless (book, bookbinder) succeeds. 2. aless ( elephant, elevator) succeeds. 3. aless (lazy, leather) is decided by aless (azy, eather). 4. aless (same, same) fails. 5. aless (alphabetic, alp) fails. ˆ Use the predicate name/2 which returns the name of a symbol: ˆ The program:? name(x, [ 9 7, 1 0 8, ] ). X=alp. 29

30 a l e s s (X, Y): name(x, L), name(y, M), a l e s s x (L,M). a l e s s x ( [ ], [ ] ). a l e s s x ( [X ], [Y ]): X < Y. a l e s s x ( [H X], [H Y]): a l e s s (X, Y). 5.4 Joining Structures ˆ We want to append two lists, i.e.? appendlists ( [ a, b, c ], [ 3, 2, 1 ], [ a, b, c, 3, 2, 1 ] ). true This illustrate the use of appendlists/3 for testing that a list is the result of appending two other lists. ˆ Other uses of appendlists/3: - Total list computation:? appendlists ( [ a, b, c ], [ 3, 2, 1 ], X). - Isolate: - Split:? appendlists (X, [ 2, 1 ], [ a, b, c, 2, 1 ] ).? appendlists (X, Y, [ a, b, c, 3, 2, 1 ] ). % t h e boundary c o n d i t i o n appendlists ( [ ], L, L ). % r e c u r s i o n appendlists ( [X L1 ], L2, [X L3 ]): appendlists ( L1, L2, L3 ). 5.5 Accumulators Summary ˆ The recursive nature of structures (and in particular lists) gives a way to traverse them by recursive decomposition. ˆ When the boundary is reached, the decomposition stops and the result is composed in a reverse of the decomposition process. ˆ This process can be made more efficient: introduce an extra variable in which the result so far is accumulated. ˆ When the boundary is reached this extra variable already contains the result, no need to go back and compose the final result. ˆ This variable is called an accumulator. 30

31 Example: List Length ˆ Without accumulator: % l e n g t h o f a l i s t % boundary c o n d i t i o n l i s t l e n ( [ ], 0 ). % r e c u r s i o n l i s t l e n ( [H T], N): l i s t l e n (T, N1), N i s N1+1. ˆ With accumulator: % l e n g t h o f a l i s t with accumulators % c a l l o f t h e accumulator : l i s t l e n 1 (L, N): l e n a c c (L, 0, N). % boundary c o n d i t i o n f o r accumulator l e n a c c ( [ ], A, A). % r e c u r s i o n f o r t h e accumulator l e n a c c ( [H T], A, N): A1 i s A + 1, l e n a c c (T, A1, N). ˆ Inside Prolog, for the query? listlen1 ([ a, b, c ], N): l e n a c c ( [ a, b, c ], 0, N). l e n a c c ( [ b, c ], 1, N). l e n a c c ( [ c ], 2, N). l e n a c c ( [ ], 3, N) The return variable is shared by every goal in the trace. Example: Reverse ˆ Without accumulators: %% r e v e r s e % boundary c o n d i t i o n r e v e r s e 1 ( [ ], [ ] ). % r e c u r s i o n r e v e r s e 1 ( [X TX], L): r e v e r s e 1 (TX, NL), appendlists (NL, [X], L ). ˆ With accumulators: 31

32 %% r e v e r s e with accumulators % c a l l t h e accumulator r e v e r s e 2 (L, R): reverseacc (L, [ ], R). % boundary c o n d i t i o n f o r t h e accumulator reverseacc ( [ ], R, R). % r e c u r s i o n f o r t h e accumulator reverseacc ( [H T], A, R): reverseacc (T, [H A], R). 5.6 Difference Structures Summary ˆ Accumulators provide a technique to keep trace of the result so far (in the accumulator variable) at each step of computation, such that when the structure is traversed the accumulator contains the final result, which is then passed to the output variable. ˆ Now we consider a technique where we use a variable to hold the final result and the second to indicate a hole in the final result, where more things can be inserted. ˆ Consider [a, b, c X] - we know that this structure is a list up to a point (up to X). We call this an open list (a list with a hole ). ˆ ˆ ˆ a b c Using open lists ˆ Consider? X = [ a, b, c L ], L = [ d, e, f, g ]. X = [ a, b, c, d, e, f, g ], L = [ d, e, f, g ]. the result is the concatenation of the beginning of X (the list before the hole ) with L, i.e. we filled the hole, and this is done in one step! ˆ Now fill the hole with an open list:? X = [ a, b, c L ], L = [ d, e L1 ]. X = [ a, b, c, d, e L1 ], L = [ d, e L1 ]. 32

33 the hole was filled partially. ˆ Now express this as a Prolog predicate: d i f f a p p e n d 1 ( OpenList, Hole, L): Hole=L. i.e. we have an open list (OpenList), with a hole (Hole) is filled with a list (L):? X = [ a, b, c, d Hole ], d i f f a p p e n d 1 (X, Hole, [ d, e ] ). X = [ a, b, c, d, d, e ], Hole = [ d, e ]. ˆ Note that when we work with open lists we need to have information (i.e. a variable) both for the open list and its hole. ˆ A list can be represented as the the difference between an open list and its hole. ˆ Notation: OpenList Hole here the difference operator has no interpretation, in fact other operators could be used instead. ˆ Now modify the append predicate to use difference list notation: d i f f a p p e n d 2 ( OpenList Hole, Hole = L. its usage: L):? X = [ a, b, c, d Hole] Hole, d i f f a p p e n d 2 (X, [ d, e ] ). X = [ a, b, c, d, d, e ] [d, e ], Hole = [ d, e ]. ˆ Perhaps the fact that the answer is given as a difference list is not convenient. ˆ A new version that returns a(n open) list (with the hole filled) as the answer: d i f f a p p e n d 3 ( OpenList Hole, L, OpenList ): Hole = L. its usage: 33

34 ? X = [ a, b, c, d Hole] Hole, d i f f a p p e n d 3 (X, [ d, e ], Ans ). X = [ a, b, c, d, d, e ] [d, e ], Hole = [ d, e ], Ans = [ a, b, c, d, d, e ]. ˆ diff append3 has a difference list as its first argument, a proper list as its second argument, returns a proper list. ˆ A further modification to be systematic for this version the arguments are all difference lists: d i f f a p p e n d 4 (OL1 Hole1, OL2 Hole2, OL1 Hole2 ): Hole1 = OL2. and its usage:? X=[a, b, c Ho] Ho, d i f f a p p e n d 4 (X, [ d, e, f Hole2 ] Hole2, Ans ). X = [ a, b, c, d, e, f Hole2 ] [d, e, f Hole2 ], Ho = [ d, e, f Hole2 ], Ans = [ a, b, c, d, e, f Hole2 ] Hole2. or, if we want the result to be just the list, fill the hole with the empty list:? X=[a, b, c Ho] Ho, d i f f a p p e n d 6 (X, [ d, e, f Hole2 ] Hole2, Ans [ ] ). X = [ a, b, c, d, e, f ] [d, e, f ], Ho = [ d, e, f ], Hole2 = [ ], Ans = [ a, b, c, d, e, f ]. ˆ One last modification is possible: its usage: a p p e n d d i f f (OL1 Hole1, Hole1 Hole2, OL1 Hole2 ).? X=[a, b, c H] H, a p p e n d d i f f (X, [ d, e, f Hole2 ] Hole2, Ans [ ] ). X = [ a, b, c, d, e, f ] [d, e, f ], H = [ d, e, f ], Hole2 = [ ], Ans = [ a, b, c, d, e, f ]. 34

35 Example: adding to back ˆ Let us consider the program for adding one element to the back of a list: % boundary c o n d i t i o n add to back ( El, [ ], [ El ] ). % r e c u r s i o n add to back ( El, [ Head T a i l ], [ Head NewTail ): add to back ( El, Tail, NewTail ). ˆ The program above is quite inefficient, at least compared with the similar operation of adding an element at the beginning of a list (linear in the length of the list one goes through the whole list to find its end versus constant one step). ˆ But difference lists can help - the hole is at the end of the list: add to back d ( El, OpenList Hole, Ans): a p p e n d d i f f ( OpenList Hole, [ El ElHole ] ElHole, Ans [ ] ). Problems with difference lists ˆ Consider:? a p p e n d d i f f ( [ a, b ] [ b ], [ c, d] [d ], L ). f a l s e. The above does not work! (no holes to fill). ˆ There are also problems with the occurs check (or lack there of): empty (L L ).? empty ( [ a Y] Y). Y = [ a * * ]. ˆ in difference lists is a partial function. It is not defined for [a, b, c] [d] :? a p p e n d d i f f ( [ a, b] [ c ], [ c ] [d ], L ). L = [ a, b] [d ]. The query succeeds, but the result is not the one expected. ˆ This can be fixed: a p p e n d d i f f f i x (X Y, Y Z, X Z): s u f f i x (Y, X), s u f f i x (Z, Y). however, now the execution time becomes linear again. 35

36 5.7 Further Reading and Exercises ˆ Read Chapter 3, Chapter 7, Sections 7.5, 7.6, 7.7 of [Clocksin and Mellish, 2003]. ˆ Read Chapter 7 of [Nilsson and Maluszynski, 2000]. ˆ Read Section 12.2 of [Brna, 1988]. ˆ Try out in Prolog the examples. ˆ Solve the corresponding exercises. 36

37 6 Backtracking and the Cut (!) 6.1 Backtracking behaviour Undesired backtracking behavior ˆ There are situations where Prolog does not behave as expected. ˆ Example: f a t h e r ( mary, george ). f a t h e r ( john, george ). f a t h e r ( sue, harry ). f a t h e r ( george, edward ). ˆ This works as expected for:? f a t h e r (X, Y). X = mary, Y = george ; X = john, Y = george ; X = sue, Y = harry ; X = george, Y = edward. ˆ However, for this:? f a t h e r (, X). X = george ; X = george ; X = harry ; X = edward. ˆ Once we find that george is a father, we don t expect to get the answer again. ˆ Consider the following recursive definition: i s n a t ( 0 ). i s n a t (X): i s n a t (Y), X i s Y+1. In the following, by issuing the backtracking request, all natural numbers can be generated: 37

38 ? i s n a t (X). X = 0 ; X = 1 ; X = 2 ; X = 3 ; X = 4 ; X = 5 ; X = 6 ;... ˆ There is nothing wrong with this behavior! ˆ Consider: member(x, [X ] ). member(x, [ Y]): member(x, Y). ˆ the query:? member( a, [ a, b, a, a, a, v, d, e, e, g, a ] ). true ; true ; true ; true ; true ; f a l s e. ˆ Backtracking confirms the answer several times. But we only need it once! 6.2 The cut (!) The cut predicate (!) ˆ The cut predicate!/0 tells Prolog to discard certain choices of backtracking. ˆ It has the effect of pruning branches of the search space. ˆ As an effect, the programs will run faster, the programs will occupy less memory (less backtracking points to be remembered). 38

39 Example:library ˆ Reference library: determine which facilities are available: basic, general. if one has an overdue book, only basic facilities are available. f a c i l i t y ( Pers, Fac ): book overdue ( Pers, Book ), b a s i c f a c i l i t y ( Fac ). b a s i c f a c i l i t y ( r e f e r e n c e s ). b a s i c f a c i l i t y ( e n q u i r i e s ). a d d i t i o n a l f a c i l i t y ( borrowing ). a d d i t i o n a l f a c i l i t y ( i n t e r l i b r a r y e x c h a n g e ). g e n e r a l f a c i l i t y (X): b a s i c f a c i l i t y (X). g e n e r a l f a c i l i t y (X): a d d i t i o n a l f a c i l i t y (X). c l i e n t ( C. Wetzer ). c l i e n t ( A. Jones ). book overdue ( C. Wetzer, book00101 ). book overdue ( C. Wetzer, book00111 ). book overdue ( A. Jones, book ).? c l i e n t (X), f a c i l i t y (X, Y). X = C. Wetzer, Y = r e f e r e n c e s ; X = C. Wetzer, Y = e n q u i r i e s ; X = A. Jones, Y = r e f e r e n c e s ; X = A. Jones, Y = e n q u i r i e s. Example: library revisited (and with cut) f a c i l i t y ( Pers, Fac ): book overdue ( Pers, Book ),!, b a s i c f a c i l i t y ( Fac ). 39

40 b a s i c f a c i l i t y ( r e f e r e n c e s ). b a s i c f a c i l i t y ( e n q u i r i e s ). a d d i t i o n a l f a c i l i t y ( borrowing ). a d d i t i o n a l f a c i l i t y ( i n t e r l i b r a r y e x c h a n g e ). g e n e r a l f a c i l i t y (X): b a s i c f a c i l i t y (X). g e n e r a l f a c i l i t y (X): a d d i t i o n a l f a c i l i t y (X). ˆ The goal? client(x), facility (X, Y) is answered by Prolog in the following way:? - client(x), X= C.Wetzer facility(x, Y).? - facility( C.Wetzer, Y)....?- book overdue( C.Wetzer, Book),!, basic facility( C.Wetzer, Y). Book=book00101 Book=book00111?-!, basic facility( C.Wetzer, Y).?- basic facility( C.Wetzer, Y). Y =references Y =enquiries ˆ Guarded gate metaphor! ˆ Effect of the cut: if a client has an overdue book, only allow basic facilities, no need to look for all overdue books, no need to consider any other rules about facilities. ˆ! always succeeds (with empty substitutions). ˆ When the cut is encountered as a goal: the system becomes commited to all the choices made since the parent (here this is facility ) was invoked, all other alternatives are discarded (e.g. the branch indicated by above), 40

41 an attempt to satisfy any goal between the parent and the cut goal will fail, if the user asks for a different solution, Prolog goes to the backtrack point above the parent goal (if any). In the example above the first goal is ( client (X)) is the backtrack point above the goal. 6.3 Common uses of the cut (!) 1. Confirm the choice of a rule (tell the system the right rule was found). 2. Cut-fail combination (tell the system to fail a particular goal without trying to find alternative solutions). 3. Terminate a generate-and-test (tell the system to terminate the generation of alternative solutions by backtracking). Confirm the choice of a rule ˆ Situation: There are some clauses associated with the same predicate. Some clauses are appropriate for arguments of certain forms. Often argument patterns can be provided (e.g. empty and nonempty lists), but not always. If no exhaustive set of patterns can be provided, give rules for specific arguments and a catch all rule at the end. sum to ( 1, 1 ). sum to (N, Res ): N1 i s N 1, sum to (N1, Res1 ), Res i s Res1 + N. When backtracking, there is an error (it loops - why?):? sum to ( 5, X). X = 15 ; ERROR: Out o f l o c a l s t a c k ˆ Now using the cut (!): csum to ( 1, 1):!. csum to (N, Res ): N1 i s N 1, csum to (N1, Res1 ), Res i s Res1 + N. 41

42 The system is commited to the boundary condition, it will not backtrack for others anymore:? csum to ( 5, X). X = 1 5. However:? csum to ( 3, Res ). ERROR: Out o f l o c a l s t a c k Placing the condition N =< 1 in the boundary condition fixes the problem: ssum to (N, 1): N =< 1,!. ssum to (N, Res ): N1 i s N 1, ssum to (N1, Res1 ), Res i s Res1 + N. Cut! and not ˆ Where! not/1. is used to confirm the choice of a rule, it can be replaced by ˆ not(x) succeeds when the goal X fails. ˆ using not is considered to be good programming style: but programs may become less efficient (why?) there is a trade-off between readability and efficiency! ˆ A variant with not: nsum to ( 1, 1 ). nsum to (N, Res ): not (N =< 1 ), N1 i s N 1, nsum to (N1, Res1 ), Res i s Res1 + N. ˆ When not is used, there may be double work: A: B, C. A: not (B), D. in the above, B is tried twice after backtracking. 42

43 The cut-fail combination ˆ fail /0 is a built-in predicate. ˆ When it is a goal, it fails and causes backtracking. ˆ Using fail after the cut changes the backtracking behavior. ˆ Example: we are interested in average taxpayers, foreigners are not average, if the taxpayer is not foreigner, apply some general criteria. a v e r a g e t a x p a y e r (X): f o r e i g n e r (X),!, f a i l. a v e r a g e t a x p a y e r (X): s a t i s f i e s g e n e r a l c r i t e r i o n (X). What if the cut! isnt used? then a foreigner that satisfied the general criterion will be considered an average taxpayer. ˆ The general criterion: a person whose spouse earns more than 3000 is not average, otherwise, a person is average if they earn between 1000 and s a t i s f i e s g e n e r a l c r i t e r i o n (X): spouse (X, Y), g r o s s i n c o m e (Y, Inc ), Inc > 3000,!, f a i l. s a t i s f i e s g e n e r a l c r i t e r i o n (X): g r o s s i n c o m e (X, Inc ), Inc < 3000, Inc > ˆ Gross income: pensioners with less than 500 have no gross income, otherwise, gross income is the sum of the gross salary and the investment income. 43

44 g r o s s i n c o m e (X, Y): r e c e i v e s p e n s i o n (X, P), P < 500,!, f a i l. g r o s s i n c o m e (X, Y): g r o s s s a l a r y (X, Z ), investment income (X, W), Y i s Z + W. ˆ not can be implemented with the cut-fail combination. not (P): c a l l (P),!, f a i l. not (P ). ˆ Note though that Prolog will take issue with you trying to redefine essential predicates. Replacing the cut in cut-fail situations ˆ The cut can be replaced with not. ˆ This replacement does not affect the efficiency in the cut-fail situations. ˆ However, programs have to be rearranged: a v e r a g e t a x p a y e r (X): not ( f o r e i g n e r (X) ), not ( ( spouse (X, Y), g r o s s i n c o m e (Y, Inc ), Inc > 3000) ),... Terminating a generate and test ˆ Tic-tac-toe. ˆ Natural number division: d i v i d e (N1, N2, Result ): i s n a t ( Result ), Product1 i s Result * N2, Product2 i s ( Result +1)*N2, Product1 =< N1, Product2 > N1,!. 44

45 Problems with the cut ˆ Consider the example: cappend ( [ ], X, X) :!. cappend ( [A B], C, [A D]): cappend (B, C, D).? cappend ( [ 1, 2, 3 ], [ a, b, c ], X). X = [ 1, 2, 3, a, b, c ].? cappend ( [ 1, 2, 3 ], X, [ 1, 2, 3, a, b, c ] ). X = [ a, b, c ].? cappend (X, Y, [ 1, 2, 3, a, b, c ] ). X = [ ], Y = [ 1, 2, 3, a, b, c ]. ˆ The variant of append with a cut works as expected for the first two queries above. However, for the third, it only offers one solution (all the others are cut!) ˆ Consider: number of parents (adam, 0 ) :!. number of parents ( eve, 0 ) :!. number of parents (X, 2 ).? number of parents ( eve, X). X = 0.? number of parents ( john, X). X = 2.? number of parents ( eve, 2 ). true. ˆ The first two queries work as expected. ˆ The third query gives an unexpected answer. This is due to the fact that the particular instantiation of the arguments does not match the special condition where the cut was used. ˆ In fact, here, the pattern that distinguishes between the special condition and the general case is formed by both arguments together. ˆ The predicate above can be fixed in two ways: 45

46 n u m b e r o f p a r e n t s 1 (adam, N) :!, N = 0. n u m b e r o f p a r e n t s 1 ( eve, N) :!, N = 0. n u m b e r o f p a r e n t s 1 (X, 2 ). n u m b e r o f p a r e n t s 2 (adam, 0 ) :!. n u m b e r o f p a r e n t s 2 ( eve, 0 ) :!. n u m b e r o f p a r e n t s 2 (X, 2): X \ = adam, X, \= eve. ˆ The cut is a powerful construct and should be used with great care. ˆ The advantages of using the cut can be major, but so are the dangers. ˆ There are two types of cut: green cuts: when no solutions are discarded by cutting, red cuts: the part of the search space is cut, and this part contains solutions. ˆ Green cuts are harmless, whereas red cuts should be used with great care. 6.4 Reading and Exercises ˆ Read: Chapter 4 of [Clocksin and Mellish, 2003]. ˆ Also read: Chapter 5, Section 5.1 of [Nilsson and Maluszynski, 2000]. ˆ Carry out the examples in Prolog. ˆ Items of interest: what is the effect of the cut predicate (!), guarded gate metaphor, common uses of the cut: 1. confirming the use of a rule, 2. cut-fail combination, 3. terminate a generate and test, cut elimination (can it be done, does it cost in terms of computational complexity?) problems with cut (green cuts/red cuts). 46

47 7 Efficient Prolog 7.1 Declarative vs. Procedural Thinking The procedural aspect of Prolog ˆ While Prolog is described as a declarative language, one can see Prolog clauses from a procedural point of view: i n (X, usa ): i n (X, m i s s i s s i p p i ). The above can be seen: from a declarative point of view: X is in the USA if X is in Mississippi, from a procedural point of view: To prove that X is in the USA, prove X is in Mississippi, or To find X in USA, (it is sufficient to) find them in Mississippi. ˆ Procedural programming languages can also contain declarative aspects. Something like x = y + z ; can be read declaratively, as the equation x = y + z, procedurally: load y, load z, store x. The need to understand the procedural/declarative aspects ˆ The declarative/procedural aspects are not symmetrical : there are situations where one not understanding one aspect can lead to problems. ˆ For procedural programs: A = (B + C) + D and A = B + (C +D) appear to have equivalent declarative readings but: imagine the biggest number that can be represented is 1000, then for B = 501, C = 501, D = -3, the two expressions yield totally different results! ˆ The same can happen in Prolog. Declaratively, the following is correct: a n c e s t o r (A, C): a n c e s t o r (A, B), a n c e s t o r (B, C). However, ignoring its procedural meaning, this can lead to infinite loops (when B and C are both unknown). 47

48 7.2 Narrow the search ˆ The task of a Prolog programmer is to build a model of the problem and to represent it in Prolog. ˆ Knowledge about this model can improve performance significantly.? horse(x), gray(x). will find the answer much faster than? gray(x), horse(x). in a model with 1000 gray objects and 10 horses. ˆ Narrowing the search can be even more subtle: s e t e q u i v a l e n t ( L1, L2): permute ( L1, L2 ). i.e. to find whether two lists are set-equivalent it is enough to see whether they are permutations of eachother. But for N element lists, there are N! permutations (e.g. for 20 elements, possible permutations). ˆ Now considering a faster program: s e t e q u i v a l e n t ( L1, L2): s o r t ( L1, L3 ), s o r t ( L2, L3 ). i.e. two lists are set equivalent if their sorted versions are the same. And sorting can be done in NlogN steps (e.g. approx 86 steps for 20 element lists). 7.3 Let Unification do the Work ˆ When patterns are involved, unification can do some of the work that the programmer may have to do. ˆ E.g. consider variants the predicate that detects lists with 3 elements: h a s 3 e l e m e n t s (X): length (X, N), N = 3. h a s 3 e l e m e n t s ( [,, ] ). ˆ Also consider the predicate for swapping the first two elements from a list: s w a p f i r s t 2 ( [ A, B Rest ], [ B, A Rest ] ). Letting unification work saves having to go through the whole list. 48

49 7.4 Understand Tokenization ˆ Atoms are represented in Prolog in a symbol table where each atom appears once - a process called tokenization. ˆ Atoms in a program are replaced by their address in the symbol table. ˆ Because of this: f ( What an a w f u l l y long atom t h i s appears to be, What an a w f u l l y long atom t h i s appears to be, What an a w f u l l y long atom t h i s appears to be ). will actually take less memory than g(a, b, c) ˆ Comparison of atoms can be performed very fast because of tokenization. ˆ For example a \= b and aaaaaaaaa \= aaaaaaaab can both be done in the same time, without having to parse the whole atom names. 7.5 Tail recursion Continuations, backtracking points ˆ Consider the following: a: b, c. a: d. ˆ For? a., when b is called, Prolog has to save in the memory: the continuation, i.e. what has to be done after returning with success from b (i.e. c), the backtrack point, i.e. where can an alternative be tried in case of returning with failure from b (i.e. d). ˆ For recursive procedures the continuation and backtracking point have to be remembered for each of the recursive calls. ˆ This may lead to large memory requirements Tail recursion ˆ If a recursive predicate has no continuation, and no backtracking point, Prolog can recognize this and will not allocate memory. ˆ Such recursive predicates are called tail recursive (the recursive call is the last in the clause and there are no alternatives). ˆ They are much more effficient than the non-tail recursive variants. 49

50 ˆ The following is tail recursive: t e s t 1 (N): write (N), nl, NewN i s N+1, t e s t 1 (NewN ). In the above write writes (prints) the argument on the console and succeeds, nl moves on a new line and succeeds. The predicate will print natural numbers on the console until the resources run out (memory or number representations limit). ˆ The following is not tail recursive (it has a continuation): t e s t 2 (N): write (N), nl, NewN i s N+1, t e s t 2 (NewN), nl. When running this, it will run out of memory relatively soon. ˆ The following is not tail recursive (it has a backtracking point): t e s t 3 (N): write (N), nl, NewN i s N+1, t e s t 3 (NewN ). t e s t 3 (N): N<0. ˆ The following is tail recursive (the alternative clause comes before the recursive clause so there is no backtracking point for the recursive call): t e s t 3 a (N): N<0. t e s t 3 a (N): write (N), nl, NewN i s N+1, t e s t 3 a (NewN ). ˆ The following is not tail recursive (it has alternatives for predicates in the recursive clause preceding the recursive call, so backtracking may be necessary): t e s t 4 (N): write (N), nl, m(n, NewN), t e s t 4 (NewN ). m(n, NewN): N >= 0, NewN i s N + 1. m(n, NewN): N < 0, NewN i s ( 1)*N. Making recursive predicates tail recursive ˆ If a predicate is not tail recursive because it has backtracking points, then it can be made so by using the cut before the recursive call. ˆ The following are now tail recursive: t e s t 5 (N): write (N), nl, NewN i s N+1,!, t e s t 5 (NewN ). t e s t 5 (N): N<0. t e s t 6 (N): write (N), nl, m(n, NewN),!, t e s t 6 (NewN ). m(n, NewN): N >= 0, NewN i s N + 1. m(n, NewN): N < 0, NewN i s ( 1)*N. 50

Brief Introduction to Prolog

Brief Introduction to Prolog Brief to Prolog Joana Côrte-Real jcr@dcc.fc.up.pt CRACS & INESC TEC Faculty of Sciences University of Porto University of Aizu 5th December 2014 1 / 27 Overview 1 to Prolog Prolog Syntax Tutorial 1 2 Lists

More information

Accumulators More on Arithmetic and Recursion

Accumulators More on Arithmetic and Recursion Accumulators More on Arithmetic and Recursion Shakil M. Khan adapted from Gunnar Gotshalks listlen ( L, N ) L is a list of length N if... listlen ( [], 0 ). listlen ( [ H T ], N ) :- listlen ( T, N1 ),

More information

Most General computer?

Most General computer? Turing Machines Most General computer? DFAs are simple model of computation. Accept only the regular languages. Is there a kind of computer that can accept any language, or compute any function? Recall

More information

CS1021. Why logic? Logic about inference or argument. Start from assumptions or axioms. Make deductions according to rules of reasoning.

CS1021. Why logic? Logic about inference or argument. Start from assumptions or axioms. Make deductions according to rules of reasoning. 3: Logic Why logic? Logic about inference or argument Start from assumptions or axioms Make deductions according to rules of reasoning Logic 3-1 Why logic? (continued) If I don t buy a lottery ticket on

More information

TDDD08 Tutorial 1. Who? From? When? 6 september Victor Lagerkvist (& Wªodek Drabent)

TDDD08 Tutorial 1. Who? From? When? 6 september Victor Lagerkvist (& Wªodek Drabent) TDDD08 Tutorial 1 Who? From? Victor Lagerkvist (& Wªodek Drabent) Theoretical Computer Science Laboratory, Linköpings Universitet, Sweden When? 6 september 2015 1 / 18 Preparations Before you start with

More information

Lecture Notes on Inductive Definitions

Lecture Notes on Inductive Definitions Lecture Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 August 28, 2003 These supplementary notes review the notion of an inductive definition and give

More information

Supplementary Notes on Inductive Definitions

Supplementary Notes on Inductive Definitions Supplementary Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 August 29, 2002 These supplementary notes review the notion of an inductive definition

More information

CS187 - Science Gateway Seminar for CS and Math

CS187 - Science Gateway Seminar for CS and Math CS187 - Science Gateway Seminar for CS and Math Fall 2013 Class 3 Sep. 10, 2013 What is (not) Computer Science? Network and system administration? Playing video games? Learning to use software packages?

More information

Rule-Based Classifiers

Rule-Based Classifiers Rule-Based Classifiers For completeness, the table includes all 16 possible logic functions of two variables. However, we continue to focus on,,, and. 1 Propositional Logic Meta-theory. Inspection of a

More information

Introduction to Turing Machines. Reading: Chapters 8 & 9

Introduction to Turing Machines. Reading: Chapters 8 & 9 Introduction to Turing Machines Reading: Chapters 8 & 9 1 Turing Machines (TM) Generalize the class of CFLs: Recursively Enumerable Languages Recursive Languages Context-Free Languages Regular Languages

More information

cis32-ai lecture # 18 mon-3-apr-2006

cis32-ai lecture # 18 mon-3-apr-2006 cis32-ai lecture # 18 mon-3-apr-2006 today s topics: propositional logic cis32-spring2006-sklar-lec18 1 Introduction Weak (search-based) problem-solving does not scale to real problems. To succeed, problem

More information

Understanding Computation

Understanding Computation Understanding Computation 1 Mathematics & Computation -Mathematics has been around for a long time as a method of computing. -Efforts to find canonical way of computations. - Machines have helped with

More information

Introduction to Metalogic

Introduction to Metalogic Philosophy 135 Spring 2008 Tony Martin Introduction to Metalogic 1 The semantics of sentential logic. The language L of sentential logic. Symbols of L: Remarks: (i) sentence letters p 0, p 1, p 2,... (ii)

More information

(a) Definition of TMs. First Problem of URMs

(a) Definition of TMs. First Problem of URMs Sec. 4: Turing Machines First Problem of URMs (a) Definition of the Turing Machine. (b) URM computable functions are Turing computable. (c) Undecidability of the Turing Halting Problem That incrementing

More information

CSE355 SUMMER 2018 LECTURES TURING MACHINES AND (UN)DECIDABILITY

CSE355 SUMMER 2018 LECTURES TURING MACHINES AND (UN)DECIDABILITY CSE355 SUMMER 2018 LECTURES TURING MACHINES AND (UN)DECIDABILITY RYAN DOUGHERTY If we want to talk about a program running on a real computer, consider the following: when a program reads an instruction,

More information

Lecture Notes on Inductive Definitions

Lecture Notes on Inductive Definitions Lecture Notes on Inductive Definitions 15-312: Foundations of Programming Languages Frank Pfenning Lecture 2 September 2, 2004 These supplementary notes review the notion of an inductive definition and

More information

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata CISC 4090: Theory of Computation Chapter Regular Languages Xiaolan Zhang, adapted from slides by Prof. Werschulz Section.: Finite Automata Fordham University Department of Computer and Information Sciences

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 15 Ana Bove May 17th 2018 Recap: Context-free Languages Chomsky hierarchy: Regular languages are also context-free; Pumping lemma

More information

Turing Machines, diagonalization, the halting problem, reducibility

Turing Machines, diagonalization, the halting problem, reducibility Notes on Computer Theory Last updated: September, 015 Turing Machines, diagonalization, the halting problem, reducibility 1 Turing Machines A Turing machine is a state machine, similar to the ones we have

More information

Theory of Computation

Theory of Computation Theory of Computation Lecture #2 Sarmad Abbasi Virtual University Sarmad Abbasi (Virtual University) Theory of Computation 1 / 1 Lecture 2: Overview Recall some basic definitions from Automata Theory.

More information

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010

CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010 CSC 5170: Theory of Computational Complexity Lecture 4 The Chinese University of Hong Kong 1 February 2010 Computational complexity studies the amount of resources necessary to perform given computations.

More information

Lecture 7. Logic. Section1: Statement Logic.

Lecture 7. Logic. Section1: Statement Logic. Ling 726: Mathematical Linguistics, Logic, Section : Statement Logic V. Borschev and B. Partee, October 5, 26 p. Lecture 7. Logic. Section: Statement Logic.. Statement Logic..... Goals..... Syntax of Statement

More information

Foundations of Logic Programming

Foundations of Logic Programming Foundations of Logic Programming Deductive Logic e.g. of use: Gypsy specifications and proofs About deductive logic (Gödel, 1931) Interesting systems (with a finite number of axioms) are necessarily either:

More information

Gödel s Incompleteness Theorem. Overview. Computability and Logic

Gödel s Incompleteness Theorem. Overview. Computability and Logic Gödel s Incompleteness Theorem Overview Computability and Logic Recap Remember what we set out to do in this course: Trying to find a systematic method (algorithm, procedure) which we can use to decide,

More information

Turing machines and linear bounded automata

Turing machines and linear bounded automata and linear bounded automata Informatics 2A: Lecture 30 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 25 November 2016 1 / 17 The Chomsky hierarchy: summary Level Language

More information

Advanced Topics in LP and FP

Advanced Topics in LP and FP Lecture 3: Logic Programming in Prolog Outline 1 2 3 The control of execution Logic Programming Declarative programming style: Emphasis on What is the solution? instead of How to find the solution? Symbolic

More information

Introduction to Turing Machines

Introduction to Turing Machines Introduction to Turing Machines Deepak D Souza Department of Computer Science and Automation Indian Institute of Science, Bangalore. 12 November 2015 Outline 1 Turing Machines 2 Formal definitions 3 Computability

More information

Theory of Computing Tamás Herendi

Theory of Computing Tamás Herendi Theory of Computing Tamás Herendi Theory of Computing Tamás Herendi Publication date 2014 Table of Contents 1 Preface 1 2 Formal languages 2 3 Order of growth rate 9 4 Turing machines 16 1 The definition

More information

3 Propositional Logic

3 Propositional Logic 3 Propositional Logic 3.1 Syntax 3.2 Semantics 3.3 Equivalence and Normal Forms 3.4 Proof Procedures 3.5 Properties Propositional Logic (25th October 2007) 1 3.1 Syntax Definition 3.0 An alphabet Σ consists

More information

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 8 Nancy Lynch

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 8 Nancy Lynch 6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, 2010 Class 8 Nancy Lynch Today More undecidable problems: About Turing machines: Emptiness, etc. About

More information

Strong AI vs. Weak AI Automated Reasoning

Strong AI vs. Weak AI Automated Reasoning Strong AI vs. Weak AI Automated Reasoning George F Luger ARTIFICIAL INTELLIGENCE 6th edition Structures and Strategies for Complex Problem Solving Artificial intelligence can be classified into two categories:

More information

Introduction to Languages and Computation

Introduction to Languages and Computation Introduction to Languages and Computation George Voutsadakis 1 1 Mathematics and Computer Science Lake Superior State University LSSU Math 400 George Voutsadakis (LSSU) Languages and Computation July 2014

More information

Introducing Proof 1. hsn.uk.net. Contents

Introducing Proof 1. hsn.uk.net. Contents Contents 1 1 Introduction 1 What is proof? 1 Statements, Definitions and Euler Diagrams 1 Statements 1 Definitions Our first proof Euler diagrams 4 3 Logical Connectives 5 Negation 6 Conjunction 7 Disjunction

More information

Price: $25 (incl. T-Shirt, morning tea and lunch) Visit:

Price: $25 (incl. T-Shirt, morning tea and lunch) Visit: Three days of interesting talks & workshops from industry experts across Australia Explore new computing topics Network with students & employers in Brisbane Price: $25 (incl. T-Shirt, morning tea and

More information

Logic for Computer Science - Week 2 The Syntax of Propositional Logic

Logic for Computer Science - Week 2 The Syntax of Propositional Logic Logic for Computer Science - Week 2 The Syntax of Propositional Logic Ștefan Ciobâcă November 30, 2017 1 An Introduction to Logical Formulae In the previous lecture, we have seen what makes an argument

More information

First-Order Logic. Chapter Overview Syntax

First-Order Logic. Chapter Overview Syntax Chapter 10 First-Order Logic 10.1 Overview First-Order Logic is the calculus one usually has in mind when using the word logic. It is expressive enough for all of mathematics, except for those concepts

More information

6. Logical Inference

6. Logical Inference Artificial Intelligence 6. Logical Inference Prof. Bojana Dalbelo Bašić Assoc. Prof. Jan Šnajder University of Zagreb Faculty of Electrical Engineering and Computing Academic Year 2016/2017 Creative Commons

More information

Propositional and Predicate Logic - V

Propositional and Predicate Logic - V Propositional and Predicate Logic - V Petr Gregor KTIML MFF UK WS 2016/2017 Petr Gregor (KTIML MFF UK) Propositional and Predicate Logic - V WS 2016/2017 1 / 21 Formal proof systems Hilbert s calculus

More information

Turing machines and linear bounded automata

Turing machines and linear bounded automata and linear bounded automata Informatics 2A: Lecture 29 John Longley School of Informatics University of Edinburgh jrl@inf.ed.ac.uk 27 November 2015 1 / 15 The Chomsky hierarchy: summary Level Language

More information

The non-logical symbols determine a specific F OL language and consists of the following sets. Σ = {Σ n } n<ω

The non-logical symbols determine a specific F OL language and consists of the following sets. Σ = {Σ n } n<ω 1 Preliminaries In this chapter we first give a summary of the basic notations, terminology and results which will be used in this thesis. The treatment here is reduced to a list of definitions. For the

More information

Predicate Logic: Syntax

Predicate Logic: Syntax Predicate Logic: Syntax Alice Gao Lecture 12 Based on work by J. Buss, L. Kari, A. Lubiw, B. Bonakdarpour, D. Maftuleac, C. Roberts, R. Trefler, and P. Van Beek 1/31 Outline Syntax of Predicate Logic Learning

More information

About the relationship between formal logic and complexity classes

About the relationship between formal logic and complexity classes About the relationship between formal logic and complexity classes Working paper Comments welcome; my email: armandobcm@yahoo.com Armando B. Matos October 20, 2013 1 Introduction We analyze a particular

More information

INTRODUCTION TO PREDICATE LOGIC HUTH AND RYAN 2.1, 2.2, 2.4

INTRODUCTION TO PREDICATE LOGIC HUTH AND RYAN 2.1, 2.2, 2.4 INTRODUCTION TO PREDICATE LOGIC HUTH AND RYAN 2.1, 2.2, 2.4 Neil D. Jones DIKU 2005 Some slides today new, some based on logic 2004 (Nils Andersen), some based on kernebegreber (NJ 2005) PREDICATE LOGIC:

More information

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65 Undecidable Problems Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, 2018 1/ 65 Algorithmically Solvable Problems Let us assume we have a problem P. If there is an algorithm solving

More information

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory CS 275 Automata and Formal Language Theory Course Notes Part III: Limits of Computation Chapter III.1: Introduction Anton Setzer http://www.cs.swan.ac.uk/ csetzer/lectures/ automataformallanguage/current/index.html

More information

CHAPTER 1: Functions

CHAPTER 1: Functions CHAPTER 1: Functions 1.1: Functions 1.2: Graphs of Functions 1.3: Basic Graphs and Symmetry 1.4: Transformations 1.5: Piecewise-Defined Functions; Limits and Continuity in Calculus 1.6: Combining Functions

More information

Mathematics 114L Spring 2018 D.A. Martin. Mathematical Logic

Mathematics 114L Spring 2018 D.A. Martin. Mathematical Logic Mathematics 114L Spring 2018 D.A. Martin Mathematical Logic 1 First-Order Languages. Symbols. All first-order languages we consider will have the following symbols: (i) variables v 1, v 2, v 3,... ; (ii)

More information

A Little History Incompleteness The First Theorem The Second Theorem Implications. Gödel s Theorem. Anders O.F. Hendrickson

A Little History Incompleteness The First Theorem The Second Theorem Implications. Gödel s Theorem. Anders O.F. Hendrickson Gödel s Theorem Anders O.F. Hendrickson Department of Mathematics and Computer Science Concordia College, Moorhead, MN Math/CS Colloquium, November 15, 2011 Outline 1 A Little History 2 Incompleteness

More information

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory CS 275 Automata and Formal Language Theory Course Notes Part III: Limits of Computation Chapt. III.1: Introduction Anton Setzer http://www.cs.swan.ac.uk/ csetzer/lectures/ automataformallanguage/current/index.html

More information

What are the recursion theoretic properties of a set of axioms? Understanding a paper by William Craig Armando B. Matos

What are the recursion theoretic properties of a set of axioms? Understanding a paper by William Craig Armando B. Matos What are the recursion theoretic properties of a set of axioms? Understanding a paper by William Craig Armando B. Matos armandobcm@yahoo.com February 5, 2014 Abstract This note is for personal use. It

More information

INTRODUCTION TO LOGIC. Propositional Logic. Examples of syntactic claims

INTRODUCTION TO LOGIC. Propositional Logic. Examples of syntactic claims Introduction INTRODUCTION TO LOGIC 2 Syntax and Semantics of Propositional Logic Volker Halbach In what follows I look at some formal languages that are much simpler than English and define validity of

More information

Extensions to the Logic of All x are y: Verbs, Relative Clauses, and Only

Extensions to the Logic of All x are y: Verbs, Relative Clauses, and Only 1/53 Extensions to the Logic of All x are y: Verbs, Relative Clauses, and Only Larry Moss Indiana University Nordic Logic School August 7-11, 2017 2/53 An example that we ll see a few times Consider the

More information

First-Order Logic First-Order Theories. Roopsha Samanta. Partly based on slides by Aaron Bradley and Isil Dillig

First-Order Logic First-Order Theories. Roopsha Samanta. Partly based on slides by Aaron Bradley and Isil Dillig First-Order Logic First-Order Theories Roopsha Samanta Partly based on slides by Aaron Bradley and Isil Dillig Roadmap Review: propositional logic Syntax and semantics of first-order logic (FOL) Semantic

More information

1 Propositional Logic

1 Propositional Logic CS 2800, Logic and Computation Propositional Logic Lectures Pete Manolios Version: 384 Spring 2011 1 Propositional Logic The study of logic was initiated by the ancient Greeks, who were concerned with

More information

Propositional logic. First order logic. Alexander Clark. Autumn 2014

Propositional logic. First order logic. Alexander Clark. Autumn 2014 Propositional logic First order logic Alexander Clark Autumn 2014 Formal Logic Logical arguments are valid because of their form. Formal languages are devised to express exactly that relevant form and

More information

1 Maintaining a Dictionary

1 Maintaining a Dictionary 15-451/651: Design & Analysis of Algorithms February 1, 2016 Lecture #7: Hashing last changed: January 29, 2016 Hashing is a great practical tool, with an interesting and subtle theory too. In addition

More information

Advanced Topics in LP and FP

Advanced Topics in LP and FP Lecture 1: Prolog and Summary of this lecture 1 Introduction to Prolog 2 3 Truth value evaluation 4 Prolog Logic programming language Introduction to Prolog Introduced in the 1970s Program = collection

More information

CS 125 Section #10 (Un)decidability and Probability November 1, 2016

CS 125 Section #10 (Un)decidability and Probability November 1, 2016 CS 125 Section #10 (Un)decidability and Probability November 1, 2016 1 Countability Recall that a set S is countable (either finite or countably infinite) if and only if there exists a surjective mapping

More information

Turing Machines Part III

Turing Machines Part III Turing Machines Part III Announcements Problem Set 6 due now. Problem Set 7 out, due Monday, March 4. Play around with Turing machines, their powers, and their limits. Some problems require Wednesday's

More information

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010)

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010) http://math.sun.ac.za/amsc/sam Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics 2009-2010 Lecture notes in progress (27 March 2010) Contents 2009 Semester I: Elements 5 1. Cartesian product

More information

Computation. Some history...

Computation. Some history... Computation Motivating questions: What does computation mean? What are the similarities and differences between computation in computers and in natural systems? What are the limits of computation? Are

More information

Introduction to Metalogic

Introduction to Metalogic Introduction to Metalogic Hans Halvorson September 21, 2016 Logical grammar Definition. A propositional signature Σ is a collection of items, which we call propositional constants. Sometimes these propositional

More information

Turing Machines. Lecture 8

Turing Machines. Lecture 8 Turing Machines Lecture 8 1 Course Trajectory We will see algorithms, what can be done. But what cannot be done? 2 Computation Problem: To compute a function F that maps each input (a string) to an output

More information

A Short Introduction to Hoare Logic

A Short Introduction to Hoare Logic A Short Introduction to Hoare Logic Supratik Chakraborty I.I.T. Bombay June 23, 2008 Supratik Chakraborty (I.I.T. Bombay) A Short Introduction to Hoare Logic June 23, 2008 1 / 34 Motivation Assertion checking

More information

Guest Speaker. CS 416 Artificial Intelligence. First-order logic. Diagnostic Rules. Causal Rules. Causal Rules. Page 1

Guest Speaker. CS 416 Artificial Intelligence. First-order logic. Diagnostic Rules. Causal Rules. Causal Rules. Page 1 Page 1 Guest Speaker CS 416 Artificial Intelligence Lecture 13 First-Order Logic Chapter 8 Topics in Optimal Control, Minimax Control, and Game Theory March 28 th, 2 p.m. OLS 005 Onesimo Hernandez-Lerma

More information

Proof Rules for Correctness Triples

Proof Rules for Correctness Triples Proof Rules for Correctness Triples CS 536: Science of Programming, Fall 2018 A. Why? We can t generally prove that correctness triples are valid using truth tables. We need proof axioms for atomic statements

More information

Gödel s Incompleteness Theorem. Overview. Computability and Logic

Gödel s Incompleteness Theorem. Overview. Computability and Logic Gödel s Incompleteness Theorem Overview Computability and Logic Recap Remember what we set out to do in this course: Trying to find a systematic method (algorithm, procedure) which we can use to decide,

More information

CS 301. Lecture 17 Church Turing thesis. Stephen Checkoway. March 19, 2018

CS 301. Lecture 17 Church Turing thesis. Stephen Checkoway. March 19, 2018 CS 301 Lecture 17 Church Turing thesis Stephen Checkoway March 19, 2018 1 / 17 An abridged modern history of formalizing algorithms An algorithm is a finite, unambiguous sequence of steps for solving a

More information

Resolution (14A) Young W. Lim 8/15/14

Resolution (14A) Young W. Lim 8/15/14 Resolution (14A) Young W. Lim Copyright (c) 2013-2014 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version

More information

Herbrand Theorem, Equality, and Compactness

Herbrand Theorem, Equality, and Compactness CSC 438F/2404F Notes (S. Cook and T. Pitassi) Fall, 2014 Herbrand Theorem, Equality, and Compactness The Herbrand Theorem We now consider a complete method for proving the unsatisfiability of sets of first-order

More information

Introduction to Logic in Computer Science: Autumn 2006

Introduction to Logic in Computer Science: Autumn 2006 Introduction to Logic in Computer Science: Autumn 2006 Ulle Endriss Institute for Logic, Language and Computation University of Amsterdam Ulle Endriss 1 Plan for Today Today s class will be an introduction

More information

Classical Propositional Logic

Classical Propositional Logic The Language of A Henkin-style Proof for Natural Deduction January 16, 2013 The Language of A Henkin-style Proof for Natural Deduction Logic Logic is the science of inference. Given a body of information,

More information

where Q is a finite set of states

where Q is a finite set of states Space Complexity So far most of our theoretical investigation on the performances of the various algorithms considered has focused on time. Another important dynamic complexity measure that can be associated

More information

Definite Logic Programs

Definite Logic Programs Chapter 2 Definite Logic Programs 2.1 Definite Clauses The idea of logic programming is to use a computer for drawing conclusions from declarative descriptions. Such descriptions called logic programs

More information

Automata Theory and Formal Grammars: Lecture 1

Automata Theory and Formal Grammars: Lecture 1 Automata Theory and Formal Grammars: Lecture 1 Sets, Languages, Logic Automata Theory and Formal Grammars: Lecture 1 p.1/72 Sets, Languages, Logic Today Course Overview Administrivia Sets Theory (Review?)

More information

The semantics of propositional logic

The semantics of propositional logic The semantics of propositional logic Readings: Sections 1.3 and 1.4 of Huth and Ryan. In this module, we will nail down the formal definition of a logical formula, and describe the semantics of propositional

More information

CISC4090: Theory of Computation

CISC4090: Theory of Computation CISC4090: Theory of Computation Chapter 2 Context-Free Languages Courtesy of Prof. Arthur G. Werschulz Fordham University Department of Computer and Information Sciences Spring, 2014 Overview In Chapter

More information

Theory of Computation

Theory of Computation Theory of Computation Dr. Sarmad Abbasi Dr. Sarmad Abbasi () Theory of Computation / Lecture 3: Overview Decidability of Logical Theories Presburger arithmetic Decidability of Presburger Arithmetic Dr.

More information

Decision Problems with TM s. Lecture 31: Halting Problem. Universe of discourse. Semi-decidable. Look at following sets: CSCI 81 Spring, 2012

Decision Problems with TM s. Lecture 31: Halting Problem. Universe of discourse. Semi-decidable. Look at following sets: CSCI 81 Spring, 2012 Decision Problems with TM s Look at following sets: Lecture 31: Halting Problem CSCI 81 Spring, 2012 Kim Bruce A TM = { M,w M is a TM and w L(M)} H TM = { M,w M is a TM which halts on input w} TOTAL TM

More information

Predicate Logic - Undecidability

Predicate Logic - Undecidability CS402, Spring 2016 Undecidable Problems Does the following program halts? (1) N : n, total, x, y, z (2) n GetUserInput() (3) total 3 (4) while true (5) for x 1 to total 2 (6) for y 1 to total x 1 (7) z

More information

Propositional Logic: Methods of Proof (Part II)

Propositional Logic: Methods of Proof (Part II) Propositional Logic: Methods of Proof (Part II) You will be expected to know Basic definitions Inference, derive, sound, complete Conjunctive Normal Form (CNF) Convert a Boolean formula to CNF Do a short

More information

Deterministic Finite Automata

Deterministic Finite Automata Deterministic Finite Automata COMP2600 Formal Methods for Software Engineering Ranald Clouston Australian National University Semester 2, 2013 COMP 2600 Deterministic Finite Automata 1 Pop quiz What is

More information

Math 300 Introduction to Mathematical Reasoning Autumn 2017 Inverse Functions

Math 300 Introduction to Mathematical Reasoning Autumn 2017 Inverse Functions Math 300 Introduction to Mathematical Reasoning Autumn 2017 Inverse Functions Please read this pdf in place of Section 6.5 in the text. The text uses the term inverse of a function and the notation f 1

More information

Church s undecidability result

Church s undecidability result Church s undecidability result Alan Turing Birth Centennial Talk at IIT Bombay, Mumbai Joachim Breitner April 21, 2011 Welcome, and thank you for the invitation to speak about Church s lambda calculus

More information

Lecture Notes on Logic Programming

Lecture Notes on Logic Programming Lecture Notes on Logic Programming 15-317: Constructive Logic Frank Pfenning Lecture 13 October 13, 2009 1 Computation vs Deduction Logic programming is a particular way to approach programming Other paradigms

More information

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014 Clojure Concurrency Constructs, Part Two CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014 1 Goals Cover the material presented in Chapter 4, of our concurrency textbook In particular,

More information

ILP = Logic, CS, ML Stop counting, start reasoning

ILP = Logic, CS, ML Stop counting, start reasoning ILP = Logic, CS, ML Stop counting, start reasoning Gilles Richard AOC team The story so far Several actors K. Brouwer K. Godel J. Herbrand A. Colmerauer R. Kowalski S. Muggleton L. Brouwer (1881-1966)

More information

The Turing machine model of computation

The Turing machine model of computation The Turing machine model of computation For most of the remainder of the course we will study the Turing machine model of computation, named after Alan Turing (1912 1954) who proposed the model in 1936.

More information

Proof Techniques (Review of Math 271)

Proof Techniques (Review of Math 271) Chapter 2 Proof Techniques (Review of Math 271) 2.1 Overview This chapter reviews proof techniques that were probably introduced in Math 271 and that may also have been used in a different way in Phil

More information

Lecture Notes on From Rules to Propositions

Lecture Notes on From Rules to Propositions Lecture Notes on From Rules to Propositions 15-816: Substructural Logics Frank Pfenning Lecture 2 September 1, 2016 We review the ideas of ephemeral truth and linear inference with another example from

More information

Introduction to Computer Science and Programming for Astronomers

Introduction to Computer Science and Programming for Astronomers Introduction to Computer Science and Programming for Astronomers Lecture 8. István Szapudi Institute for Astronomy University of Hawaii March 7, 2018 Outline Reminder 1 Reminder 2 3 4 Reminder We have

More information

Turing Machines Part Three

Turing Machines Part Three Turing Machines Part Three What problems can we solve with a computer? What kind of computer? Very Important Terminology Let M be a Turing machine. M accepts a string w if it enters an accept state when

More information

7. Propositional Logic. Wolfram Burgard and Bernhard Nebel

7. Propositional Logic. Wolfram Burgard and Bernhard Nebel Foundations of AI 7. Propositional Logic Rational Thinking, Logic, Resolution Wolfram Burgard and Bernhard Nebel Contents Agents that think rationally The wumpus world Propositional logic: syntax and semantics

More information

Lecture 1 : Data Compression and Entropy

Lecture 1 : Data Compression and Entropy CPS290: Algorithmic Foundations of Data Science January 8, 207 Lecture : Data Compression and Entropy Lecturer: Kamesh Munagala Scribe: Kamesh Munagala In this lecture, we will study a simple model for

More information

Large Numbers, Busy Beavers, Noncomputability and Incompleteness

Large Numbers, Busy Beavers, Noncomputability and Incompleteness Large Numbers, Busy Beavers, Noncomputability and Incompleteness Food For Thought November 1, 2007 Sam Buss Department of Mathematics U.C. San Diego PART I Large Numbers, Busy Beavers, and Undecidability

More information

SLD-Resolution And Logic Programming (PROLOG)

SLD-Resolution And Logic Programming (PROLOG) Chapter 9 SLD-Resolution And Logic Programming (PROLOG) 9.1 Introduction We have seen in Chapter 8 that the resolution method is a complete procedure for showing unsatisfiability. However, finding refutations

More information

Foundations of Artificial Intelligence

Foundations of Artificial Intelligence Foundations of Artificial Intelligence 7. Propositional Logic Rational Thinking, Logic, Resolution Joschka Boedecker and Wolfram Burgard and Bernhard Nebel Albert-Ludwigs-Universität Freiburg May 17, 2016

More information

Propositional and Predicate Logic. jean/gbooks/logic.html

Propositional and Predicate Logic.   jean/gbooks/logic.html CMSC 630 February 10, 2009 1 Propositional and Predicate Logic Sources J. Gallier. Logic for Computer Science, John Wiley and Sons, Hoboken NJ, 1986. 2003 revised edition available on line at http://www.cis.upenn.edu/

More information

Opleiding Informatica

Opleiding Informatica Opleiding Informatica Tape-quantifying Turing machines in the arithmetical hierarchy Simon Heijungs Supervisors: H.J. Hoogeboom & R. van Vliet BACHELOR THESIS Leiden Institute of Advanced Computer Science

More information

This is logically equivalent to the conjunction of the positive assertion Minimal Arithmetic and Representability

This is logically equivalent to the conjunction of the positive assertion Minimal Arithmetic and Representability 16.2. MINIMAL ARITHMETIC AND REPRESENTABILITY 207 If T is a consistent theory in the language of arithmetic, we say a set S is defined in T by D(x) if for all n, if n is in S, then D(n) is a theorem of

More information