COP 4020 Programming Languages Logical programming with Prolog

  • Slides: 18
Download presentation
COP 4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan

COP 4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan

Topics n Logic programming with Prolog 10/28/2020 COP 4020 Spring 2014 2

Topics n Logic programming with Prolog 10/28/2020 COP 4020 Spring 2014 2

Definitions: Prolog Terms n n Terms are symbolic expressions that are Prolog’s building blocks

Definitions: Prolog Terms n n Terms are symbolic expressions that are Prolog’s building blocks A Prolog program consists of Horn clauses (axioms) that consist of terms Data structures processed by a Prolog program are terms A term is either ¨ ¨ ¨ n a variable: a name beginning with an upper case letter a constant: a number or string an atom: a symbol or a name beginning with a lower case letter a structure of the form: functor(arg 1, arg 2, . . . , argn) where functor is an atom and argi are sub-terms a list (also a structure with a functor) of the form [term 1, term 2, …, termn] Examples: X, Y, ABC, and Alice are variables ¨ 7, 3. 14, and ”hello” are constants ¨ foo, bar. Fly, and + are atoms ¨ bin_tree(foo, bin_tree(bar, glarch)) and +(3, 4) are structures ¨ 10/28/2020 COP 4020 Spring 2014 3

Term Manipulation n Terms can be analyzed and constructed ¨ Built-in predicates functor and

Term Manipulation n Terms can be analyzed and constructed ¨ Built-in predicates functor and arg, for example: n n ¨ ? - functor(foo(a, b, c), foo, 3). yes ? - functor(bar(a, b, c), F, N). F = bar N = 3 ? - functor(T, bee, 2). T = bee(_G 1, _G 2) ? - functor(T, bee, 2), arg(1, T, a), arg(2, T, b). T = bee(a, b) The “univ” operator =. . n Break up a term into a list or form a term from a list. ¨ ¨ 10/28/2020 ? - foo(a, b, c) =. . L L = [foo, a, b, c] ? - T =. . [bee, a, b] T = bee(a, b) COP 4020 Spring 2014 4

Unification and Variable Instantiation n n Variables are used in clauses: A variable is

Unification and Variable Instantiation n n Variables are used in clauses: A variable is instantiated to a term as a result of unification, which takes place when goals are matched to head predicates Goal in query: rainy(C) ¨ Fact: rainy(seattle) ¨ Unification is the result of the goal-fact match: C=seattle ¨ n Unification is recursive: An uninstantiated variable unifies with anything, even with other variables which makes them identical (aliases) ¨ An atom unifies with an identical atom ¨ A constant unifies with an identical constant ¨ A structure unifies with another structure if the functor and number of arguments are the same and the arguments unify recursively ¨ n Once a variable is instantiated to a non-variable term, it cannot be changed: “proofs cannot be tampered with” 10/28/2020 COP 4020 Spring 2014 5

Examples of Unification n The built-in predicate =(A, B) succeeds if and only if

Examples of Unification n The built-in predicate =(A, B) succeeds if and only if A and B can be unified, where the goal =(A, B) may be written as A = B ¨ ¨ ¨ ¨ ? - a = a. yes ? - a = 5. No ? - 5 = 5. 0. No ? - a = X. X = a ? - foo(a, b) = foo(a, b). Yes ? - foo(a, b) = foo(X, b). X = a ? - foo(X, b) = Y. Y = foo(X, b) ? - foo(Z, Z) = foo(a, b). no 10/28/2020 COP 4020 Spring 2014 6

