[Haskell-cafe] Re: Heterogeneous Data Structures - Nested Pairs
and functional references
ajs at 2piix.com
Thu Feb 18 15:11:01 EST 2010
> sry for being a bit thick, but how would this code be used?
> I'm unable to figure out the application yet. Could you give some
> examples how you use it?
So, the type (View view) -- ignoring class instances -- is basically
isomorphic to this (slightly simpler) type:
data View = EmptyView | TextView String | ConcatView View View |
NestViews View View View | ...
instance Monoid View where ...
Now, consider the problem of "generic programming" on the simpler
type: you quantify over the data constructors "generically", and in
doing so you gain "traversals" for the type. You gain the same
things by turning View into (View view) -- a functor, a foldable
functor, and so on. When it comes time to "render" a format for a
View (for example, a bit of Html from Text.XHtml.Strict), I use some
higher order functions I'm already familiar with. Something like
renderXHtml :: (View view) -> Html
renderXHtml (ConcatViews l r) = fold $ renderXHtml (ConcatViews l r)
renderXHtml (NestViews l m r) = fold $ renderXHtml (NestViews l m r)
renderXHtml (TextView string) = stringToHtml string
renderXHtml (PageView v_title, v_heading, v_header, v_footer,
(the_title << (renderXHtml v_title)) +++ -- (We assume
v_title is a TextView String)
(body << ( renderXHtml v_header ) +++
(render_page_contents v_contents v_heading) +++
(renderXHtml v_footer) )
where render_page_contents contents heading = undefined -- takes a
View and uses the page's heading View
-- so I guess we assume v_heading is a function
-- into a TextView
You could potentially use (>>=) for this, directly.
And if you were using the simpler type, you could do the same thing
with Uniplate, for example. It's going to construct an automorphism
 Actually, it's "the other way around". But the container/
contained adjunction makes them equivalent.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe