History Automatic Theorem Proving Robinson 1965 “PROgramming in LOGic” Kowalski, 1974 Deduction as computation “Prolog”, Colmerauer, 1973 Edinburgh Prolog,

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
3. Lists, Operators, Arithmetic. Contents Representation of lists Some operations on lists Operator notation Arithmetic.
Prolog Nonmonotonic logic.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Prolog.
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.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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).
Prolog for Linguists Symbolic Systems 139P/239P John Dowding Week 4, October 29, 2001
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Meta-interpreters Interpreted languages like Prolog can easily treat program code as data Interpreted languages like Prolog can easily treat program code.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
JavaScript, Third Edition
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Prolog for Linguists Symbolic Systems 139P/239P John Dowding Week 7, November 12, 2001
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.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
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.
F28PL1 Programming Languages Lecture 16: Prolog 1.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Practical Erlang Programming Basic Erlang. © Erlang Training and Consulting Ltd2Basic Erlang Practical Erlang Programming.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
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.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Prolog Harry R. Erwin, PhD COMM2M University of Sunderland.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Programming Languages Third Edition Chapter 4 Logic Programming.
Artificial Intelligence
Prolog for Linguists Symbolic Systems 139P/239P John Dowding Week 3, October 22, 2001
9/19/01 1 Prolog Tim Finin Tim Finin University of Maryland Baltimore County.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
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.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Section 16.5, 16.6 plus other references
Prolog for Linguists Symbolic Systems 139P/239P
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog a declarative language
Logic Programming Language
Prolog III.
Programming Techniques
Presentation transcript:

History Automatic Theorem Proving Robinson 1965 “PROgramming in LOGic” Kowalski, 1974 Deduction as computation “Prolog”, Colmerauer, 1973 Edinburgh Prolog, 1977 “Prolog 10”, David Warren Warren Abstract Machine Clocksin&Mellish, 1981

What kind of a thing is Prolog? Very different from C, C++, Java, and other Procedural languages Symbolic Programming Language Logic Programming Language Declarative Programming Language

Prolog program A Prolog program is defined by a set of Predicates Each Predicate has a unique Functor and Arity parent(Parent, Child) a predicate whose functor is “parent” with arity 2. Each Predicate is defined by a sequence of Clauses A Clause is either a Fact or a Rule fact(…). head(…) :- goal1(…), …, goalN(…). Each argument to a predicate is a Term A Term is either: Atomic, Variable, or Compound

Atomic terms Atomic terms can be either atoms or numbers Atoms can be a sequence of alphanumeric (including ‘_’) characters starting with a lower case letter, or a sequence of any characters embedded in single quote marks: johnDowding symbsys139p ‘quoted atom’ non_quoted_atom Numbers can be integers or floating point

Variables Variables start with a capital letter or _ character X Parent _Sibling _ Variables can stand in for arbitrary Prolog terms (including other variables) Variables that occur with the same name in the same clause represent the same variable, except for The anonymous variable “_”, which always represents a unique variable.

Compound Terms A compound term is a structure with a functor and N arguments. functor(Arg1, Arg2, …, ArgN) Each argument can be an arbitrary Prolog term

Example: Successor Arithmetic Simple arithmetic using compound terms to represent integers: 0 1 s(0) 2 s(s(0)) 3 s(s(s(0))) 4 s(s(s(s(0)))).

Structural Induction and Recursion Inductive proofs have a base (or terminating) case and an inductive step. Mathematical induction – over the space of integers Structural induction – over subsets of the space of Prolog Terms. Many Prolog predicates have this structure: is_number(0). is_number(s(Number)) :- is_number(Number).

BUILT-IN Predicate: consult/1 consult(FileName) Load the Prolog program in FileName into memory.

Factorial factorial(+N, ?Factorial) fact(1) = 1 fact(N+1)= (N+1) * fact(N) %factorial(+Number, ?Factorial) factorial(s(0), s(0)). factorial(s(Number), Factorial):- factorial(Number, PartialFactorial), mult(s(Number), PartialFactorial, Factorial).

