From: Christian Sternagel <Christian.Sternagel@uibk.ac.at>
To: caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] camlp4
Date: Sat, 19 Jan 2008 16:09:01 +0100 [thread overview]
Message-ID: <1200755341.4792128d5dad6@web-mail2.uibk.ac.at> (raw)
In-Reply-To: <1200685853-sup-3987@ausone.local>
Quoting Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> Excerpts from oandrieu's message of Fri Jan 18 20:30:59 +0100 2008:
> > On Jan 18, 2008 6:08 PM, Christian Sternagel
> > <Christian.Sternagel@uibk.ac.at> wrote:
> > > When using `camlp4o -parser Camlp4ListComprehension' as preprocessor,
> > > is the resulting code the naive translation, like in,
> > >
> > > [(x, y) | x <- xs, even xs, y <- ys]
> > >
> > > =>
> > >
> > > List.flatten (
> > > List.map (fun x -> List.map (fun y -> (x, y)) ys) (List.filter even xs)
> > > )
> > >
> > > or is there an optimization in order to avoid appends and minimize the
> > > number of cons?
> >
> >
> > FYI, my (old) syntax extension¹ for camlp4 <= 3.09 expands list
> > comprehensions to a combination of folds:
> >
> > [+ (x, y) | x <- xs | when even x | y <- ys]
> >
> > =>
> >
> > List.fold_right
> > (fun x __acc__ ->
> > if even x then
> > List.fold_right (fun y __acc__ -> (x, y) :: __acc__) ys __acc__
> > else __acc__)
> > xs []
> >
> > Less cons operations, but it's not tail recursive.
>
> Hum... That's nice and I could accept patches in that direction. However
> can
> you do it without introducing variables, that's always dangerous to
> add
> variables because we should check that the user is not using the same.
>
> > [1] http://oandrieu.nerim.net/ocaml/#pa_compr
>
> --
> Nicolas Pouillard aka Ertai
>
>
How about the transformation from Chapter 7 of [1] (by Philip Wadler)?
It should be similar to the `pseudo code':
type expr = ...;;
type patt = ...;;
type qualifier = Gen of patt * expr | Filt of expr;;
type compr = (expr * qualifier list);;
let rec expr = function
| ...
| (e, qs) -> transform [] (e, qs)
| ...
and transform l = function
| (e, []) -> expr e :: expr l
| (e, Filt f :: qs) -> if expr f then transform l (e, qs) else expr l
| (e, Gen (p, l1) :: qs) ->
let rec h = function
| [] -> expr l
| u :: us -> (function p -> transform (h us) (e, qs) | _ -> h us) u
in h (expr l1)
;;
(* where h, u, us are fresh variables not occurring in e, l1, l, or qs *)
Sorry I'm not yet familiar with camlp4 grammar extensions, but of course
above code would make use of them otherwise.
It is stated in [1] that the resulting code is optimal in that it
performs the minimum number of cons operations.
I'm not sure whether this isn't equivalent to oandrieu's code.
And I did ignore the hint that fresh variables make things
complicated :).
cheers
christian
[1] Simon P. Jones, 1987. The implementation of functional programming
languages. Available online at:
http://research.microsoft.com/~simonpj/papers/slpj-book-1987/index.htm
next prev parent reply other threads:[~2008-01-19 15:09 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 [this message]
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
-- 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=1200755341.4792128d5dad6@web-mail2.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