1
votes

I have the following code in OCaml that produces the error " Unbound type constructor variable" :

module OrderedVar = struct
 type t = variable
 let compare v1 v2 = v1#get_name - v2#get_name
end

module VarSet = Set.Make(OrderedVar)

class variable n =
object
  val mutable name = n

  method get_name = name
end

How can I declare the type "variable" ?

Thank you


edit :

thank you for your answers but my probleme is a bit more difficult. In fact, I have two modules and two classes that "interlaced". Here, I can't declare the classes "variable" and "clause" before the modules, because they need the modules :

module OrderedVar = struct
 type t = variable
 let compare v1 v2 = v1#get_name - v2#get_name
end

module VarSet = Set.Make(OrderedVar)

module OrderedClause = struct
  type t = clause
  let compare = compare
end

module ClauseSet = Set.Make(OrderedClause)

class variable n =
object
  val mutable name = n
  val mutable cpos = ClauseSet.empty
  method get_name = name
end

class clause =
object
  val mutable vpos = VarSet.empty
end
2
The documentation on Recursive Modules should help you identify how to implement these correctly. caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc75 Though you might want to rethink the design to try to avoid this if possible. This example might be a little lighter reading. ocaml.janestreet.com/?q=node/84 Essential you want to use and keyword to declare the two modules in the same declaration. - Calvin

2 Answers

0
votes

Declare the variable class before you use it. Order is important in Ocaml unlike typical C based OO languages where the classes and methods can appear anywhere in files.

This quote comes from https://realworldocaml.org/v1/en/html/files-modules-and-programs.html

Unlike C, programs in OCaml do not have a unique main function. When an OCaml program is evaluated, all the statements in the implementation files are evaluated in the order in which they were linked together.

0
votes

Two comments:

  1. Mostly this is just an ordering problem.

  2. The - operator applies to ints, so I assume your names are ints?

The following compiles for me:

class variable n =
    object
    val mutable name: int = n
    method get_name = name
    end

module OrderedVar = struct
    type t = variable
    let compare v1 v2 = v1#get_name - v2#get_name
end

module VarSet = Set.Make(OrderedVar)

Update

For the new code, the easiest place to break the cycle (it seems to me) is with the class variable. Its visible type is rather simple. The following compiles for me:

type variabletype = < get_name : int >

module OrderedVar = struct
 type t = variabletype
 let compare v1 v2 = v1#get_name - v2#get_name
end

module VarSet = Set.Make(OrderedVar)

class clause =
object
  val mutable vpos = VarSet.empty
end

module OrderedClause = struct
  type t = clause
  let compare = compare
end

module ClauseSet = Set.Make(OrderedClause)

class variable n =
object
  val mutable name: int = n
  val mutable cpos = ClauseSet.empty
  method get_name = name
end

Maybe this will generalize OK to your actual problem.