Prolog Lists n A list is of the form: [elt 1, elt 2, .

Prolog Lists n A list is of the form: [elt 1, elt 2, . . . , eltn] n where elti are terms The special list form [elt 1, elt 2, . . . , eltn | tail] n denotes a list whose tail list is tail Examples ? - [a, b, c] = [a|T]. T = [b, c] ¨ ? - [a, b, c] = [a, b|T]. T = [c] ¨ ? - [a, b, c] = [a, b, c|T]. T = [] ¨ 10/28/2020 COP 4020 Spring 2014 7

List Operations: List Membership n n List membership definitions: member(X, [X|T]). member(X, [H|T]) :

List Operations: List Membership n n List membership definitions: member(X, [X|T]). member(X, [H|T]) : - member(X, T). ? - member(b, [a, b, c]). ¨ ¨ ¨ ¨ Execution: member(b, [a, b, c]) does not match member(X, [X|T]) member(b, [a, b, c]) matches predicate member(X 1, [H 1|T 1]) with X 1=b, H 1=a, and T 1=[b, c] Sub-goal to prove: member(b, [b, c]) matches predicate member(X 2, [X 2|T 2]) with X 2=b and T 2=[c] The sub-goal is proven, so member(b, [a, b, c]) is proven (deduced) Note: variables can be "local" to a clause (like the formal arguments of a function) Local variables such as X 1 and X 2 are used to indicate a match of a (sub)-goal and a head predicate of a clause 10/28/2020 COP 4020 Spring 2014 8

Predicates and Relations n n Predicates are not functions with distinct inputs and outputs

Predicates and Relations n n Predicates are not functions with distinct inputs and outputs Predicates are more general and define relationships between objects (terms) member(b, [a, b, c]) relates term b to the list that contains b ¨ ? - member(X, [a, b, c]). X = a ; % type '; ' to try to find more solutions X = b ; %. . . try to find more solutions X = c ; %. . . try to find more solutions no ¨ ? - member(b, [a, Y, c]). Y = b ¨ ? - member(b, L). L = [b|_G 255] where L is a list with b as head and _G 255 as tail, where _G 255 is a new variable ¨ 10/28/2020 COP 4020 Spring 2014 9

Predicates and Relations ? - member(b, L). L = [b|_G 545] ; L =

Predicates and Relations ? - member(b, L). L = [b|_G 545] ; L = [_G 544, b|_G 548] ; L = [_G 544, _G 547, b|_G 551] ; L = [_G 544, _G 547, _G 550, b|_G 554] ; L = [_G 544, _G 547, _G 550, _G 553, b|_G 557] ; L = [_G 544, _G 547, _G 550, _G 553, _G 556, b|_G 560] ; L = [_G 544, _G 547, _G 550, _G 553, _G 556, _G 559, b|_G 563]. 10/28/2020 COP 4020 Spring 2014 10

Example: List Append n n n L 3 = L 1 || L 2.

Example: List Append n n n L 3 = L 1 || L 2. How to write this in Prolog? Predicates do not have return value ¨ n L 1, L 2, L 3 must be arguments to a predicate, the programming will then specify the relation. ¨ n Implication? Append(L 1, L 2, L 3). Define this recursively Case 1: when L 1 is empty ¨ Case 2: when L 1 is not empty ¨ Prolog has not if constructs, how to do two cases? ¨ 10/28/2020 COP 4020 Spring 2014 11

Example: List Append n Append(L 1, L 2, L 3). %L 3 = L

Example: List Append n Append(L 1, L 2, L 3). %L 3 = L 1 || L 2 n Define this recursively ¨ Case 1: when L 1 is empty n ¨ append([], L 2). Case 2: when L 1 is not empty n n append([H | T], L 2, [H | append(T, L 2) ]) – of course this is incorrect, append does not have a return value. Solution: ¨ n append ([H | T], L 2, [H | L]) : - append(T, L 2, L). Final solution: append([], A, A). append([H|T], A, [H|L]) : - append(T, A, L). 10/28/2020 COP 4020 Spring 2014 12

Example: List Append n n n n List append predicate definitions: append([], A, A).

Example: List Append n n n n List append predicate definitions: append([], A, A). append([H|T], A, [H|L]) : - append(T, A, L). Prolog append is more power then the append function in other languages ? - append([a, b, c], [d, e], X). X = [a, b, c, d, e] ? - append(Y, [d, e], [a, b, c, d, e]). Y = [a, b, c] ? - append([a, b, c], Z, [a, b, c, d, e]). Z = [d, e] ? - append([a, b], [a, b, c]). No ? - append([a, b], [X|Y], [a, b, c]). X = c Y = [] 10/28/2020 COP 4020 Spring 2014 13

Example: take out one element from a list n n predicate prototype: takeout(Item, Src.

Example: take out one element from a list n n predicate prototype: takeout(Item, Src. L, Dst. L) Three cases: Src. L is empty ¨ Src. L is not empty: the first element is Item (take out this item) ¨ Src. L is not empty: the first element is not Item (Keep this item) ¨ 10/28/2020 COP 4020 Spring 2014 14

Example: take out one element from a list n n Example 6. pl Item

Example: take out one element from a list n n Example 6. pl Item may or may not be in Src. L ¨ Three cases: n Src. L is empty: ¨ n Src. L is not empty: the first element is Item (take out this item) ¨ n takeout(Item, [Item | L], L). Src. L is not empty: the first element is not Item (Keep this item) ¨ n takeout(Item, []). Takeout(Item, [X | L], [X | L 1]) : - takeout(Item, L, L 1). Item must be in Src. L? 10/28/2020 COP 4020 Spring 2014 15

Permutation of a list n perm(L 1, L 2). %L 2 is a permutation

Permutation of a list n perm(L 1, L 2). %L 2 is a permutation of L 1 n n perm([], []). perm([X|Y], Z) : - perm(Y, W), takeout(X, Z, W). n Example 7. pl 10/28/2020 COP 4020 Spring 2014 16

Example: sorting n Interface: sort(List, Sortedlist). ¨ n All variables must start with a

Example: sorting n Interface: sort(List, Sortedlist). ¨ n All variables must start with a capital letter. Naïve_sort (very inefficient sorting) naive_sort(List, Sorted): -perm(List, Sorted), is_sorted(Sorted). n How to write is_sorted? 10/28/2020 COP 4020 Spring 2014 17

Example: sorting n How to write is_sorted? is_sorted([]). is_sorted([_]). is_sorted([X, Y | L]) :

Example: sorting n How to write is_sorted? is_sorted([]). is_sorted([_]). is_sorted([X, Y | L]) : - X < Y, is_sorted([Y|L]). 10/28/2020 COP 4020 Spring 2014 18