Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logic Hospital

A small hospital simulation written in Prolog for studying declarative programming, first-order logic, and automated reasoning.

Prolog

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.

Table of contents

Learning goals

The simulation demonstrates:

  • Facts: statements such as paciente(joao) and leito(leito_1).
  • Rules: relationships derived from existing facts.
  • Unification: variables such as X, Pessoa, and Paciente match compatible terms.
  • Backtracking: Prolog searches for alternative solutions automatically.
  • Negation as failure: \+ Goal succeeds when Prolog cannot prove Goal.
  • 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.

Project structure

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

Logic model

The knowledge base contains:

  1. People who are patients, doctors, or nurses.
  2. Beds available in the hospital.
  3. Symptoms associated with patients.
  4. Assignments between staff, patients, and beds.
  5. 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.

Running the simulation

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.

Example queries

Checking patients at risk

?- paciente(X), risco(X).
X = joao ;
X = maria.

Checking whether João needs treatment

?- necessita_tratamento(joao).
true.

Finding staff assigned to a patient

?- designado(Pessoa, maria).
Pessoa = paulo.

The direct fact for João can also be queried:

?- designado(Pessoa, joao).
Pessoa = silva.

Finding an unoccupied bed

?- leito_desocupado(Leito).
Leito = leito_2.

Automated tests

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.pl

The 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.

Visual demonstrations

The animations show examples of Prolog deriving relationships from the facts and rules:

Demonstration Preview
Staff and patient assignment Animation showing the designado/2 relation
Treatment inference Animation showing treatment reasoning

Report

The accompanying logic report documents the formal deductions and the relationship between the Prolog rules and their logical interpretation.

Formal-logic notation

The predicates can be read as:

  • P(x): x is a patient.
  • M(x): x is a doctor.
  • E(x): x is a nurse.
  • L(x): x is a bed.
  • D(x, y): x is assigned to y.

For a fuller mathematical treatment, consult the report linked above.

References

  • 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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages