Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2004-02-27 12:46:26


"Brian McNamara" <lorgon_at_[hidden]> wrote

> On Wed, Feb 25, 2004 at 12:24:40PM -0500, Brian McNamara wrote:
> > Objects are not anathema to FP. (But _mutable_ objects are anathema in
> > _pure_ FP.) Both paradigms (OOP and FP) deal with both data and
> > behavior. In OOP, the data (objects) are the focus. In FP, the
> > behaviors (functions) are the focus.
> >
> > A lot of this discussion may be "too abstract" to be useful. To make
> > things more concrete, in a little while I will post a short example
> > illustrating some differences of how the paradigms deal with data and
> > behaviors.
>
> Ok, as promised...

Thanks for the Examples. Apologies for not giving more constructive feedback
on them. I will have to spend a lot more time reading the docs to understand
all of it.... but I am getting an inkling of what the thing is about. Maybe
if I had to manipulate generic trees I would be keener yet. Meanwhile I
have spent some time on it but gradually got carried away with an expression
tree. Somewhere in the FC++ Docs is a call for applications. A calculator
ala C++3rd Ed examples might be just what is needed as a, less abstract yet,
working example. This is my guess at one way the FP expression might
look(Guessing from a quick look at Haskell):

Expr <-- TokenStream. //(Whatever :-) )

FWIW below is a very OOP attempt(not tested). OTOH maybe this is not FC++
arena?

Thanks again for time on the examples.. Appreciate I may be straying... :-)

regards
Andy Little.

------------------------

 template <typename TokenStream>
    struct ExprTree; //wrapper for the expression, maybe needs a Rule
    struct Expr; // data, ops etc

    template<typename TokenStream>
    struct ExprTree{
        Expr& node;
        ExprTree (TokenStream& in); // build in ctor
    };

     //generic expression tree node
    struct Expr {
        //ideally leaves us with one Prim node in ExprTree
        virtual Expr& optimise()=0;
    };

    // null statement
    struct Empty : Expr{
        Expr& optimise(){return *this;}
    };

    // data
    template <typename Value_type>
    struct Prim : Expr{
        typedef Value_type value_type;
        Value_type m_value;
        Prim(Value_type value_in):m_value(value_in){}
        Prim& optimise(){return *this;}
    };

    //ops
    template <typename Op> //e.g plus,divide etc
    struct Binary : Expr{
        typedef Op type;
        Expr& left,& right;
        Binary(Expr& left_in, Expr& right_in)
        : left(left_in.optimise()),right(right_in.optimise()){}
        Expr& optimise()
        {
            // Expr* Op::operator()(const Expr&,const Expr&);
            Expr* t = Op()(left,right);
            return t? delete this,*t : *this;
        }
        ~Binary(){ delete &left, delete &right;}
    };

    template <typename Op> //e.g minus, inc , call etc
    struct Unary : Expr{
        typedef Op type;
        Expr& expr;
        Unary( Expr& expr_in)
        : expr(expr_in.optimise()){}
        Expr& optimise()
        {
           Expr* t= Op()(expr);
           return t? delete this,*t : *this;
        }
    };


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk