[Haskell-cafe] Thoughts on program annotations.

Jason Dusek jason.dusek at gmail.com
Fri Mar 4 07:32:53 CET 2011

  Hi List,

  I am working on a Bash config generation system. I've decided
  to factor out the Bash AST and pretty printer, here in a
  pre-release state:


  One thing I'd like to support is generic annotations, so that
  at a future time I can add (and render) comments, mark
  subscripts with what privilege (package install, sudo) they
  require or otherwise characterize the script outside its
  lexical structure. I ended up making my statement type a
  Functor with a Foldable instance.

  Given that every statement has an annotation, it seemed better
  to me to use mutually recursive datatypes, using one datatype
  to capture "annotatedness", like this:

    --  From https://github.com/solidsnack/bash/blob/c718de36d349efc9ac073a2c7082742c45606769/hs/Language/Bash/Syntax.hs

    data Annotated t = Annotated t (Statement t)
    data Statement t = SimpleCommand Expression [Expression]
                     | ...
                     | IfThen (Annotated t) (Annotated t)
                     | ...

  I wonder what folks think of this approach? It does mean I end
  up with all leaf-level annotations being potentially without
  annotations; this allows for relatively generic definitions,
  on the one hand; but forces type annotations at the use-site
  in many cases. It also means I have mutually recursive Functor
  and Foldable instances.

  Another option for annotations would be a sort of tree of
  zippers pointing in to the statement tree; this seems horrible
  at first glance since it leaves open the question of how the
  annotations are associated with their nodes in the first
  place. However, it does have the nice feature of simplifying
  the type of statements and also is just much more modular

  What does the list think?

Jason Dusek
Linux User #510144 | http://counter.li.org/

More information about the Haskell-Cafe mailing list