Weird "Unbound Value" in Ocaml

1.7k Views Asked by At

I've encountered this problem where this code does not compile due to a Unbound Value error.

module Nim  : Game =
struct

   type state = int;;
   type action = int;;

   let getInitialState () = 21;;

   let getActions e = 
      if e = 1 then [1] else
      if e = 2 then [1;2] else
      if e >= 3 then [1;2;3] else []
   ;;

   let getResult e a = e-a;;

   let isTerminal e = (e = 1 || e = 0);;

   let getUtility e p = e;;

   let getDepth () = 5;;

   let print_action a = Printf.printf "On enlève %d allumettes. \n" a ;;

   let print_state e = Printf.printf "Il y a %d allumettes. \n" e ;;


   let partie (p:bool) = 
       let s = getInitialState () in
       let rec partie_aux (j:bool) (st:state) =
            if isTerminal st then (if st = 1 then not j else j) else
            let () = print_state st in
            let () = Printf.printf "Au tour du joueur %b. \n" j in
            let rec check_move () = 
               let i = Stdlib.read_int () in
               if List.mem i (getActions st) then i else ( let () = Printf.printf "Ce coup n'est pas valide ! Réessayer. \n" in check_move () )
            in
            let move = check_move () in
            partie_aux (not j) (getResult st move)
       in partie_aux p s
   ;;

   (* Printf.printf "Le gagnant est le joueur %b ! \n" (partie true) ;; *)
   
end ;;

Printf.printf "Le gagnant est le joueur %b ! \n" (Nim.partie true) ;;

(*
module NimTest = MinimaxSearch (Nim) ;;

Nim.print_action (NimTest.makeDecision (Nim.getInitialState ()));;
Printf.printf "exploration de %d noeuds\n"  (NimTest.getMetrics ());;

*)

The Nim module is, as far as I know, well implemented, but when I try to compile I get

File "tpjeux.ml", line 124, characters 50-60:
124 | Printf.printf "Le gagnant est le joueur %b ! \n" (Nim.partie true) ;;
                                                        ^^^^^^^^^^
Error: Unbound value Nim.partie

I do not understand since the similar portion of code that is in comments works just fine when I uncomment it, does anyone know the reason for this ?

1

There are 1 best solutions below

0
On

This has been answered in comments, but to sum it up, the issue is with the signature hiding a name.

A minimal example:

module type S = 
sig
  val foo : unit -> int
end

module A : S =
struct
  let bar () = 42
  let foo () = bar ()
end

We can call A.foo (), which internally calls bar () but if we try to directly call A.bar ()...

utop # A.bar () ;;
Error: Unbound value A.bar