[Haskell-cafe] "quoting" in Haskell

Bas van Dijk v.dijk.bas at gmail.com
Mon Aug 27 11:13:16 EDT 2007

On 8/27/07, Peter Verswyvelen <bf3 at telenet.be> 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)
> 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 at 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.


Bas van Dijk

More information about the Haskell-Cafe mailing list