<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/01/9d3495098b6736864d7a911f80eacf02"
  from="Yoann Padioleau &lt;padator@w...&gt;"
  author="Yoann Padioleau"
  date="2009-01-19T16:22:12"
  subject="Visitor in OCaml [was Re: [Caml-list] C++/C# inheritance is bad?]"
  prev="2009/01/65ae6dbba6f7a5a9f4abdb2e6e7b295d"
  next="2009/01/15619ef0d6556c727c807856c0540121"
  prev-in-thread="2009/01/81422a330b59f6967afaba609aa0aa1c"
  next-in-thread="2009/01/15619ef0d6556c727c807856c0540121"
  prev-thread="2009/01/7d9ffe8f9682d9fa8b4a85b38c339f33"
  next-thread="2009/01/000cddff1b409b39cd14cc392083ce8a"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="C++/C# inheritance is bad?">
<msg 
  url="2009/01/f038b8a453e89bfa6350495a826080fe"
  from="Kuba Ober &lt;ober.14@o...&gt;"
  author="Kuba Ober"
  date="2009-01-16T15:18:51"
  subject="C++/C# inheritance is bad?">
<msg 
  url="2009/01/7f3ed02f83caca6c67c43c70d569e674"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2009-01-16T15:58:08"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/62a2d4cfa48d2f7cdc9e412c097ea6e8"
  from="Jan Kybic &lt;kybic@f...&gt;"
  author="Jan Kybic"
  date="2009-01-16T16:29:10"
  subject="On the benefits of OO, Was: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/c69523eaafbcbe2312b568706d7a7c98"
  from="Oliver Bandel &lt;oliver@f...&gt;"
  author="Oliver Bandel"
  date="2009-01-17T12:40:24"
  subject="Re: On the benefits of OO, Was: [Caml-list] C++/C# inheritance is bad?">
</msg>
</msg>
</msg>
<msg 
  url="2009/01/6cca6c9feefd88566702defdd9eb4640"
  from="Raoul Duke &lt;raould@g...&gt;"
  author="Raoul Duke"
  date="2009-01-16T18:27:41"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/5977e5c50685f9511c2a7aaeb88f2aaa"
  from="Kuba Ober &lt;ober.14@o...&gt;"
  author="Kuba Ober"
  date="2009-01-16T21:42:40"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/1688f5de4636fc2a074a556bdd4e13c5"
  from="Sashan Govender &lt;sashang@g...&gt;"
  author="Sashan Govender"
  date="2009-01-17T03:14:40"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/3da7c22bc8951acf11dc387c75d70ce3"
  from="Kuba Ober &lt;ober.14@o...&gt;"
  author="Kuba Ober"
  date="2009-01-17T14:07:23"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/0d0df2a5f73e6806a082ed1385428205"
  from="Stefano Zacchiroli &lt;zack@u...&gt;"
  author="Stefano Zacchiroli"
  date="2009-01-18T06:24:43"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
</msg>
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2009/01/bf9090ae9b10f70576fe56884cc0bb1d"
  from="Jon Harrop &lt;jon@f...&gt;"
  author="Jon Harrop"
  date="2009-01-17T13:23:25"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/9830bc229413613d2df787f6c4190432"
  from="Kuba Ober &lt;ober.14@o...&gt;"
  author="Kuba Ober"
  date="2009-01-17T14:35:25"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/484729b8c91b7ed08f7a809ef4800784"
  from="Jon Harrop &lt;jon@f...&gt;"
  author="Jon Harrop"
  date="2009-01-17T16:55:53"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/bc0b728019ecc0ed06c6c620d4829306"
  from="Kuba Ober &lt;ober.14@o...&gt;"
  author="Kuba Ober"
  date="2009-01-17T21:22:22"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/b83f81eef8ccb535ebb9797ff38dee68"
  from="Jon Harrop &lt;jon@f...&gt;"
  author="Jon Harrop"
  date="2009-01-17T22:14:04"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/e6aec16698cc330e90186b9637b316b6"
  from="David Teller &lt;David.Teller@u...&gt;"
  author="David Teller"
  date="2009-01-17T23:29:08"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
<msg 
  url="2009/01/81422a330b59f6967afaba609aa0aa1c"
  from="Jon Harrop &lt;jon@f...&gt;"
  author="Jon Harrop"
  date="2009-01-17T23:46:02"
  subject="Re: [Caml-list] C++/C# inheritance is bad?">
</msg>
</msg>
</msg>
</msg>
<msg 
  url="2009/01/9d3495098b6736864d7a911f80eacf02"
  from="Yoann Padioleau &lt;padator@w...&gt;"
  author="Yoann Padioleau"
  date="2009-01-19T16:22:12"
  subject="Visitor in OCaml [was Re: [Caml-list] C++/C# inheritance is bad?]">
