Logic Programming Lecture 8: Term manipulation & Meta-programming.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
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.
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: ?-
Logic Programming Lecture 1: Course orientation; Getting started with Prolog.
Logic Programming Lecture 9: Constraint logic programming.
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
SLD-resolution Introduction Most general unifiers SLD-resolution
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
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.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
Logic Programming Lecture 3: Recursion, lists, and data structures.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
CPSC 422, Lecture 21Slide 1 Intelligent Systems (AI-2) Computer Science cpsc422, Lecture 21 Mar, 4, 2015 Slide credit: some slides adapted from Stuart.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
META-INTERPRETERS AND META-PROGRAMMING Ivan Bratko Faculty of Computer and Info. Sc. Univ. of Ljubljana.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 Solving Problems by Searching. 2 Terminology State State Space Initial State Goal Test Action Step Cost Path Cost State Change Function State-Space.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Logic Programming Part 2: Semantics James Cheney CS 411.
Daniel Kroening and Ofer Strichman 1 Decision Procedures in First Order Logic Decision Procedures for Equality Logic.
Solving Problems by Searching
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
Logic Programming Lecture 7: Search Strategies: Problem representations Depth-first, breadth-first, and AND/OR search.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
Logic Programming Programming Lecture 1: Getting started with Prolog.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Lecture 9: Constraint logic programming
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
CPSC 422, Lecture 21Slide 1 Intelligent Systems (AI-2) Computer Science cpsc422, Lecture 21 Oct, 30, 2015 Slide credit: some slides adapted from Stuart.
Computer Science CPSC 322 Lecture 22 Logical Consequences, Proof Procedures (Ch 5.2.2)
Arc Consistency CPSC 322 – CSP 3 Textbook § 4.5 February 2, 2011.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Logic Programming Lecture 6: Parsing and Definite Clause Grammars.
Logic Programming Lecture 7: Search Strategies:
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
Introduction to Logic for Artificial Intelligence Lecture 2
Prolog syntax + Unification
Chapter 2 Syntax and meaning of prolog programs
Presentation transcript:

Logic Programming Lecture 8: Term manipulation & Meta-programming

James CheneyLogic ProgrammingNovember 14, 2010 Outline for today Special predicates for examining & manipulating terms var, nonvar, functor, arg, =.. Meta-programming call/1 Symbolic programming Prolog self-interpretation

James CheneyLogic ProgrammingNovember 14, 2010 var/1 and nonvar/1 var(X) holds var(a) does not hold nonvar(X) does not hold nonvar(a) holds (tutorial questions...) None of these affects binding

James CheneyLogic ProgrammingNovember 14, 2010 Other term structure predicates nonvar/1 atomic/1 atom/1a,b,ac,... number/1 integer/1...,-1,0,1,2,3... float/10.1,10.42,... f(X,a) []

James CheneyLogic ProgrammingNovember 14, 2010 Structural equality The ==/2 operator tests whether two terms are exactly identical Including variable names! (No unification!) ?- X == X. yes ?- X == Y. no

James CheneyLogic ProgrammingNovember 14, 2010 Structural comparison \==/2: tests that two terms are not identical ?- X \== X. no ?- X \== Y. yes

James CheneyLogic ProgrammingNovember 7, 2011 From last week: Breadth-first search Keep track of all possible solutions, try shortest ones first Maintain a "queue" of solutions bfs([[Node|Path],_], [Node|Path]) :- goal(Node). bfs([Path|Paths], S) :- extend(Path,NewPaths), append(Paths,NewPaths,Paths1), bfs(Paths1,S). bfs_start(N,P) :- bfs([[N]],P).

James CheneyLogic ProgrammingNovember 7, 2011 \== means "not equal as term" A difference list implementation bfs_dl([[Node|Path]|_], _, [Node|Path]) :- goal(Node). bfs_dl([Path|Paths], Z, Solution) :- extend(Path,NewPaths), append(NewPaths,Z1,Z), Paths \== Z1, % (Paths,Z1) is not empty DL bfs_dl(Paths,Z1,Solution). bfs_dl_start(N,P) :- bfs_dl([[N]|X],X,P).

James CheneyLogic ProgrammingNovember 14, 2010 functor/3 Takes a term T, gives back the atom and arity ?- functor(a,F,N). F = a N = 0 ?- functor(f(a,b),F,N). F = f N = 2

James CheneyLogic ProgrammingNovember 14, 2010 arg/3 Given a number N and a complex term T Produce the Nth argument of T ?- arg(1,f(a,b),X). X = a ?- arg(2,f(a,b),X). X = b

James CheneyLogic ProgrammingNovember 14, 2010 =../2 ("univ") We can convert between terms and "universal" representation ?- f(a,f(b,c)) =.. X. X = [f,a,f(b,c)] ?- F =.. [g,a,f(b,c)]. F = g(a,f(b,c))

James CheneyLogic ProgrammingNovember 14, 2010 call/1 Given a Prolog term G, solve it as a goal ?- call(append([1],[2],X)). X = [1,2]. ?- read(X), call(X). |: member(Y,[1,2]). X = member(1,[1,2])

James CheneyLogic ProgrammingNovember 14, 2010 Call + =.. Can do some evil things... callwith(P,Args) :- Atom =.. [P|Args], call(Atom). map(P,[],[]). map(P,[X|Xs],[Y|Ys]) :- callwith(P,[X,Y]), map(P,Xs,Ys) plusone(N,M) :- M is N+1. ?- map(plusone,[1,2,3,4,5],L). L = [2,3,4,5,6].

James CheneyLogic ProgrammingNovember 14, 2010 Symbolic programming Logical formulas prop(true). prop(false). prop(and(P,Q)) :- prop(P), prop(Q). prop(or(P,Q)) :- prop(P), prop(Q). prop(imp(P,Q)) :- prop(P), prop(Q). prop(not(P)) :- prop(P).

James CheneyLogic ProgrammingNovember 14, 2010 Formula simplification simp(and(true,P),P). simp(or(false,P),P). simp(imp(P,false), not(P)). simp(imp(true,P), P). simp(and(P,Q), and(P1,Q)) :- simp(P,P1)....

James CheneyLogic ProgrammingNovember 14, 2010 Satisfiability checking Given a formula, find a satisfying assignment for the atoms in it Assume atoms given [p 1,...,p n ]. A valuation is a list [(p 1,true|false),...] gen([],[]). gen([P|Ps], [(P,V)|PVs]) :- (V=true;V=false), gen(Ps,PVs).

James CheneyLogic ProgrammingNovember 14, 2010 Evaluation sat(V,true). sat(V,and(P,Q)) :- sat(V,P), sat(V,Q). sat(V,or(P,Q)) :- sat(V,P) ; sat(V,Q). sat(V,imp(P,Q)) :- \+(sat(V,P)) ; sat(V,Q). sat(V,not(P)) :- \+(sat(V,P)). sat(V,P) :- atom(P), member((P,true),V).

James CheneyLogic ProgrammingNovember 14, 2010 Satisfiability Generate a valuation Test whether it satisfies Q satisfy(Ps,Q,V) :- gen(Ps,V), sat(V,Q). (On failure, backtrack & try another valuation)

James CheneyLogic ProgrammingNovember 14, 2010 Prolog in Prolog Represent definite clauses rule(Head,[Body,....,Body]). A Prolog interpreter in Prolog: prolog(Goal) :- rule(Goal,Body), prologs(Body) prologs([]). prologs([Goal|Goals]) :- prolog(Goal), prologs(Goals).

James CheneyLogic ProgrammingNovember 14, 2010 Example rule(p(X,Y), [q(X), r(Y)]). rule(q(1)). rule(r(2)). rule(r(3)). ?- prolog(p(X,Y)). X = 1 Y = 2

James CheneyLogic ProgrammingNovember 14, 2010 OK, but so what? Prolog interpreter already runs programs... Self-interpretation is interesting because we can examine or modify behavior of interpreter.

James CheneyLogic ProgrammingNovember 14, 2010 Witnesses Produce proof trees showing which rules were used prolog_pf(Goal,[Tag|Proof]) :- rule_pf(Goal,Body,Tag), prologs_pf(Body,Proof). prologs_pf([],[]). prologs_pf([Goal|Goals],[Proof|Proofs]) :- prolog_pf(Goal,Proof), prologs_pf(Goals,Proofs).

James CheneyLogic ProgrammingNovember 14, 2010 Rules with "justifications" rule_pf(p(1,2), [], rule1). rule_pf(p(X,Y), [q(X), r(Y)], rule2(X,Y)). rule_pf(q(1),[],rule3). rule_pf(r(2),[],rule4). rule_pf(r(3),[],rule5).

James CheneyLogic ProgrammingNovember 14, 2010 Witnesses "Is there a proof of p(1,2) that doesn't use rule 1?" ?- prolog_pf(p(1,2),Prf), \+(member(rule1,Prf)). Prf = [rule2,[rule3, rule4]].

James CheneyLogic ProgrammingNovember 14, 2010 Other applications Tracing Can implement trace/1 this way Declarative debugging Given an error in output, "zoom in" on input rules that were used These are likely to be the ones with problems

James CheneyLogic ProgrammingNovember 14, 2010 Further reading: LPN, ch. 9 Bratko, ch. 23 Next time Constraint logic programming Course review