caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [camlp4 extension] pa_refutable : request for comments
@ 2007-12-25 17:36 blue storm
  2007-12-25 20:50 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 3+ messages in thread
From: blue storm @ 2007-12-25 17:36 UTC (permalink / raw)
  To: caml-list

Hi,

I just created my first "serious" camlp4 (3.10) extension. I'm looking
for comments, and have some questions too.

The extension (to the classical syntax) enables an explicit use of
non-irrefutable pattern matching in "let" declarations :
let refutable hd::tl = ...
let refutable [a; b; c] = List.map foo ['a'; 'b'; 'c']
let rec refutable func (Some thing) = ...

The code is available here : http://bluestorm.info/tmp/pa_refutable.ml.html
(side question : are 80 lines of code short enough to be included in
my message ? what is the mailing-list recommended behaviour here ?)

The actual error-reporting is rather naive : the extension generate a
string containing the location of the refutable pattern in the
original source, wich is raised at runtime using "failwith".. Is there
a more elegant way to do that ?

There are 6 repetitive lines of code in my refutable_mono function :
    let binds_patt binds _loc =
      let patt_of_id (id, _loc) = <:patt< $lid:id$ >> in
      Ast.PaTup _loc (Ast.paCom_of_list (List.map patt_of_id binds)) in
    let binds_expr binds _loc =
      let expr_of_id (id, _loc) = <:expr< $lid:id$ >> in
      Ast.ExTup _loc (Ast.exCom_of_list (List.map expr_of_id binds)) in
Is there an better way to do this ?

Lastly, i had to duplicate work in the grammar-modification part,
because i didn't manage to use the "opt_rec" rule. I'm not aware of
the subtleties of camlp4 parsing; is there another way ?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] [camlp4 extension] pa_refutable : request for comments
  2007-12-25 17:36 [camlp4 extension] pa_refutable : request for comments blue storm
@ 2007-12-25 20:50 ` Nicolas Pouillard
  2007-12-25 22:49   ` blue storm
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Pouillard @ 2007-12-25 20:50 UTC (permalink / raw)
  To: bluestorm.dylc; +Cc: caml-list

Excerpts from bluestorm.dylc's message of Tue Dec 25 18:36:27 +0100 2007:
> Hi,
Hi,

> I just created my first "serious" camlp4 (3.10) extension. I'm looking
> for comments, and have some questions too.
> 
> The extension (to the classical syntax) enables an explicit use of
> non-irrefutable pattern matching in "let" declarations :
> let refutable hd::tl = ...
> let refutable [a; b; c] = List.map foo ['a'; 'b'; 'c']
> let rec refutable func (Some thing) = ...

Looks useful indeed.

> The code is available here : http://bluestorm.info/tmp/pa_refutable.ml.html
> (side question : are 80 lines of code short enough to be included in
> my message ? what is the mailing-list recommended behaviour here ?)

I think that's short enough, but colored html page is perhaps more readable.

> The actual error-reporting is rather naive : the extension generate a
> string containing the location of the refutable pattern in the
> original source, wich is raised at runtime using "failwith".. Is there
> a more elegant way to do that ?

Seems sufficient.

> There are 6 repetitive lines of code in my refutable_mono function :
>     let binds_patt binds _loc =
>       let patt_of_id (id, _loc) = <:patt< $lid:id$ >> in
>       Ast.PaTup _loc (Ast.paCom_of_list (List.map patt_of_id binds)) in
>     let binds_expr binds _loc =
>       let expr_of_id (id, _loc) = <:expr< $lid:id$ >> in
>       Ast.ExTup _loc (Ast.exCom_of_list (List.map expr_of_id binds)) in
> Is there an better way to do this ?

In  fact  I  think this part is wrong. What's happen if you get no variable or
just   one   in  the  pattern?  You  will  try  to  build  a  nil-tuple  or  a
singleton-tuple and that's forbidden.

Here is the untested code for patterns:

let binds_patt binds _loc =
  let patt_of_id (id, _loc) = <:patt< $lid:id$ >> in
  match binds with
  | [] -> <:patt< () >>
  | [c] -> patt_of_id c
  | c :: cs -> <:patt< ( $patt_of_id c$, $list:List.map patt_of_id cs$ ) >>
in

The  cool  thing is that by making explicit the tuple (by having more than one
element)  one avoid resorting to concrete constructors and can use the $list:$
special antiquotation that will insert the paCom_of_list call.

