i have a DSL where terminal are defined like : terminal< simd<proto::_> >
What I want is that any binary operator that apply on those terminal should
be available only if both lhs and rhs parts have the same underlying
type. E.G :
simd<float> + simd<float> is ok while
(simd<char> + simd<char>)*simd<float> is not ok.
How I can enforce this check ? Is having a transform that take an
expression and returns
the underlying type OR mpl::void_ if an error occurs the good solution ?
Thanks in advance
--
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
This question has come up a couple of times. The solution isn't very
pretty ... write a grammar that allows expressions with incompatible
terminals, and then write a separate transform that checks the terminals
for compatibility. This issue came up during Proto's review and I posted
some example code here (see the SameTerminals transform):
http://lists.boost.org/Archives/boost/2008/03/135169.php
I wish I knew of a better solution.
--
Eric Niebler
BoostPro Computing
http://www.boostpro.com
template<class T> struct simd_grammar : terminal< simd<T> > .... {};
Then vec<T> extends a domain which is based on simd_grammar<T>.
Not sure it's more elegant though. I'll dig this during the week.
--
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35
Ah, well that's a clever thought. Here's some code to get you started ...
#include <boost/proto/proto.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
using proto::_;
template<typename T>
struct simd
{};
template<typename T>
struct simd_grammar
: proto::or_<
proto::terminal<simd<T> >
, proto::nary_expr<_, proto::vararg<simd_grammar<T> > >
>
{};
template<typename Expr, typename T>
struct simd_expr;
template<typename T>
struct simd_generator
{
template<typename Sig>
struct result;
template<typename This, typename Expr>
struct result<This(Expr)>
{
typedef simd_expr<Expr, T> type;
};
template<typename Expr>
simd_expr<Expr, T> const operator()(Expr const &expr) const
{
simd_expr<Expr, T> that = {expr};
return that;
}
};
template<typename T>
struct simd_domain
: proto::domain<simd_generator<T>, simd_grammar<T> >
{};
template<typename Expr, typename T>
struct simd_expr
{
BOOST_PROTO_EXTENDS(Expr, simd_expr, simd_domain<T>)
};
simd_expr<proto::terminal<simd<char> >::type, char> const simd_char
= {{{}}};
simd_expr<proto::terminal<simd<int> >::type, int> const simd_int =
{{{}}};
int main()
{
(simd_char + simd_char) * simd_char;
(simd_int + simd_int) * simd_int;
// ERROR, these are in different domains
// (simd_char + simd_char) * simd_int;
}
--
Eric Niebler
BoostPro Computing
http://www.boostpro.com
Used as the first parameter to the domain<> template. I would have used
proto::pod_generator<simd_expr>, except that the pod_generator<>
template accepts only class templates with one parameter, and simd_expr
has two. See the section in the docs on generators ... they're just
function objects that return wrapped Proto expression objects.
--
Eric Niebler
BoostPro Computing
http://www.boostpro.com
--
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35
Eric Niebler wrote:
Joel Falcou wrote:> Hello, > > i have a DSL where terminal are defined like : terminal< simd<proto::_> > > What I want is that any binary operator that apply on those terminal should > be available only if both lhs and rhs parts have the same underlying > type. E.G : > > simd<float> + simd<float> is ok while > (simd<char> + simd<char>)*simd<float> is not ok. > > How I can enforce this check ? Is having a transform that take an > expression and returns > the underlying type OR mpl::void_ if an error occurs the good solution ?
This question has come up a couple of times. The solution isn't very pretty ... write a grammar that allows expressions with incompatible terminals, and then write a separate transform that checks the terminals for compatibility. This issue came up during Proto's review and I posted some example code here (see the SameTerminals transform): http://lists.boost.org/Archives/boost/2008/03/135169.php
I wish I knew of a better solution.
The canonical way to incorporate typing (including much richer type systems than this) in a formal grammar is through van Wijngaarden grammars. See http://en.wikipedia.org/wiki/Algol68. It is possible to generate both the parser and type analysis from such a grammar, and compilers that do so still exist.
I hadn't even considered the notion of a type system for a DSEL, but it
makes perfect sense. Unfortunately, the only tutorial I found for van
Wijngaarden grammars is here:
http://homepages.cwi.nl/~steven/vw.html
Perhaps it's because I'm tired, but I'm not making heads or tails of it
at the moment. Can you point me to a gentler introduction?
(Way back when I started with expression templates, I thought that
calling them an "embedded language" was just a cute metaphor, but much
can be gained from taking the serious view that these really *are*
languages, deserving of grammars, semantic actions, ... type systems,
too. Once we fully embrace that view, there's decades worth of
programming language theory we can leverage.)
--
Eric Niebler
BoostPro Computing
http://www.boostpro.com
Ivan : Thanks for the references.
--
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35
Seems to me that vW meta-rules *looks like* tempalte grammar rule but
maybe the example is too simple.
--
___________________________________________
Joel Falcou - Assistant Professor
PARALL Team - LRI - Universite Paris Sud XI
Tel : (+33)1 69 15 66 35
Eric Niebler wrote:
I hadn't even considered the notion of a type system for a DSEL, but it makes perfect sense. Unfortunately, the only tutorial I found for van Wijngaarden grammars is here:
and:http://homepages.cwi.nl/~steven/vw.html Perhaps it's because I'm tired, but I'm not making heads or tails of it at the moment. Can you point me to a gentler introduction? (Way back when I started with expression templates, I thought that calling them an "embedded language" was just a cute metaphor, but much can be gained from taking the serious view that these really *are* languages, deserving of grammars, semantic actions, ... type systems, too. Once we fully embrace that view, there's decades worth of programming language theory we can leverage.)
Joel Falcou wrote:
Eric Niebler a ?crit :
> (Way back when I started with expression templates, I thought that > calling them an "embedded language" was just a cute metaphor, but much > can be gained from taking the serious view that these really *are* > languages, deserving of grammars, semantic actions, ... type systems, > too. Once we fully embrace that view, there's decades worth of > programming language theory we can leverage.) >You just have described parts of my research plans ;) My current work try to get a way using ET and Concept in C++ to have a condensed way to describe operationnal and/or denotanionnal semantic for C++ DSEL. Guess I'll indeed have to swallow up this typing system rules myself. Ivan : Thanks for the references.
http://en.wikipedia.org/wiki/Algol68I'm biased here - I was on the working group that produced the second reference above - but I think that the only way to introduce semantics into a practical and general DSEL system is via formal means. However, both VWG and its exemplar in Algol68 have a very high intellectual tractability barrier, akin to the barrier facing newcomers to Functional Programming or Metaprogramming. But a casual note in Boost has already turned up three people who at least see the problem. That's a start :-)
http://burks.brighton.ac.uk/burks/language/other/a68rr/rrtoc.htm
Lindsey, C.H. and van der Meulen, S.G., Informal Introduction to ALGOL 68, North-Holland, 1971 (also sometimes known as "Algol68 without Tears")
I don't see any discussion there of van Wijngaarden grammars, or even
anything that is recognizable as a formal grammar. What am I supposed to
be looking at on that page?
> http://burks.brighton.ac.uk/burks/language/other/a68rr/rrtoc.htm
> Lindsey, C.H. and van der Meulen, S.G., /Informal Introduction to
> ALGOL 68/, North-Holland, 1971 (also sometimes known as "Algol68
> without Tears")
I see a large table of contents there. Care to narrow it down for me?
> I'm biased here - I was on the working group that produced the second
> reference above -
Very cool.
> but I think that the only way to introduce semantics
> into a practical and general DSEL system is via formal means. However,
> both VWG and its exemplar in Algol68 have a very high intellectual
> tractability barrier
Confirmed. ;-)
> , akin to the barrier facing newcomers to Functional
> Programming or Metaprogramming. But a casual note in Boost has already
> turned up three people who at least see the problem. That's a start :-)
I want to learn this stuff, but the references you've provided so far
haven't helped. How badly do you want to see something like this in
Boost? Badly enough to jump in and get your hands dirty with some code?
Maybe you could help me to add two-level grammar support to Proto.
--
Eric Niebler
BoostPro Computing
http://www.boostpro.com
I want to learn this stuff, but the references you've provided so far haven't helped. How badly do you want to see something like this in Boost? Badly enough to jump in and get your hands dirty with some code? Maybe you could help me to add two-level grammar support to Proto.
FWIW, there's an annex on this in the ISO EBNF documentation.
Here's the final draft:
http://www.cl.cam.ac.uk/~mgk25/iso-14977.pdf
Annex A suggests how Extended BNF might be extended to define
a two-level grammar. I've had interest on this when I came
across that part. Nothing much there, just FYI...
Regards,
--
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net
Anybody who understands van Wijngaarden grammars can help by explaining
them to me in small words. :-)
A van Wijngaarden Grammar definition for some target language (Algol68
in this case) is essentially an executable program written in a text
substitution language i.e. VWG itself. If a candidate program
purportedly written in the language defined by the grammar (Algol68) is
submitted to an interpreter for the VWG language definition and the
result is the empty string then the submitted program is well formed
according to the grammar. If the result of the exercise is not the
empty string then the submitted program contains an error. <snip helpful description>
A very dim light bulb appears over my head. I'll go off an read what I
can find and come back to this. Many thanks for pointing me in the right
direction.
--
Eric Niebler
BoostPro Computing
http://www.boostpro.com