user546482872
user546482872

Reputation: 51

How would I alter the Set module in OCAML to fit my own representation of a set?

The default implementation for OCAML's set module for strings has the following format: ["here"; "is"; "a"; "set"] but I would like my sets to have the format {"here", "is", "a", "set"}.

Is there a simple way of changing the module to fit my format (so [] would be equivalent to {} and ; equivalent to ,) without re-writing the entire module? And if I do have to rewrite the entire module what would be the best way to go about this and can I access the current methods for the default set module? Thanks for your time.

Upvotes: 0

Views: 106

Answers (1)

ivg
ivg

Reputation: 35280

The default implementation for OCAML's set module for strings has the following format.

The default implementation doesn't specify or implement any format. In fact, OCaml doesn't have a default Set module for strings, it has a functor that can be parametrized with any comparable type to produce a module for the set of values of this type.

If you are talking about a format in which a set is printed, then you can implement it yourself, and print it in a way you like. If you would like to see this output in the toplevel, then the required type of the function that will print your sets is Format.formatter -> your_set_type -> unit, where your_set_type is a type constructor of types of your set. You can tell the OCaml top-level or debugger to use your printer with the install_printer directive.

If you are talking about how a string list is represented in OCaml program, then this is mandated by the OCaml language implementation. If you don't like it, you can clone the OCaml implementation and change the syntax. Alternatively, you can use camlp4 or camlp5 syntax rewriters to change the syntax of the OCaml parser, without changing the actual code.

Upvotes: 1

Related Questions