<msg 
  url="2009/01/15619ef0d6556c727c807856c0540121"
  from="blue storm &lt;bluestorm.dylc@g...&gt;"
  author="blue storm"
  date="2009-01-19T16:41:37"
  subject="Re: Visitor in OCaml [was Re: [Caml-list] C++/C# inheritance is bad?]">
<msg 
  url="2009/01/982cca226b947ab9e2f9f544aa708026"
  from="Richard Jones &lt;rich@a...&gt;"
  author="Richard Jones"
  date="2009-01-19T17:49:27"
  subject="Re: Visitor in OCaml [was Re: [Caml-list] C++/C# inheritance is bad?]">
</msg>
<msg 
  url="2009/01/0eb7ca2a446affb91a9277e3c12af881"
  from="Christophe TROESTLER &lt;Christophe.Troestler+ocaml@u...&gt;"
  author="Christophe TROESTLER"
  date="2009-01-19T17:52:12"
  subject="Re: Visitor in OCaml">
</msg>
<msg 
  url="2009/01/bbed03c4ad81907e983814572437326f"
  from="Yoann Padioleau &lt;padator@w...&gt;"
  author="Yoann Padioleau"
  date="2009-01-21T20:09:07"
  subject="Re: Visitor in OCaml">
<msg 
  url="2009/01/9828bde42f61b10aa9f08a2126773cd0"
  from="Yaron Minsky &lt;yminsky@g...&gt;"
  author="Yaron Minsky"
  date="2009-01-22T04:03:22"
  subject="Re: [Caml-list] Re: Visitor in OCaml">
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
Jon Harrop &lt;jon@ffconsultancy.com&gt; writes:

&gt; On Saturday 17 January 2009 14:35:22 Kuba Ober wrote:
&gt;
&gt; In contrast, metaprogramming is the pedagogical example for algebraic 
&gt; datatypes. There is almost no need for users to extend the abstract syntax 
&gt; tree but there is huge value in being able to rewrite expressions using 
&gt; pattern matching. 
&gt; There is an idiomatic OOP solution to this problem that 
&gt; involves visitor patterns and so forth but it is comparatively verbose, 
&gt; obfuscated and unmaintainable.

Actually I think there are some good ideas in the visitor pattern. I
personnaly use a mix of pattern matching and of the visitor idiom in
one of my project about C program transformation
(http://www.emn.fr/x-info/coccinelle/).

The problem we had is that we manipulate the AST of C programs 
and some of our analysis need only to specify an action for 
specific cases, such as the function call case, and recurse
for the other cases. 
Here is an simplification of our AST: 

type ctype = 
 | Basetype of ...
 | Pointer of ctype
 | Array of expression option * ctype
 | ...
and expression = 
 | Ident of string
 | FunCall of expression * expression list
 | Postfix of ...
 | RecordAccess of ..
 | ...
and statement = 
 ...
and declaration =
 ...
and program = 
 ...

What we want is really give code like 

let my_analysis program = 
 analyze_all_expressions program (fun expr -&gt;
   match expr with
   | FunCall (e, es) -&gt; do_something()
   | _ -&gt; &lt;find_a_way_to_recurse_for_all_the_other_cases&gt; 
 )

The problem is how to write analyze_all_expressions
and find_a_way_to_recurse_for_all_the_other_cases.

Our solution is to mix the ideas of visitor, pattern matching, 
and continuation. Here is how it looks like in our coccinelle project
using our hybrid-visitor API: 

let my_analysis program = 
 Visitor.visit_iter program {
   Visitor.kexpr = (fun k e -&gt; 
    match e with
    | FunCall (e, es) -&gt; do_something()
    | _ -&gt; k e
   );
  }

You can of course also give action "hooks" for 
kstatement, ktype, or kdeclaration. But we don't overuse
visitors and so it would be stupid to provide
kfunction_call, kident, kpostfix hooks as one can just
use pattern matching with kexpr to achieve the same effect.


The code in Visitor.visit_iter is then of course a little bit tricky.
You can have a look in the source of coccinelle.

&gt;
&gt; -- 
&gt; Dr Jon Harrop, Flying Frog Consultancy Ltd.
&gt; http://www.ffconsultancy.com/?e
&gt;
&gt; _______________________________________________
&gt; Caml-list mailing list. Subscription management:
&gt; http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
&gt; Archives: http://caml.inria.fr
&gt; Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
&gt; Bug reports: http://caml.inria.fr/bin/caml-bugs

</contents>

</message>

