Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Unquantifiable escaping type in variation of visitor pattern
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-02-08 (22:55)
From: Christian Stork <cstork@i...>
Subject: Unquantifiable escaping type in variation of visitor pattern

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
    and ['baton] visitor =
    object (self)
      method visitSomeRuleNode (n:someRule node) (b:'baton) =
          (fun b' k -> k.rule#accept k (self:>'baton visitor) b')
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

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 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 Stork   <>  Support!  <>
OpenPGP fingerprint:  B08B 602C C806 C492 D069  021E 41F3 8C8D 50F9 CA2F