Hi Sam,
You can't do exactly that, because OCaml values like chars don't exist at the type level. So you can't say e.g.
let a : 'a' = a
...or other similar things where values would be types.
What you would usually do is make an abstract (or private) type that allows constructing only valid values. E.g.,
module Ab : sig
type t = private char
val make : char -> t option
end = struct
type t = char
let make char = match char with 'a' | 'b' -> Some char | _ -> None
end
This allows constructing only values containing 'a' or 'b', with the guarantee provided by the module's implementation. So if you call `Ab.make some_char`, you'll get back an `Ab.t option`, but if it's `Some`, then you have a guarantee that it contains 'a' or 'b'.
You can convert the `Ab.t` value to a `char` using `(value :> char)` (basically, upcasting).
Regards,
Yawar