O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.

Slides:



Advertisements
Similar presentations
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Advertisements

Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
CS848: Topics in Databases: Foundations of Query Optimization Topics covered  Introduction to description logic: Single column QL  The ALC family of.
SLD-resolution Introduction Most general unifiers SLD-resolution
Semantics Static semantics Dynamic semantics attribute grammars
10 October 2006 Foundations of Logic and Constraint Programming 1 Unification ­An overview Need for Unification Ranked alfabeths and terms. Substitutions.
1 Chapter 4:Constraint Logic Programs Where we learn about the only programming concept rules, and how programs execute.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
AR for Horn clause logic Introducing: Unification.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Standard Logical Equivalences
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Automated Reasoning Systems For first order Predicate Logic.
First Order Logic Resolution
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
Jan. 2013Dr. Yangjun Chen ACS Outline Signature Files - Signature for attribute values - Signature for records - Searching a signature file Signature.
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
1 Constraint Programming Maurizio Gabbrielli Universita’ di Bologna Slides by: K. Marriott.
Inference and Resolution for Problem Solving
Winter 2004/5Pls – inductive – Catriel Beeri1 Inductive Definitions (our meta-language for specifications)  Examples  Syntax  Semantics  Proof Trees.
Lesson 6. Refinement of the Operator Model This page describes formally how we refine Figure 2.5 into a more detailed model so that we can connect it.
Logic Programming Part 2: Semantics James Cheney CS 411.
Induction and recursion
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
DECIDABILITY OF PRESBURGER ARITHMETIC USING FINITE AUTOMATA Presented by : Shubha Jain Reference : Paper by Alexandre Boudet and Hubert Comon.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Mathematical Preliminaries
Programming Languages Third Edition Chapter 4 Logic Programming.
Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Strings Basic data type in computational biology A string is an ordered succession of characters or symbols from a finite set called an alphabet Sequence.
Logic Programming. Formal Logics- Recap Formulas – w/out quantifiers Free Variables Bound Variables Assignments and satisfaction Validity and satisfiability.
Knowledge Based Information System
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
6/11/2016 Linear Resolution and Introduction to First Order Logic Michael Leuschel Softwaretechnik und Programmiersprachen Lecture 5.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Logic Programming Lecture 2: Unification and proof search.
Logic Programming: Introduction
CS589 Principles of DB Systems Spring 2014 Unit 2: Recursive Query Processing Lecture 2-1 – Naïve algorithm for recursive queries Lois Delcambre (slides.
Introduction to Logic for Artificial Intelligence Lecture 2
Tests, Backtracking, and Recursion
Prolog syntax + Unification
Soundness of SLD-Resolution
Programming Techniques
Presentation transcript:

o A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining relations in the program domain. % Signature: parent (Parent, Child) /2 % Purpose: Parent is a parent of Child parent (erik, jonas). parent (lena, jonas). % Signature: male(Person) /1 % Purpose: Person is a male male (erik). % Signature: father (Dad, Child) /2 % Purpose: Dad is father of Child father (Dad, Child) :- parent(Dad, Child), male (Dad). program rule fact arity procedure predicate The relation father holds between Dad and Child if parent holds and Dad is male 1 Logic Programming: Introduction

2 The prolog interpreter operates in a read-eval-print loop. Given a query, it attempts to prove it based on the program: o If it fails, it answers false. o Else, if the query has no variables, it answers true. o Else, it outputs all possible variables assignments found during proof process. Logic Programming: Introduction % Signature: parent (Parent, Child) /2 % Purpose: Parent is a parent of Child parent(erik, jonas). parent(lena, jonas). % Signature: male(Person) /1 % Purpose: Person is a male male(erik). % Signature: father (Dad, Child) /2 % Purpose: Dad is father of Child father(Dad, Child) :- parent(Dad, Child), male(Dad). ? - father(X,Y). X=erik, Y=jonas ? - parent(X,jonas). X=erik ; X=lena Next possible assignment query

3 Logic Programming: Example 1 – logic circuits Connection point Resistor Ground Transistor Power Ground N1 N2 N3 N4 N5 % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). Note: In contrast to resistor, the arguments order in transistor is important. Each has a different role. n1 Tran n5 n3 n4 n2 Res Tran Res Power Ground Source Drain Gate Source Drain Gate Source Drain Gate

A program that models electronic logic circuits: o Connection points: are individual constants. o Logic gates: are relations on the constants. 4 Logic Programming: Example 1 – logic circuits An electronic logic circuit combines: o Electronic components: resistor, transistor. Implement simple logic functions. o Connection points: connects one electronic component to another, or to power or ground. Connection point Resistor Ground Transistor Power Ground N1 N2 N3 N4 N5 % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). % Signature: transistor (Gate, Source, Drain)/3 % Purpose: … 1transistor(n2,ground,n1). 2transistor(n3,n4,n2). 3transistor(n5,ground,n4). Note: In contrast to resistor, the arguments order in transistor is important. Each has a different role.

5 Logic Programming: Example 1 – logic circuits % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). Reminders… o A procedure begins with a contract. o Constants start with lowercase characters. o A predicate name is also a constant, which defines a relation between its arguments. o variables start with uppercase characters (‘_’ for wildcard). o Atomic formulas are either true, false or of the form predicate(t 1,..., t n ), t i is a term. o A rule is a formula defining a relation that depends on certain conditions. o A fact is an atomic formula which is unconditionally true. % Signature: resistor(End1,End2)/2 % Purpose 3resistor(n1, power). 1 resistor(power, n1). 2resistor(power, n2). End1,End2 terms

6 Logic Programming: Example 1 – logic circuits % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). % Signature: resistor(End1,End2)/2 % Purpose: A resistor component connects two ends 1 resistor(power, n1). 2resistor(power, n2). 3resistor(n1, power). 4resistor(n2, power). Reminders… o A query is a sequence of atomic formulas that require a proof: % Signature: resistor(End1,End2)/2 % Purpose 3resistor(n1, power). 1 resistor(power, n1). 2resistor(power, n2). End1,End2 “Does an X exists such that the resistor relation holds for (power, X)?” ?- resistor(power, n1),resistor(n2, power). true; false No more answers ?- resistor(power, X). X = n1 ; X = n2; false Is there another answer?

7 Combining logic gates to create a “not” logic circuit: o The resistor and transistor relations are based on facts. o The relations can be combined by a rule to determine whether or not the not_circuit relation stands for Input and Output. Logic Programming: Example 1 – logic circuits power ground transistor resistor Input Output

% Signature: not_circuit(Input, Output)/2 % Purpose: not logic circuit. not_circuit(Input, Output) :- transistor(Input, ground, Output), resistor(power, Output). % Signature: not_circuit(Input, Output)/2 % Purpose: not logic circuit. not_circuit(Input, Output) :- transistor(Input, ground, Output), resistor(power, Output). Rule head: an atomic formula with variables Rule body “and” 8 power ground transistor resistor Input Output Logic Programming: Example 1 – logic circuits Combining logic gates to create a NOT logic circuit: ?- not_circuit(X,Y). X=n2, Y=n1; false “For all Input and Output: (Input, Output) stands in the relation not_circuit if: (Input, ground, Output) stand in the relation transistor and (power, Output) stand in the relation resistor.”

9 Input2 Output Logic Programming: Example 1 – logic circuits Combining logic gates to create a NAND logic circuit: Input1 % Signature: nand_circuit(Input1, Input2, Output)/3 % Purpose: nand logic circuit nand_circuit(Input1, Input2, Output) :- transistor(Input1, X, Output), transistor(Input2, ground, X), resistor(power, Output). % Signature: nand_circuit(Input1, Input2, Output)/3 % Purpose: nand logic circuit nand_circuit(Input1, Input2, Output) :- transistor(Input1, X, Output), transistor(Input2, ground, X), resistor(power, Output).

10 Logic Programming: Example 1 – logic circuits האם יש יחס and במערכת ? ?- not_circuit(X, Y), nand_circuit(In1, In2, X). X = n2, Y = n1, In1 = n3, In2 = n5; false Connection point Ground Power Ground N1 N2 N3 N4 N5

11 Semantics: Unification algorithm A program execution is triggered by a query in attempt to prove it goals: o To find a possible proof, the answer-query algorithm is used. o It makes multiple attempts to apply rules on a selected goal. o This is done by applying a unification algorithm, Unify, to the rule head and the goal.

