LINUX.ORG.RU

OCaml module system problem


0

0

Учу Ocaml. Вот пример модуля очереди приоритетов, не могу понять,
почему не работает. Помогите пожалуйста.


(* prioQueue.mli: *)

module type OrderedType =
  sig
    type t
    val compare : t -> t -> int
  end

module type PrioQueue =
  sig
    type priority
    type 'a queue

    exception Queue_is_empty

    val empty : 'a queue

    val insert : 'a queue -> priority -> 'a -> 'a queue
    val extract : 'a queue -> priority * 'a * 'a queue
  end

module DPrioQueue (Elt: OrderedType) : (PrioQueue with type priority = Elt.t) 


(* prioQueue.ml: *)

module type OrderedType =
  sig
    type t
    val compare : t -> t -> int
  end

module type PrioQueue =
  sig
    type priority
    type 'a queue

    exception Queue_is_empty

    val empty : 'a queue

    val insert : 'a queue -> priority -> 'a -> 'a queue
    val extract : 'a queue -> priority * 'a * 'a queue
  end

module DPrioQueue (Elt: OrderedType) : (PrioQueue with type priority = Elt.t) =
  struct
    type priority = Elt.t
    type 'a queue = Empty | Node of priority * 'a * 'a queue * 'a queue
    
    exception Queue_is_empty

    let empty = Empty

    let rec insert queue p e =
      match queue with
	Empty                         -> Node (p, e, Empty, Empty)
      | Node (prio, elt, left, right) ->
	  if Elt.compare p prio <= 0
	  then Node (p, e, insert right prio elt, left)
	  else Node (prio, elt, insert right p e, left)

    let rec remove_top = function
	Empty                          -> raise Queue_is_empty
      | Node (prio, elt, left, Empty)  -> left
      | Node (prio, elt, Empty, right) -> right
      | Node (_, _, (Node (lprio, lelt, _, _) as left),
                    (Node (rprio, relt, _, _) as right)) ->
	  if Elt.compare lprio rprio <= 0
	  then Node (lprio, lelt, remove_top left, right)
	  else Node (rprio, relt, left, remove_top right)

    let extract = function
	Empty -> raise Queue_is_empty
      | Node (prio, elt, _, _) as queue -> (prio, elt, remove_top queue)
  end


(* main.ml: *)

module IntOrderedType : PrioQueue.OrderedType =
  struct
    type t = int
    let compare x y =
      if x < y then -1 else if x > y then 1 else 0
  end ;;

module IntPrioQueue = PrioQueue.DPrioQueue (IntOrderedType)

let main () =
  let instream  = open_in "input.txt" in
  let outstream = open_out "output.txt" in

  let rec input_iter pq =
    try
      let i = Scanf.fscanf instream "%d" (function x -> x) in

      input_iter (IntPrioQueue.insert pq i i)
    with
      End_of_file            -> pq
    | Scanf.Scan_failure (s) -> pq
  in

  let rec output_iter pq =
    try
      let prio, elt, new_pq = IntPrioQueue.extract pq in

      Printf.fprintf outstream "%d\n" elt ;
      output_iter new_pq
    with
      IntPrioQueue.Queue_is_empty -> ()
  in

  output_iter (input_iter PrioQueue.empty) ;;

main () ;;


(* Makefile *)

prioqueue: prioQueue.cmo prioQueue.cmi main.cmo
	ocamlc -o $@ prioQueue.cmo main.cmo
prioQueue.cmo: prioQueue.ml prioQueue.cmi
	ocamlc -c prioQueue.ml
prioQueue.cmi: prioQueue.mli
	ocamlc -c $^
main.cmo: main.ml
	ocamlc -c $^

clean:
	rm -Rf *.cm[oi] *~ 



При компиляции выдает следующее:
ocamlc -c prioQueue.mli
ocamlc -c prioQueue.ml
ocamlc -c main.ml
File "main.ml", line 18, characters 41-42:
This expression has type int but is here used with type
  IntPrioQueue.priority = IntOrderedType.t
make: *** [main.cmo] Ошибка 2

Спасибо.
anonymous

Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.