% segment(SegId, Duration, Covers) specifies a video clip, where
% SegId is an identifier for the segment. 
% Duration is the time of the segment (in seconds). 
% Covers is a list of topics that is covered by the video segment. 


segment(seg0,10,[welcome]).
segment(seg1,30,[skiing,views]).
segment(seg2,50,[welcome,computational_intelligence,robots]).
segment(seg3,40,[graphics,dragons]).
segment(seg4,50,[skiing,robots]).


% presentation(MustCover, Maxtime, Segments) is true if Segments is a
%    presentation whose total running time is less than or equal to
%    Maxtime seconds, such that all of the topics in the list MustCover
%    are covered by a segment in the presentation. The aim of this
%    predicate is to design presentations that cover a certain number
%    of topics within a time limit.

presentation([],T,[]) <-
   T >= 0.
presentation([Topic|OtherTops],T,[Seg|Rpres]) <-
   segment(Seg,ST,Covers) &
   ST =< T &
   member(Topic,Covers) &
   listdiff(OtherTops,Covers,RemTops) &
   RT is T-ST &
   presentation(RemTops,RT,Rpres).

% listdiff(L1,L2,R) is true if R contains all of the elements of L1
% that are not in list L2

listdiff([],_,[]).
listdiff([H|T],S,RT) <-
   member(H,S) &
   listdiff(T,S,RT).
listdiff([H|T],S,[H|RT]) <-
   notin(H,S) &
   listdiff(T,S,RT).

% member(E,L) is true if E is a member of list L
member(A,[A|_]).
member(A,[_|L]) <-
   member(A,L).

% notin(E,L) is true if E is not in list L. This assumes E and L are
% ground when called.
notin(_,[]).
notin(A,[B|L]) <-
   A \= B &
   notin(A,L).

% Example query:
% ask presentation([welcome,skiing,robots], 90, Segs).
