
In Scheme, on can "quote" code, so that it becomes data. Microsoft's F# and C# 3.0 also have something similar that turns code into "expression trees". The latter is used extensively in LINQ which translates plain C# code into SQL code or any other code at runtime (this idea came from FP I heared) I can't find something similar for Haskell? Maybe I am looking at the wrong places? In Haskell, I know one can use a data constructor as a function (as in (map Just [1..3])), but a function cannot be turned into a data constructor (= "quoting"), can it? Now this is all really fuzzy for a newbie like me, because aren't all functions initially just data constructors waiting to be evaluated in a lazy language? I'm actually looking for something like (loose terminilogy follows) "context-based-semi-quoting". The idea is to only quote a set of functions, while evaluating all the others. For example, in the code 1 `add` 2 `mul` 3 where add = (+) mul = (*) I want to write something like selectiveQuote [add] (1 `add` 2 `mul` 3) which would result in an expression tree like add / \ 1 6 So the `mul` is not quoted because it is not part of the "context" = [add] Maybe this is just impossible, I did not dig deep into this. Thanks, Peter

On 8/27/07, Peter Verswyvelen
In Scheme, on can "quote" code, so that it becomes data. Microsoft's F# and C# 3.0 also have something similar that turns code into "expression trees". The latter is used extensively in LINQ which translates plain C# code into SQL code or any other code at runtime (this idea came from FP I heared)
I can't find something similar for Haskell? Maybe I am looking at the wrong places?
In Haskell, I know one can use a data constructor as a function (as in (map Just [1..3])), but a function cannot be turned into a data constructor (= "quoting"), can it?
Now this is all really fuzzy for a newbie like me, because aren't all functions initially just data constructors waiting to be evaluated in a lazy language?
I'm actually looking for something like (loose terminilogy follows) "context-based-semi-quoting". The idea is to only quote a set of functions, while evaluating all the others.
For example, in the code
1 `add` 2 `mul` 3 where add = (+) mul = (*)
I want to write something like
selectiveQuote [add] (1 `add` 2 `mul` 3)
which would result in an expression tree like
add / \ 1 6
So the `mul` is not quoted because it is not part of the "context" = [add]
Maybe this is just impossible, I did not dig deep into this.
Thanks, Peter
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Look at Template Haskell. quote from http://haskell.org/th : "Intuitively Template Haskell provides new language features that allow us to convert back and forth between concrete syntax, i.e. what you would type when you write normal Haskell code, and abstract syntax trees. These abstract syntax trees are represented using Haskell datatypes and, at compile time, they can be manipulated by Haskell code. This allows you to reify (convert from concrete syntax to an abstract syntax tree) some code, transform it and splice it back in (convert back again), or even to produce completely new code and splice that in, while the compiler is compiling your module." However I don't know if your 'selectiveQuote' is possible using TH. regards, Bas van Dijk

Look at Template Haskell. "Intuitively Template Haskell provides new language features that allow us to convert back and forth between concrete syntax, i.e. what
Gee coming from C++ that was the last thing I expected "templates" to do. It seems a bit more powerful in Haskell though! I'll look into that! Thanks, Peter

On Mon, 2007-08-27 at 17:56 +0200, Peter Verswyvelen wrote:
Look at Template Haskell. "Intuitively Template Haskell provides new language features that allow us to convert back and forth between concrete syntax, i.e. what
Gee coming from C++ that was the last thing I expected "templates" to do. It seems a bit more powerful in Haskell though!
I'll look into that!
They aren't related to "templates" in C++ at all. It follows from the general meaning of the word "template" (as does C++'s usage). Really, it's not all that appropriate a name anyway. You may also find Liskell interesting http://liskell.org/

On 8/27/07, Peter Verswyvelen
Look at Template Haskell. Gee coming from C++ that was the last thing I expected "templates" to do. It seems a bit more powerful in Haskell though!
There's much in common between C++ template metaprogramming and template Haskell - they both allow compile-time computation. On the question of which is more 'powerful', check out the side by side comparison here: http://www.cs.rice.edu/~taha/publications/journal/dspg04b.pdf -- Dan

"Peter Verswyvelen"
In Scheme, on can "quote" code, so that it becomes data. Microsoft's F# and C# 3.0 also have something similar that turns code into "expression trees". The latter is used extensively in LINQ which translates plain C# code into SQL code or any other code at runtime (this idea came from FP I heared)
The normal way of doing such things in Haskell is to have 1) functions that generate the component data structures (these functions are often called smart constructors) 2) other functions to put the functions/data structures together (these other functions are often call combinators). The resulting data structure that represents the sql query for example is then processed to produce the real (textual) sql query which this then sent to the database.
I can't find something similar for Haskell? Maybe I am looking at the wrong places?
HaskellDB for example does this for database queries. Parsec does this parsers. HSXML (if I got the name right) does this for XML.
In Haskell, I know one can use a data constructor as a function (as in (map Just [1..3])), but a function cannot be turned into a data constructor (= "quoting"), can it?
A data constructor is a special case of a function, or perhaps better said, a particular way a function is defined. Either a function is a data constructor or it isn't. For example you can also do just = Just Just is a data constuctor. It was defined with a data statement (and as a result starts with a capital letter). data Maybe a = Nothing | Just a just is not a data constructor. Why? It wasn't defined with a data statement. However just and Just behave almost identically. (you can't pattern match on just, only on Just) Rene.

At Mon, 27 Aug 2007 17:04:17 +0200, Peter Verswyvelen wrote:
In Scheme, on can "quote" code, so that it becomes data. Microsoft's F# and C# 3.0 also have something similar that turns code into "expression trees". The latter is used extensively in LINQ which translates plain C# code into SQL code or any other code at runtime (this idea came from FP I heared)
Depending on what you are trying to do, you might also be able to use some of the DSL techniques that Lennart Augustsson has been exploring in his blog over the past couple months. This is probably a good starting point: http://augustss.blogspot.com/2007_06_01_archive.html j.

Peter Verswyvelen wrote:
In Scheme, on can "quote" code, so that it becomes data. Microsoft's F# and C# 3.0 also have something similar that turns code into "expression trees".
I can't find something similar for Haskell? Maybe I am looking at the wrong places?
Quoting/Inspecting code at runtime is not possible in Haskell since this would break referential transparency, i.e. one could tell that two extensionally equal values like 3 `add` 4 1 `add` (2 `mul` 3) are different by inspecting their internal structure. Of course, you can use constructors for add and mul and then inspect and transform the result data Expr = Val Int | Add Expr Expr | Mul Expr Expr add, mul :: Expr -> Expr -> Expr add = Add mul = Mul x :: Expr x = Val 1 `add` (Val 2 `mul` Val 3) By making Expr an instance of the class Num , you can use overloaded arithmetic operations instance Num Expr where (+) = Add (*) = Mul fromInteger = Val . fromInteger x :: Expr x = 1 + 2*3
I want to write something like
selectiveQuote [add] (1 `add` 2 `mul` 3)
which would result in an expression tree like
add / \ 1 6
So the `mul` is not quoted because it is not part of the "context" = [add]
I'm not sure why you'd want to do that, but it's not well-defined. What would selectiveQuote [add] ((1 `add` 2) `mul` 3) be? How to expand `mul` here when `add` isn't expanded? Regards, apfelmus
participants (7)
-
apfelmus
-
Bas van Dijk
-
Dan Piponi
-
Derek Elkins
-
Jeremy Shaw
-
Peter Verswyvelen
-
Rene de Visser