简体   繁体   中英

How can I go through a list of facts in Answer Set Prolog?

I have a list of facts like

student(mary).
student(john).

etc, and also

course(math).
course(a).
course(b).

etc, and

took(john,math).
...

I have to say if a student can or can not graduate.

To graduate a student have to have all the courses taken. But how can I say this without write all term in the rule?

what I think was

can_graduate(X) :- took_all_courses(X). 

but I dont know how to explain the rule took all courses without writing all courses. Can someone help me?

thanks.

took_all_courses(Student) :-
  student(Student),
  forall( course(C), took(Student,C) ).

Since this is Answer-Set Prolog and not Prolog, higher-order predicates such as forall are not available. What you want to do is count firstly the number of courses and secondly the number of courses the student has taken. How about using aggregates for this?

took_all_courses(Student) :-
    student(S),
    TotalCourses = #max{C:course(C)}, CoursesTaken = #max{C:took(S,C)},
    TotalCourses == CoursesTaken.

I've not tested, but that should work, you might have to play around a bit with the syntax of the aggregates depending on the version of the grounder you are using, (eg see Clingo 3 vs 4).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM