CPSC 312 Functional and Logic Programming March 2021
Assignment Five: Relations and Functions
Due: 11:59pm, Thursday March 18, 2021. Submit solution to Canvas
Submit your answers in text files using Canvas. The files you submit must be properly commented (including the intended interpretation for all symbols) and run in SWI Prolog. You can do this alone or with a partner (but you both should understand the solution, as the midterm will assume you have done the assignment). Make sure you name(s) and student number(s) are at the top of each file.
Question One
Consider the domain of house plumbing from assignment 4.
Represent the domain (that included the sink, overflowing and the hot water system) to use
individuals and relations, instead of just simple propositions (atoms without arguments). Your pro- gram should use constants for the individuals, and should not have any atoms that have predicates without arguments.
You can either base it on the posted solution or on your solution to assignment 4.
You need to hand in a complete program that runs in SWI Prolog (including the intended interpretation for all symbols) and a trace of a session to show it runs for various setting of taps on and off, and plugs in and out.
Question Two
Suppose that times are represented as am(H,M) for the time M minutes after hour H in the morning or as pm(H , M ) for M minutes after hour H in the afternoon. For example, am(11, 30) is 11:30 in the morning, pm(1,30) is 1:30 in the afternoon, and pm(12,30) is halfway between these times. Write a predicate before(T1,T2) that is true when time T1 is before time T2. You can only use the built-in predicate <, which compares two numbers. (Make sure you only write true statements. It should not producte wrong answers on retry. Remember that 12:59pm is before 1:00pm).
Question Three
For this question you are only allowed to use the built-in predicates member (where member(E,L) is true if E is a member of list L), number (where number(N) is true if N is a number) and is (where V is E is true if arithmetic expression E evaluates to value V ).
(a) Suppose an environment is a list of val(x,v) where x is an algebraic variable (a constant), and v is a value (a number). Define relation:
lookup(X,Env,V)
which is true when X has value V in environment Env. For example:
1
?- lookup(b,[val(aa,3), val(b,7), val(dd,23)],V). V=7.
?- lookup(slithy,[val(aa,3), val(b,7), val(dd,23)],V). false.
(b) Define the relation:
eval(Exp, Env, V )
which is true when arithmetic expression Exp has value V in environment Env . An arithmetic expression is either an algebraic variable (in which case its value is looked up in Env), a number, or of the form (A + B) or (A ∗ B) where A and B are arithmetic expressions. [You can, but do not need to, implement a richer set of expressions.]
For example:
?- eval(aa*aa+b*11, [val(aa,3), val(b,7), val(dd,23)], V).
V = 86
Question Four
For each question, specify how long you spend on it, and what you learned. Was the question reasonable? (This question is part of the assignment, so please do it!)
2