caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Unquantifiable escaping type in variation of visitor pattern
@ 2005-02-08 22:55 Christian Stork
  2005-02-09  0:16 ` [Caml-list] " John Prevost
  2005-02-09  1:53 ` Jacques Garrigue
  0 siblings, 2 replies; 8+ messages in thread
From: Christian Stork @ 2005-02-08 22:55 UTC (permalink / raw)
  To: caml-list

Hello,

I'm tying to implement a framework for visitors over trees (e.g. ASTs).
These trees are built according to some rules.  Rules are much like the
productions of a grammar.

The following minimal example demonstrates my current design problem:


    type 'rule node = { rule:'rule; kids:'rule node list } 

    class someRule =
    object (self)
      method accept
        : 'baton . someRule node -> 'baton visitor -> 'baton -> 'baton
        = fun n v b -> v#visitSomeRuleNode n b
    end
    and ['baton] visitor =
    object (self)
      method visitSomeRuleNode (n:someRule node) (b:'baton) =
        List.fold_left
          (fun b' k -> k.rule#accept k (self:>'baton visitor) b')
          b n.kids
    end
   
 
The idea is that nodes are simple records that refer to their specific
rule, have child nodes, and carry some additional data (not shown
above).  Rules and visitors are implemented as objects.  They are
responsible for implementing the accept & visitSomeRuleNode methods.  (In
the minimalistic example there's only one rule but there can/will be
many.)  

The above code has one special twist, it allows the visitor's visit...
methods to hand eachother some argument, called the baton.  Different
visitors might want to use different types of batons.  The above code
tries to support this by parametrizing the accept method and the
visitor class.  Sadly, Ocaml complains about then with 

    ...in method accept...
    This type scheme cannot quantify 'baton :
    it escapes this scope.

I don't really understand this error message.  I assume it has to do
with 'baton being a parameter in 'baton visitor and its use in
visitSomeRuleNode.  Any clarifications are welcome! :-)

Anyway, is there maybe a different, workable way to model this variation
of the visitor pattern in OCaml?

Thanks for you time,
Chris

-- 
Chris Stork   <>  Support eff.org!  <>   http://www.ics.uci.edu/~cstork/
OpenPGP fingerprint:  B08B 602C C806 C492 D069  021E 41F3 8C8D 50F9 CA2F


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-02-25  0:30 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-08 22:55 Unquantifiable escaping type in variation of visitor pattern Christian Stork
2005-02-09  0:16 ` [Caml-list] " John Prevost
2005-02-22 17:29   ` Christian Stork
2005-02-23  3:08     ` Jacques Garrigue
2005-02-24 12:16       ` Christian Stork
2005-02-24 23:57         ` Jacques Garrigue
2005-02-25  0:30         ` Jacques Garrigue
2005-02-09  1:53 ` Jacques Garrigue

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).