Prolog Execution Model/Prolog Debugger CALL EXIT FAILREDO parent(james, john). parent(james, alan). parent(florence, john). parent(florence, alan). parent(alan, elizabeth). parent(alan, emily). Goal = parent(P, john) parent(james, john) parent(florence, john)

Execution Model (conjunctions) parent(james, john). parent(james, alan). parent(florence, john). parent(florence, alan). parent(alan, elizabeth). parent(alan, emily). female(emily). female(florence). female(elizabeth). parent(Mother, john)female(Mother) parent(james, john) female(james) parent(florence, john) female(florence)

Execution Model – mult(s(s(s(0))), s(s(0)), Result) mult(0, Term, 0):- is_number(Term). mult(s(Term1), Term2, Product):- mult(Term1, Term2, Partial), add(Term2, Partial, Product). add(0, Sum, Sum):- is_number(Sum). add(s(Addend1), Addend2, s(Sum)):- add(Addend1, Addend2, Sum). mult(s(s(0)),s(s(0)), Prod) mult(s(0), s(s(0)), Partial)add(s(s(0)), Partial, Prod) add(s(s(0)), Prod, Result) mult(0, Term, 0):- is_number(Term). mult(s(Term1), Term2, Product):- mult(Term1, Term2, Partial), add(Term2, Partial, Product).

Prolog Debugger Demo Built-In predicates: trace/0. notrace/0. Debugger actions: ‘c’ or - creep ‘s’ – skip ‘l’ – leap ‘r’ – retry ‘f’ – fail ‘a’ - abort

And-Or Trees or and or and or

And-Or Tree (cont.) or and mult(0, Term, 0):- is_number(Term). mult(s(Term1), Term2, Product):- mult(Term1, Term2, Partial), add(Term2, Partial, Product). or is_number(Term) mult(Term1, Term2, Partial) add(Term2,Partial,Product) and

Linked Lists Prolog allows a special syntax for lists: [a,b,c] is a list of 3 elements [] is a special atom indicating a list with 0 elements Internally, Prolog lists are regular Prolog terms with the functor ‘.’ (so called “dotted pairs”) [a,b,c] = ‘.’(a, ‘.’(b, ‘.’(c, []))). The symbol | in a list indicates “rest of list”, or the term that is the 2 nd argument of a dotted pair. [a,b,c] = [a|[b,c]]. [Head|Tail] is a common expression for dividing a list into its first element (Head) and the rest of the list (Tail).

Example: list/1 % list(?List) list([]). list([_Head|Tail]):- list(Tail). Since Prolog is untyped, we don’t have to know anything about Head except that it is a term.

Example: member/2 % member(?Element, ?List) member(Element, [Element|_Tail]). member(Element, [_Head|Tail]):- member(Element, Tail).

Example: delete/3 % delete(+Element, +List, -NewList) % delete/3 succeeds if NewList results from removing % one occurrence of Element from List. delete(Element, [Element|Tail], Tail). delete(Element, [Head|Tail], [Head|NewTail]):- delete(Element, Tail, NewTail).

Example: append/3 % append(?List1, ?List2, ?List3) % append/3 succeds if List3 contains all the elements of % List1, followed by all the elements of List2. append([], List2, List2). append([Head|List1], List2, [Head|List3]):- append(List1, List2, List3).

Example: “naïve” reverse % "naive reverse": nreverse(?List, ?ReversedList). nreverse([], []). nreverse([Head|Tail], ReversedList):- nreverse(Tail, ReversedTail), append(ReversedTail, [Head], ReversedList).

“Pure Prolog” and non-logical built-ins All the examples so far have been “pure Prolog” Contain no built-ins with non-logical side-effects Prolog has many built-in predicates: Type checking of terms Arithmetic Control execution Input and output Modify the program during execution Perform aggregation operations Use of non-logical built-in predicates usually effects the reversability of your program.

