caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christian Sternagel <christian.sternagel@uibk.ac.at>
To: caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] camlp4
Date: Thu, 24 Jan 2008 10:01:26 +0100	[thread overview]
Message-ID: <20080124090126.GB761@pc6197-c703.uibk.ac.at> (raw)
In-Reply-To: <1201025959-sup-3129@ausone.local>

On Tue, Jan 22, 2008 at 07:20:55PM +0100, Nicolas Pouillard wrote:
> Excerpts from christian.sternagel's message of Tue Jan 22 17:43:20 +0100 2008:
> > I tried to implement the optimizations for list comprehensions
> > as stated in the book by simon peyton jones (1987). The problem
> > of using fresh variables I circumvented in an admittedly `ugly' way,
> > however (which is not fool proof since there could be programmers
> > that use variable names like __h__0, __h__1, ...).
> > 
> > The Starting point was the file Camlp4ListComprehension.ml from
> > camlp4.
> > 
> > Any comments are wellcome.
> 
> Nice  start,  however  can you make it more closer to the original code (as in
Yes. Here it is. The `fresh variable' problem, however,
is not really solved. Maybe, using some dedicated namespace
is indeed the easiest solution (hence I did that in my code).

cheers

christisn

@Ertai: sorry for the duplicate... the first time I forgot to
reply to the list.
--------------------------------------------------------------------------
open Camlp4;                                             (* -*- camlp4r -*- *)
(****************************************************************************)
(*                                                                          *)
(*                              Objective Caml                              *)
(*                                                                          *)
(*                            INRIA Rocquencourt                            *)
(*                                                                          *)
(*  Copyright  2007  Institut  National  de  Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed under   *)
(*  the terms of the GNU Library General Public License, with the special   *)
(*  exception on linking described in LICENSE at the top of the Objective   *)
(*  Caml source tree.                                                       *)
(*                                                                          *)
(****************************************************************************)

(* Authors:
 * - Nao Hirokawa: initial version
 * - Nicolas Pouillard: revised syntax version
 * - Christian Sternagel: optimization of Chapter 7 of
 *   [Simon Peyton Jones, 1987. The Implementation of
 *    Functional Programming Languages]
 *)


module Id = struct
  value name = "Camlp4ListComprenhsion";
  value version = "$Id: Camlp4ListComprehension.ml,v 1.1.2.1 2007/05/27 16:23:35 pouillar Exp $";
end;

module Make (Syntax : Sig.Camlp4Syntax) = struct
  open Sig;
  include Syntax;

  value rec loop n =
    fun
    [ [] -> None
    | [(x, _)] -> if n = 1 then Some x else None
    | [_ :: l] -> loop (n - 1) l ];

  value stream_peek_nth n strm = loop n (Stream.npeek n strm);

  (* usual trick *)
  value test_patt_lessminus =
    Gram.Entry.of_parser "test_patt_lessminus"
      (fun strm ->
        let rec skip_patt n =
          match stream_peek_nth n strm with
          [ Some (KEYWORD "<-") -> n
          | Some (KEYWORD ("[" | "[<")) ->
              skip_patt (ignore_upto "]" (n + 1) + 1)
          | Some (KEYWORD "(") -> 
              skip_patt (ignore_upto ")" (n + 1) + 1)
          | Some (KEYWORD "{") -> 
              skip_patt (ignore_upto "}" (n + 1) + 1)
          | Some (KEYWORD ("as" | "::" | "," | "_"))
          | Some (LIDENT _ | UIDENT _) -> skip_patt (n + 1)
          | Some _ | None -> raise Stream.Failure ]
        and ignore_upto end_kwd n =
          match stream_peek_nth n strm with
          [ Some (KEYWORD prm) when prm = end_kwd -> n 
          | Some (KEYWORD ("[" | "[<")) ->
              ignore_upto end_kwd (ignore_upto "]" (n + 1) + 1)
          | Some (KEYWORD "(") ->
              ignore_upto end_kwd (ignore_upto ")" (n + 1) + 1)
          | Some (KEYWORD "{") -> 
              ignore_upto end_kwd (ignore_upto "}" (n + 1) + 1)
          | Some _ -> ignore_upto end_kwd (n + 1)
          | None -> raise Stream.Failure ]
        in
        skip_patt 1);

  value index = ref 0;
  value var base =
   Format.sprintf
    "__camlp4_list_comprehension__%s__%i"
    base
    (incr index; !index);

  value compr _loc e qs =
   let rec transform _loc e acc =
    fun
    [ [] -> <:expr< [ $e$ :: $acc$ ] >>
    | [`cond b :: qs] ->
     <:expr< if $b$ then $transform _loc e acc qs$ else $acc$ >>
    | [`gen (p, l) :: qs] ->
     let h = var "h" and u = var "u" and us = var "us" in
     <:expr<
      let rec $lid:h$ = fun
      [ [] -> $acc$
      | [ $lid:u$ :: $lid:us$ ] ->
       $if Ast.is_irrefut_patt p then
        <:expr< (fun
         [ $p$ -> $transform _loc e <:expr< ($lid:h$ $lid:us$) >> qs$ ]
        ) $lid:u$
        >>
       else
        <:expr< (fun
         [ $p$ -> $transform _loc e <:expr< ($lid:h$ $lid:us$) >> qs$
         | _ -> $lid:h$ $lid:us$ ]
        ) $lid:us$
        >>
        $
       ]
     in $lid:h$ $l$ 
     >>
    | _ -> <:expr< [] >> ]
   in
   transform _loc e <:expr< [] >> qs;

  DELETE_RULE Gram expr: "["; sem_expr_for_list; "]" END;

  value is_revised =
    try do {
      DELETE_RULE Gram expr: "["; sem_expr_for_list; "::"; expr; "]" END;
      True
    } with [ Not_found -> False ];

  value comprehension_or_sem_expr_for_list =
    Gram.Entry.mk "comprehension_or_sem_expr_for_list";

  EXTEND Gram
    GLOBAL: expr comprehension_or_sem_expr_for_list;

    expr: LEVEL "simple"
      [ [ "["; e = comprehension_or_sem_expr_for_list; "]" -> e ] ]
    ;

    comprehension_or_sem_expr_for_list:
      [ [ e = expr LEVEL "top"; ";"; mk = sem_expr_for_list ->
            <:expr< [ $e$ :: $mk <:expr< [] >>$ ] >>
        | e = expr LEVEL "top"; ";" -> <:expr< [$e$] >>
        | e = expr LEVEL "top"; "|"; l = LIST1 item SEP ";" -> compr _loc e l
        | e = expr LEVEL "top" -> <:expr< [$e$] >> ] ]
    ;

    item: 
      [ [ test_patt_lessminus; 
          p = patt; "<-" ; e = expr LEVEL "top" -> `gen (p, e)
        | e = expr LEVEL "top" -> `cond e ] ]
    ;

  END;

  if is_revised then
    EXTEND Gram
      GLOBAL: expr comprehension_or_sem_expr_for_list;

      comprehension_or_sem_expr_for_list:
      [ [ e = expr LEVEL "top"; ";"; mk = sem_expr_for_list; "::"; last = expr ->
            <:expr< [ $e$ :: $mk last$ ] >>
        | e = expr LEVEL "top"; "::"; last = expr ->
            <:expr< [ $e$ :: $last$ ] >> ] ]
      ;
    END
  else ();

