ocamlcamlp4

Camlp4 example : unbound module Printers.Ocaml


i'm exploring Camlp4 following this useful series of blog posts, but I'm having compilation problems. This is the code of my test.ml file :

open Camlp4.PreCast

let _loc = Loc.ghost in

let cons =
 let rec loop () =
   try
     match read_line () with
       | "" -> []
       | c -> c :: loop ()
   with End_of_file -> [] in
 loop () in
 Printers.Ocaml.print_implem
 <:str_item<
    type t =
    $Ast.TySum (_loc,
           Ast.tyOr_of_list
             (List.map
                 (fun c -> <:ctyp< $uid:c$ >>)
                 cons))$
    let to_string = function
    $Ast.mcOr_of_list
    (List.map
          (fun c -> <:match_case< $uid:c$ -> $`str:c$ >>)
        cons)$
    let of_string = function
    $let ors =
   Ast.mcOr_of_list
     (List.map
         (fun c -> <:match_case< $`str:c$ -> $uid:c$ >>)
         cons) in
    Ast.McOr(_loc,
           ors,
             <:match_case< _ -> invalid_arg "bad string" >>)$
>>

I'm using this compilation command: ocamlc -pp camlp4of -I +camlp4 -o variant camlp4lib.cma test.ml but ocamlc emits : Error: Unbound module Printers.Ocaml

I guess is a matter of compilation command but I don't find where Printers.Ocaml is implemented.

thank you for your help! _ Fr.


Solution

  • You are trying to access to Camlp4.PreCast.Printers.OCaml.print_implem, accessible after your open Camlp4.PreCast as Printers.OCaml.print_implem; note the different capitalization of OCaml vs Ocaml; OCaml is standard and should be consistently used across OCaml tools and documentation (you can file a minor bug report if some library distributed with the compiler breaks the convention).

    PS: for your information, the next version of OCaml (4.01) will probably print the following error message (tested with the development version)

    File "test.ml", line 13, characters 1-43:
    Error: Unbound module Camlp4.PreCast.Printers.Ocaml
    Did you mean OCaml?