Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
pm variant type question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: pm variant type question
I'm having trouble typing this: we have:

type 'a regexp_t' =
  | `REGEXP_seq of 'a regexp_t' * 'a regexp_t' (** concatenation *)
  | `REGEXP_alt of 'a regexp_t' * 'a regexp_t' (** alternation *)
  | `REGEXP_aster of 'a regexp_t'  (** Kleene closure *)
  | `REGEXP_string of string   (** concatenation of chars of string *)
  | `REGEXP_epsilon            (** epsilon: null string *)

type regexp_t = 'a regexp_t' as 'a
type 'c xreg_t = [
  'a regexp_t' | 
  `REGEXP_sentinel | 
  `REGEXP_code of 'c ] as 'a

let extend (r:regexp_t):'c xreg_t = (r:regexp_t :> 'c xreg_t)

let mklexer rs =
  match rs with
  | [] -> failwith "Empty Lexer list"
  | (hr,hc)::t ->
    let re = List.fold_left
      (fun acc (r,c) -> 
        `REGEXP_alt (acc, `REGEXP_seq (r, `REGEXP_code c)))
      (`REGEXP_seq (hr, `REGEXP_code hc)) t
    in process_regexp re

Basically, mklexer takes a list of regexp_t, but the supertype
formed by adding the sentinel and code combinators is used

The code above works, but I cannot seem to write an mli interface
for mklexer, which should be:

val 'c mklexer : (regexp_t * 'c) list -> 'c something

[Take a list of pairs, of regexp and some code, and generate
a lexer]

The coercion 'extend' above exhibits the desired widening ..
well I think it does, but it doesn't work even if I try

let mkelxer (rs: (regexp_t * 'c) list) : 'c something =
	let rs = map extend rs in ...

This is a very simple covariant extension .. why can't I get
it right? 

John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: