Prolog syntax + Unification

Slides:



Advertisements
Similar presentations
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: ?-
Advertisements

Inference Rules Universal Instantiation Existential Generalization
SLD-resolution Introduction Most general unifiers SLD-resolution
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
10 October 2006 Foundations of Logic and Constraint Programming 1 Unification ­An overview Need for Unification Ranked alfabeths and terms. Substitutions.
Prolog.
AR for Horn clause logic Introducing: Unification.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
First Order Logic Resolution
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
Cs774 (Prasad)L7Negation1 Negation by Failure
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
CSE (c) S. Tanimoto, 2007 Unification 1 Unification Predicate calculus rules are sometimes so general that we need to create specializations of.
Logic Programming Part 2: Semantics James Cheney CS 411.
Introduction to Logic for Artificial Intelligence Lecture 2 Erik Sandewall 2010.
Binary Decision Diagrams for First Order Predicate Logic By: Jan Friso Groote Afsaneh Shirazi.
0 1 Todays Topics Resolution – top down and bottom up j-DREW BU procedure Subsumption – change to procedure Infinite Loops RuleML input – Prolog output.
Formal Models of Computation Part II The Logic Model
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: ?-
Conjunctive normal form: any formula of the predicate calculus can be transformed into a conjunctive normal form. Def. A formula is said to be in conjunctive.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
First Order Predicate Logic
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
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.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Web Science & Technologies University of Koblenz ▪ Landau, Germany Procedural Semantics Soundness of SLD-Resolution.
第 1 6 章 谓词演算中的归结. 2 Outline Unification Predicate-Calculus Resolution Completeness and Soundness Converting Arbitrary wffs to Clause Form Using Resolution.
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)
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
1-1 An Introduction to Logical Programming Sept
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),
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
1 Interactive Computer Theorem Proving CS294-9 November 30, 2006 Adam Chlipala UC Berkeley Lecture 14: Twelf.
Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.
Introduction to Logic for Artificial Intelligence Lecture 2
tautology checking continued
ML: a quasi-functional language with strong typing
Completeness of the SLD-Resolution
The function of knowledge representation scheme is
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
Horn Clauses and Unification
Artificial Intelligence CS370D
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE 311: Foundations of Computing
Soundness of SLD-Resolution
Chapter 11 :: Logic Languages
Motivation for Datalog
Horn Clauses and Unification
Horn Clauses and Unification
CSE (c) S. Tanimoto, 2004 Unification
CSE S. Tanimoto Unification
Chapter Two: Syntax and Meaning of Prolog Programs
Horn Clauses and Unification
CSE (c) S. Tanimoto, 2002 Unification
Chapter 2 Syntax and meaning of prolog programs
Horn Clauses and Unification
Controlling Backtracking : Cuts
Herbrand Semantics Computational Logic Lecture 15
Soundness of SLD-Resolution
Resolution Preliminaries
Presentation transcript:

Prolog syntax + Unification t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ cs7120 (Prasad) L15-PrologUnify

Prolog Syntax Terms (Data objects) Atoms Structures (symbolic) constants E.g., anna, x_25, ‘Tom_’, ::=, <--->, etc Numbers E.g., -25, 100.25e+5, etc Variables E.g., X, Result, _23, _, AnonymousVariable, etc Structures E.g., f(X,23,g(‘Tom’,2)), etc Formulae (Predicate-logic clauses) Facts Rules cs7120 (Prasad) L15-PrologUnify

Using variables Lexical scope of a variable is one clause (fact/rule). eq(X,X). ?-eq(a,b). No ?-eq(b,b). Yes ?-eq(A,B). A = B. p(X,Y). ?-p(a,b). q(_,_). ?-q(a,b). Lexical scope of a variable is one clause (fact/rule). Multiple named variable occurrences can be used to capture “equality” constraints. Bindings to distinct variables are independent (but can be same too). Multiple anonymous variable occurrences are considered distinct. Unique Name Hypothesis – one object cannot have two syntactically different names (soundness of unification) cs7120 (Prasad) L15-PrologUnify

Anonymous Variable p(X,Y). is equivalent to p(_,_). ?- p(a,_). Informally, it is equivalent to: For all x, for all y: p(x, y) holds. ?- p(a,_). There exists x, such that p(a, x) holds. cs7120 (Prasad) L15-PrologUnify

(cont’d) eq(X,X). ?- eq(1,Ans). Ans = 1 q(_,_). ?- q(1,Ans). answer extraction q(_,_). ?- q(1,Ans). Ans unbound interpreted as: for all x, q(1,x) holds cs7120 (Prasad) L15-PrologUnify

Structures (~ records) add(5, mul(a,b)) arguments functor [name/arity] Structures encode labeled trees (or DAGs if they contain variables). cs7120 (Prasad) L15-PrologUnify

Matching Basic operation (cf. assignment) t1 matches t2 if t1 is (syntactically) identical to t2, or if there exists bindings to variables such that the two terms become identical after substitution. declarative definition cf. Unification (LP) vs Term Matching (FP) (In the latter case, one of the terms is ground and the other cannot have repeated variables.) cs7120 (Prasad) L15-PrologUnify

Inductive Definition of Matching Terms If S and T are constants, then S and T match only if they are the same constants. If S is a variable and T is anything, then they match, and S is instantiated to T. (Similarly, if T is a variable.) … (cont’d)… constructive definition Robinson: In the context of ‘Resolution Theorem Proving’ introduced: unifier, most general unifier and proved the uniqueness of mgu (Any unifier is an instance of mgu.) cs7120 (Prasad) L15-PrologUnify

(cont’d) If S and T are structures, then S and T match only if: S and T have the same principal functor, and all their corresponding components (arguments) match, recursively. The resulting instantiation is determined by matching of the components (arguments). f(x,y) –> functor – name f , arity 2 cs7120 (Prasad) L15-PrologUnify

Matching Substituition Examples Term Matching Substituition a b No match X { X <- a} f(X) g(Y) f(X,g(a)) f(b,g(Y)) {X <- b, Y <- a} f(a,g(X)) {X <- a} f(X,g(X)) f(a,g(a)) f(Y) {X <- Y} or {Y <- X} Identical upto variable renaming f(g(X,a), g(b,X)) f(Z,Z) Conflicting substitution Unique name hypothesis implicitly assumed If two terms to be matched were independent, due to locality of variables, same variable appearing in two terms will be treated as different. Matching causes computation -> especially the trickling sort in the last one cs7120 (Prasad) L15-PrologUnify

Extended Example f(X, g(a)) f(Y, g(Y)) {X<-Y} {X<-a, Y<-a} Common instance: f(a,g(a)) cs7120 (Prasad) L15-PrologUnify

(cont’d) f(X, h(b), g(X)) f(a, h(Y), g(Y)) {X<-a} {Y<-b} Not unifiable Common instance: ?? cs7120 (Prasad) L15-PrologUnify

Occur’s Check Problem f(X, g(X)) f(Y, Y) These terms cannot be unified because there is no finite term substituition {X <- t} such that X = g(X). In practice, this can cause non-terminating computation, requiring special handling. unifying substituition: g(g(g(g(…t)] cs7120 (Prasad) L15-PrologUnify

(Cont’d) ?-member(a,[f(a),a]). Yes ?-member(X,[f(X),X]). Infinite loop: X = f(f(f(…))) SWI-Prolog 1 ?- member(X,[f(X)]). X = f(**) ; false. cs7120 (Prasad) L15-PrologUnify

Executable Specification in Prolog type(i,int). type(x,real). type(+(E,F),T) :- type(E,T), type(F,T). type(+(E,F),real) :- type(E,T1),type(F,T2), T1 \= T2. Type Checking ?- type(+(i,x),real). Type Inference ?- type(+(x,x),T). expression over +, i, and x cs7120 (Prasad) L15-PrologUnify

Alternative : with conditional type(i,int). type(x,real). type(+(E,F),T) :- type(E,TT), ( (TT == real) -> T = real ; type(F,T) ). Type Checking ?- type(+(i,x),real). Type Inference ?- type(+(x,x),T). = unify == syntactic identity if-then-else expression cs7120 (Prasad) L15-PrologUnify