<<

INF 3110 – 2016 . UiO 1 and Martin Giese, University of Oslo Torjusen – A. , VolkerStolz [email protected] G. Schneider slides by Logic Department of Informatics Informatics of Department Based on INF 3110 – 2016 2

Differentviews ofa program Lists in Prolog in Lists Facts,rules, queries and unification Brief overviewBrief paradigmof the logical A bit ofhistorybit A

Outline ◆

◆ ◆

◆ ◆ INF 3110 – 2016 K. Gödel Gödel K. – 3 paradigm ” order logic order - order logic order - Oriented Logic Based on the Resolution Resolution Logic the Based Oriented on - Robinson: Resolution, unification and a and unification Resolution, Robinson: – Computation as deduction Computation ” A Machine “

The proof process results in a satisfyingresultsina substitution. proof process The Certaininterpretedprograms logical as be can formulas Possible to prove theorems of first

• •

• Early seventies: of with aLogic restricted form programs Kowalski by R. introduced resolution unification . Principle & J. Herbrand Herbrand J. & 1965: 1930s: Based on the syntax first the of Basedon Origin in automated theorem proving theorem automated in Origin

History of Programming History Logic

◆ ◆

◆ ◆ INF 3110 – 2016

) 4 1991 - 1982 Philippe Roussel Philippe – W -

s) ’ 70 Kowalski and Colmerauer teamsKowalski Colmerauer and - Prolog , Warren Abstract Machine Warren , : WAM

