Looking for feedback on my attempt at a tree construction edsl

Hi, With my "edsl", one can describe a tree like this - import TreeEdsl import Data.Tree createTree :: TreeContext String () createTree = do insertSubTree "Fruits" $ do insertLeaf "Apple" insertLeaf "Mango" insertSubTree "Arbitrary" $ do insertSubTree "Numbers" $ do insertLeaf "1" insertLeaf "2" insertLeaf "3" insertSubTree "Letters" $ do insertLeaf "A" insertLeaf "B" insertLeaf "C" return () main = do tree <- process "root" createTree putStrLn (drawTree (fmap show tree)) return () and get a tree like this - "root" | +- "Arbitrary" | | | +- "Letters" | | | | | +- "C" | | | | | +- "B" | | | | | `- "A" | | | `- "Numbers" | | | +- "3" | | | +- "2" | | | `- "1" | `- "Fruits" | +- "Mango" | `- "Apple" My code is here https://github.com/ckkashyap/LearningPrograms/blob/master/Haskell/edsl/TreeE... I'd appreciate your feedback on this. Does this qualify to be a edsl? Regards, Kashyap

You could turn 'insertSubTree' into and operator, and shorten "insertLeaf"
createTree = do
"Fruits" +> do
leaf "Apple"
leaf "Mango
"Arbitrary" +> do
leaf "1"
-- and so on...
It's a little bit more concise.
But I fail to see the use of TreeContext being an instance of Monad.
2011/3/22 C K Kashyap
Hi, With my "edsl", one can describe a tree like this -
import TreeEdsl import Data.Tree
createTree :: TreeContext String () createTree = do insertSubTree "Fruits" $ do insertLeaf "Apple" insertLeaf "Mango" insertSubTree "Arbitrary" $ do insertSubTree "Numbers" $ do insertLeaf "1" insertLeaf "2" insertLeaf "3" insertSubTree "Letters" $ do insertLeaf "A" insertLeaf "B" insertLeaf "C" return () main = do tree <- process "root" createTree putStrLn (drawTree (fmap show tree)) return ()
and get a tree like this -
"root" | +- "Arbitrary" | | | +- "Letters" | | | | | +- "C" | | | | | +- "B" | | | | | `- "A" | | | `- "Numbers" | | | +- "3" | | | +- "2" | | | `- "1" | `- "Fruits" | +- "Mango" | `- "Apple"
My code is here
https://github.com/ckkashyap/LearningPrograms/blob/master/Haskell/edsl/TreeE...
I'd appreciate your feedback on this. Does this qualify to be a edsl?
Regards, Kashyap
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