end;

let module M = Register.OCamlSyntaxExtension Id Make in ();
--------------------------------------------------------------------------
> 
> -- 
> Nicolas Pouillard aka Ertai


  reply	other threads:[~2008-01-24  9:01 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-18 17:08 camlp4 Christian Sternagel
2008-01-18 18:56 ` [Caml-list] camlp4 Nicolas Pouillard
2008-01-18 19:30 ` Olivier Andrieu
2008-01-18 19:53   ` Nicolas Pouillard
2008-01-19 15:09     ` Christian Sternagel
2008-01-20 15:23       ` Nicolas Pouillard
2008-01-22 13:33         ` Christian Sternagel
2008-01-22 13:42           ` Nicolas Pouillard
2008-01-22 14:06             ` Loup Vaillant
2008-01-22 14:26               ` Nicolas Pouillard
2008-01-22 16:43             ` Christian Sternagel
2008-01-22 18:20               ` Nicolas Pouillard
2008-01-24  9:01                 ` Christian Sternagel [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-02-06  1:16 camlp4 Andy Ray
2010-02-06 11:15 ` [Caml-list] camlp4 blue storm
2010-02-06 12:14 ` Tiphaine Turpin
2010-02-06 12:44   ` Guillaume Yziquel
2010-02-09 15:30     ` Guillaume Yziquel
2010-02-09 18:29       ` Jake Donham
2010-02-07 17:19   ` Martin DeMello
2010-02-08  1:14     ` Ashish Agarwal
2010-02-08  2:01       ` Yoann Padioleau
2010-02-08  2:03       ` Erik de Castro Lopo
2010-02-06 13:37 ` Ed Keith
2010-02-07 13:51 ` Joseph Young
2004-01-04 16:49 [Caml-list] novice puzzled by speed tests Xavier Leroy
2004-01-05 19:50 ` [Caml-list] camlp4 Ker Lutyn
2003-07-08 12:49 [Caml-list] -unsafe and camlp4 "Dmitry Bely" 
2003-07-08 13:38 ` Xavier Leroy
2003-07-08 15:38   ` [Caml-list] camlp4 Dmitry Bely
2003-07-22 11:14     ` Damien Doligez
2003-06-10 14:22 Pierre CHATEL
2003-02-07 11:11 [Caml-list] Camlp4 Daniel de Rauglaudre
2003-02-08  0:26 ` Issac Trotts
2003-02-08 17:23 ` Geoff Wozniak
2002-05-17 13:19 Ohad Rodeh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20080124090126.GB761@pc6197-c703.uibk.ac.at \
    --to=christian.sternagel@uibk.ac.at \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).