Search code examples
ocamlocaml-dune

OCaml Module Naming


I'm trying to get started with OCaml, and I've created a new project with:

dune init project 01_calorie_counting

And that created a sensible enough looking project skeleton:

.
├── 01_calorie_counting.opam
├── _build
│  └── log
├── bin
│  ├── dune
│  └── main.ml
├── dune-project
├── lib
│  └── dune
└── test
   ├── 01_calorie_counting.ml
   └── dune

However, when I cd into the folder and run dune build I get this error:

File "lib/dune", line 2, characters 7-26: 2 |  (name 01_calorie_counting))
           ^^^^^^^^^^^^^^^^^^^ 
Error: "01_calorie_counting" is an 
invalid module name. Module names must be non-empty and composed only 
of the following characters: 'A'..'Z', 'a'..'z', '_', ''' or '0'..'9'. 
Hint: M01_calorie_counting would be a correct module name

So, am I going insane? Last I checked, that name only contains characters in the allowed range.

Is there an additional restriction on it not being able to start with a number that's not listed in the help message?


Solution

  • Module names must start with a letter, and the first letter will be upper-cased in the module name if the file name starts with a lower-case letter.

    See the manual's section on how modules relate to the file system:

    The compiler always derives the module name by taking the capitalized base name of the source file (.ml or .mli file). That is, it strips the leading directory name, if any, as well as the .ml or .mli suffix; then, it set the first letter to uppercase, in order to comply with the requirement that module names must be capitalized.

    as well as the section on the lexical format of identifiers:

    ident               ::=  (letter ∣ _) { letter ∣ 0…9 ∣ _ ∣ ' } 
     
    capitalized-ident   ::=  (A…Z) { letter ∣ 0…9 ∣ _ ∣ ' } 
     
    lowercase-ident     ::=  (a…z ∣ _) { letter ∣ 0…9 ∣ _ ∣ ' } 
     
    letter              ::=  A…Z ∣ a…z
    

    and of object names:

    module-name         ::=  capitalized-ident