Hello Jacques, Thanks a lot for your reply. This is very helpful. On Wed, Feb 23, 2005 at 12:08:15PM +0900, Jacques Garrigue wrote: > From: Christian Stork ... > I read your code, but couldn't completely understand what you are > trying to do. It looks very imperative in flavor, with lots of > mutables and refs all over the place. It's not that I didn't try otherwise. These are the reasons (I attached illustrative code): - The mutable parts/alts/item fields in aggrRule/choiceRule/listRule seem necessary since I cannot define grammars (ie recursively defined objects). Object instantiation is not allowed to the right of a let rec! "This kind of expression is not allowed as right-hand side of `let rec'" I saw the reasoning for this documented in the manual and it seems that my only choice, if I want to stick to modelling rules with objects, is to break he recurrence with refs. Not pretty but seems necessary if I want to be able to generate grammars at runtime which are base on a parsed DTD, for example. - The refs in the node variants are (probably) needed since I will need to instantiate nodes before attached data/children are determined. I didn't explain this in my previous emails and I am not yet 100% sure that I really need it. For example, the parent ref in nCommon could be unnecessary but when I used the make...Node methods to create a tree OCaml complained again that it does not allow me to use object instantiation on the right-hand side of let rec. It seems that I have to write (after removing all the refs in the types): let rec (t:node) = NChoice (exp, {parent=None; myself=t}, Some ( let rec (i:node) = NInt (intLit, {parent=(Some t); myself=i}, Some ( Int64.of_int 0)) in i));; Not pretty either, but functional. The only drawback now is that I might need more flexibility in creating parent nodes independently. I'll get back to you once I know. > Maybe you should first try to > write some standard AST code (purely functional), and then try to see > how you can adapt it to your problem. Even if you need extensibility, > you should not need mutability. Also, there are known ways to handle > extensible languages, using polymorphic variants for instance. > http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/papers/fose2000.html > or even objects > http://cristal.inria.fr/~remy/work/expr/ Thanks for the references. I'll look at them. (Actually I looked at polymorphic variants (including your web page) already and I could not see the immediate benefit for my problem and it seemed to me that it is very easy to switch over from "monomorphic variants" once the need becomes evident.) ... > I hope this can still be improved a lot. I'm all for it, but it depends on the answers to the above points. :-) > If you want to keep this design, you can at least reduce the number of > parameters by collecting them in a single one. > Note that I reported, a few months ago, a bug in ocaml up to 3.08.2, > which means that this kind of parameter collecting is unsafe when > combined with subtyping. This is fixed in CVS and 3.08.3. (Does that mean that 3.08.3 will be released soon?) > But in your particular example there is no subtyping at all, so there > should be no problem anyway. I don't know which particular subtyping you refer to, but, of course, I intend to subtype the rules and visitors. > I attach the modified part of your example, which uses a few tricks to > make the code much less verbose. Hmm, are these tricks documentd anywhere? I didn't even see the option to use constraints in regular variant types in the official OCaml manual. Anyway, thanks a lot for the improvements. Some examplary code attached. Thanks, Chris -- Chris Stork <> Support eff.org! <> http://www.ics.uci.edu/~cstork/ OpenPGP fingerprint: B08B 602C C806 C492 D069 021E 41F3 8C8D 50F9 CA2F