* pm variant type question
@ 2007-08-24 5:01 skaller
2007-08-24 10:14 ` [Caml-list] " Jeremy Yallop
0 siblings, 1 reply; 3+ messages in thread
From: skaller @ 2007-08-24 5:01 UTC (permalink / raw)
To: caml-list
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
internally.
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++: http://felix.sf.net
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] pm variant type question
2007-08-24 5:01 pm variant type question skaller
@ 2007-08-24 10:14 ` Jeremy Yallop
2007-08-24 10:23 ` skaller
0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Yallop @ 2007-08-24 10:14 UTC (permalink / raw)
To: skaller; +Cc: caml-list
skaller wrote:
> 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 *)
> ]
If I understand rightly, then this should be
type 'a regexp_t' =
[
| `REGEXP_seq of 'a * 'a (** concatenation *)
| `REGEXP_alt of 'a * 'a (** alternation *)
| `REGEXP_aster of 'a (** Kleene closure *)
| `REGEXP_string of string (** concatenation of chars of string *)
| `REGEXP_epsilon (** epsilon: null string *)
]
The version you gave will result in an xreg_t type that doesn't allow
things like
`REGEXP_alt (`REGEXP_code x, `REGEXP_code x)
(In fact, in your version, the 'a parameter is never actually used.)
Jeremy.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] pm variant type question
2007-08-24 10:14 ` [Caml-list] " Jeremy Yallop
@ 2007-08-24 10:23 ` skaller
0 siblings, 0 replies; 3+ messages in thread
From: skaller @ 2007-08-24 10:23 UTC (permalink / raw)
To: Jeremy Yallop; +Cc: caml-list
On Fri, 2007-08-24 at 11:14 +0100, Jeremy Yallop wrote:
> skaller wrote:
> > 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 *)
> > ]
>
> If I understand rightly, then this should be
>
> type 'a regexp_t' =
> [
> | `REGEXP_seq of 'a * 'a (** concatenation *)
> | `REGEXP_alt of 'a * 'a (** alternation *)
> | `REGEXP_aster of 'a (** Kleene closure *)
> | `REGEXP_string of string (** concatenation of chars of string *)
> | `REGEXP_epsilon (** epsilon: null string *)
> ]
Ouch .. yes of course. Thanks! Can't see the wood for the trees ..
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-08-24 10:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-24 5:01 pm variant type question skaller
2007-08-24 10:14 ` [Caml-list] " Jeremy Yallop
2007-08-24 10:23 ` skaller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox