I am currently a PhD student working at Inria Sophia-Antipolis (France) in Indes team and advised by Manuel Serrano.
Haskell type class in OCaml
In Haskell, a declared type may belong to type classes. A type class declare a list of signature like
class Show a where show :: a -> string
Then declaring a type belonging to that class is done by declaring a new instance of that class
data Boolean = True | False instance Show Boolean where show True = "True" show False = "False"
Then it's possible to call the show function over a Boolean typed object. The system automatically now what instance of the show function to call.
How about OCaml ? We usually do it in that way.
type boolean = True | False let string_of_boolean = function | True -> "True" | False -> "False" print_string (string_of_boolean True)
The problem is that to print a new type we need to define a new function. We would like to have a similar dispatch than Haskell. It is done in OCaml encapsulating the type within the class. First we declare a class show with a single method showing the object
class type show = object method show : unit -> string end
Then we declare the type.
type boolean = True | False
We declare a class type that will ...
class type iboolean = object inherit show end
We define a function converting the type to a string representation.
let string_of_boolean = function | True -> "True" | False -> "False"
Finally we declare the class encapsulating the type.
class cboolean v : iboolean = object val v = v method show () = string_of_boolean v end
Then adding a new type can be done like.
type natural = Zero | Succ of natural let rec show_natural = function | Zero -> "Zero" | Succ n -> "Succ (" ^ (show_natural n) ^ ")" let rec eq_natural n n' = match (n, n') with | (Zero, Zero) -> Yes | (Succ n, Succ n') -> eq_natural n n' | _ -> No class type inat = object inherit show inherit [inat] eq method getn : unit -> natural end class nat n : inat = object val n = n method show () = show_natural n method eq n' = new cyesno (eq_natural n (n'#getn())) method getn () = n end
Using this mecanism into a main function.
let _ = let o1 = new nat (Zero) in let o2 = new nat (Succ(Zero)) in print_endline (o1#show()) ; print_endline (o2#show()) ; print_endline ((o1#eq(o1))#show()) ; print_endline ((o1#eq(o2))#show())
I am designing an extension of Hop and Hop.js with an embedded query language that can be applied to multiple data sources. This extension considers any kind of data sources, like classical databases, Web pages, Web services and user-defined structures.