# Prolog by Example. - A predicate may have several alternative definitions (both facts and rules).

Save this PDF as:

Size: px
Start display at page:

Download "Prolog by Example. - A predicate may have several alternative definitions (both facts and rules)."

## Transcription

1 Prolog by Example - A Prolog program is composed of predicates defined by facts and rules, which are special cases of definite (or Horn) clauses, i.e. clauses with at most one positive literal (the head of the clause). - A predicate may have several alternative definitions (both facts and rules). - A fact has no negative literals and expresses positive knowledge that is definite (no disjunctions). Example: The knowledge that John is a child of Ann and Alex, and that Ann is a child of Bob, is expressed by two facts, namely child_of(john, ann). child_of(john, alex). child_of(ann, bob). - A rule has one or more negative literals (the body of the clause), and is used to infer predicates from other predicates. Example: A grand child is the child of the child, i.e. grand_child_of(x,y) :- child_of(x,z), child_of(z,y). - The name rule is due to the fact that a definite clause, H B 1 B 2... B k can be written in the form of an if expression H B 1 B 2... B k, which is the syntax adopted in Prolog. Foundations of Logic and Constraint Programming 1

2 Prolog by Example child_of(john, ann). child_of(john, alex). child_of(ann,bob). grand_child_of(x,y) :- child_of(x,z), child_of(z,y). - All variables (starting with capital letters) are universally quantified. The rule can be read For any X, Y and Z, if X is a child of Z and Z is a child of Y, then X is a grand child of Y. - Rules can be rephrased such that variables appearing only in the body of a rule are existentially quantfied. In this case, X is a grand child of Y, if there is some Z, of which X is a child, and is a child of Y. - A program may be regarded as a knowledge base, combining explicit knowledge of facts as well as implicit knowledge by means of rules. This knowledge may be queried by negative rules (no head). Notice that due to the negation, variables are read existentially. - Examples: Are there any parents of Alex (any X of which John is a parent)??- child_of(x,alex). Are there any A and B such that A is a grand parent of B (or B is a grand child of A)??- grand_child_of(a,b). Foundations of Logic and Constraint Programming 2

3 Prolog by Example child_of(john, ann). child_of(john, alex). child_of(bob, john). grand_child_of(x,y) :- child_of(x,z), child_of(z,y). - A Prolog engine is able to answer the queries (by implementing resolution on Horn clauses), finding all solutions and presenting them, one at a time.?- child_of(x, alex). X = john; no.?- grand_child_of(a,b). A = bob, B = ann ; A = bob, B = alex; no. - In practice, to program in Prolog, it is important to understand the options that Prolog engines assume, in applying resolution. - Informally, to search for all solutions, Prolog uses the query as an initial resolvent, and organises all the other resolvents in an execution tree. - Solutions, if any, are the success leafs of the tree (the other leafs are failed). Foundations of Logic and Constraint Programming 3

4 Prolog by Example child_of(john, ann). child_of(john, alex). child_of(bob, john). grand_child_of(x,y) :- child_of(x,z), child_of(z,y). - For example, query?- grand_child_of(a,b), expands into the execution tree grand_child_of(a,b) X/A, Y/B child_of(a,z), child_of(z,b) A/john Z/ann A/john Z/alex child_of(ann,b) child_of(alex,b) child_of(john,b) B/ann A/bob Z/john B/alex - Notice that Instances of the answers are obtained by composing the substitutions in a path to a successful leaf The tree is searched depth-first, left-to-right. Foundations of Logic and Constraint Programming 4

5 Prolog by Example: Family relationships - Let us assume some elements from the larger family tree. doug carol ann john mary vicky peter bob alex tess alice - The basic relationship that must be recorded is parent-child (the one kept in the ID cards and databases), modeled by the binary predicate child_of/2. fred child_of(john,doug). child_of(john,carol). child_of(mary,carol). child_of(tess,mary). child_of(fred,tess). child_of(peter,john). child_of(bob,john). child_of(alex,john). child_of(peter,ann). child_of(bob,ann). child_of(alex,ann). child_of(alice, peter). child_of(alice,vicky). Foundations of Logic and Constraint Programming 5

6 Prolog by Example: Family relationships - Other useful information can be introduced, such as gender (although usually implicit in the name but still this info is explicit in ID cards and passports). This is expressed by the unary predicate is_male/1 and is_female/1. is_male(doug). is_male(john). is_male(fred). is_male(peter). is_male(bob). is_male(alex). is_female(mary). is_female(carol). is_female(tess). is_female(ann). is_female(alice). is_female(vicky). vicky alice doug ann john peter bob alex carol mary tess fred - Once the basic relationships are established among the elements of the family, other family relationsships and predicates. For example, the relationship parent_of is the opposite of child_of: parent_of(x,y):- child_of(y,x). - The predicate mother is assigned to any female that is the parent of someone. mother_of(x,y):- parent_of(x,y), is_female(x). - A mother is the mother of someone. is_mother(x):- mother_of(x,_). Foundations of Logic and Constraint Programming 6

7 Prolog by Example: Repeated Answers - For any mother with many children, the predicate is_mother/1 may be deduced several times, once for each child. This leads to repeated answers to query?- is_mother(x). X= carol? ; X = carol? ; X = mary? ; X = tess? ; X = ann? ; is_mother(x) X = ann? ; X = ann? ; mother_of(x,y) X = vicky? ; no. parent_of(x,y), is_female(x). child_of(john,doug). child_of(john,carol). child_of(mary,carol). child_of(tess,mary). child_of(fred,tess). child_of(peter,john). child_of(bob,john). child_of(alex,john). child_of(peter,ann). child_of(bob,ann). child_of(alex,ann). child_of(alice, peter). child_of(alice,vicky). is_female(doug) X/john Y/doug child_of(y,x), is_female(x). X/john Y/carol is_female(carol) Foundations of Logic and Constraint Programming 7

8 Prolog by Example: Different Variables - Other indirect relationships may be derived from their definition, namely grand_child_of/2 sibling_of/2, brother_of/2, sister_of/2 uncle_of/2, aunt_of/2, great_uncle_of, great_aunt_of/2, cousin_of, second_cousin_of/2, third_cousin_of/2,... - Although most definitions are direct, some care must be taken. For example, siblings are children of a common parent sibling_of(x,y):- child_of(x,z), child_of(y,z). - This definition suffers from a quite common mistake: to assume that different variables should stand for different values. In the definition above a person is a sibling of him/herself! - Here is the correct definition, where the difference (\=) is made explicitely: sibling_of(x,y):- child_of(x,z), child_of(y,z). X \= Y. Foundations of Logic and Constraint Programming 8

9 Prolog by Example: Different Variables child_of(john,doug). child_of(john,carol). child_of(mary,carol). child_of(tess,mary). child_of(fred,tess). child_of(peter,john). child_of(bob,john). child_of(alex,john). child_of(peter,ann). child_of(bob,ann). child_of(alex,ann). child_of(alice, peter). child_of(alice,vicky). sibling_of(x,y):- child_of(x,z), child_of(y,z), X \= Y. sibling_of(x,y) vicky alice doug ann john peter bob alex carol mary tess fred X/john Z/doug child_of(x,z), child_of(y,z), X \= Y. X/john Z/carol child_of(y,doug), john\= Y child_of(y,carol), john\= Y Y/john john\= john Y/mary john\= mary Foundations of Logic and Constraint Programming 9

