? users online
  • Logout
    • Open hangout
    • Open chat for current file
<div class="notebook">

<div class="nb-cell markdown" name="intro">
# Challenge: Museum Mystery

Work in groups of **2-4** to build an explainable museum detective in Prolog.

The notebook contains a knowledge base of artworks, creators, artistic movements, topics, museums, cities, and influence relations. Mystery cases describe an unknown artwork through clues. Your program should:

- find every artwork matching all clues;
- classify a case as `unique`, `ambiguous`, or `unsolved`;
- return a readable explanation for each candidate;
- support direct and multi-step clues;
- avoid infinite recursion in the movement-influence graph.

The public tests show the expected interface. Your implementation should also work for new cases that use the same clue types.

## Rules

- Use facts and general rules. Do not hard-code answers for individual cases.
- Keep resource identifiers as Prolog atoms.
- A missing fact means that the program cannot prove a clue. It does not prove that the opposite is true in the real world.
- You may change the predicate structure, but the public interface must remain:

```prolog
solve_case(Case, Status, Candidates).
```

Each candidate should have the form:

```prolog
candidate(Object, Explanation)
```

where `Explanation` is a list of terms showing how the clues were satisfied.
</div>

<div class="nb-cell markdown" name="problem">
## Challenge cases

The supplied cases include:

- a uniquely solvable mystery;
- an ambiguous mystery with several candidates;
- an unsolved mystery whose clues cannot all be proved;
- a case requiring a multi-step movement-influence relation.

After passing the public tests, create:

1. one new case with exactly one answer;
2. one new case with at least two answers.

At the next laboratory, another group may test its detective using your cases.
</div>

<div class="nb-cell markdown" name="swish_scope_note">
## Before you run the queries: check the program cell scope

In SWISH, click the scope icon in a program cell to choose how that cell is used:

- **🌐 Global** — the program cell applies throughout the notebook and is available to every query.
- **📍 Local** — the program cell is used only by the first query below it.
</div>

<div class="nb-cell markdown" name="md1">
After selecting a query, check which program cells SWISH will load. Loaded cells have a **blue line on the left** and a **white background**. Cells that are not used by the selected query are **greyed out**.
</div>

<div class="nb-cell program" data-background="true" name="knowledge_base">
% ================================================================
% KNOWLEDGE BASE
% ================================================================

artwork(starry_night).
artwork(sunflowers).
artwork(irises).
artwork(guernica).
artwork(les_demoiselles_davignon).
artwork(the_scream).
artwork(girl_with_a_pearl_earring).
artwork(the_persistence_of_memory).
artwork(the_kiss).
artwork(nighthawks).
artwork(american_gothic).
artwork(composition_viii).

created_by(starry_night, van_gogh).
created_by(sunflowers, van_gogh).
created_by(irises, van_gogh).
created_by(guernica, picasso).
created_by(les_demoiselles_davignon, picasso).
created_by(the_scream, munch).
created_by(girl_with_a_pearl_earring, vermeer).
created_by(the_persistence_of_memory, dali).
created_by(the_kiss, klimt).
created_by(nighthawks, hopper).
created_by(american_gothic, wood).
created_by(composition_viii, kandinsky).

artist_movement(van_gogh, post_impressionism).
artist_movement(picasso, cubism).
artist_movement(munch, expressionism).
artist_movement(vermeer, dutch_golden_age).
artist_movement(dali, surrealism).
artist_movement(klimt, symbolism).
artist_movement(hopper, realism).
artist_movement(wood, regionalism).
artist_movement(kandinsky, abstract_art).

depicts(starry_night, night_sky).
depicts(sunflowers, flowers).
depicts(irises, flowers).
depicts(guernica, war).
depicts(les_demoiselles_davignon, people).
depicts(the_scream, anxiety).
depicts(girl_with_a_pearl_earring, portrait).
depicts(the_persistence_of_memory, time).
depicts(the_kiss, people).
depicts(nighthawks, people).
depicts(american_gothic, portrait).
depicts(composition_viii, geometry).