> Lastly, i had to duplicate work in the grammar-modification part,
> because i didn't manage to use the "opt_rec" rule. I'm not aware of
> the subtleties of camlp4 parsing; is there another way ?

I don't think there is any subtlety here, just replace:

| "let"; "rec"; "refutable"; bi = binding ->
    <:str_item< value rec $refutable bi$ >>
| "let"; "refutable"; bi = binding ->
    <:str_item< value $refutable bi$ >>

by something like:

| "let"; r = opt_rec; "refutable"; bi = binding ->
    <:str_item< value $rec:r$ $refutable bi$ >>

Best regards,
-- 
Nicolas Pouillard aka Ertai


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] [camlp4 extension] pa_refutable : request for comments
  2007-12-25 20:50 ` [Caml-list] " Nicolas Pouillard
@ 2007-12-25 22:49   ` blue storm
  0 siblings, 0 replies; 3+ messages in thread
From: blue storm @ 2007-12-25 22:49 UTC (permalink / raw)
  To: caml-list

On 12/25/07, Nicolas Pouillard <nicolas.pouillard@gmail.com> wrote:
> > There are 6 repetitive lines of code in my refutable_mono function :
> >     let binds_patt binds _loc =
> >       let patt_of_id (id, _loc) = <:patt< $lid:id$ >> in
> >       Ast.PaTup _loc (Ast.paCom_of_list (List.map patt_of_id binds)) in
> >     let binds_expr binds _loc =
> >       let expr_of_id (id, _loc) = <:expr< $lid:id$ >> in
> >       Ast.ExTup _loc (Ast.exCom_of_list (List.map expr_of_id binds)) in
> > Is there an better way to do this ?
>
> In  fact  I  think this part is wrong. What's happen if you get no variable or
> just   one   in  the  pattern?  You  will  try  to  build  a  nil-tuple  or  a
> singleton-tuple and that's forbidden.
>
> Here is the untested code for patterns:
>
> let binds_patt binds _loc =
>   let patt_of_id (id, _loc) = <:patt< $lid:id$ >> in
>   match binds with
>   | [] -> <:patt< () >>
>   | [c] -> patt_of_id c
>   | c :: cs -> <:patt< ( $patt_of_id c$, $list:List.map patt_of_id cs$ ) >>
> in
>
> The  cool  thing is that by making explicit the tuple (by having more than one
> element)  one avoid resorting to concrete constructors and can use the $list:$
> special antiquotation that will insert the paCom_of_list call.

That's right. I fixed (or at least tried to) that in my new version,
http://bluestorm.info/tmp/pa_refutable-0.4.ml.html :

    let binds = (id_folder#patt patt)#get_binds in
    let (binds_patt, binds_expr) =
      let patt_of_id (id, _loc) = <:patt< $lid:id$ >> in
      let expr_of_id (id, _loc) = <:expr< $lid:id$ >> in
      match binds with
      [ [] -> ( <:patt< () >> , <:expr< () >> )
      | [hd] -> ( patt_of_id hd, expr_of_id hd )
      | [hd::tl] ->
         ( <:patt< ( $patt_of_id hd$, $list:List.map patt_of_id tl$ ) >>,
           <:expr< ( $expr_of_id hd$, $list:List.map expr_of_id tl$ ) >> ) ]
    in
    <:binding< $binds_patt$ = match $expr$ with
                  [ $patt$ -> $binds_expr$
                  | _ -> $refutable_err patt$ ] >> ;

> > Lastly, i had to duplicate work in the grammar-modification part,
> > because i didn't manage to use the "opt_rec" rule. I'm not aware of
> > the subtleties of camlp4 parsing; is there another way ?
>
> I don't think there is any subtlety here, just replace:
>
> | "let"; "rec"; "refutable"; bi = binding ->
>     <:str_item< value rec $refutable bi$ >>
> | "let"; "refutable"; bi = binding ->
>     <:str_item< value $refutable bi$ >>
>
> by something like:
>
> | "let"; r = opt_rec; "refutable"; bi = binding ->
>     <:str_item< value $rec:r$ $refutable bi$ >>

Hm. I thought opt_rec wouldn't work, but it looks like i was wrong. I
changed the code to use opt_rec and my test file works flawlessly.

Thanks for your reply


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-12-25 22:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-25 17:36 [camlp4 extension] pa_refutable : request for comments blue storm
2007-12-25 20:50 ` [Caml-list] " Nicolas Pouillard
2007-12-25 22:49   ` blue storm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).