On 04/10/2012 7:15 PM, bob zhang wrote:
   Has anyone have the experience using polymorphic variant for a big Ast? 
  The benefit I can think of is  open recursion, global namespace(not in a module). Did anyone give a try?

I have -- mostly because I needed subtying, since the language I was modelling had a kind of subtyping that polymorphic variants could track 'for free'.

It works.  But it can be a real pain too: depending on your use cases, you may need a fair amount of annotations (casts).  And if you make a mistake, the error messages are truly frightening, especially when you have an AST with 4 mutually recursive parts, totaling about 25 cases, and the mistake is 3 or 4 levels deep -- the error messages can go on for pages and pages.  Buried in there will be the information you need to fix the mistake, but finding it can be extremely challenging.

I would say: use it only if you really really need what polymorphic variants 'buy' you, else stay away.  In my original code, I have rewritten most of it to use normal variants (except for one case) and use explicit open recursion (i.e. extra type variable + tying the knot) to get the job done.  The error messages are sane now.

Note that I don't think the error messages were incorrect in any way (I am sure they were not).  It might have been possible to have made them friendlier / more precise, but I am not even sure of that. 

Jacques