held_by(starry_night, moma).
held_by(irises, getty_museum).
held_by(guernica, reina_sofia).
held_by(les_demoiselles_davignon, moma).
held_by(the_scream, national_museum_oslo).
held_by(girl_with_a_pearl_earring, mauritshuis).
held_by(the_persistence_of_memory, moma).
held_by(the_kiss, belvedere).
held_by(nighthawks, art_institute_chicago).
held_by(american_gothic, art_institute_chicago).
held_by(composition_viii, guggenheim).
% No holding institution is recorded here for sunflowers.

located_in(moma, new_york).
located_in(getty_museum, los_angeles).
located_in(reina_sofia, madrid).
located_in(national_museum_oslo, oslo).
located_in(mauritshuis, the_hague).
located_in(belvedere, vienna).
located_in(art_institute_chicago, chicago).
located_in(guggenheim, new_york).

% A small influence graph. It intentionally contains two routes to
% modernism, so a cycle-safe traversal is required for robust extensions.
influenced(impressionism, post_impressionism).
influenced(post_impressionism, expressionism).
influenced(cubism, surrealism).
influenced(symbolism, expressionism).
influenced(expressionism, modernism).
influenced(surrealism, modernism).
influenced(abstract_art, modernism).

movement_of(Object, Movement) :-
    created_by(Object, Artist),
    artist_movement(Artist, Movement).

city_of(Object, City) :-
    held_by(Object, Museum),
    located_in(Museum, City).

% ================================================================
% PUBLIC MYSTERY CASES
% ================================================================

clue(case_1, movement(post_impressionism)).
clue(case_1, topic(night_sky)).
clue(case_1, city(new_york)).

clue(case_2, topic(portrait)).

clue(case_3, movement(surrealism)).
clue(case_3, city(madrid)).

clue(case_4, influenced_by(cubism)).
clue(case_4, topic(time)).

clue(case_5, creator(van_gogh)).
clue(case_5, museum(moma)).

% ================================================================
% EXPECTED PUBLIC RESULTS
% ================================================================

expected(case_1, unique, [starry_night]).
expected(case_2, ambiguous, [american_gothic, girl_with_a_pearl_earring]).
expected(case_3, unsolved, []).
expected(case_4, unique, [the_persistence_of_memory]).
expected(case_5, unique, [starry_night]).
</div>

<div class="nb-cell markdown" name="interface">
## Program interface

The solver checks every artwork against every clue in a case.

A clue succeeds only when the knowledge base can prove it. Each successful clue also returns an explanation term, for example:

```prolog
creator(van_gogh, via_created_by)
city(new_york, via_museum(moma))
influenced_by(cubism, movement(surrealism), path([cubism, surrealism]))
```

`solve_case/3` should return:

- `unique` for one candidate;
- `ambiguous` for more than one candidate;
- `unsolved` for no candidates.
</div>

<div class="nb-cell program" data-background="true" name="solver">
% ================================================================
% STARTER SOLUTION
% ================================================================

% Three direct clue types are supplied as a working baseline.
clue_matches(Object, creator(Creator),
             creator(Creator, via_created_by)) :-
    created_by(Object, Creator).

clue_matches(Object, topic(Topic),
             topic(Topic, via_depicts)) :-
    depicts(Object, Topic).

clue_matches(Object, museum(Museum),
             museum(Museum, via_held_by)) :-
    held_by(Object, Museum).

% FIXME 1: Add a movement(Movement) clue.
% The explanation should identify the creator used to obtain the movement.

% FIXME 2: Add a city(City) clue.
% The explanation should identify the museum connecting the object to the city.

% FIXME 3: Add an influenced_by(SourceMovement) clue.
% It should use a cycle-safe path through influenced/2 and explain the path.

% FIXME 4: Implement influence_path/3 without revisiting a movement on one path.