12 Definitions: 1.binding: a non-circular expression, X=t, where X is a variable and t is a term not including X. 2.Substitution: a function from a finite set of variables to a finite set of terms (in other words: a finite set of bindings with no variable repetitions). 3.Application (º) of a substitution S to an atomic formula A replaces variables in A with corresponding terms in S (the result is an instance of A). For example: S = {J=X}A = not_circuit(J, J), A º S = not_circuit(X, X) B = not_circuit(X,Y), B º S = not_circuit(X, Y) 4.Unifier: a substitution S is called a unifier of formulas A and B if A º S = B º S. For example: S = {J=X} º {X=Y} = {J=Y, X=Y}A º S = not_circuit(Y, Y) B º S = not_circuit(Y, Y) Semantics: Unification algorithm The algorithm Unify receives two atomic formulas and returns their most general unifier. S = {J=5, X=5, Y=5} is a unifier for A and B, but not the most general one.

Combinations of substitutions (shown in class): In the following example: We combine the substitution S1={X=Y, Z=3, U=V} with the substitution S2= {Y=4, W=5, V=U, Z=X}. This is denote by: S1 º S2. (2 nd step) Variables in S2 that have a binding in S1 are removed from S2. So far : S1={ X=4, Z=3, U=U }, S2 remains the same So far : S1={ X=4, Z=3, U=U }, S2= { Y=4, W=5, V=U} (4 rd step) Identity bindings are removed. The result : S1={ X=4, Z=3, Y=4, W=5, V=U} (3 rd step) S2 is added to S1. So far : S1={ X=4, Z=3, U=U, Y=4, W=5, V=U} (1 st step) S2is applied to the terms of S1: { X=Y, Z=3, U=V }, { Y=4, W=5, V=U, Z=X } 13 Semantics: Unification algorithm

14 Semantics: Proof trees Executing answer-query: o The interpreter searches for a proof for a given query (a conjunction of goals). o The search is done by building and traversing a proof tree where all possibilities are examined. o The possible outcome is one of the following:  The algorithm finishes, and possible values of the query variables are given.  The algorithm finishes, but there is no proof for the query (false).  The proof attempt loops and never ends.

15 Semantics: Proof trees The tree structure depends on Prolog's goal selection and rule selection policies: 1.Query goals (Q 1,…,Q n ) are at the root of the proof tree. 2.Choose current goal (atomic formula). Prolog's policy: the leftmost goal. 3.Choose current rule to prove current goal. (top to bottom program order). 4.Rename the variables in the rule and unify the rule head with the goal. If unification succeeds: 1.A new child node is created. 2.The query for this node is the rule body. 5.A leaf may be created if the goal list is empty (success), or if the goal cannot be proven (failure). 6.Backtracking: When a leaf is reached, the search travels up to the first parent node where another rule can be matched. Q = ?- Q 1,..., Q n

resistor(power, n1). resistor(power, n2). resistor(n1, power). resistor(n2, power). resistor(power, n1). resistor(power, n2). resistor(n1, power). resistor(n2, power). nand_circuit(In1, In2, Out) transistor(In1, X_1, Out), transistor(In2, ground, X_1), resistor(power, Out) transistor(In2,ground,ground), resistor(power, n1) transistor(In2,ground,n4), resistor(power, n2) nand_circuit(Input1,Input2,Output) :- transistor(Input1,X,Output), transistor(Input2,ground,X), resistor(power, Output). nand_circuit(Input1,Input2,Output) :- transistor(Input1,X,Output), transistor(Input2,ground,X), resistor(power, Output). { In1=n2, X_1=ground, Out=n1} Fact 1 – transistor transistor(n2,ground,n1). transistor(n3,n4,n2). transistor(n5,ground,n4). transistor(n2,ground,n1). transistor(n3,n4,n2). transistor(n5,ground,n4). { In1=n3, X_1=n4, Out=n2} Fact 2 – transistor fail { In2=n3} Fact 2 – transistor fail { In2=n5} Fact 3 – transistor true resistor(power, n2) { In2=n2} Fact 1 – transistor fail { In2=n3} Fact 2 – transistor { In2=n5} Fact 3 – transistor { In2=n2} Fact 1 – transistor fail { Input1_1 = In1, Input2_1 = In2, Output_1 = Out } Rule 1 mgu 16 Semantics: Example 2 – A generated proof tree ?- nand_circuit(In1, In2, Out). transistor(In2,ground,ground), resistor(power, n2) { In1=n5, X_1=ground, Out=n4} Fact 2 – transistor a finite success tree with one success path.