1973 : – Parts of Semantic Webreasoning ofParts Semantic learning) (machine Programming Logic Inductive Deductive databases ( Japanese Fifth Generation Project ( Programming Logic Constraint

A. Colmerauer & colleagues & Colmerauer A.

• • • • •

1983 paradigm: Influences the of 1971 workingtogether Algol in Firstimplementation Programming languages for natural language processing language natural languagesfor Programming -

◆ ◆ ◆

◆ ◆

(cont.) History of Logic Programming of Programming History Logic INF 3110 – 2016

5 Overview Oriented/Declarative (Logic)Oriented/Declarative Programming - Oriented, Intensional, ... Programming ... Intensional, Oriented, Oriented Programming Oriented - - a reala or imaginary part of theworld ofset a equations as regarded is program A A program is regarded as a mathematicala as function regarded is program A simulating physicala model as executionregarded is program A manipulating a set of registers (programmable calculator) ofset a registers (programmable manipulating A program execution is regarded as a sequence of operations sequence a as executionregarded is program A

• • • •

Aspect Constraint Object Procedural/ Procedural/imperative

◆ ◆

◆ ◆

◆ Paradigms: Paradigms: INF 3110 – 2016 6

Differentviews ofa Prolog program Lists in Prolog in Lists Facts,rules, queries and unification Brief overviewBrief paradigmof the logical A bit ofhistorybit A

Outline ◆

◆ ◆

◆ ◆ INF 3110 – 2016 7 ” R. Kowalski programming ” Program = Logic + Control + Program Logic = “ traditional

“ The programmer only worries about the Logic the worriesonly about programmer The takes careControl of The Programmer takes care of both aspectstakes careboth Programmer of

• • •

Indeclarative programming In

◆ Declarative Programming Declarative INF 3110 – 2016 . 8 executable specifications method meaning declarativeprogramming is being computed is the computation takes place What Concerned with the A program is viewed as a formula; possible to reason about its correctness without any reference to the underlying computational meaning How Concerned with the A program is a description of an algorithm which can be executed – – – – – –

Declarativeinterpretation: Proceduralinterpretation:

• •

This means that we Thismeansthat can write Logic prog. supports supports prog. Logic twointerpretations admits declarative A program

◆ ◆ ◆ Declarative Programming Declarative INF 3110 – 2016 9 parent to a first find a description To find a grandchild to X, child, then check if this parent is a child to X Then find a child to this child To find a grandchild to X, first find a child to X. C is grandchild of P if C is child of a child of P

• • • explains how to find a grandchild):

Imperative description II: ( Imperative Find all grandchildren for a specific person X? Declarative description (defines the relation):

◆ ◆ ◆ ◆ Example INF 3110 – 2016 10 person:

to nrPeople do nrPeople to 1 writeln(j); to nrPeople do nrPeople to 1 if child[j, if i] then fi (namesas coded Nat) for j := j for od bea matrix representing the parent fi if child[i, if person]then od read(person); ifor := child child

For finding all theFor grandchildrenall finding of Let relationship

◆ ◆ Example: Imperative solution Imperative Example: INF 3110 – 2016 is

11 child(Z,Y) child(Z,Y) y and and . grandchild(x,y)) y

) → ← child(X,Z) child(X,Z) is child of child is is grandchild of is child(z,y)) child(z,y)) child(X,Z), child(Z,Y). child(X,Z), x x - ∧ if if ) ) y y , , x x and z(child(x,z) child( ∃ is the reverseis ( the implication

between the twoterms betweenthe y( ” grandchild(X,Y) :

- ” : ,

the logical the ” ” Prolog: Logic (specification): Logic Write grandchild( x

◆ ◆ ◆

∀ ◆ ◆ ◆ Example: Declarative solution Declarative Example: INF 3110 – 2016 12 unified means returnvalues not queries may be bound to values to bound may be various various

arithmetic expressions demand arithmetic evaluated on Terms can only be unified with each can unified other Terms be only If they succeed, unbound variables Iftheysucceed, are unbound and of User write a program and interact by and Userwrite a program

• • • •

NoProlog! in functions Predicates do Predicatesmayor fail succeed Supportinteractive programming

◆ ◆

◆ Important features of Prog. Logic features Important INF 3110 – 2016 13

Differentviews ofa Prolog program Lists in Prolog in Lists Facts,rules, queries and unification Brief overviewBrief paradigmof the logical A bit ofhistorybit A

Outline ◆

◆ ◆

◆ ◆ INF 3110 – 2016 14 535 bytes written, - for byte code... lists.pl / compiled, 2 lines read stolz ). Xs lists.pl /a03/ / 2009 Daniel Diaz 2009 Daniel - stolz asgard gprolog / ([1,2,3],[4,5,6], ifi /a03/ ~]$ yes ms asgard ) [lists]. / 14

halt. my_append ms - = [1,2,3,4,5,6] - -

ifi stolz@vor | ? | ... yes Xs (1 ? | compiling / compiling / | ? By Daniel Diaz By Daniel (C) 1999 Copyright GNU Prolog 1.3.1 GNU Prolog [ Running Prolog at IFI IFI at Prolog Running INF 3110 – 2016 ) by by ) ” 15 queries “ ).(Facts Rules and ”

subgoals ) “ Clauses : Prolog answers Prolog : ( questions : Describe how to divide the problem into into problem dividethe Describe : howto : Basic truths in the world. world. Basic : the in truths

Queries facts using rules and Rules ( subproblems simpler called both are Facts Describe the formal world. world. formal Describe the (themachine answers) it Askabout questions

Thedescription ofthe problem done is through We program by creatinga (formal) whichworld program weWe Twophases: explore. • • • 1. 2.

◆ Some programming principles programming Some INF 3110 – 2016 father b, 16 , mother a caseletters - caseletters ), brother(kain,abel) brother(kain,abel) ), - 2,3,5 ). ). ). ). ), ), sum( 1960 1965 1962 3,1 1989 d. denotes a person with name person a denotes ) and numbers. ” ), greaterThan( ), : wordswith : starting lower 7 : words:with starting lower

sofia ) “ ” , ” and year of birth and

