Problem with building with dune: Unbound module

Hi! I’m having a problem with dune, when building and running tests on it.

I’m using dune 2.9.1.

Here is the brief structure of my project.

├── dune-project
├── src
│  ├── dune
│  ├── lexer.mll
│  ├── parser.mly
│  ├── parser_interface.ml
│  ├── syntax.ml
│  └── type.ml
├── test
│  ├── dune
│  └── parsing_test.ml
└── titan.opam

I’d like to run this test in parsing_test.ml.

open OUnit2
open Titan.Parser_interface
open Titan.Syntax

let peq (s : string) (v : 'a) = assert_equal v (parse_program s)

let test_parse_int_constants _ =
  peq "0" (Syntax.program [ IntLiteral 0 ]);
  peq "123" (Syntax.program [ IntLiteral 123 ])
;;

However, I got an error like this.

dune runtest
File "test/parsing_test.ml", line 8, characters 11-25:
8 |   peq "0" (Syntax.program [ IntLiteral 0 ]);
               ^^^^^^^^^^^^^^
Error: Unbound module Syntax

The content of test/dune is like this.

(library
 (name titan_test)
 (libraries base core stdio titan ounit2)
 (inline_tests)
 (preprocess
  (pps ppx_jane)))

The content of src/dune is like this.

(library
 (name titan)
 (libraries base stdio))

(menhir
 (modules parser))

(ocamllex
 (modules lexer))

src/syntax.ml is like this.

(* indentifier for variables *)
type id = string

(* brackets *)
type brackets =
  | Lparen
  | Rparen

(* binary operator *)
type binOp =
  | Plus
  | Minus
  | Mult
  | Div
  | Eq
  | Neq
  | Less
  | Leq
  | Greater
  | Geq
  | And
  | Or
  | Xor
  | Nor
  | Nand

(* unary operator *)
type unOp =
  | Excl
  | Not

(* expressions *)
type exp =
  | Var of id
  | IntLiteral of int
  | BoolLiteral of bool
  | StringLiteral of string
  | BinOp of binOp * exp * exp
  | UnOp of unOp * exp
  | IfExp of exp * exp * exp
  | LetExp of id * exp * exp

(* program *)
type program = Exp of exp list

(* type *)
type tyvar = int

type ty =
  | TyVar of tyvar
  | TyInt
  | TyBool
  | TyFun of ty * ty
  | TypList of ty

How should I fix this error? Any help is welcome!

1 Like

Hi @diohabara,

I suspect what’s going on here is that the Titan module is not opened by the tests, so the Syntax module is not in scope. You could use do one of:

  • access the functions under tests via Titan directly, e.g. Titan.Syntax.program;
  • open Titan in parsing_test.ml and then use Syntax.program;
  • just use e.g. program as you already have the module Titan.Syntax open.

Hope this helps!


edit: I just spotted that your syntax.ml file doesn’t contain a function called program (just a type with that name), so even after fixing the above your test will still fail. I guess you want to replace the existing type name with the corresponding data constructor name, to actually build a value:

...
let test_parse_int_constants _ =
-  peq "0" (Syntax.program [ IntLiteral 0 ]);
+  peq "0" (Syntax.Exp [ IntLiteral 0 ]);
...
1 Like

Also asked and answered on Reddit: https://www.reddit.com/r/ocaml/comments/qwh4au/how_to_use_a_userdefined_module/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

2 Likes