% A candidate must satisfy every clue. The starter currently gathers all
% explanations it can prove; complete the condition that verifies that no
% clue was missed.
case_candidate(Case, Object, Explanation) :-
    artwork(Object),
    findall(Reason,
            ( clue(Case, Clue),
              once(clue_matches(Object, Clue, Reason))
            ),
            Explanation),
    findall(Clue, clue(Case, Clue), Clues),
    % FIXME 5: Accept the object only if every clue has an explanation.
    same_length(Explanation, Clues).

case_candidates(Case, Candidates) :-
    findall(candidate(Object, Explanation),
            case_candidate(Case, Object, Explanation),
            RawCandidates),
    sort(RawCandidates, Candidates).

% FIXME 6: Complete the three status cases.
classify_candidates([], unsolved).
classify_candidates([_], unique).
classify_candidates([_,_|_], ambiguous).

solve_case(Case, Status, Candidates) :-
    case_candidates(Case, Candidates),
    classify_candidates(Candidates, Status).

candidate_objects(Candidates, Objects) :-
    findall(Object,
            member(candidate(Object, _Explanation), Candidates),
            Objects).

public_test(Case, passed) :-
    expected(Case, ExpectedStatus, ExpectedObjects),
    solve_case(Case, Status, Candidates),
    candidate_objects(Candidates, Objects),
    Status == ExpectedStatus,
    Objects == ExpectedObjects,
    !.

public_test(Case, failed(ExpectedStatus, ExpectedObjects,
                         ActualStatus, ActualObjects)) :-
    expected(Case, ExpectedStatus, ExpectedObjects),
    solve_case(Case, ActualStatus, Candidates),
    candidate_objects(Candidates, ActualObjects).

run_public_tests(Results) :-
    findall(Case-Result,
            ( expected(Case, _Status, _Objects),
              public_test(Case, Result)
            ),
            Results).
</div>

<div class="nb-cell markdown" name="public_tests">
## Public tests

Run the complete test suite. In the starter notebook, the direct cases should already work, while cases requiring movement, city, and influence reasoning will remain incomplete until you implement the missing predicates.

The expected final result is:

```prolog
[case_1-passed, case_2-passed, case_3-passed, case_4-passed, case_5-passed]
```
</div>

<div class="nb-cell query" name="q_tests">
run_public_tests(Results).
</div>

<div class="nb-cell markdown" name="inspect_cases">
## Inspect individual cases

Use `solve_case/3` to inspect candidates and explanations. Request all visible bindings when SWISH offers additional solutions.
</div>

<div class="nb-cell query" name="q_case1">
solve_case(case_1, Status, Candidates).
</div>

<div class="nb-cell query" name="q_case2">
solve_case(case_2, Status, Candidates).
</div>

<div class="nb-cell query" name="q_case3">
solve_case(case_3, Status, Candidates).
</div>

<div class="nb-cell query" name="q_case4">
solve_case(case_4, Status, Candidates).
</div>

<div class="nb-cell markdown" name="own_cases_prompt">
## Create your own mysteries

Add two new case identifiers:

1. one case with exactly one candidate;
2. one case with at least two candidates.

Use at least two clue types across the two cases. At least one case should require a derived or recursive clue.
</div>

<div class="nb-cell program" name="own_cases">
% Add your new clue/2 facts here.
%
% clue(my_unique_case, ...).
% clue(my_ambiguous_case, ...).
</div>

<div class="nb-cell query" name="q_own_unique">
solve_case(my_unique_case, Status, Candidates).
</div>

<div class="nb-cell query" name="q_own_ambiguous">
solve_case(my_ambiguous_case, Status, Candidates).
</div>

<div class="nb-cell markdown" name="evaluation">
## What to share at the next lab

Prepare a short demonstration covering:

1. one unique, one ambiguous, and one unsolved case;
2. one explanation returned by your program;
3. how the cycle-safe influence search works;
4. one case where missing knowledge prevents a conclusion;
5. your unique and ambiguous mysteries;
6. one modelling decision that differs from another possible implementation;
7. one limitation of your detective.

Your solution should pass the public tests and remain general enough to handle new cases using the supported clue types.
</div>

</div>