Persons have a name, a mother, a father and a birthday. birthday. a fathera mother, a and name, a have Persons person(a,b,c,d)c, isPrime(

person anne person(john, sofia, george, george, sofia, person(john, martin, sofia, person(paul, mike, anne, person(maria, person(anne, sofia, martin, sofia, person(anne, • • “ “

( Constants ( Relations Represented by facts: Represented Example: FamilyExample: relations Facts:

◆ ◆ ◆ ◆ ◆ ◆ ◆ Clauses: Facts Clauses: INF 3110 – 2016 17 answer! : what is truewhat is : is tknow ’ don closed world Thereno is - person(berta, sofia, martin, 1961). sofia, person(berta, person(anne, sofia, martin, 1960). sofia, person(anne, - -

no |? yes | ? person(paul, sofia, martin,1962). sofia, person(paul, 1989). mike, anne, person(maria, person(anne, sofia, martin, 1960). sofia, person(anne, 1965). george, sofia, person(john, whatknows, it the definedin i.e.what is database Prologworks a in

◆ Queries INF 3110 – 2016 18

casewith or letters - in the example below) intheexample ” Child “ (unification) and gives it result gives a (unification)as and and and ” ” : a word starting with astarting upper : word fits 1960 1989 ” Year “ person(Child, anne, mike, Year). mike, anne, person(Child, person(anne, sofia, martin, Year). sofia, person(anne, ( - - Prolog searches in the knowledge base until it finds something until it something base finds intheknowledge Prologsearches that returned. is substitution(s) matching The ’

_ Child = maria = Child Year= yes yes | ? | ? Year= • •

Howvariables the are used? Variable ‘

◆ ◆ Queries with variables with Queries Unification ◆Unification: instantiating variables so that terms become syntactically equal. (solving equation between terms) ◆Used to match a query with facts/rules (Cf. Sec.15.3. for a more formal exposition). ◆To unify two terms s ≣ t • f(s1,s2...) ≣ f(t1,t2...) → s1 ≣ t1 and s2 ≣ t2... • f(s1,s2) ≣ f(t1) →fail (different arity) • f(...) ≣ g(...) →fail (different head) • X ≣ t → instantiate X with t • X ≣ Y → replace all Y by X. Unification Example

◆ Unify g(X,a) ≣ g(f(Y),Y) ◆ X ≣ f(Y) and a ≣ Y ◆ instantiate X/f(Y) and Y/a ◆ together X/f(a) and Y/a ◆ makes both terms equal to g(f(a),a)

◆ Syntactic equality! 1+2 and 3 don't unify! Unification in Prolog

◆ Example • fact: child(anne,sofia) • query: child(X,sofia) • unification: X:= anne . ◆ Example • rule:grandchild(X,Z) :- child (X,Y), child(Y,Z) • query: grandchild(anne,G) • unification: X/anne, Z/G • solve: child(anne,Y), child(Y,G) INF 3110 – 2016 22 or

and ) ; ) andsemicolon ) ( , 1962 person(paul,Mother, martin, Year). person(paul,martin, Father, Year); - Semicolonrepresents the logical Commarepresents the logical

Year= yes Mother = sofia Mother • ? | • comma ( comma Compositequeries bemay done using

◆ Composite queries Clauses: Rules ◆ Let child(X,Y) represent “X is a child of Y”: person(anne, sofia, martin, 1960). person(john, sofia, george, 1965). person(paul, sofia, martin, 1962). person(maria, anne, mike, 1989). child(X,Y) :- person(X,Z,Y,U). child(X,Y) :- person(X,Y,Z,U). % :- is read “if”

| ?- child(paul,martin). yes

| ?- child(paul,Parent).

Parent = martin ? ; Parent = sofia ? ; no INF 3110 – 2016 24 person(X,Y,_Z,_U). person(X,_,Y,_). - -

child(X,Y) : child(X,Y) The two_ different! are The child(X,Y) : child(X,Y) Begin uninterestingBegin variables _: with

◆ Clauses: Rules Clauses: INF 3110 – 2016 25

Twodifferent rules are completely independent All the occurrences ofa All variable a rule in are boundto each other

• •

tryto avoid misleading names Theof names variables arbitrary, are but is the rule where it appears it where rule the is Thescope of the occurrence variableof a

◆ Scopeof variables INF 3110 – 2016 26 person(paul,sofia,martin,1962). person(X,Y,Z,U). person(X,Z,Y,U). - -

child(paul, martin). child(paul, -

Itmatches There is no corresponding fact corresponding is no There child(X,Y) : child(X,Y) : child(X,Y) prolog answers prolog yes person(paul,Z,martin,U). We can use two different rules: can We use twodifferent person(paul,martin,Z,U). | ? Finding the answer to queries answer the Finding INF 3110 – 2016

) 27 ) 1962 1962 Parent =Parent martin. Parent =Parent sofia. person(paul,sofia,martin, person(X,Z,Y,U). person(X,Y,Z,U). person(paul,sofia,martin, - -

child(paul,Parent).

-

The unification will unification The give Matches Matcheswith will unification The give | ?

child(X,Y) : child(X,Y) : child(X,Y)

person(paul,Z,Parent,U). person(paul,Parent,Z,U). Two possibilities: Two Finding the answer to queries answer the Finding INF 3110 – 2016 28 == Y. \

) ” no ” are syntactically unequal (e.g. aresyntactically (e.g. unequal Y , so all the conditions must be satisfied. all so be theconditions , must and and and X will yield yield will child(X,Z), child(Y,Z), X X child(Y,Z), child(X,Z), - means that means

== Y

\ siblings(anne,X).

siblings(X,Y) :

X siblings(anne,anne) Comma is thelogicalis Comma -

no X = paul ? ; paul = X X = john ? ; ? john = X X = paul ? ; paul = X | ? • • Rules with more than one condition one than more with Rules Search tree |-? siblings(anne,X)

child(anne,Z), child(X,Z), anne \== X

p(anne,Y,Z,U), p(anne,Z,Y,U) child(X,Z), child(X,Z), anne \== X anne \== X

p(anne,sofia,martin,1960), PROGRAM: child(X,martin), ... person(anne, sofia, martin, 1960). anne \== X person(john, sofia, george, 1965). person(paul, sofia, martin, 1962). p(X,martin,V,W), p(X,V,martin,W), person(maria, anne, mike, 1989). child(X,Y) :- person(X,Z,Y,U). anne \== X anne \== X child(X,Y) :- person(X,Y,Z,U). siblings(X,Y) :- child(X,Z), child(Y,Z), X \== Y. p(anne,sofia,martin,1960), no p(paul,sofia,martin,1962), anne \== anne anne \== paul no yes (X=paul) INF 3110 – 2016

Y and . 30 2 X ), ), ), ), 1 2 2 1 == Parent == (fatherand mother) \ 1 == Y, \ represent that child(Y,Parent Parent child(X,Parent child(Y,Parent X child(X,Parent - rsiblings(X,Y)

rsiblings(X,Y): have the same parents parents thesame have Let

◆ More rules More INF 3110 – 2016 Y and 31 X == Parent2. == \ == Parent1, == Parent2, == \ \ == Y, \ Parent Parent Parent1 child(Y,Parent), X child(X,Parent1), child(Y,Parent2), represent that child(X,Parent), - hsiblings(X,Y)

hsiblings(X,Y) : hsiblings(X,Y) Let haveexactly one parent in common

◆ More rules More INF 3110 – 2016 32

hsiblings(anne,X). rsiblings(X, anne). rsiblings(X, - -

X = john ? ; ? = john X no | ? no X = paul ? ; ? = paul X ; ? = paul X | ? Some queries INF 3110 – 2016 33 is a X representthat child(X,Y). descendant(Z,Y). child(X,Z), - - Y recursiverule first - descendant(X,Y)

Non Recursivegoal at the end.

• • descendant(X,Y): descendant(X,Y):

NB!Order ofrule definitions: Let descendantof

◆ Recursive rules Recursive INF 3110 – 2016 34 Queries -

descendant(X, sofia).descendant(X, descendant(anne,X). - -

no X = maria ? ; =? maria X X = paul ? ; ? = paul X X = anne ? ; ? = anne X ; ? = john X | ? no no X = martin ? ; ? = martin X X = sofia ; ? X | ? Recursive rules rules Recursive INF 3110 – 2016 35

Differentviews ofa Prolog program Lists in Prolog in Lists Facts,rules, queries and unification Brief overviewBrief paradigmof the logical A bit ofhistorybit A

Outline ◆

◆ ◆

◆ ◆ INF 3110 – 2016 36

,nil)))) 7 ,cons( 5 | []]]]] | 7 ,cons( 3 | [ | 5 | [ | ,cons( 2 3

| [ |

cons(hd,tl), the list with head hd and atail tl and cons(hd,tl), list withhd the head nil, the empty list empty the nil,

2 • • [ BUT: usesyntaxBUT: special [] and [hd | tl] cons( A list ofprime numbers: list A

Conceptually,either: is a list Basic idea:sameBasic ML. as in

◆ ◆ ◆ ◆ Lists Prologin INF 3110 – 2016 37 willbe unified as [A| B] B/[b,c] ≣ and

[a, b, c] A/a [a,b|X]: another wayof writing |[a [b | X] ] as |[a [b | [c | [] ] ] ] [] : the empty list [a,b,c]: list a with three elements, same

• • Unification:justlike always...

• • • • Prettier Syntax forLists Syntax Prettier INF 3110 – 2016 38 [b| T] [H | T] Tail=[b,c] [Head | Tail] |[a T] unifywith T=[ ] T=[ and [H | T] [] and not unifywith Head=a Head=a not T=[b,c] H=a unifieswith does unifieswith unifieswith unifieswith does

Result:

[] [] Result: [a,b,c] Result: [a,b,c] [a] [a,b,c]

◆ ◆ ◆

◆ ◆

◆ Unification on lists on Unification INF 3110 – 2016 39 p([H | T], H, T). H, |T], p([H

p([a,b,c], X, Y). X, p([a,b,c],

yes X=a Y=[b,c] -

Query: Assume the following fact: fact: the following Assume

Unification on lists: Example lists: on Unification | ? |

• • INF 3110 – 2016 40 p([H | T], H, T). H, |T], p([H p([], X, Y). X, p([], p([a], X, Y). X, p([a], - -

| ? | no yes X=a Y=[]

| ? | Assume the following fact: fact: the following Assume Query:

• • Unification on lists: Example lists: on Unification INF 3110 – 2016 41 >yes - member(X, Tail). member(X, - ]). >member(2,[2,3]) ? - X|Rest

member(X, [ member(X, [H Tail]) | member(X, : Either we find X or EitherX becomes the find empty.list we Ifnot, look we for thethe restelement in ofthe list. Check if theCheckthe firstelementone is areif we searchingfor.

member(2,[1,2,3])?

• •

• Find an element in a list a in element an Find INF 3110 – 2016 and and Xs 42 Should give Should append(Xs, Ys, append(Xs, Zs). - Zs: into a third list athird into

append([1,3],2, [4,5], Result).

-

Prolog program: Prolog We will define a relation to concatenate twoto lists will a relation We define Ys

append([X | Xs], Ys, [X | Zs]) Xs],Ys, | | [X append([X : append([],Ys, Ys).

• | ? = [1,2,3,4,5].Result

• Append two lists Append INF 3110 – 2016 43 relations ? ; 1 , Result). , ],X). X = 2 1,2,3 , List 1 fst([ B can be represented in Prolog as a Prolog in can represented B be -

may be seen as input parameters input as seen be may fst([X|Xs],X) . . fst([X|Xs],X) | ? → 2 List x = fst(x::xs) fun is theoutputis parameter and and append(List 1

ML: Prolog: List Result relf(a,b) may be understood as f(a)=b as understood be may relf(a,b) This allows using Prolog programs inmultiple ways Prolog programs allows using This Functions areparticular a Functions of relations case

• • • • • • •

Compare with ML: Compare So, in in So, A function f:A relf(a,b) relation There are no functions in Prolog, but but Prolog, in functions no are There

◆ ◆

◆ Functions? INF 3110 – 2016 44

Differentviews ofa Prolog program Lists in Prolog in Lists Facts,rules, queries and unification Brief overviewBrief paradigmof the logical A bit ofhistorybit A

Outline ◆

◆ ◆

◆ ◆ INF 3110 – 2016 45 parameter member(X, Tail). member(X, ’ - Head _ member(X, Tail). member(X, ‘ - variables parameter). parameter). Rest

member(X, [_| Tail]) : member(X, member(X, [X|_]). member(X,

We can We writeas it follows: member(X, [Head Tail]) | member(X, : Example: In the program InExample: the [X|Rest]). member(X, When we are not interested in the value of avaluecertain the of in interested we not are When we may useparameter,

◆ (nor in the the in (nor we are not interested in the the in interested wenot are ◆ ◆ Anonymous Anonymous INF 3110 – 2016

) 1 ( 46 is a member X means member(X,Xs). - Xs member(X, Xs)

of the list ofthe list Example: Example: SomeProlog programs may be used both for testingand for computing

member(X, [_ | Xs]): member(X, [X | _]). ◆

◆ Multiple uses program of Prolog a INF 3110 – 2016

) 1 ( 47

member(X, [mon, wed, fri]). [mon, member(X, member(wed, [mon, wed,fri]). [mon, member(wed, - -

For computing: For For testing: For

no X = fri ? = fri X X = wed ? X | ? ? = mon X

◆ yes | ? ◆ Multiple uses program of Prolog a INF 3110 – 2016

) 2 ( 48

append(Xs,Ys,Zs)

s possibles to use the same program to

’ append([first, second, third], [fourth, fifth], Zs). fifth], [fourth, append([first, second,third], -

Toconcatenate two lists: Example: Example: possible ways possible It concatenate andall two lists in to a list split

Zs = [first, = second,Zs third, fourth, fifth]. | ? ◆ ◆

◆ Multiple uses program of Prolog a INF 3110 – 2016

) 2 ( 49

append(Xs, Ys, [first, second, third, fourth, fifth]). Ys,fourth, append(Xs, [first, second,third,

-

To split lista To ways: inall possible

Xs = [first,second,third,fourth,fifth] Ys = [] ? [] = Ys [first,second,third,fourth,fifth] = Xs Xs = [first,second,third,fourth] Ys = [fifth]? = Ys [first,second,third,fourth] = Xs Xs = [first,second,third] Ys = [fourth,fifth] ?[fourth,fifth] = Ys [first,second,third] = Xs Xs = [first,second] Ys = [third,fourth,fifth] ? [third,fourth,fifth] = Ys [first,second] = Xs Xs = [first] Ys = [second,third,fourth,fifth] ? [second,third,fourth,fifth] = Ys [first] = Xs Xs = [] Ys = [first,second,third,fourth,fifth] ? [first,second,third,fourth,fifth] = Ys [] = Xs

◆ | ? Multiple uses program of Prolog a INF 3110 – 2016 50 Chapter15 , 1994. MIT Press Series. Series. MIT Press 1994. , – s books ’

Evenfurther reading: Sterling and E. Shapiro: The Art of Prolog http://buldozer.securityrisk.org/docs/PROLOG/ Seealso the tutorial by Power: James Mitchell

◆ ◆

◆ Further reading Further INF 3110 – 2016 51 an overview. an – Deals with Clauses = Rules and and Facts how they are – variables. variables. - s chap 15s chap ’ The point is to relationship a between programming logic and imperative programming. -

examples. examples. 2 Computation process 3 Clauses More how about computations take Multiple place. uses of prologprograms (testing vs. computing). Several The formal basis for unification and the unification algorithm. computed. 1 Simple Clauses

15.10 Summary 15. 9 Bibliography 15.7 Control, meta ambivalent and syntax 15.8 of Assessment prolog. 15.6 Arithmetic prolog in 15.5 Prolog's approach to programming approach 15.5 Prolog's 15.4 Clauses as parts of parts procedure as declarations15.4 Clauses 15.3 Equations solved by unification15.3 of Equations atomic actions. 15.1 History of logic programming 15.2 Brief overview of logic the Mitchell INF 3110 – 2016 52 Chapter15 , 1994. MIT Press Series. Series. MIT Press 1994. , – s books ’

The art of Prolog Evenfurther reading: Sterling and E. Shapiro: http://www.csupomona.edu/~jrfisher/www/prolog_tutor ial/pt_framer. Seealso the tutorial Power: byR. J. Mitchell

◆ ◆

◆ Further reading Further INF 3110 – 2016

) 1996 53 Colmerauer & Roussel( & Colmerauer ”

programminglanguage theoremprover à laRobinson. contribution Our wasto transform that theorem prover into a Thereno questionis that Prologessentially is a

“ Prolog