'prolog task solving using swi-prolog IDE

Given the attached knowledge base "students_courses.pl" containing students' grades in some courses and courses' prerequisites, you are required to write a Prolog program that solves the task explained below.

Get a list containing the sequence of courses that a student needs to take in order to be able to take the target course. The query must only succeed if the student has already taken and successfully passed a course that can directly or indirectly lead to the target.

Examples:

?- remainingCourses(stud01, 'Advanced Algorithms', Courses).
Courses = ['OOP', 'Data Structures', 'Algorithms']

?- remainingCourses(stud07, 'Electronics 2', Courses).
false.

?- remainingCourses(stud02, 'Networks', Courses).
Courses = []

?- remainingCourses(stud05, 'Computer Architecture', Courses).
Courses = ['Electronics 2']

?- remainingCourses(stud08, 'Data Warehouses', Courses).
false.

given this file.pl:

student(stud01, 'Programming 1', 90).
student(stud01, 'Math 1', 78).
student(stud01, 'Statistics 1', 94).
student(stud01, 'Electronics 1', 81).
student(stud01, 'Management', 66).
student(stud01, 'English', 83).
student(stud02, 'OS 1', 65).
student(stud02, 'Math 1', 50).
student(stud02, 'Data Communication', 76).
student(stud03, 'OOP', 68).
student(stud04, 'Database', 59).
student(stud04, 'Math 3', 67).
student(stud05, 'Programming 1', 88).
student(stud05, 'Math 1', 75).
student(stud05, 'Statistics 1', 96).
student(stud05, 'Electronics 1', 89).
student(stud05, 'Management', 84).
student(stud06, 'Robotics', 62).
student(stud07, 'Programming 1', 50).
student(stud07, 'Math 2', 8).
student(stud07, 'Statistics 2', 70).
student(stud07, 'Electronics 1', 47).
student(stud08, 'OS 1', 71).

prerequisite('Programming 1', 'OOP').
prerequisite('OOP', 'OS 1').
prerequisite('OS 1', 'OS 2').
prerequisite('OOP', 'Data Structures').
prerequisite('Data Structures', 'Algorithms').
prerequisite('Algorithms', 'Advanced Algorithms').
prerequisite('Math 1', 'Math 2').
prerequisite('Math 2', 'Math 3').
prerequisite('Math 3', 'Math 4').
prerequisite('Statistics 1', 'Statistics 2').
prerequisite('Electronics 1', 'Electronics 2').
prerequisite('Electronics 2', 'Computer Architecture').
prerequisite('Computer Architecture', 'Microprocessors').
prerequisite('Data Communication', 'Networks').    
prerequisite('Database', 'Data Warehouses').



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source