17 Semantics: proof trees Possible types of proof trees: o A success tree has at least one success path in it. o A failure tree is one in which every path is a failure path. o A proof tree is an infinite tree if it contains an infinite path. o Otherwise, it is a finite tree. Example: An infinite is generated by repeatedly applying the rule p(X):-p(Y),q(X,Y) (left recursion). To avoid the infinite path, we could rewrite the rule: p(X):- q(X,Y),p(Y) (tail recursion).

An example: Relational logic programming & SQL operations. Table name: resistor Schema: End1, End2 Data: (power, n1), (power, n2), (n1, power), (n2, power). Table name: transistor Schema: Gate, Source, Drain Data: (n2, ground, n1) (n3, n4, n2), (n5, ground, n4). % Signature: res_join_trans(End1, X, Gate, Source)/4 % Purpose: Join between resistor and transistor % according to End2 of resistor and Gate of transistor. res_join_trans(End1, X, Source, Drain):- resistor(End1,X), transistor(X, Source, Drain). % Signature: res_join_trans(End1, X, Gate, Source)/4 % Purpose: Join between resistor and transistor % according to End2 of resistor and Gate of transistor. res_join_trans(End1, X, Source, Drain):- resistor(End1,X), transistor(X, Source, Drain). End1= power, X= n2, Source= ground, Drain= n1 ; false. ?-res_join_trans(End1,X,Source,Drain). SQL Operation: Natural join 18 Semantics: Example 3 – SQL in Relational Logic Programming o Relations may be regarded as tables in a database of facts. o Recall the resistor and transistor relations presented earlier:

19 An example: Relational logic programming & SQL operations. Semantics: Example 3 – SQL in Relational Logic Programming o Relations may be regarded as tables in a database of facts. o Recall the resistor and transistor relations presented earlier: Table name: resistor Schema: End1, End2 Data: (power, n1), (power, n2), (n1, power), (n2, power). Table name: transistor Schema: Gate, Source, Drain Data: (n2, ground, n1) (n3, n4, n2), (n5, ground, n4). X = power, Y = n1 ; X = power, Y = n2 ; X = n1, Y = power ; X = n2, Y = power ; X = power, Y = power ; X = power, Y = n1 ; X = power, Y = n2 ; X = power, Y = power ; X = power, Y = n1;... The resistor relation is symmetric. Therefore, we get a finite series of answers repeated an infinite number of times. Transitive closure for the resistor relation %Signature: res_closure(X, Y)/2 res_closure(X, Y) :- resistor(X, Y). res_closure(X, Y) :- resistor(X, Z), res_closure(Z, Y). %Signature: res_closure(X, Y)/2 res_closure(X, Y) :- resistor(X, Y). res_closure(X, Y) :- resistor(X, Z), res_closure(Z, Y). ?- res_closure(X, Y).

% Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). Q: In the following procedure, which symbols are predicates? Which are functors? Q: how can you tell? by their relative location in the program: A predicate appears as an identifier of an atomic formula. A functor is way to construct a term. A term is a part of a formula. A functor can be nested – a predicate cannot. NOTE: The same name may be used for both a predicate and a functor! 20 An example: Relational logic programming & SQL operations. Full Logic Programming o Relational logic programming has no ability to describe composite data, only atoms. o Full logic programming is equipped with functors to describe composite data.

Unification is more complex with functors. Here is an execution of the Unify algorithm, step by step: Unify(A,B) where A = tree_member (tree (X, 10, f(X)), W) ; B = tree_member (tree (Y, Y, Z), f(Z)). Initially, s={}  Disagreement-set = {X=Y}  X does not occur in Y  s=s  {X=Y} = {X=Y} A  s= tree_member (tree (X, 10, f(Y)), W ) B  s= tree_member (tree (Y, Y, Z ), f(Z)) A  s ≠ B  s  Disagreement-set = {Y=10}  s=s  {Y=10} = {X=10, Y=10} A  s= tree_member (tree (Y, 10, f(Y)), W ) B  s= tree_member (tree (Y, Y,Z ), f(Z)) A  s ≠ B  s  Disagreement-set = {Z=f(10)}  s=s  {Z=f(10)} = {X=10, Y=10, Z=f(10)} A  s= tree_member (tree (10, 10, f(10)), W ) B  s= tree_member (tree (10, 10, Z ), f(Z)) A  s ≠ B  s  Disagreement-set = {W=f(f(10))}  s={X=10, Y=10, Z=f(10), W=f(f(10))} A  s= tree_member (tree (10, 10, f(10)), W ) B  s= tree_member (tree (10, 10, f(10)), f(f(10))) A  s ≠ B  s A  s= tree_member (tree (10, 10, f(10)), f(f(10)) ) B  s= tree_member (tree (10, 10, f(10)), f(f(10))) 21 An example: Relational logic programming & SQL operations. Full Logic Programming: Unification with functors

22 An example: Relational logic programming & SQL operations. Full Logic Programming: Unification with functors A: Consider the following two atomic formulas: A = tree_member (tree (X, Y, f(X)), X ) B = tree_member (tree (Y, Y, Z ), f(Z)) Applying Unify(A,B) will result in a loop: X=Y, Z=f(Y), Y=f(Z)=f(f(Y))… the substitution cannot be successfully solved.

% Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). ?- tree_member(1, tree(1,nil, nil)). true ?- tree_member(2,tree(1,tree(2,nil,nil), tree(3,nil, nil))). true. ?- tree_member(1, tree(3,1, 3)). false. ?- tree_member(X,tree(1,tree(2,nil,nil), tree(3,nil, nil))). X=1; X=2; X=3; false. 23 Full Logic Programming: Example queries

tree_member(X, tree(1, tree(2, nil, nil), tree(3, nil, nil))) 24 ?- tree_member(X, tree(1, tree(2, nil, nil), tree(3, nil, nil))). Full Logic Programming: An example proof tree % Signature: tree_member(Element,Tree)/ 2 % … tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % … tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). {X_1 = 1, X = 1 Left_1 = tree(2,nil,nil, Right_1 = tree(3, nil, nil)} true {X=1 } tree_member (X, tree(2,nil,nil)) {X_1 = X, Y_1 = 1, Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} true {X=2 } tree_member (X, nil) {X_2 = X, Y_2 = 2, Left_2 = nil, Right_2 = nil} fail tree_member (X, nil) {X_2 = X, Y_2 = 2, Left_2 = nil, Right_2 = nil} fail {X_2 = 2, X = 2 Left_2 = nil, Right_2 = nil} {X_1 = X, Y_1 = 1, Left_1 = tree(2,nil,nil), Right_1 = tree(3, nil, nil)} tree_member (X, tree(3,nil,nil)) true {X=3 } tree_member (X, nil) {X_2 = X, Y_2 = 3, Left_2 = nil, Right_2 = nil} fail tree_member (X, nil) {X_2 = X, Y_2 = 3, Left_2 = nil, Right_2 = nil} fail {X_2 = 3, X = 3 Left_2 = nil, Right_2 = nil}

% Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). % Signature: tree_member(Element,Tree)/ 2 % Purpose: Testing tree membership, checks if Element is % an element of the binary tree Tree. tree_member (X,tree(X,Left,Right)). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Left). tree_member (X,tree(Y,Left,Right)):- tree_member(X,Right). ?- tree_member(1, T). T = tree(1, _G445, _G446) ; T = tree(_G444, tree(1, _G449, _G450), _G446) ; T = tree(_G444, tree(_G448, tree(1, _G453, _G454), _G450), _G446) ;... o Looking for all trees in which 1 is a member, we get an infinite success tree with partially instantiated answers (containing variables). o We use a rule that requires a defined input, but our input is a variable. Possible answers are generated by the proof algorithm. o In this case we call the rule a generator rule. 25 Full Logic Programming: An example proof tree