10 Prolog by Example: Non-termination - Care must also be taken to avoid definitions that, correct as they might be, eventually become circular, leading to non termination. - For example, the simetric property of the sibling_of relationship could, in principle, be explicitely specified, by the additional rule sibling_of(x,y):- sibling_of(y,x). - But this definition would lead, when X and Y are non-ground, to an infinite tree: sibling_of(x,y):- child_of(x,z), child_of(y,z), X \= Y. sibling_of(x,y):- sibling_of(y,x). child_of(x,z), child_of(y,z), X \= Y. sibling_of(x,y) sibling_of(y,x) sibling_of(x,y) sibling_of(y,x) sibling_of(x,y) Foundations of Logic and Constraint Programming 10

11 Prolog by Example: Peano Arithmetics - Logic has been used to formalise arithmetics, and a number of interesting (mostly theoretical) results where obtained from this effort (for example, the famous Gödel incompletness theorem). - The standard formalisation is due to Peano (end of 19th century), that proposed a simple set of axioms, for this purpose. - To begin with, integers are defined inductively: an integer is either 0 or the sucessor of another integer. - The basic algebraic operations can also be formalised, again inductively. For example the addition of two integers: Base clause: The sum of 0 and some integer M, is that integer. Inductive clause: The sum of the successor of an integer M and another integer N is the successor of the sum of the integers M and N. - Similar definitions can be used to define the operations of multiplication and potentiation. - Logic programming can thus be used to directly implement arithmetics (although in a very inefficient way). Foundations of Logic and Constraint Programming 11

12 Prolog by Example: Peano Arithmetics - The definition of integers is straighgtforward. Below the predicate int/1 is used to assign the property intehger, and the functor s/1 to denote the successor of an integer. int(0). % 0 is an integer int(s(m)):- int(m). % the successor of an integer is integer. - The addition follows directly the definition. sum(0,m,m). % The sum of 0 and any integer M is M sum(s(n),m,s(k)):- % The sum the successor of N and M is the sum(n,m,k). % successor of the sum of N and M. - The product, as a repeated sum, is modelled by means of addition. prod(0,m,0). % The product of 0 with any integer is 0. prod(s(n),m,p):- % The product of the successor of N and M is prod(n,m,k), % the sum of M with the product of M and N. sum(k,m,p). % i.e. (N+1)*M = N*M + M - And so is the power, as a repeated multiplication (the exponent is the first argument) pow(0,0, s(0)). % 0 raised to 0 is 0. pow(0,s(_),s(0)). % Integer M (>0) raised to 0 is 1 (i.e. S(0)). pow(s(n),m,p):- % Integer M raised to the successor of N pow(n,m,k), % is the product of M with M raised to N. prod(k,m,p). % i.e. M^(N+1) = M^N * M Foundations of Logic and Constraint Programming 12

13 Prolog by Example: Peano Arithmetics - It is interesting to note the functioning of Prolog, with these predicates. - First, the predicate integer can be used either to check whether an argument is an integer, either to generate integers.?- int(s(s(0))). yes?- int(x). X = 0? ; % X=0 X = s(0)? ; % X=1 X = s(s(0))? ; % X=2 X = s(s(s(0)))? % X=3?- int(s(s(x))). X = 0? ; % X = s(0)? ; - Notice in the last case, that the arguments of predicate int that are reported in the answers are usually denoted by 2, 3,... Foundations of Logic and Constraint Programming 13

14 Prolog by Example: Peano Arithmetics - The predicate sum performs as expected. Given two numbers to be summed, the predicate returns their sum. Let us sum 3 with 2?- sum(s(s(0)),s(s(s(0))),x). X = s(s(s(s(s(0)))))? ;no - The corresponding execution tree can be displayed sum(s(s(0)))),s(s(s(0))),x) sum(0,m,m). sum(s(n),m,s(k)):- sum(n,m,k). M1 / s(s(s(0))) N1 / s(0); X / s(k1) sum(s(0),s(s(s(0))),s(k1)) M2 / s(s(s(0))) N2 / 0 ; K1 / s(k2) sum(s(s(s(0))),0,s(k2)) K2 / s(s(s(0))) The result X is then obtained by composition of substitutions X/s(K1) but K1/s(K2)) so X/s(s(K2)) but K2/s(s(s(0))) so X/s(s(s(s(s(0))))) i.e. X = 5 Foundations of Logic and Constraint Programming 14

