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

Request for complete pattern matching
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: -- (:) From: luc.maranget@i... Subject: Re: [Caml-list] Request for complete pattern matching
```>
> No this is not a problem of test. I want to be able to write
>
> match e with
>   (0 <= 0, g) -> g
> | (f, 0 <= 0) -> f
> | (f, g) -> fun x -> f x + g x
>
Well, I understand better and (as you may have guessed yourself!) I do
not incline to adopt the idea.

However provided you have some 'break' construct to transfert control to
next matching, you can perhaps compile your construct by syntactic
transformation.

My idea is to transform your patterns into
normal ones, replacing p <= e1 e2 ... en by fresh variables (say v)
and then to match 'v e1 ... en' against p.

Here you have :

match e with
| (v1, g) -> (match v1 0 with 0 -> g |_ -> break)
| (f, v2) -> (match v2 0 with 0 -> f |_ -> break)
| f, g -> fun x -> f x + g x

At a little additional cost in complexity,
you can replace 'break' (which does not exists) by exceptions as follows

let x = e in
try (match x with
| (v1, g) -> (match v1 0 with 0 -> g |_ -> raise Exit)
| _ -> raise Exit)
with Exit ->
try (match x with
| (f, v2) -> (match v2 0 with 0 -> f |_ -> raise Exit)
| _ -> raise Exit)
with Exit ->
(match x with  f, g -> fun x -> f x + g x)

I am not familiar enough with Camlp4, but I have the feeling
that some purely syntactic compilation of your construct is doable.
Since, only from the presence of <=,
can you introduce the extra matchings and try .. with Exit)
I am not saying it is easy, just that it looks feasible.

Typing and warnings are yet another issue!

-- Luc

```