digitalmars.D - Specializing on Compile Time Constants
- dsimcha (11/11) Oct 12 2009 I'm working on a mathematical expression interpreter for D, which would ...
- Jacob Carlborg (2/13) Oct 12 2009 Doesn't all values to a template have to be known at compile time
- dsimcha (2/17) Oct 12 2009 No, I mean the *function* parameters of a template function.
- Jacob Carlborg (4/21) Oct 12 2009 Can't you pass the values as template arguments? Seems you working with
- dsimcha (4/26) Oct 12 2009 This would work, but it's a bit kludgey because it requires the user to ...
- Lars T. Kyllingstad (13/31) Oct 13 2009 I may be wrong, but I seem to remember reading somewhere that DMD always...
- Fawzi Mohamed (7/16) Oct 13 2009 you are wrong, compile time evaluation is not performed if not
- Andrei Alexandrescu (3/16) Oct 12 2009 As an aside, I'd so use that if you allowed LaTeX math expressions.
- bearophile (5/7) Oct 12 2009 Do you mean this?
- Lars T. Kyllingstad (3/14) Oct 13 2009 This is *so* cool. :)
I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?
Oct 12 2009
On 10/12/09 23:49, dsimcha wrote:I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?Doesn't all values to a template have to be known at compile time
Oct 12 2009
== Quote from Jacob Carlborg (doob me.com)'s articleOn 10/12/09 23:49, dsimcha wrote:No, I mean the *function* parameters of a template function.I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?Doesn't all values to a template have to be known at compile time
Oct 12 2009
On 10/12/09 23:55, dsimcha wrote:== Quote from Jacob Carlborg (doob me.com)'s articleCan't you pass the values as template arguments? Seems you working with strings (and numbers?). mathExp!("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); Note the ! after the function name.On 10/12/09 23:49, dsimcha wrote:No, I mean the *function* parameters of a template function.I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?Doesn't all values to a template have to be known at compile time
Oct 12 2009
== Quote from Jacob Carlborg (doob me.com)'s articleOn 10/12/09 23:55, dsimcha wrote:This would work, but it's a bit kludgey because it requires the user to explicitly specify that the string is known at compile time. I was wondering if this could be done transparently.== Quote from Jacob Carlborg (doob me.com)'s articleCan't you pass the values as template arguments? Seems you working with strings (and numbers?). mathExp!("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); Note the ! after the function name.On 10/12/09 23:49, dsimcha wrote:No, I mean the *function* parameters of a template function.I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?Doesn't all values to a template have to be known at compile time
Oct 12 2009
dsimcha wrote:== Quote from Jacob Carlborg (doob me.com)'s articleI may be wrong, but I seem to remember reading somewhere that DMD always tries to evaluate as much as possible at compile time. That is, if a function is CTFE-enabled, and its input is known at compile time, it is calculated at compile time. void main(string[] args) { auto x = foo(args[1]); // foo() is evaluated at run time auto y = bar("baz"); // foo() is evaluated at compile time } That said, I also seem to remember that the use of structs and classes is very limited (or nonexistent) in CTFE, and I guess your mathExp() is supposed to return a MathExp struct... -LarsOn 10/12/09 23:49, dsimcha wrote:No, I mean the *function* parameters of a template function.I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?Doesn't all values to a template have to be known at compile time
Oct 13 2009
On 2009-10-13 09:11:08 +0200, "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> said:I may be wrong, but I seem to remember reading somewhere that DMD always tries to evaluate as much as possible at compile time. That is, if a function is CTFE-enabled, and its input is known at compile time, it is calculated at compile time. void main(string[] args) { auto x = foo(args[1]); // foo() is evaluated at run time auto y = bar("baz"); // foo() is evaluated at compile time }you are wrong, compile time evaluation is not performed if not requested (by making that constant for example). The compiler cannot know the complexity of the calculation, and making it at compile time is much slower... thus you have to explicitly instruct the computer to do it...
Oct 13 2009
dsimcha wrote:I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function. Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?As an aside, I'd so use that if you allowed LaTeX math expressions. Andrei
Oct 12 2009
dsimcha:Is there any way to test whether the value of an argument to a template function is known at compile time and specialize on this?Do you mean this? http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=97424 Bye, bearophile
Oct 12 2009
dsimcha wrote:I'm working on a mathematical expression interpreter for D, which would allow for closed form mathematical expressions to be specified as string literals at runtime and be evaluated. For example: MathExp myExpression = mathExp("x^2 + e^cos(-x) - 2 * sqrt(pi)", "x"); writeln(myExpression(2)); // Does exactly what you think it does. I've found the syntax so convenient that I'd like to transparently specialize it on strings known at compile time. The idea is that, when the expression is hard-coded, you will still be able to use the MathExp interface, but your expression will evaluate at the full speed of a statically compiled function.This is *so* cool. :) -Lars
Oct 13 2009