If you add an instance of IsString to handle leaf construction you can get
it down to
"Fruits" +> do
"Apple"
"Mango"
"Arbitrary" +> do
"1"
"..."
But I also don't see the point of doing this in a monad.
-Edward
On Tue, Mar 22, 2011 at 1:15 PM, Yves Parès
You could turn 'insertSubTree' into and operator, and shorten "insertLeaf"
createTree = do "Fruits" +> do leaf "Apple" leaf "Mango "Arbitrary" +> do leaf "1" -- and so on...
It's a little bit more concise. But I fail to see the use of TreeContext being an instance of Monad.
2011/3/22 C K Kashyap
Hi, With my "edsl", one can describe a tree like this -
import TreeEdsl import Data.Tree
createTree :: TreeContext String () createTree = do insertSubTree "Fruits" $ do insertLeaf "Apple" insertLeaf "Mango" insertSubTree "Arbitrary" $ do insertSubTree "Numbers" $ do insertLeaf "1" insertLeaf "2" insertLeaf "3" insertSubTree "Letters" $ do insertLeaf "A" insertLeaf "B" insertLeaf "C" return () main = do tree <- process "root" createTree putStrLn (drawTree (fmap show tree)) return ()
and get a tree like this -
"root" | +- "Arbitrary" | | | +- "Letters" | | | | | +- "C" | | | | | +- "B" | | | | | `- "A" | | | `- "Numbers" | | | +- "3" | | | +- "2" | | | `- "1" | `- "Fruits" | +- "Mango" | `- "Apple"
My code is here
https://github.com/ckkashyap/LearningPrograms/blob/master/Haskell/edsl/TreeE...
I'd appreciate your feedback on this. Does this qualify to be a edsl?
Regards, Kashyap
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Andy Gill uses a monad in his Dot library to allow graphs to have references as they are built. It's a pattern I like a lot and has been very useful for my graphics kit Wumpus. That said, while it's a good technique for graphs, its use is more equivocal for trees where nesting is more prominent. I use a reference monad for tree building/drawing at the moment (references are essential) but I'd switch to a technique that is better with nesting if I could think of one. At the moment the Tree EDSL doesn't use references, so I'd "third" the notion that the code would be simpler if it was pure.

On Tue, Mar 22, 2011 at 2:20 PM, Edward Kmett
If you add an instance of IsString to handle leaf construction you can get it down to "Fruits" +> do "Apple" "Mango" "Arbitrary" +> do "1" "..." But I also don't see the point of doing this in a monad.
Having it in a monad has the same benefit as blaze-html being built in a monad: the square braces and comas for the list are gone. Although it does leave you with an awkward type parameter hanging around. And the same as the Binary Put monad vs. the Binary Builder type. I prefer the monoid interface vs. the monad interface to both of these, but I can see the appeal. Antoine
-Edward
On Tue, Mar 22, 2011 at 1:15 PM, Yves Parès
wrote: You could turn 'insertSubTree' into and operator, and shorten "insertLeaf"
createTree = do "Fruits" +> do leaf "Apple" leaf "Mango "Arbitrary" +> do leaf "1" -- and so on...
It's a little bit more concise. But I fail to see the use of TreeContext being an instance of Monad.
2011/3/22 C K Kashyap
Hi, With my "edsl", one can describe a tree like this - import TreeEdsl import Data.Tree createTree :: TreeContext String () createTree = do insertSubTree "Fruits" $ do insertLeaf "Apple" insertLeaf "Mango" insertSubTree "Arbitrary" $ do insertSubTree "Numbers" $ do insertLeaf "1" insertLeaf "2" insertLeaf "3" insertSubTree "Letters" $ do insertLeaf "A" insertLeaf "B" insertLeaf "C" return () main = do tree <- process "root" createTree putStrLn (drawTree (fmap show tree)) return ()
and get a tree like this - "root" | +- "Arbitrary" | | | +- "Letters" | | | | | +- "C" | | | | | +- "B" | | | | | `- "A" | | | `- "Numbers" | | | +- "3" | | | +- "2" | | | `- "1" | `- "Fruits" | +- "Mango" | `- "Apple"
My code is here
https://github.com/ckkashyap/LearningPrograms/blob/master/Haskell/edsl/TreeE... I'd appreciate your feedback on this. Does this qualify to be a edsl? Regards, Kashyap _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi, I've tried a non-monadic version based on the suggestions here - https://github.com/ckkashyap/LearningPrograms/blob/master/Haskell/edsl/TreeW... This implementation seems to lack the "indentation based" approach that the do syntax allows. Would I be right if I said that the non-monadic version is "shallow embedding" and the monadic approach is deep embedding? Can we do deep embedding without using monads? Regards, Kashyap

A shallow embedding would typically use just functions - a famous example is Paul Hudak's "region server". A deep embedding would build syntax - represented with data types - and interpret the syntax or compile the syntax for another use (so called "off-shoring" e.g. Conal Elliott's Pan).

A shallow embedding would typically use just functions - a famous example is Paul Hudak's "region server". A deep embedding would build syntax - represented with data types - and interpret the syntax or compile the syntax for another use (so called "off-shoring" e.g. Conal Elliott's Pan).
I am not able to ascertain if what you are saying is consistent with http://www.haskell.org/haskellwiki/Embedded_domain_specific_language Regards, Kashyap

On 23 March 2011 10:28, C K Kashyap
I am not able to ascertain if what you are saying is consistent with http://www.haskell.org/haskellwiki/Embedded_domain_specific_language Regards, Kashyap
Well - I'm not sure if the description of a shallow embedding on that page is particularly enlightening...
participants (5)
-
Antoine Latter
-
C K Kashyap
-
Edward Kmett
-
Stephen Tetley
-
Yves Parès