(****
 *
 * This file defines operations related to the Main User-Interface Functionality.  
 *
 *)


module MainUIFunctionality;

  from Database import Database;
  from Questions import all;
  export all;
  
  object FormattedString is 
    components: t:string, f:Formatting*;
    description: (*
      A formatted string is a string with special formatting, such as
      highlighted text.
    *);
  end FormattedString;
  
  object Formatting is
    components: int:Interval, s:Style;
    description: (*
      A Formatting object represents an interval over which special
      formatting is applied.
    *);
  end Formatting;
  
  object Interval is
    components: last:integer and first:integer;
  end Interval;
  
  object Style is s:string;

  operation PurgeDuplicates is
    inputs: QuestionDB:Database;
    outputs: QuestionDB':Database;
    precondition: ;
    postcondition: (* QuestionDB has no duplicate questions *)
      forall (q':Question) 
        (q' in QuestionDB') iff (not (hasDuplicate(q', QuestionDB')));
    description: (*
      Searches through the question database and removes
      duplicate questions. A duplicate question is defined
      as a question in which the text, answers, and type is
      are all the same.
    *);
  end PurgeDuplicates;

  function hasDuplicate(q:Question, d:Database) =
    (*
     * The question has a duplicate in the database
     *)

    forall (q':Question)
      (q' in d) iff ((q = q') or (not ((q'.qt  = q.qt) and (q'.cl  = q.cl) and (q'.ty = q.ty))));

end MainUIFunctionality;