Hello.
For one of my projects, I need a data structure that is filled at the beginning of the execution, and which is almost only used for testing membership afterwards. I chose to use a set. However, at a point I need a find_opt
, and I am not sure I understood well how its equivalent to List.find_opt
works. Here is the set declaration, and a short code snippet that (in my head) should return Some _
but actually returns None
:
(* the type t to consider *)
type morphism =
{ from_type: c_type
; to_type: c_type
; name: string
}
(* declaration of the associated set *)
module Morphism = struct
type t = morphism
let compare = Stdlib.compare
end
module MorphismSet = Set.Make(Morphism)
(* example of set instance *)
let known_morphisms = MorphismSet.of_list
[ { from_type = c_int; to_type = c_Z; name = "Z_of_int" };
{ from_type = c_Z; to_type = c_int; name = "int_of_Z" };
{ from_type = c_nat; to_type = c_Z; name = "Z_of_nat" };
{ from_type = c_Z; to_type = c_nat; name = "nat_of_Z" } ]
MorphismSet.find_first_opt (fun m -> m.from_type = c_Z && m.to_type = c_int) known_morphisms;;
(* - : MorphismSet.elt option = None *)
Does it come from the difference between find_first
and find_last
, which I neglected, whereas it might be important? Thanks in advance for your help!