Type-checking Built-in Predicates var(X) – is true when X is an uninstantiated variable. nonvar(X) – is true when X is not a variable. atom(X) – is true when X is a symbolic constant. number(X) – is true when X is a number atomic(X) – is true when atom(X) or number(X). compound(X) – is true when X is a compound term.

Term constructor/selectors: functor/3, arg/3 functor(+Term, ?Functor, ?Arity) Find the Functor and Arity of Term functor(?Term, +Functor, +Arity) Constructs a new Term with Functor and Arity arg(+N, +Term, ?SubTerm) Unifies SubTerm with the Nth argument of Term

Arithmetic: Built-In is/2 Arithmetic expressions are not normally evaluated in Prolog. Built-In infix operator is/2 evaluates it’s 2 nd argument, and unifies the result with it’s 1 st argument. | ?- X = X = 5+2? yes | ?- X is X = 7 ? yes Any variables in the right-hand side of is/2 must be instantiated when it is evaluated. Revisit operator and arithmetic at a later time

Cut (!) The ! Symbol (pronounced “cut”) modifies the execution of your program by committing to certain choices. That is, it removes choice points. Easy to describe what it does, more difficult to get used to using it properly.

Cut (cont.) Head1 :- Goal1, Goal2, …, GoalN, !, … Head2 :- … Head3 :- … … HeadN :- … Removes the choice point that allows Head2 …HeadN Removes any choice points that may have been introduced in Goal1 … GoalN. We will discuss Cut in more detail later on.

Example: delete_first/3 % delete_first(+Element, +List, -NewList) % removes the 1st occurrence of Element in List. delete(Element, [Element|Tail], Tail):- !. delete(Element, [Head|Tail], [Head|NewTail]):- delete(Element, Tail, NewTail).

Built-Ins: Term Comparison Operators Unifies with Term1 = Term2 iff unifies Term1 and Term2 if they are unifiable Does not unify with Term1 \= Term2, Term1 and Term2 are unchanged Identical Term1 == Term2 iff are the same terms Not identical Term1 \== Term2

Built-ins: true/0 and fail/0. true. Always succeeds. fail. Always fails.

Meta-Predicates: call/1, \+/1 Meta-predicates take a Goal as an argument, and execute it. call/1 executes it’s one argument. \+(Goal) succeeds if call(Goal) fails. \+/1 could also be defined using cut and fail. \+(Goal) :- call(Goal), !, fail. \+(_Goal).

More about cut! Common to distinguish between red cuts and green cuts Red cuts change the solutions of a predicate Green cuts do not change the solutions, but effect the efficiency Most of the cuts we have used so far are all red cuts %delete_all(+Element, +List, -NewList) delete_all(_Element, [], []). delete_all(Element, [Element|List], NewList) :- !, delete_all(Element, List, NewList). delete_all(Element, [Head|List], [Head|NewList]) :- delete_all(Element, List, NewList).

Green cuts Green cuts can be used to avoid unproductive backtracking % identical(?Term1, ?Term2) identical(Var1, Var2):- var(Var1), var(Var2), !, Var1 == Var2. identical(Atomic1,Atomic2):- atomic(Atomic1), atomic(Atomic2), !, Atomic1 == Atomic2. identical(Term1, Term2):- compound(Term1), compound(Term2), functor(Term1, Functor, Arity), functor(Term2, Functor, Arity), identical_helper(Arity, Term1, Term2).

Advice on cuts Dangerous, easy to misuse Rules of thumb: Use sparingly Use with as narrow scope as possible

intersect/3 %intersect(+Set1, +Set2, ?Intersection) intersect([], _Set2, []). intersect([Element|RestSet1], Set2, [Element|Intersection]):- member(Element, Set2), !, intersect(RestSet1, Set2, Intersection). intersect([_Element|RestSet1], Set2, Intersection):- intersect(RestSet1, Set2, Intersection).

Example: Input/Output repeat/0 is a built-in predicate that will always resucceed % classifing terms classify_term :- repeat, write('What term should I classify? '), nl, read(Term), process_term(Term), Term == end_of_file.

I/O Example (cont) process_term(Atomic):- atomic(Atomic), !, write(Atomic), write(' is atomic.'), nl. process_term(Variable):- var(Variable), !, write(Variable), write(' is a variable.'), nl. process_term(Term):- compound(Term), write(Term), write(' is a compound term.‘), nl.

assert/1, asserta/1, and assertz/1 Asserting facts (most common) assert(Fact) Asserting rules assert( (Head :- Body) ). asserta/1 adds the new clause at the front of the predicate assertz/1 adds the new clause at the end of the predicate assert/1 leaves the order unspecified

Built-In: retract/1 retract(Goal) removes the first clause that matches Goal. On REDO, it will remove the next matching clause, if any. Retract facts: retract(Fact) Retract rules: retract( (Head :- Body) ).

Built-in: retractall/1 retractall(Head) removes all facts and rules whose head matches. Could be implemented with retract/1 as: retractall(Head) :- retract(Head), fail. retract(Head):- retract( (Head :- _Body) ), fail. retractall(_Head).

Built-In: abolish(Predicate/Arity) abolish(Predicate/Arity) is almost the same as retract(Predicate(Arg1, …, ArgN)) except that abolish/1 removes all knowledge about the predicate, where retractall/1 only removes the clauses of the predicate.

Aggregation: findall/3. findall/3 is a meta-predicate that collects values from multiple solutions to a Goal: findall(Value, Goal, Values) findall(Child, parent(james, Child), Children) Prolog has other aggregation predicates setof/3 and bagof/3, but we’ll ignore them for now.

findall/3 and assert/1 findall/3 and assert/1 both let you preserve information across failure. :- dynamic solutions/1. findall(Value, Goal, Solutions):- retractall(solutions/1), assert(solutions([])), call(Goal), retract(solutions(S)), append(S, [Value], NextSolutions), assert(solutions(NextSolutions)), fail. findall(_Value, Goal, Solutions):- solutions(Solutions).

Special Syntax III: Operators Convenience in writing terms We’ve seem them all over already: union([Element|RestSet1], Set2, [Element|SetUnion]):- union(RestSet1, Set2, SetUnion), \+ member(Element, SetUnion), !. This is just an easier way to write the term: ‘:-’(union([Element|RestSet],Set2,[Element|SetUnion]), ‘,’(union(RestSet1,Set2,SetUnion), ‘,’(‘\+’(member(Element, SetUnion), !)))

Operators (cont) Operators can come before their arguments (prefix) \+ Or between their arguments (infix), + is < Of after their arguments (postfix) Prolog doesn’t use any of these (yet) The same Operator can be more than one type :-

Precedence and Associativity Operators also have precedence 5 * = (5 * 2) + 3 Operators can be associative, or not, Left associative or right associative Explicit parenthesization can override defaults for associatiativity and precendence

Built-in: current_op/3 current_op/3 gives the precedence and associativity of all current operators. current_op(Precedence, Associativity, Operator) where Precedence in an integer and Associativity is of fx or fy for prefix operators xf or yf for postfix operators xfx, xfy, yfx, yfy for infix operators

Associativity These atoms: fx, fy, xf, yf, xfx, xfy, yfx, yfy draw a “picture” of the associativity of the operator: The location of the f tells if the operator is prefix, infix, or postfix. x means that the argument must be of lower precedence y means that the argument must be of equal or lower precedence. A y on the left means the operator is left associative A y on the right means the operator is right associative

Creating new operators Built-in op/3 creates new operators op(+Precedence, +Associativity, +Operator) :- op(700, xfx, equals). :- op(650, fx, $). :- op(650, xf, cents). $Dollars equals Cents cents :- Cents is 100 * Dollars.