Jon Harrop wrote:
On Wednesday 27 June 2007 16:48:44 Mattias Engdegård wrote:
  
The cost is a slightly clumsier use of symbols as strings (Symbol.str),
but my feeling is that the extra safety is worth it.
    

That is exactly what I did. Also, you cannot pattern match over the Sym.t but 
I believe the OCaml compiler doesn't optimize pattern matches over strings 
anyway.

Incidentally, can we add this to the list of wanted optimizations: O(log n) 
matching of strings, arrays and polymorphic variants.

  
Actually, what I'd like is a more powerful regular expression engine- one where I can give multiple different patterns with constant values, and create a single regular expression that if the first pattern is matched, the first constant value is matched, etc.  Something with a signature like:

type 'a regex_t
val compile : (string * 'a) -> default:'a -> 'a regex_t
val re_match : 'a regex_t -> string -> 'a

Which would allow me to do stuff like:

let re = compile [ ("foo", 1); ("bar", 2); ("baz", 3) ] ~default:(-1);;

let f str =
    match re_match re str with
    | 1 -> (* it's a foo *)
    | 2 -> (* it's a bar *)
    | 3 -> (* it's a baz *)
    | -1 -> (* it didn't match *)
;;

In other words, something like ocamllex, except dynamic.

Brian