Understanding and using the Boost Phoenix Library with a focus on lazy evaluation
Asked Answered
L

1

7

I just found out about the Boost Phoenix library (hidden in the Spirit project) and as a fan of the functional-programming style (but still an amateur; some small experience with haskell and scheme) i wanted to play around with this library to learn about reasonable applications of this library.

Besides the increasement of expressiveness and clarity of the code using fp-style, i'm especially interested in lazy-evaluation for speeding up computations at low costs.

A small and simple example would be the following: there is some kind of routing problem (like the tsp), which is using a eucliedean distance matrix. We assume, that some of the values of the distance matrix are never used, and some are used very often (so it isn't a good idea to compute them on the fly for every call). Now it seems to be reasonable to have a lazy data-structure holding the distance values. How would that be possible with phoenix? (ignoring the fact that i would be easily done without fp-style-programming at all) Reading the official documentation of phoenix didn't let me understand enough to answer that.

Is it possible at all? (in Haskell for example the ability to create thunks which are guaranteeing that the value can be computed later are in the core of the language).

What does using a vector with all the lazy functions defined in phoenix mean? As naive as i am, i tried to fill two matrices (vector >) with random values, one with the normal push_back, the other with boost::phoenix::push_back and tried to read out only a small amount of values from these matrices and store them in a container for printing out. The lazy one was alway empty. Am i using phoenix in a wrong way / it should be possible? Or did i misunderstand the function of the containers/algorithms in phoenix. A small clue for the latter one is the existence of a special list-data-structure in the FP++ library, which influenced phoenix.

Additionally:

  • What are you using phoenix for?
  • Do you know some good ressources regarding phoenix? (tutorials, blog entries...)

Thanks for all your input!

Lit answered 3/10, 2010 at 11:58 Comment(3)
I'd advice you to stay away from Phoenix. I like FP too, but FP in C++ is even uglier than mutability in Haskell (they'd be head to head but C++ is already ugly and Haskell is, according to Larry Wall, the most beautiful language ever). Learn and use FP, and when you're good at it and forced to use C++, use Phoenix. But for learning, a library that bolt a wholly different paradigm on an already complex language is not advisable.Curator
Quite depressing :-/. But i got your point. Maybe i was a little bit too optimistic regarding the ease of use of fp-features in a really complex language like c++. Thanks four your answer.Lit
@delnan: could you convert your comment in an answer? It appears to have satisfied sascha, and it is high on the "unanswered" query.Obola
C
4

As requested, my comment (with additions and small modifications) as an answer...

I know your position exactly, I too played around with Phoenix (although I didn't dig in very deeply, mostly a byproduct of reading the Boost::Spirit tutorial) a while ago, relatively soon after catching the functional bug and learning basic Haskell - and I didn't get anything working :( This is btw in synch with my general experience with dark template magic: Very easy to misunderstand, screw up and get punched in the face with totally unexpected behaviour or incomprehensible error messages.

I'd advice you to stay away from Phoenix for a long time. I like FP too, but FP in C++ is even uglier than mutability in Haskell (they'd be head to head but C++ is already ugly and Haskell is, at least according to Larry Wall, the most beautiful language ever ;) ). Learn and use FP, and when you're good at it and forced to use C++, use Phoenix. But for learning, a library that bolts a wholly different paradigm on an already complex language (i.e. FP in C++) is not advisable.

Curator answered 4/10, 2010 at 19:10 Comment(1)
(-1) FP is beatifull in C++, compiler technology and C++ standards are evolving to make FP first class citizens in C++. C++ FP generates efficient code vs requiring a runtime execution engine. I suspect you are misinformed and haven't tried to use C++ FP in anger and turned around at the first hurdle. C++ is meant for real-world systems design, better to have FP that you can use for real-world problems than to not have it.... Understand and Write correct C++ FP code in the first place and you won't get essoteric compiler messages.Recursion

© 2022 - 2024 — McMap. All rights reserved.