[Proto] Higher order function
Hi
I have just started experimenting with proto. I have the following grammer
G ::= Apply f G | Arrray
where the code of the simple array and apply classes are given below: How can I transform this code in the proto
template<typename T>
class Array
{
public:
Array(int size) : size(size)
{
data = new T[size];
}
operator=() { ... }
private:
int size;
T* data;
};
I have another class apply that applies the function (its first argument) on its other arguments
template
On 4/27/2010 9:31 AM, noman javed wrote:
Hi
I have just started experimenting with proto. I have the following grammer
G ::= Apply f G | Arrray
That's BNF. You have to pick C++ syntax that corresponds to that.
where the code of the simple array and apply classes are given below: How can I transform this code in the proto
template<typename T> class Array { public: Array(int size) : size(size) { data = new T[size]; }
operator=() { ... }
private: int size; T* data; };
Rename Array as ArrayImpl, and then define Array as:
// untested
template<typename T>
class Array
: proto::extends<
typename proto::terminal
I have another class apply that applies the function (its first argument) on its other arguments
template
struct apply { Array<typename F::result_type>& operator()(EXP & exp) { ... } typename F::result_type operator[](typename F::input_type i) { return f(i); }
};
The Apply class can be a unary or binary or infact an n-ary class. How can I protofy this expression template?
It doesn't look to me like there is anything lazy about apply. So it isn't part of your expression template. Rather, it accepts expressions and an evaluator, and evaluates it. There may be a more elegant solution. What are your evaluators? -- Eric Niebler BoostPro Computing http://www.boostpro.com
Eric Niebler
Now Array is a full proto terminal type, and expressions involving them will build expression trees.
I have tried that Array code, its working. But when I try to print the contents of the Array directly through std::cout it gives me errors. Should I need to implement a printcontext ?
It doesn't look to me like there is anything lazy about apply. So it isn't part of your expression template. Rather, it accepts expressions and an evaluator, and evaluates it.
There may be a more elegant solution. What are your evaluators?
The idea is to implement lazy evaluated expressions like e.g Array<int> res = evaluate(apply(inc,apply(add,apply(dec,data1),data2) ) ); where inc is a user defined increment function or function object data1 and data2 are already created Array<int>
On 4/27/2010 1:46 PM, Noman Javed wrote:
Eric Niebler
writes: Now Array is a full proto terminal type, and expressions involving them will build expression trees.
I have tried that Array code, its working. But when I try to print the contents of the Array directly through std::cout it gives me errors. Should I need to implement a printcontext ?
Your ArrayImpl type needs to be streamable; that is, it needs to define a stream insertion operator, like operator<<(std::ostream&, ArrayImpl const&). That's mentioned in the docs.
It doesn't look to me like there is anything lazy about apply. So it isn't part of your expression template. Rather, it accepts expressions and an evaluator, and evaluates it.
There may be a more elegant solution. What are your evaluators?
The idea is to implement lazy evaluated expressions like e.g
Array<int> res = evaluate(apply(inc,apply(add,apply(dec,data1),data2) ) );
where inc is a user defined increment function or function object data1 and data2 are already created Array<int>
IMO, your language could be more expressive. Array<int> res = ++( --data1 + data2 ); There's no reason why that can't have the same semantics and efficiency as what you gave above. If you want the set of operations to be user-extensible, you can allow additional functions of the form: Array<int> res = fun(data1); The evaluation can be done implicitly by giving Array an user-defined assignment operator. I suggest you spend some time with Proto's docs and have a look at the examples. It'll save you a LOT of time. -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (3)
-
Eric Niebler
-
noman javed
-
Noman Javed