Prolog with Visual Studio Code ================================= This tutorial explains how to download, install, and run Prolog using Visual Studio Code (VS Code). We will use **SWI-Prolog**, the most popular and beginner-friendly Prolog implementation. Prerequisites ------------- - A computer running Windows, macOS, or Linux - Internet access - Basic familiarity with using a terminal Installing SWI-Prolog --------------------- 1. Go to the official SWI-Prolog website: https://www.swi-prolog.org 2. Download the installer for your operating system. 3. Run the installer and follow the default installation steps. 4. Make sure SWI-Prolog is added to your system PATH. 5. Verify the installation by opening a terminal and running:: swipl If Prolog starts and shows a prompt like ``?-``, the installation was successful. Installing Visual Studio Code ------------------------------ 1. Download Visual Studio Code from: https://code.visualstudio.com 2. Install it using the default settings. 3. Launch Visual Studio Code. 4. Set it up with Debian (You can do that yourself I'm too lazy to make a tutorial for that). Installing the Prolog Extension ------------------------------- 1. Open VS Code. 2. Click the Extensions icon on the left sidebar (or press ``Ctrl+Shift+X``). 3. Search for **Prolog**. 4. Install the extension named **"Prolog" by Jan Burse**. This extension provides: - Syntax highlighting - Error detection - Code navigation - Prolog REPL support Creating Your First Prolog Program ---------------------------------- 1. In VS Code, create a new file named:: family.pl 2. Add the following Prolog code: .. code-block:: prolog parent(john, mary). parent(mary, susan). grandparent(X, Z) :- parent(X, Y), parent(Y, Z). 3. Save the file. Running Prolog Code ------------------- 1. Open the VS Code terminal: - ``Terminal > New Terminal`` 2. Start the Prolog interpreter:: swipl 3. Load your Prolog file: .. code-block:: prolog ?- [family]. 4. Run a query: .. code-block:: prolog ?- grandparent(john, susan). If Prolog returns ``true.``, your program is working correctly. Reloading Code Changes ---------------------- After editing your Prolog file, reload it inside Prolog using: .. code-block:: prolog ?- make. Debugging with Trace -------------------- To see how Prolog evaluates a query, use: .. code-block:: prolog ?- trace. Then run a query to step through execution. Conclusion ---------- You have successfully installed Prolog, configured Visual Studio Code, and run your first Prolog program. VS Code provides a clean and efficient environment for learning and developing Prolog applications. Practical Example: Medical Triage Expert System ---------------------------------------------------- One real-world application of Prolog is building **expert systems**. Expert systems use logical rules to make decisions based on known facts. Prolog has historically been used in medical diagnosis, legal reasoning, and decision support systems. In this example, we create a simple **medical triage system** that suggests possible conditions based on patient symptoms. Problem Description ------------------- A clinic wants a system that can: - Record patient symptoms - Infer possible illnesses - Suggest whether the case is urgent This type of reasoning is well-suited for Prolog because the rules can be expressed declaratively. Creating the Prolog Program --------------------------- Create a new file named:: triage.pl Add the following code: .. code-block:: prolog % Symptom rules symptom(fever). symptom(cough). symptom(fatigue). symptom(headache). symptom(shortness_of_breath). % Illness rules illness(flu) :- has_symptom(fever), has_symptom(cough), has_symptom(fatigue). illness(migraine) :- has_symptom(headache), has_symptom(fatigue). illness(covid19) :- has_symptom(fever), has_symptom(cough), has_symptom(shortness_of_breath). % Urgency rules urgent :- illness(covid19). % Dynamic symptoms entered by the user :- dynamic has_symptom/1. Running the Expert System ------------------------- 1. Start Prolog in the VS Code terminal:: swipl 2. Load the program: .. code-block:: prolog ?- [triage]. 3. Enter patient symptoms: .. code-block:: prolog ?- assert(has_symptom(fever)). ?- assert(has_symptom(cough)). ?- assert(has_symptom(fatigue)). 4. Ask Prolog to infer the illness: .. code-block:: prolog ?- illness(X). Prolog will respond with: :: X = flu. Checking Urgency ---------------- To determine if the case is urgent: .. code-block:: prolog ?- urgent. Prolog will return ``false.`` in this case. If symptoms indicate COVID-19, Prolog will infer urgency automatically. Why This Is a Real-World Use Case --------------------------------- This example demonstrates how Prolog can: - Separate **facts** from **rules** - Automatically infer conclusions - Be extended easily by adding new rules Similar systems are used in: - Medical decision support - Legal reasoning systems - Configuration systems - Artificial intelligence research Conclusion ---------- This expert system illustrates how Prolog is used in practical, real-world applications where logical inference and rule-based reasoning are required. AI Example: Intelligent Course Recommendation System ------------------------------------------------------- Artificial Intelligence is not only about machine learning. Early and modern AI systems often rely on **symbolic reasoning**, where knowledge is represented explicitly and conclusions are inferred logically. Prolog is a classic language for this approach. In this example, we build an intelligent system that recommends university courses based on a student's interests and skills. Knowledge Representation ------------------------ Create a file named:: advisor.pl Add the following knowledge base: .. code-block:: prolog % Student interests interest(alice, ai). interest(alice, programming). interest(bob, networks). % Student skills skill(alice, math). skill(alice, logic). skill(bob, hardware). % Course descriptions course(ai_fundamentals). course(machine_learning). course(logic_programming). course(computer_networks). % Course requirements requires(ai_fundamentals, math). requires(machine_learning, math). requires(machine_learning, programming). requires(logic_programming, logic). requires(computer_networks, hardware). % Course topics topic(ai_fundamentals, ai). topic(machine_learning, ai). topic(logic_programming, ai). topic(logic_programming, programming). topic(computer_networks, networks). Reasoning Rules --------------- The system recommends a course if: - The course matches the student's interests - The student satisfies the required skills - The ":-" is pretty much an if statement .. code-block:: prolog recommend(Student, Course) :- interest(Student, Interest), topic(Course, Interest), requires(Course, Skill), skill(Student, Skill). Running the AI System --------------------- 1. Start Prolog and load the program: .. code-block:: prolog ?- [advisor]. 2. Ask the system for recommendations: .. code-block:: prolog ?- recommend(alice, Course). Prolog responds with intelligent suggestions such as: :: Course = ai_fundamentals ; Course = logic_programming. Explainable AI -------------- One advantage of symbolic AI systems is explainability. Prolog can show *why* a recommendation was made by revealing the logical chain of rules that led to the conclusion. Enable tracing: .. code-block:: prolog ?- trace. ?- recommend(alice, Course). This allows inspection of each reasoning step. Why This Is AI -------------- This system demonstrates: - Knowledge-based reasoning - Automated decision making - Explainable artificial intelligence (XAI) - Separation of knowledge and inference logic Such techniques are used in: - Academic advising systems - Rule-based recommendation engines - Planning and scheduling AI - Hybrid AI systems combined with machine learning