A small hospital simulation written in Prolog for studying declarative programming, first-order logic, and automated reasoning.
Note
This project models a simplified hospital using facts and rules. It is not a real medical system and must not be used for clinical decisions. Its purpose is to test the Prolog language and explore how a declarative program derives conclusions from a knowledge base. It was built as an academic experiment in discrete mathematics and declarative programming.
- Learning goals
- Project structure
- Logic model
- Running the simulation
- Example queries
- Automated tests
- Visual demonstrations
- Report
- References
The simulation demonstrates:
- Facts: statements such as
paciente(joao)andleito(leito_1). - Rules: relationships derived from existing facts.
- Unification: variables such as
X,Pessoa, andPacientematch compatible terms. - Backtracking: Prolog searches for alternative solutions automatically.
- Negation as failure:
\+ Goalsucceeds when Prolog cannot proveGoal. - Declarative reasoning: the program describes what is true, rather than an imperative sequence of instructions.
Why a hospital?
The hospital domain provides an intuitive vocabulary: patients, staff, beds, symptoms, and treatments while keeping the knowledge base small enough to inspect manually. The same reasoning patterns can be applied to scheduling, configuration validation, expert systems, and other rule-based domains.
| Path | Description |
|---|---|
sim.pl |
Prolog facts, rules, and sample queries |
descricao.pdf |
Portuguese report with the formal logic deductions |
assets/vid/ |
Animated demonstrations of derived relations |
out/ |
Generated report files |
The knowledge base contains:
- People who are patients, doctors, or nurses.
- Beds available in the hospital.
- Symptoms associated with patients.
- Assignments between staff, patients, and beds.
- Rules that infer risk, treatment needs, valid assignments, and unoccupied beds.
For example, the following rule states that a doctor or nurse assigned to the same bed as a patient is also considered assigned to that patient:
designado(Pessoa, Paciente) :-
(medico(Pessoa) ; enfermeiro(Pessoa)),
paciente(Paciente),
leito(Leito),
designado(Pessoa, Leito),
designado(Paciente, Leito).In symbolic form, the rule can be summarized as:
If a person is a doctor or nurse, a patient and a bed exist, and both people are assigned to that bed, then the staff member is assigned to the patient.
The complete implementation, including the original comments and additional deductions, is available in sim.pl.
Install a Prolog implementation such as SWI-Prolog, then open the project directory and load the source file:
$ swipl sim.pl
?- [sim].Run a query at the ?- prompt and finish it with a period. Press ; to ask Prolog for another solution, or press Enter to stop searching.
Note
The predicates and identifiers in the source file use Portuguese names because they are part of the original academic exercise. The explanations in this README are in English so the project is easier to share and study.
?- paciente(X), risco(X).
X = joao ;
X = maria.?- necessita_tratamento(joao).
true.?- designado(Pessoa, maria).
Pessoa = paulo.The direct fact for João can also be queried:
?- designado(Pessoa, joao).
Pessoa = silva.?- leito_desocupado(Leito).
Leito = leito_2.The sim_tests.pl file contains regression tests for the facts and predicates in sim.pl. The tests use SWI-Prolog's plunit framework to verify both successful deductions and expected failures.
Run the complete test suite from the project directory:
$ swipl -q -g run_tests -t halt sim_tests.plThe suite checks:
- the declared patients, staff, beds, symptoms, and treatment;
- risk and treatment deductions;
- direct and derived staff assignments;
- valid assignments and unoccupied beds;
- negative cases, such as unknown patients and invalid assignments.
Tip
A test marked with [fail] is expected to fail. This is useful for checking that the knowledge base does not derive conclusions that are not supported by its facts and rules.
The animations show examples of Prolog deriving relationships from the facts and rules:
| Demonstration | Preview |
|---|---|
| Staff and patient assignment | ![]() |
| Treatment inference | ![]() |
The accompanying logic report documents the formal deductions and the relationship between the Prolog rules and their logical interpretation.
The predicates can be read as:
P(x):xis a patient.M(x):xis a doctor.E(x):xis a nurse.L(x):xis a bed.D(x, y):xis assigned toy.
For a fuller mathematical treatment, consult the report linked above.
- SWI-Prolog Prolog implementation used to run the simulation.
- LaTeX Typesetting system used to produce the logic report.
- babel Language and localization support for the Portuguese report.
- fontenc Font encoding support for consistent PDF text rendering.
- inputenc UTF-8 source encoding support.
- geometry LaTeX package used to configure the report page layout.
- amsmath LaTeX package used for mathematical notation and equations.
- amsfonts Additional mathematical symbols used by the report.
- array Enhanced table column definitions.
- booktabs Professional table rules and spacing.
- logicproof LaTeX package used to typeset formal logic proofs.
- xcolor Color support for the report title and hyperlinks.
- parskip Paragraph spacing and indentation configuration.
- fancyhdr Academic-style page headers and footers.
- hyperref PDF metadata and clickable table-of-contents links.

