Learning objectives:
This labs aims to prepare you for homework 3, which covers logic programming in Prolog. It also will help you understand how uC code is structured, which will be useful for projects 4 and 5.
Use the following commands to download and unpack the distribution code:
$ wget https://eecs390.github.io/lab/lab10/starter-files.tar.gz $ tar xzf starter-files.tar.gz
Logic puzzle. Write a Prolog program that finds a solution to this logic puzzle:
There are four different fathers: Smith, Baker, Carpenter, and Tailor. Each of them also has a son: Smithson, Bakerson, Carpenterson, and Tailorson.
Assume you know the following:
What is each person's profession?
Complete the following Prolog program to solve this puzzle:
% professions(Professions). % % True if Professions is the list [smith, baker, carpenter, tailor]. professions([smith, baker, carpenter, tailor]). % solve_puzzle(Smith, Baker, Carpenter, Tailor, % Smithson, Bakerson, Carpenterson, Tailorson). % % Solves the puzzle described above. % % All parameters are output parameters. solve_puzzle(Smith, Baker, Carpenter, Tailor, Smithson, Bakerson, Carpenterson, Tailorson) :- Fathers = [Smith, Baker, Carpenter, Tailor], Sons = [Smithson, Bakerson, Carpenterson, Tailorson], % rule 1 professions(Professions), fail. % replace with your solution
Hint: Use the built-in permutation predicate to generate permutations of the professions.
Logic queries. For each of the following Prolog predicates and set of queries, do the following:
sum([], 0). sum([First|Rest], Total) :- sum(Rest, RestTotal), Total is First + RestTotal.
Query | Succeeds | Fails | Error |
---|---|---|---|
sum([1, 2], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
sum([1, 2], 4). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
sum([1, A], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
sum([1 | 2], Total). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
sum([1, 2], Total). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
contains([Item], Number) :- Item =:= Number. contains([_|Rest], Number) :- contains(Rest, Number).
Query | Succeeds | Fails | Error |
---|---|---|---|
contains([], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
contains([3], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
contains([-1], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
contains([A], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
contains([-1, 3, 7], 3). | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) | \(~~~~~~~~\bigcirc~~~~~~~~\) |
uC errors. Consider the following code samples from the uC language in Project 4. Consult the uC spec and the Project 4 spec to determine the appropriate errors that should be output by your semantic analyzer (if any) as well as which phase the errors are caught in.
uC Code | Error(s) Reported | Phase |
---|---|---|
int foo(int a) { boolean a = false; return; } |
||
void main(string[] args) { int b = 0; int c = b = 7; 5 = 7; } |
||
void int_to_long() {} |
||
struct foo { boolean b; }; void main(string[] args) { new foo(null); new foo(true, false); new foo { 3 }; } |
||
void main(string[] args) { foo f = new foo(args[0]); bar(f); } void bar(foo f) { println(f.s); } struct foo { string s; }; |