As a side project, I try to implement the basics of an RDF library in OCaml.
As you may (or may not) know, a RDF statement (or triple) is composed of 3 parts:
- The subject can be an IRI or a blank node;
- The predicate must be an IRI;
- The object can be an IRI, a blank node or a literal.
I have module and types for IRIs, blank nodes and literals, and in order to type-proof the rules described above, here is what I've started to write:
(* In `triple.ml` *)
type subject = Iri of Iri.t | Bnode of Bnode.t
type objekt = Iri of Iri.t | Bnode of Bnode.t | Literal of Literal.t
type t = subject * Iri.t * objekt
let create s p o = s, p, o
So this is nice and everything, but one thing grinds my gears: whenever I want to use Triple.create
, I must explicitly state the constructor of the variant:
let iri = (* Some Iri.t value *) in
let literal = (* Literal.t value *) in
Triple.create (Iri iri) iri (Literal literal)
I'm pretty sure OCaml has ways to work around that, but I'm not sure how.
Some thoughts: I could parameterize the Triple.t
type with the type of its subject and the type of its object, but then how do I enforce the restrictions on the parameter types? Or maybe it is a good use case for a GADT?
Triple.create (Iri iri) iri (Literal literal)
for it to have typet
.object_
instead ofobjekt
because the same rule can be used systematically for all keywords.