Logic Programming: Exercises Week 2 Exercise 1. Sets. One way to represent sets is with monadic predicates. The set S = {a, b, c,...} is represented by the predicates s(a), s(b), etc. Assume for the moment, that there are only 2 sets of interest in the world, whose elements are represent- ed by the monadic predicates p/1 and q/1. Note: You will keep coming up against the notation Name/Number (like p/1). By this, it will be understood that we are talking about a predicate with a particular name, and with some number of arguments. Set operations with this monadic representation are easy. Consider the union operator. Union of P and Q is a set p_union_q = {e| e in P or e in Q} Or, as definite-clauses: p_union_q(X):- p(X). p_union_q(X):- q(X). Instead of having a new unary predicate for each set, it is possible to represent sets with predicates that have the name of the set as first argument. For example, the sets P = {a,b} and Q = {1,2} can both be represented with a binary predicate elem/2, as elem(a,p), elem(b,p), elem(1,q), and elem(2,q). The statement: x in S1 U S2 iff x in S1 or x in S2 can now be represented by the following definite clauses: in_union(X,S1,S2):- elem(X,S1). in_union(X,S1,S2):- elem(X,S2). Write rules for the following statements: x in S1 & S2 iff x in S1 and x in S2 (x,y) in S1 x S2 iff x in S1 and y in S2 x in S1 \ S2 iff x in S1 and x not in S2 January 24, 1996 - 2 - Exercise 2. Numbers Let N5 = {0,1,2,3,4}, the set of the first 5 natural numbers. The successor function that associates a natural number with another one, namely the next one can be represented as a set of ordered pairs. For the set N5: successor5 = { | x in N5, y in N5, and y = x + 1} Later we will look at representing functions like addition (+). For the moment, the ordered pairs in successor5 can be represented by the following "extensional" definition: successor5(0,1). successor5(1,2). successor5(2,3). successor5(3,4). Write a similar extensional definition for the less_than5 relation, that describes all ordered pairs such that the first element in the pair is less than (<) the second. Given sets A, B with elements from N5 write a definition for the binary relation: lt_AxB = { | in AxB, and in less_than5} Exercise 3. Digital logic circuits The transistor is a basic component in digital circuits. It is simply a semiconductor device with 3 terminals: Drain | |__ |____ Gate __| | | Source A resistor is a 2 terminal device that "resists" the flow of current: January 24, 1996 - 3 - | _|_ | | | | | R | | | |___| | | Two other entities that will be seen in circuits are the power source and the "ground" rail. o------------------------------o Power o-------------------------------o Ground ___|___ _____ _ Particular configurations of resistors and transistors achieve certain functional tasks. An inverter is built using a transistor with the source con- nected to the ground, and a resistor with one end connected to the power source and the other end to the drain of the transistor. The input to the inverter is the gate, and the output is at the drain. Here is one way of representing such a circuit: inverter(In,Out):- resistor(power,Out), transistor(In,ground,Out). In effect, this represents the constraints on voltage levels at different terminals on the devices: the transistor being represented by the levels at the Gate, Source and Drain. Logical switches or gates can be built using these com- ponents. The following circuit is a "nand" (not-and) gate: January 24, 1996 - 4 - o-------------------------------o Power _|_ | | | R | |___| Output_____|____________ | D |___ G |____________ A ___| | |___ G |_____________ B ___| | S | o-------------------------------o Ground ___|___ _____ _ Write a clausal definition for this circuit. The following circuit implements an "and" gate. Write a a definition for the gate using a nand gate and an inverter. o-------------------------------o Power _|_ | | | | | R | _|_ |___| | | Out _____|_____ | R | |D |___| |__ G | |____|______ __| | | | D | |___ G | |____________ A | ___| | | | |___ G | |_____________ B | ___| | | S | | o-------------------------------o Ground ___|___ _____ _ January 24, 1996 - 5 - To test the behaviour of these circuits, you will have to examine actual voltage levels. One way to represent these is by additional positive ground unit clauses. For example, for the and-gate: resistor(power,n1). resistor(power,n2). transistor(n2,ground,n1). transistor(n3,n4,n2). transistor(n5,ground,n4). where the ni represent different voltage levels. Using your representation for resistors and transistors, verify that the and gate is functioning according to specifications for these voltages. Try and(A,B,C)? Exercise 4. N-ary relations Consider the map below: ___________________________________ | 1 | | | | ______________________ | | | 2 | 5 | | | | ______ | | | | | | | | | | | |___| 4 |____| | | | | |______| | | | | | |______| | | | 3 | 6 | | | |_______________|______| | |___________________________________| We want to colour this map with the four colours red, green, blue and yellow. No two adjacent countries are to have the same colour. The colourings allowed can be considered exten- sionally as a set of ordered 6-tuples, with each entry representing a colour for each region. Consider the following sets: Colour = {red, green, blue, yellow} DiffColour = { | x in Colour and y in Colour, and x =/= y} Define the set Map as a set of ordered 6-tuples, where each argument corresponds to a colour for that region, and adja- cent regions have a different colour. Somewhat informally: Map = { | x1..x6 in Colour and in DiffColour for all i and j that share a border} Write definitions for Colour using a monadic predicate. Write definitions for DiffColour using a binary predicate. Now complete this definition for Map: map(X1,X2,X3,X4,X5,X6):- ..... Check an assignment of colours to regions 1..6 is consistent with the requirements. You may need to issue the following command to Progol: set(h,10000)? January 24, 1996 - 6 - In fact, you should be able to do better than just check a particular assignment. What does the following Progol query accomplish: map(C1,C2,C3,C4,C5,C6)? Exercise 5. Negation Consider the following definitions: bachelor(X):- not(married(X)), male(X). married(john). male(john). male(bill). Does the following query succeed correctly: bachelor(X)? What about: bachelor(bill)? and bachelor(john)? What happens if the definition of bachelor was written as: bachelor(X):- male(X), not(married(X)). Recall the warning about the idiosyncratic behaviour of not. We now have a rule that MUST be followed when using not: There should be NO uninstantiated variables in the call to not. Progol's theorem-prover is not guaranteed to be correct if this rule is not followed.