Question

I am new to OCaml and confused about the Module system in Ocaml

module type Queue = 
sig 
  type element
  type queue
  val enq: queue * element -> queue
end

module StringQ : Queue with type element = string =                                                                                                                          
struct                                                                                                                                                                       
  type element = string         (* If I remove this it also doesn't work *)                                                                                                                                                  
  type queue = element list                                                                                                                                                  
  exception EMPTY_Q                                                                                                                                                          
  let emptyq = []                                                                                                                                                            

  let enq: queue * element -> queue =                                                                                                                                        
    fun arg ->                                                                                                                                                               
      let (q, elem) = arg in                                                                                                                                                 
        elem @ [q]                                                                                                                                                           

  let rec deq: queue -> element * queue =                                                                                                                                    
    fun q ->                                                                                                                                                                 
      match q with                                                                                                                                                           
      | [] -> raise EMPTY_Q                                                                                                                                                  
      | x :: xs -> (x, xs)                                                                                                                                                   

end                                

I get a compile error: This expression has type element list, but an expression was expected of type 'a list

In the meantime, I would appreciate any links about Modules in Ocaml.

Was it helpful?

Solution

Just change

elem @ [q]

to

q @ [elem]

It compiles for me (OCaml 4.00.1).

(You don't actually seem confused, this code is teaching me things about modules.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top