15 Prolog by Example: Peano Arithmetics - Logic programming is flexible. Predicate sum also defines subtraction! Given two numbers to be subtracted, the predicate also returns their difference. Let us subtract 2 from 5?- sum(s(s((0)),s(s(s(0))),x). X = s(s(s(s(s(0)))))? ;no sum(0,m,m). sum(s(n),m,s(k)):- sum(n,m,k). sum(s(s(0)),x,s(s(s(s(s(0)))))) M1 / X ; N1 / s(0) K1 / s(s(s(s(0)))) sum(s(0),x, s(s(s(s(0)))) M2 / X ; N2 / 0 K2 / s(s(s(0))) sum(0,x,s(s(s(s(s(0))))) X / s(s(s(0))) Foundations of Logic and Constraint Programming 15

16 Prolog by Example: Peano Arithmetics - The flexibility of logic programming allows that still the same definition enables to find all operands whose sum is 5.?- sum(y,s, sum(s(s(s((0)))). Y = s(s(s(s(s(0))))), X = 0? ; Y = s(s(s(s(0)))), X = s(0)? ; Y = s(s(s(0))), X = s(s(0))? ; Y = s(s(0)), X = s(s(s(0)))? ; Y = s(0), X = s(s(s(s(0))))? ; Y = 0, X = s(s(s(s(s(0)))))? ; no X / s(s(s(s(s(0))))) Y / 0 sum(y,x,s(s(s(s(s(0)))))) M1 / X ; Y / s(n1) K1 / s(s(s(s(0)))) sum(0,m,m). sum(s(n),m,s(k)):- sum(n,m,k). sum(n1,x,s(s(s(s(0))))) X / s(s(s(s(0)))) N1 / 0 M2 / X ; N1 / s(n2) K2 / s(s(s(0))) sum(n2,x,s(s(s(0)))) X / s(s(s(0))) N2 / 0 Foundations of Logic and Constraint Programming 16

17 Prolog by Example: Peano Arithmetics - The other predicates, for product and potentiation, behave similarly to the predicate for sum. However, if no solutions exist there might be termination problems, if the recursion is performed in the wrong argument. For example, there are no problems with 2 * X = 3 prod(s(s(0)),x,s(s(s(0)))) prod(s(0),x,k1), sum(x,k1,s(s(s(0)))) prod(0,x,k2), sum(x,k2,k1), sum(x,k1,s(s(s(0)))) sum(0,x,k1), sum(x,k1,s(s(s(0)))) X / M1, K1 / M1 ) sum(m1,m1,s(s(s(0)))) M1 / s(n2) sum(n2, s(n2), s(s(0))) N2 / s(n3) sum(n3, s(s(n3)), s(0)) N3 / s(n4) sum(n4, s(s(s(s(n4)), 0) prod(0,m,0). prod(s(n),m,p):- prod(n,m,k) sum(k,m,p). sum(0,m,m). sum(s(n),m,s(k)):- sum(n,m,k). Foundations of Logic and Constraint Programming 17

18 Prolog by Example: Peano Arithmetics - However, the same is not true for X * 2 = 3, where the unknown argument is the one used in the recursion. prod(0,m,0). prod(s(n),m,p):- prod(x,s(s(0)),s(s(s(0)))) prod(n,m,k) M1/s(s(0)), X /s(n1) sum(k,m,p). prod(n1,s(s(0)),k1), sum(n1,s(s(0)),s(s(s(0)))) sum(0,m,m). N1 / 0, K1 / 0 sum(s(n),m,s(k)):- sum(0,s(s(0)),s(s(s(0)))) sum(n,m,k). M2/s(s(0)) ; N1/s(N2), K1 / P2 prod(n2,s(s(0)),k2), sum(k2,s(s(0)),p2), sum(p2,s(s(0)),s(s(s(0)))) M3/s(s(0)) ; N2/s(N3), K2 / P3 prod(n3,s(s(0)),k3), sum(k3,s(s(0)),p3), sum(...), sum(...) M4/s(s(0)) ; N3/s(N4), K3 / P4 prod(n4,s(s(0)),k4), sum(k4,s(s(0)),p4), sum(...), sum(...), sum(...) - It is left as an exercise to check what types of calls do not terminate with the above definitions. Foundations of Logic and Constraint Programming 18

19 Prolog by Example: Peano Arithmetics - Although important from a theoretical viewpoint, Peano arithmetics is not very useful in practice due to the complexity of both the representation of numbers and the basic operations. - For example, the usual repretsentation of an integer N in the usual base 2 or 10 requires O(lg 2 N) or O(lg 10 N) bits. In contrast, Peano representation requires O(N) symbols (more precisely N s s + 2N parenthesis + 1 zero, which is significantly larger. - As to the sum operation, say N plus N, the usual method is proportional to the number of bits / digits i.e. it has complexity O(lg N). - In contrast, peano sum requires N calls to the sum definition, i.e. It has complexity O(N) sum(s(s(s(0))),x,p) sum(s(s(0)),x,p) sum(s(0),x,p) sum(0,x,p) sum(m,0,m). sum(s(n),m,s(k)):- sum(n,m,k). Foundations of Logic and Constraint Programming 19

20 List Processing in Prolog: Length - If Peano arithmetics is not very useful in practice, the same kind of definitions are widely applicable, namely in structures like lists. - Assuming the Peano representation of integers, the following definition of predicate p_lenght/2 highlights the correspondence between 0 and the empty list as well as between the s and the list functors p_length([],0). p_length([_ T],s(N)):- p_lenght(t,n). - Of course, if the usual arithmetics is used, the definition has to be adapted, such that the notion of successor is replaced by the usual +1 operation a_length([],0). a_length([_ T], M):- a_length(t,n), M is N+1. - There are however disadvantages in this formulation, namely non-termination. This is illustrated by the execution trees of both predicates to obtain lists of size 2. Foundations of Logic and Constraint Programming 20

21 List Processing in Prolog : Length - There are however disadvantages in this formulation, namely non-termination. This is illustrated by the execution trees of both predicates to obtain lists of size 2. - In the peano modelling, the goal is of course?- p_length(l,s(s(0))). and the execution tree is as follows p_length(l,s(s(0))) L/ [_ T1] p_length(t1,s(0))) L1/ [_ T2] p_length(t2,0) P_length([],0). P_length([_ T],s(N)):- p_lenght(t,n). T2/ [] Composing substitutions, the value of L is obtained T2 = [] T1 = [_ T2] = [_] L = [_ T1] = [_,_] Foundations of Logic and Constraint Programming 21

22 List Processing in Prolog : Length - With the modelling with standard arithmentics, the corresponding tree, for query?- a_length(l,2), is more complex a_length([],0). a_length([_ T],M):- a_lenght(t,n), M is N+1. a_length(l,2) a_length(t1,n1), 2 is N1+1 L/ [_ T1], M / 2 T1/ [], N1 / 0 2 is 0+1 T2/[], N2/0 N1 is 0+1, 2 is N1+1 T1/ [_ T2], M2 / N1 a_length(t2,n2), N1 is N2+1, 2 is N1+1 T2/ [_ T3], M3 / N2 a_length(t3,n3), N2 is N3+1,... 2 is 1+1 N1/1 - The correct answer is obtained as before, in the left part of the tree T2 = [] ; T1 = [_ T2] = [_]; L = [_ T1] = [_,_] - However, there is now a right part of the tree... Foundations of Logic and Constraint Programming 22

23 List Processing in Prolog : Length - However, the right part of the execution tree is infinite! a_length([],0). a_length([_ T],M):- a_lenght(t,n), M is N+1. a_length(t3,n3), N2 is N3+1,... T4/ [_ T3], M3 / N2 N3 is 0+1,..., 2 is N1+1 a_length(t4,n4), N3 is N4+1,... T4/ [_ T3], M3 / N2 N4 is 0+1,..., 2 is N1+1 a_length(t5,n5), N4 is N5+1,... T4/ [_ T3], M3 / N2 N5 is 0+1,..., 2 is N1+1 a_length(t6,n6), N5 is N6+1,... Foundations of Logic and Constraint Programming 23

24 List Processing in Prolog: Concatenation - A very common operation between lists is their concatenation. Predicate cat/3 below is true, when the list in the third argument is the concatenation of the lists in the first and second arguments. - As usual, the definition below relies on the inductive structure of the first list, and considers the cases where it is empty (the base clause) or not (the induction clause). cat([],l,l). cat([h T],L,[H R]):- cat(t,l,r). - It is interesting to notice in the execution tree, that concatenating a list with N 1 elements with a list with N 2 elements, requires N 1 calls to the recursive clause, for lists with sizes N 1, N 1-1, N 1-2,..., 2, 1. N 1 trivial failed calls to the base clause, for these lists (in fact, implementations of prolog indexing on the first argument do not make these calls). 1 successful call with an empty list as first argument. - The concatenation of a list requires thus N 1 +1 (non-trivial) calls, and presents thus a complexity of O(N), where N is the size of the first list argument. Foundations of Logic and Constraint Programming 24

25 List Processing in Prolog: Concatenation - From an algorithmic view point, this complexity is the one that can be expected for lists, maintained as simply linked lists, with a pointer to its head (first element). - This is in fact the case with Prolog, where lists are maintained in this form. However, a different form of representation, difference lists, allow the tail append to be a simple operation, as if lists were implemented with a pointer to their tail. - In any case, the declarativity of this definition allows the use of this cat/3 predicate either to concatenate two lists into a third one, or to remove a sublist (prefix or postfix) from a list.?- cat([1,2],[3,4,5],l). L = [1,2,3,4,5]; no.?- cat(l,[3,4,5],[1,2,3,4,5]). L = [1,2]; no.?- cat([1,2],l,[1,2,3,4,5]). L = [3,4,5]; no.?- cat(l1,l2,[1,2,3]). L1 = [], L2 = [1,2,3]; L1 = [1], L2 = [2,3]; L1 = [1,2], L2 = [3]; L1 = [1,2,3], L2 = []; no. cat([],l,l). cat([h T],L,[H R]):- cat(t,l,r). Foundations of Logic and Constraint Programming 25

26 List Processing in Prolog: Sublists - The declarativity of logic programming, makes it possible to reuse the definition of concatenation, to define other relations between a list and its (non-empty) sublists. - Prefix: List P is a (non-empty) prefix of list L if there is some other list X such that L is the concatenation of P and X (in Prolog, X may be declared as an anonimous list). prefix([h T],L):- cat([h T],_,L). - Posfix: List P is a (non-empty) postfix of list L if there is some other list X such that L is the concatenation of X and P (in Prolog, X may be declared as an anonimous list). posfix_of([h T],L):- cat(_,[h T],L). - Sublist: The sublist relation can be defined upon the prefix and posfix relations: list S is a (non-empty) sublist of L if there is a prefix P of list L, of which S is a posfix. sublist(s,l):- prefix(p,l), posfix(s,p). Foundations of Logic and Constraint Programming 26

27 List Processing in Prolog: Member and Insert - In addition to relationships bewteen lists, it is often important to consider relationships between lists and their members. - The most useful predicate, member/2, relates a list with any of its members member(x,[x _]). member(x,[_ T]):- member(x,t). - Another important predicate relates two lists, L1 and L2, with an element X. Given the declarativeness of logic programming this predicate can either be interpreted as Inserting X into L1 to obtain L2 Removing X from L2 to otain L1 - The definition is similar to that above, considering the cases where the insertion is done in the beginning or not of list L1. insert(x,l,[x L]). insert(x,[h T], [H R]):- insert(x,t,r). Foundations of Logic and Constraint Programming 27

28 List Processing in Prolog: Permutation and Reversion - Other interesting predicates relate a list with any of its permutations. As usual the definition considers two cases The (only) permutation of an emptry list is an empty list A permutation of a non-empty list is a permutation of its tail, to which the head is inserted. perm([],[]). perm([h T],L):- perm(t,p), insert(h,p,l). - A similar definition is used to relate a list with its reverse. The (only) reversion of an empty list is an empty list A reversion of a non-empty list is a reversion of its tail, to which the head is inserted at the end (this append at the end can be obtained by concatenation of the list with a list composed of the element to be appended) reverse([],[]). reverse([h T], L):- reverse(t, R), cat(r,[h], L). - Notice the non termination problems when the first argument is a variable. Why? Foundations of Logic and Constraint Programming 28

29 List Processing in Prolog : Permutation and Reversion - The reversion of a list is not very efficient. Analysing the execution tree (left as an exercise), the reversion of a list of size N requires One (non-trivial) call to revert a list (of length N-1) One call to append an element to the end of this list, which requires N non-trivial calls to predicate cat/3. - Hence, the complexity of this algorithm measured by the number of non-trivial calls, is 1 call to reverse and a call to cat for a list of length N (i.e. N+1 calls to cat) 1 call to reverse and a call to cat for a list of length N-1 (i.e. (N-1)+1 calls to cat)... 1 call to reverse and a call to cat for a list of length 1 (i.e. 1+1 calls to cat) 0 call to reverse and 1 call to cat for a list of length 0 (i.e. 0+1 calls to cat) - The number of calls is thus N (non-trivial) calls to predicate reverse [N+1]+ [(N-1)+1] = (N+1) (N+2) / 2 (non-trivial) calls to predicate cat - The complexity of the algorithm is then O(N 2 ) which is quite inefficient, since it is quite easy to get an algorithm to perform reversion of a list in O(N) steps. Foundations of Logic and Constraint Programming 29

30 List Processing in Prolog: Difference Lists - As seen before, concatenation of lists could be made more efficient if a pointer to the end of the list is maintained. This is the basic idea of the coding of lists as difference lists. - A difference list is a structure L-T where L and T are lists and T is the tail of L. - This structure represents a list whose elements are those belonging to L but not to T, (hence the name and the functor used). For example, the difference list [1,2,3,4,5,6]-[5,6] encodes list [1,2,3,4]. - The interesting case for processing is the case where T is a variable. In this case, appending to the end of the list requires processing T. - For example, assume list [1,2,3,4], encoded as [1,2,3,4 T]-T. Appending 5 to this list, corresponds to Obtaining a new tail T = [5 S] Subtracting L by S. - This is the declarative meaning of predicate td_app/3 td_app(x,l-t,l-s):- T = [X S]. which can be simplified with head unification td_app(x,l-[x S],L-S). Foundations of Logic and Constraint Programming 30

31 List Processing in Prolog: Difference Lists - The same technique can be used to concatenate two difference lists, L1-T1 and L2-T2, in to a third one L3-T3. - To exemplify, let us concatenate lists [1,2,3] to [4,5] resulting into [1,2,3,4,5], all encoded as difference lists [1,2,3 T1] - T1 + [4,5 T2]-T2 = [1,2,3,4,5 T3] T3 - By analysing this example, it is clear that We can make L1 = L3 if the substitution T1 = [4,5 T3] is imposed Since L2 = [4,5 T2], the above substitution is imposed when T2 = T3 and T1 = L2 - Hence predicate cat_diff/3, describes the concatenation of difference lists cat_diff(l1-t1,l2-t2,l3-t3):- L3 = L1, T3 = T2, L2 = T1 - By using head unification the concatenation becomes quite intuitive cat_diff(l1-t1,t1-t2,l1-t2). Foundations of Logic and Constraint Programming 31

32 List Processing in Prolog: Difference Lists - The concatenation of difference lists is thus performed through simple unification, instead of requiring the inefficient recursion of the usual lists. - This result can thus be used to reverse difference lists, by adapting the previous definition of reverse/2 (for standard lists) to the new encoding as difference lists (the special encoding of the empty difference list is exaplained in the next slide) rev_diff(l-t,l-t):- L == T. rev_diff([h L1]-T1, L2-T2):- rev_diff (L1-T1, Lx-Tx), cat_diff(lx-tx,[h T]-T,L2-T2). reverse([],[]). reverse([h T], L):- reverse(t, R), cat(r,[h], L). - The definition can be improved by head unification. Since the call to cat_diff/3 performs substitutions Lx = L2, Tx = [H T] and T = T2, such substitutions can be made directly, avoiding the explicit call to cat_diff/3 rev_diff(l-t,l-t):- L == T. rev_diff([h L1]-T1, L2-T):- rev_diff (L1-T1, T1-[H T]). Foundations of Logic and Constraint Programming 32

33 List Processing in Prolog: Difference Lists - In principle, the encoding of an empty difference list is L-L. As such the reversion of an empty difference list could be made with a clause such as rev_diff(l-l,l-l). rev_diff(l-t,l-t):- L = T - Hence, when this clause is tested to reverse a non-empty list, say rev_diff([1,2 Z]-Z, X-Y), the call should fail, since [1,2 Z]-Z does not encode an empty list (it encodes of course list [1,2]) and should not unify with L-L. - More precisely, it is not possible to unify L = [1,2 Z] and L = Z, because that would require Z = [1,2 Z], i.e. to substitute a variable, Z, by a term, [1,2 Z], where the variable occurs. - This occurs-check failure does not occur in most Prolog systems that do not implement it correctly (ending in an infinite loop X = f(x) = f(f(x)) = f(f(f(x))) =...). - For this reason, instead of checking if the two variable L-T are the same through unification (i.e. with predicate =/2 ), the clause tests this case with predicate ==/2, that only succeeds if the terms are already the same at the time of call. rev_diff(l-t,l-t):- L == T Foundations of Logic and Constraint Programming 33

34 List Processing in Prolog: Langford - The declarativity of Prolog allows simple programs to solve apparently complex problems. - The Langford(M,N) problem aims at finding a list of M*N elements, such that the integers 1 to M each appear N times, in such positions that between two occurrences of any J (in the range 1..M) are separated by J different elements. - The specification of the Langford(4,2) is quite obvious us when the sublist/2 predicate is used langford42(l):- L = [_,_,_,_,_,_,_,_], sublist([1,_,1], L), sublist([2,_,_,2], L), sublist([3,_,_,_,3], L), sublist([4,_,_,_,_,4], L). - Of course, such simple specification is only possible due to the flexibility of logic programming, namely the lack of fixed input-output arguments. Foundations of Logic and Constraint Programming 34

35 List Processing in Prolog: Zebra - A more challenging problem is the classic zebra puzzle: 1. There are five houses, each of a different color and inhabited by men of different nationalities, with different pets, drinks, and cigarettes. 2. The Englishman lives in the red house. 3. The Spaniard owns the dog. 4. Coffee is drunk in the green house. 5. The Ukrainian drinks tea. 6. The green house is immediately to the right of the ivory house. 7. The Old Gold smoker owns snails. 8. Kools are smoked in the yellow house. 9. Milk is drunk in the middle house. 10.The Norwegian lives in the first house on the left. 11.The man who smokes Chesterfields lives in the house next to the man with the fox. 12.Kools are smoked in the house next to the house where the horse is kept. 13.The Lucky Strike smoker drinks orange juice. 14.The Japanese smoke Parliaments. 15.The Norwegian lives next to the blue house. 16.NOW, who drinks water? And who owns the zebra? Foundations of Logic and Constraint Programming 35

36 List Processing in Prolog: Zebra - Again, this problem is solved with a very simple program, but requires slightly more involved data structures. Here is a possible solution A solution is a list of 5 entities, each composed of a house, a nationality, a pet, a drink and a cigarrette brand. These different components can be associated in a single term, h(h,n,p,d,c). In list L, an element A that is before another element B, encodes the fact that house H A is to the left of house H B. - Once this is done, the program has the following structure, where the constraints are still to be specified zebra(l):- L = [h(h1,n1,p1,d1,c1), h(h2,n2,p2,d2,c2), h(h3,n3,p3,d3,c3), h(h4,n4,p4,d4,c4), h(h5,n5,p5,d5,c5)],... constraints... Foundations of Logic and Constraint Programming 36

### Artificial Intelligence 2007 Spring Midterm Solution

Artificial Intelligence 2007 Spring Midterm Solution May 31, 2007 1. (10 points) Minesweeper is a famous one-player computer game as shown in Figure 1. The player has to find all the mines in a given minefield

### Jeff Wickstrom Jason Slattery Discrete Mathematics - Logic. Lesson Plans. Discrete Mathematics

Jeff Wickstrom Jason Slattery Discrete Mathematics - Logic Lesson Plans Discrete Mathematics Discrete mathematics, also called finite mathematics, is the study of mathematical structures that are fundamentally

### The Foundations: Logic and Proofs. Chapter 1, Part III: Proofs

The Foundations: Logic and Proofs Chapter 1, Part III: Proofs Rules of Inference Section 1.6 Section Summary Valid Arguments Inference Rules for Propositional Logic Using Rules of Inference to Build Arguments

### Proofs by induction, Alphabet, Strings [1] Proofs by Induction

Proofs by induction, Alphabet, Strings [1] Proofs by Induction Proposition: If f(0) = 0 and f(n + 1) = f(n) + n + 1 then, for all n N, we have f(n) = n(n + 1)/2 Let S(n) be f(n) = n(n + 1)/2 We prove S(0)

### CHAPTER 7 GENERAL PROOF SYSTEMS

CHAPTER 7 GENERAL PROOF SYSTEMS 1 Introduction Proof systems are built to prove statements. They can be thought as an inference machine with special statements, called provable statements, or sometimes

### Predicate Logic. M.A.Galán, TDBA64, VT-03

Predicate Logic 1 Introduction There are certain arguments that seem to be perfectly logical, yet they cannot be specified by using propositional logic. All cats have tails. Tom is a cat. From these two

### Predicate Logic. For example, consider the following argument:

Predicate Logic The analysis of compound statements covers key aspects of human reasoning but does not capture many important, and common, instances of reasoning that are also logically valid. For example,

### Predicate Calculus. There are certain arguments that seem to be perfectly logical, yet they cannot be expressed by using propositional calculus.

Predicate Calculus (Alternative names: predicate logic, first order logic, elementary logic, restricted predicate calculus, restricted functional calculus, relational calculus, theory of quantification,

### Artificial Intelligence

Artificial Intelligence ICS461 Fall 2010 1 Lecture #12B More Representations Outline Logics Rules Frames Nancy E. Reed nreed@hawaii.edu 2 Representation Agents deal with knowledge (data) Facts (believe

### Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay Lecture - 17 Shannon-Fano-Elias Coding and Introduction to Arithmetic Coding

### CHAPTER 3. Methods of Proofs. 1. Logical Arguments and Formal Proofs

CHAPTER 3 Methods of Proofs 1. Logical Arguments and Formal Proofs 1.1. Basic Terminology. An axiom is a statement that is given to be true. A rule of inference is a logical rule that is used to deduce

### 3. Recurrence Recursive Definitions. To construct a recursively defined function:

3. RECURRENCE 10 3. Recurrence 3.1. Recursive Definitions. To construct a recursively defined function: 1. Initial Condition(s) (or basis): Prescribe initial value(s) of the function.. Recursion: Use a

### 3. Mathematical Induction

3. MATHEMATICAL INDUCTION 83 3. Mathematical Induction 3.1. First Principle of Mathematical Induction. Let P (n) be a predicate with domain of discourse (over) the natural numbers N = {0, 1,,...}. If (1)

### Lecture 4: Lists. Theory

Lecture 4: Lists Theory Introduce lists, an important recursive data structure often used in Prolog programming Define the member/2 predicate, a fundamental Prolog tool for manipulating lists Illustrate

### ML for the Working Programmer

ML for the Working Programmer 2nd edition Lawrence C. Paulson University of Cambridge CAMBRIDGE UNIVERSITY PRESS CONTENTS Preface to the Second Edition Preface xiii xv 1 Standard ML 1 Functional Programming

### Borland C++ Compiler: Operators

Introduction Borland C++ Compiler: Operators An operator is a symbol that specifies which operation to perform in a statement or expression. An operand is one of the inputs of an operator. For example,

### Section 1. Statements and Truth Tables. Definition 1.1: A mathematical statement is a declarative sentence that is true or false, but not both.

M3210 Supplemental Notes: Basic Logic Concepts In this course we will examine statements about mathematical concepts and relationships between these concepts (definitions, theorems). We will also consider

### Relations: their uses in programming and computational specifications

PEPS Relations, 15 December 2008 1/27 Relations: their uses in programming and computational specifications Dale Miller INRIA - Saclay & LIX, Ecole Polytechnique 1. Logic and computation Outline 2. Comparing

### Boolean Representations and Combinatorial Equivalence

Chapter 2 Boolean Representations and Combinatorial Equivalence This chapter introduces different representations of Boolean functions. It then discuss the applications of these representations for proving

### Programming Languages in Artificial Intelligence

Programming Languages in Artificial Intelligence Günter Neumann, German Research Center for Artificial Intelligence (LT Lab, DFKI) I. AI programming languages II. Functional programming III. Functional

### WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT?

WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT? introduction Many students seem to have trouble with the notion of a mathematical proof. People that come to a course like Math 216, who certainly

### MATH20302 Propositional Logic. Mike Prest School of Mathematics Alan Turing Building Room

MATH20302 Propositional Logic Mike Prest School of Mathematics Alan Turing Building Room 1.120 mprest@manchester.ac.uk April 10, 2015 Contents I Propositional Logic 3 1 Propositional languages 4 1.1 Propositional

### Puzzler: An Automated Logic Puzzle Solver

Puzzler: An Automated Logic Puzzle Solver Aleksandar Milicevic, Joseph P. Near, and Rishabh Singh Massachusetts Institute of Technology (MIT) Abstract. Solving logic puzzles is a difficult problem, requiring

### Lecture 3. Mathematical Induction

Lecture 3 Mathematical Induction Induction is a fundamental reasoning process in which general conclusion is based on particular cases It contrasts with deduction, the reasoning process in which conclusion

### School of Informatics, University of Edinburgh

CS1Ah Lecture Note 5 Java Expressions Many Java statements can contain expressions, which are program phrases that tell how to compute a data value. Expressions can involve arithmetic calculation and method

### [Refer Slide Time: 05:10]

Principles of Programming Languages Prof: S. Arun Kumar Department of Computer Science and Engineering Indian Institute of Technology Delhi Lecture no 7 Lecture Title: Syntactic Classes Welcome to lecture

### 1.1 Logical Form and Logical Equivalence 1

Contents Chapter I The Logic of Compound Statements 1.1 Logical Form and Logical Equivalence 1 Identifying logical form; Statements; Logical connectives: not, and, and or; Translation to and from symbolic

### Predicate Logic. Lucia Moura. Winter Predicates and Quantifiers Nested Quantifiers Using Predicate Calculus

Predicate Logic Winter 2010 Predicates A Predicate is a declarative sentence whose true/false value depends on one or more variables. The statement x is greater than 3 has two parts: the subject: x is

### Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras

Theory of Computation Prof. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture No. # 31 Recursive Sets, Recursively Innumerable Sets, Encoding

### (Refer Slide Time: 1:41)

Discrete Mathematical Structures Dr. Kamala Krithivasan Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture # 10 Sets Today we shall learn about sets. You must

### Full and Complete Binary Trees

Full and Complete Binary Trees Binary Tree Theorems 1 Here are two important types of binary trees. Note that the definitions, while similar, are logically independent. Definition: a binary tree T is full

### Lecture 8: Resolution theorem-proving

Comp24412 Symbolic AI Lecture 8: Resolution theorem-proving Ian Pratt-Hartmann Room KB2.38: email: ipratt@cs.man.ac.uk 2014 15 In the previous Lecture, we met SATCHMO, a first-order theorem-prover implemented

### Chapter 7: Functional Programming Languages

Chapter 7: Functional Programming Languages Aarne Ranta Slides for the book Implementing Programming Languages. An Introduction to Compilers and Interpreters, College Publications, 2012. Fun: a language

### SJÄLVSTÄNDIGA ARBETEN I MATEMATIK

SJÄLVSTÄNDIGA ARBETEN I MATEMATIK MATEMATISKA INSTITUTIONEN, STOCKHOLMS UNIVERSITET Automated Theorem Proving av Tom Everitt 2010 - No 8 MATEMATISKA INSTITUTIONEN, STOCKHOLMS UNIVERSITET, 106 91 STOCKHOLM

### Satisfiability Checking

Satisfiability Checking First-Order Logic Prof. Dr. Erika Ábrahám RWTH Aachen University Informatik 2 LuFG Theory of Hybrid Systems WS 14/15 Satisfiability Checking Prof. Dr. Erika Ábrahám (RWTH Aachen

### 1.3 Induction and Other Proof Techniques

4CHAPTER 1. INTRODUCTORY MATERIAL: SETS, FUNCTIONS AND MATHEMATICAL INDU 1.3 Induction and Other Proof Techniques The purpose of this section is to study the proof technique known as mathematical induction.

### Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Mathematics for Computer Science/Software Engineering Notes for the course MSM1F3 Dr. R. A. Wilson October 1996 Chapter 1 Logic Lecture no. 1. We introduce the concept of a proposition, which is a statement

### The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

CHAPTER 5 The Tree Data Model There are many situations in which information has a hierarchical or nested structure like that found in family trees or organization charts. The abstraction that models hierarchical

### Automata and Formal Languages

Automata and Formal Languages Winter 2009-2010 Yacov Hel-Or 1 What this course is all about This course is about mathematical models of computation We ll study different machine models (finite automata,

### What is logic? Propositional Logic. Negation. Propositions. This is a contentious question! We will play it safe, and stick to:

Propositional Logic This lecture marks the start of a new section of the course. In the last few lectures, we have had to reason formally about concepts. This lecture introduces the mathematical language

### Classical Information and Bits

p. 1/24 Classical Information and Bits The simplest variable that can carry information is the bit, which can take only two values, 0 or 1. Any ensemble, description, or set of discrete values can be quantified

### Chapter I Logic and Proofs

MATH 1130 1 Discrete Structures Chapter I Logic and Proofs Propositions A proposition is a statement that is either true (T) or false (F), but or both. s Propositions: 1. I am a man.. I am taller than

### WUCT121. Discrete Mathematics. Logic

WUCT121 Discrete Mathematics Logic 1. Logic 2. Predicate Logic 3. Proofs 4. Set Theory 5. Relations and Functions WUCT121 Logic 1 Section 1. Logic 1.1. Introduction. In developing a mathematical theory,

### a 11 x 1 + a 12 x 2 + + a 1n x n = b 1 a 21 x 1 + a 22 x 2 + + a 2n x n = b 2.

Chapter 1 LINEAR EQUATIONS 1.1 Introduction to linear equations A linear equation in n unknowns x 1, x,, x n is an equation of the form a 1 x 1 + a x + + a n x n = b, where a 1, a,..., a n, b are given

### Jieh Hsiang Department of Computer Science State University of New York Stony brook, NY 11794

ASSOCIATIVE-COMMUTATIVE REWRITING* Nachum Dershowitz University of Illinois Urbana, IL 61801 N. Alan Josephson University of Illinois Urbana, IL 01801 Jieh Hsiang State University of New York Stony brook,

### Predicate Logic. Example: All men are mortal. Socrates is a man. Socrates is mortal.

Predicate Logic Example: All men are mortal. Socrates is a man. Socrates is mortal. Note: We need logic laws that work for statements involving quantities like some and all. In English, the predicate is

### Introduction to Programming (in C++) Data types and visibility. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC

Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC Outline Data types Type conversion Visibility Introduction to

### CORRELATED TO THE SOUTH CAROLINA COLLEGE AND CAREER-READY FOUNDATIONS IN ALGEBRA

We Can Early Learning Curriculum PreK Grades 8 12 INSIDE ALGEBRA, GRADES 8 12 CORRELATED TO THE SOUTH CAROLINA COLLEGE AND CAREER-READY FOUNDATIONS IN ALGEBRA April 2016 www.voyagersopris.com Mathematical

### Algorithms and Data Structures

Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016 Overview general linked lists stacks queues trees 2 2

### First-Order Predicate Logic (2)

First-Order Predicate Logic (2) Predicate Logic (2) Understanding first-order predicate logic formulas. Satisfiability and undecidability of satisfiability. Tautology, logical consequence, and logical

### COMPUTER SCIENCE TRIPOS

CST.98.5.1 COMPUTER SCIENCE TRIPOS Part IB Wednesday 3 June 1998 1.30 to 4.30 Paper 5 Answer five questions. No more than two questions from any one section are to be answered. Submit the answers in five

### Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2

CS 70 Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2 Proofs Intuitively, the concept of proof should already be familiar We all like to assert things, and few of us

### Inference Rules and Proof Methods

Inference Rules and Proof Methods Winter 2010 Introduction Rules of Inference and Formal Proofs Proofs in mathematics are valid arguments that establish the truth of mathematical statements. An argument

### 12 Abstract Data Types

12 Abstract Data Types 12.1 Source: Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT).

### Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Today Binary addition Representing negative numbers 2 Binary Addition Consider the following binary numbers: 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 How do we add these numbers? 3 Binary Addition 0 0 1 0 0 1 1

### 8 Divisibility and prime numbers

8 Divisibility and prime numbers 8.1 Divisibility In this short section we extend the concept of a multiple from the natural numbers to the integers. We also summarize several other terms that express

### Outline. Database Theory. Perfect Query Optimization. Turing Machines. Question. Definition VU , SS Trakhtenbrot s Theorem

Database Theory Database Theory Outline Database Theory VU 181.140, SS 2016 4. Trakhtenbrot s Theorem Reinhard Pichler Institut für Informationssysteme Arbeitsbereich DBAI Technische Universität Wien 4.

### Chapter 1, Part II: Predicate Logic. With Question/Answer Animations

Chapter 1, Part II: Predicate Logic With Question/Answer Animations Summary Predicate Logic (First Order Logic (FOL), Predicate Calculus (not covered)) The Language of Quantifiers Logical Equivalences

### Cassandra. References:

Cassandra References: Becker, Moritz; Sewell, Peter. Cassandra: Flexible Trust Management, Applied to Electronic Health Records. 2004. Li, Ninghui; Mitchell, John. Datalog with Constraints: A Foundation

### An Innocent Investigation

An Innocent Investigation D. Joyce, Clark University January 2006 The beginning. Have you ever wondered why every number is either even or odd? I don t mean to ask if you ever wondered whether every number

### Probability Using Dice

Using Dice One Page Overview By Robert B. Brown, The Ohio State University Topics: Levels:, Statistics Grades 5 8 Problem: What are the probabilities of rolling various sums with two dice? How can you

### Foundations of mathematics

Foundations of mathematics 1. First foundations of mathematics 1.1. Introduction to the foundations of mathematics Mathematics, theories and foundations Sylvain Poirier http://settheory.net/ Mathematics

### University of Ostrava. Reasoning in Description Logic with Semantic Tableau Binary Trees

University of Ostrava Institute for Research and Applications of Fuzzy Modeling Reasoning in Description Logic with Semantic Tableau Binary Trees Alena Lukasová Research report No. 63 2005 Submitted/to

### RN-coding of Numbers: New Insights and Some Applications

RN-coding of Numbers: New Insights and Some Applications Peter Kornerup Dept. of Mathematics and Computer Science SDU, Odense, Denmark & Jean-Michel Muller LIP/Arénaire (CRNS-ENS Lyon-INRIA-UCBL) Lyon,

### Prolog A Tutorial Introduction James Lu Jerud J. Mead

Prolog A Tutorial Introduction James Lu Jerud J. Mead Computer Science Department Bucknell University Lewisburg, PA 17387 1 Contents 1 Introduction 1 2 Easing into Prolog 2 2.1 Logic Programming.........................................

### Notes on Factoring. MA 206 Kurt Bryan

The General Approach Notes on Factoring MA 26 Kurt Bryan Suppose I hand you n, a 2 digit integer and tell you that n is composite, with smallest prime factor around 5 digits. Finding a nontrivial factor

### Solution of Linear Systems

Chapter 3 Solution of Linear Systems In this chapter we study algorithms for possibly the most commonly occurring problem in scientific computing, the solution of linear systems of equations. We start

### Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic Lars Birkedal Rasmus Ejlers Møgelberg Rasmus Lerchedahl Petersen IT University Technical Report Series TR-00-7 ISSN 600 600 February 00

### Utility programs In utilities.pro discussed at various times throughout rest of the course

Utility programs In utilities.pro discussed at various times throughout rest of the course UT-1 member ( I, L ) Item I is a member of the list L. > Reduce the list second rule until first in list first

### A first step towards modeling semistructured data in hybrid multimodal logic

A first step towards modeling semistructured data in hybrid multimodal logic Nicole Bidoit * Serenella Cerrito ** Virginie Thion * * LRI UMR CNRS 8623, Université Paris 11, Centre d Orsay. ** LaMI UMR

### Compiler Design. Type Checking

Compiler Design Type Checking Static Checking Token Stream Parser Abstract Syntax Tree Static Checker Decorated Abstract Syntax Tree Intermediate Code Generator Intermediate Code Static (Semantic) Checks

### Logic in general. Inference rules and theorem proving

Logical Agents Knowledge-based agents Logic in general Propositional logic Inference rules and theorem proving First order logic Knowledge-based agents Inference engine Knowledge base Domain-independent

### Multimedia Communications. Huffman Coding

Multimedia Communications Huffman Coding Optimal codes Suppose that a i -> w i C + is an encoding scheme for a source alphabet A={a 1,, a N }. Suppose that the source letter a 1,, a N occur with relative

### ON FUNCTIONAL SYMBOL-FREE LOGIC PROGRAMS

PROCEEDINGS OF THE YEREVAN STATE UNIVERSITY Physical and Mathematical Sciences 2012 1 p. 43 48 ON FUNCTIONAL SYMBOL-FREE LOGIC PROGRAMS I nf or m at i cs L. A. HAYKAZYAN * Chair of Programming and Information

### Software Engineering

Software Engineering Lecture 04: The B Specification Method Peter Thiemann University of Freiburg, Germany SS 2013 Peter Thiemann (Univ. Freiburg) Software Engineering SWT 1 / 50 The B specification method

### DATA STRUCTURES USING C

DATA STRUCTURES USING C QUESTION BANK UNIT I 1. Define data. 2. Define Entity. 3. Define information. 4. Define Array. 5. Define data structure. 6. Give any two applications of data structures. 7. Give

### 2. Methods of Proof Types of Proofs. Suppose we wish to prove an implication p q. Here are some strategies we have available to try.

2. METHODS OF PROOF 69 2. Methods of Proof 2.1. Types of Proofs. Suppose we wish to prove an implication p q. Here are some strategies we have available to try. Trivial Proof: If we know q is true then

### Data Structure with C

Subject: Data Structure with C Topic : Tree Tree A tree is a set of nodes that either:is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which

### Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection

### Logic Programming using Prolog an Introductory Exercise

Logic Programming using Prolog an Introductory Exercise Brandon Bennett The purpose of this exercise is for you to see some examples of the Prolog logic programming language in action. Prolog is very different

### Analysis of Algorithms I: Binary Search Trees

Analysis of Algorithms I: Binary Search Trees Xi Chen Columbia University Hash table: A data structure that maintains a subset of keys from a universe set U = {0, 1,..., p 1} and supports all three dictionary

### Presented By: Ms. Poonam Anand

Presented By: Ms. Poonam Anand Know the different types of numbers Describe positional notation Convert numbers in other bases to base 10 Convert base 10 numbers into numbers of other bases Describe the

### CS 2336 Discrete Mathematics

CS 2336 Discrete Mathematics Lecture 2 Logic: Predicate Calculus 1 Outline Predicates Quantifiers Binding Applications Logical Equivalences 2 Predicates In mathematics arguments, we will often see sentences

### Some facts about polynomials modulo m (Full proof of the Fingerprinting Theorem)

Some facts about polynomials modulo m (Full proof of the Fingerprinting Theorem) In order to understand the details of the Fingerprinting Theorem on fingerprints of different texts from Chapter 19 of the

### Notes from Week 1: Algorithms for sequential prediction

CS 683 Learning, Games, and Electronic Markets Spring 2007 Notes from Week 1: Algorithms for sequential prediction Instructor: Robert Kleinberg 22-26 Jan 2007 1 Introduction In this course we will be looking

### Levent EREN levent.eren@ieu.edu.tr A-306 Office Phone:488-9882 INTRODUCTION TO DIGITAL LOGIC

Levent EREN levent.eren@ieu.edu.tr A-306 Office Phone:488-9882 1 Number Systems Representation Positive radix, positional number systems A number with radix r is represented by a string of digits: A n

### Chapter 6 Finite sets and infinite sets. Copyright 2013, 2005, 2001 Pearson Education, Inc. Section 3.1, Slide 1

Chapter 6 Finite sets and infinite sets Copyright 013, 005, 001 Pearson Education, Inc. Section 3.1, Slide 1 Section 6. PROPERTIES OF THE NATURE NUMBERS 013 Pearson Education, Inc.1 Slide Recall that denotes

### Copyright 2012 MECS I.J.Information Technology and Computer Science, 2012, 1, 50-63

I.J. Information Technology and Computer Science, 2012, 1, 50-63 Published Online February 2012 in MECS (http://www.mecs-press.org/) DOI: 10.5815/ijitcs.2012.01.07 Using Logic Programming to Represent

### Chapter 7. Functions and onto. 7.1 Functions

Chapter 7 Functions and onto This chapter covers functions, including function composition and what it means for a function to be onto. In the process, we ll see what happens when two dissimilar quantifiers

### The Zebra Puzzle. 1 Introduction. Carlo Tomasi Computer Science Duke University

The Puzzle Carlo Tomasi Computer Science Duke University 1 Introduction In the class textbook, the puzzle is offered as an exercise for Section 1.1 on logic (Exercise 61, page 20). Here is the puzzle:

### Relational Databases

Relational Databases Jan Chomicki University at Buffalo Jan Chomicki () Relational databases 1 / 18 Relational data model Domain domain: predefined set of atomic values: integers, strings,... every attribute

### 22C:19 Discrete Math. So. What is it? Why discrete math? Fall 2009 Hantao Zhang

22C:19 Discrete Math Fall 2009 Hantao Zhang So. What is it? Discrete mathematics is the study of mathematical structures that are fundamentally discrete, in the sense of not supporting or requiring the

### + addition - subtraction and unary minus * multiplication / division mod modulo

Chapter 4 Basic types We examine in this chapter the Caml basic types. 4.1 Numbers Caml Light provides two numeric types: integers (type int) and floating-point numbers (type float). Integers are limited

### Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University http://www.cs.stonybrook.

Elementary Number Theory and Methods of Proof CSE 215, Foundations of Computer Science Stony Brook University http://www.cs.stonybrook.edu/~cse215 1 Number theory Properties: 2 Properties of integers (whole

### MATH 433 Applied Algebra Lecture 13: Examples of groups.

MATH 433 Applied Algebra Lecture 13: Examples of groups. Abstract groups Definition. A group is a set G, together with a binary operation, that satisfies the following axioms: (G1: closure) for all elements

### CHAPTER II THE LIMIT OF A SEQUENCE OF NUMBERS DEFINITION OF THE NUMBER e.

CHAPTER II THE LIMIT OF A SEQUENCE OF NUMBERS DEFINITION OF THE NUMBER e. This chapter contains the beginnings of the most important, and probably the most subtle, notion in mathematical analysis, i.e.,

### 7.1 Our Current Model

Chapter 7 The Stack In this chapter we examine what is arguably the most important abstract data type in computer science, the stack. We will see that the stack ADT and its implementation are very simple.

### Aikaterini Marazopoulou

Imperial College London Department of Computing Tableau Compiled Labelled Deductive Systems with an application to Description Logics by Aikaterini Marazopoulou Submitted in partial fulfilment of the requirements