Reputation: 2640
I'm quite new in OCaml so it's quite complicated for me to use right now an existing code but I unfortunately need it.
I've got a variable declared as follow:
val var_of_string : (string, int) Hashtbl.t
And I would like to get an element and display it inside a function.
let rec string_of_id n = match DynArray.get id_to_fml n with
Lit -> (if n land 1 == 0 then
string_of_int (Hashtbl.find var_of_string string_of_int(n))
else
"-" ^ string_of_int (Hashtbl.find var_of_string string_of_int(n))
)
| And ids ->
(match ids with
[] -> "#true#"
| _ -> "(" ^ String.concat " & " (List.map string_of_id ids) ^ ")")
| Or ids ->
(match ids with
[] -> "#false#"
| _ -> "(" ^ String.concat " | " (List.map string_of_id ids) ^ ")")
| Dia (r, n) -> "<" ^ string_of_int r ^ ">" ^ string_of_id n
| Box (r, n) -> "[" ^ string_of_int r ^ "]" ^ string_of_id n
| True -> "true"
| False -> "false"
Basically, the only important part is :
Lit -> (if n land 1 == 0 then
string_of_int (Hashtbl.find var_of_string string_of_int(n))
else
"-" ^ string_of_int (Hashtbl.find var_of_string string_of_int(n))
)
I've got an integer n
(that can become a string
) and I've got a hashtable string ; int
. I would like to be able to get the element at the n-th case of the Hashtable but I don't know how to do this :/
I tried Hashtbl.find var_of_string n
by looking at the documentation but I get this error message :
Error: The implementation syntax.ml does not match the interface syntax.cmi:
Values do not match:
val var_of_string : (int, string) Hashtbl.t
is not included in
val var_of_string : (string, int) Hashtbl.t
File "syntax.ml", line 55, characters 4-17: Actual declaration
Looks like I do something in a wrong way, but I have no idea how to do it :/
Thanks in advance for your help !
Upvotes: 2
Views: 1524
Reputation: 9030
It is a bad habit to use (==)
for integer comparison. Use (=)
, the structural comparison instead. (==)
is the physical comparison using pointers. For integers, they are the same, but generally they behave differently. Careless use of (==)
will shoot your foot badly one day.
val var_of_string : (string, int) Hashtbl.t
is a table whose keys are string
. You can add/replace/remove/find int
values using string
keys. In your code you do opposite: trying to find string
values using int
keys. It is wrong. You need a table, say, string_of_var
of type (int, string) Hashtbl.t
if you want to query the name of your variable from its id number.
One possible way is to build this reverse table from var_of_string
. It is easily done by iterating the bindings in var_of_string
using Hashtbl.iter
. But if you could build string_of_var
directly without using var_of_string
, it would be better for code clarity and performance. Without enough background we cannot tell which is the best for you.
Upvotes: 3
Reputation: 5534
Your Hashtable has key a string and value an integer.
Hashtable.find has the following signature:
val find : ('a, 'b) t -> 'a -> 'b
see http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html
you are attempting to use Hashtable.find on a value.
Upvotes: 1