
I'm trying to write a function that builds a series of results in a very complicated way. Eventually I ended up writing things like
newtype Dye = Dye String deriving (Eq, Show)
instance Num Dye where (Dye x) + (Dye y) = Dye (x ++ " + " ++ y) (Dye x) - (Dye y) = Dye (x ++ " - " ++ y) (Dye x) * (Dye y) = Dye (x ++ " * " ++ y) abs (Dye x) = Dye ("abs " ++ x)
and so on. In this way, you can do something like
sum [Dye "x", Dye "y", Dye"z"]
and get "0 + x + y + z" as the result. (In reality you probably want to keep track of bracketing and so forth.) In this way, you can take functions that accept any Num instance and feed the "dye" through them to see what they're actually computing on a given run. Has anybody ever put anything like this on Hackage? I'd prefer to not invent this stuff if somebody has already done it... (The small problem with the approach above, of course, is that as soon as the function wants to do comparisons or take flow control decisions, you've got trouble. It's not impossible to solve, but it *is* a lot of work...)

2010/7/14 Andrew Coppin
I'm trying to write a function that builds a series of results in a very complicated way. Eventually I ended up writing things like
newtype Dye = Dye String deriving (Eq, Show)
instance Num Dye where (Dye x) + (Dye y) = Dye (x ++ " + " ++ y) (Dye x) - (Dye y) = Dye (x ++ " - " ++ y) (Dye x) * (Dye y) = Dye (x ++ " * " ++ y) abs (Dye x) = Dye ("abs " ++ x)
and so on. In this way, you can do something like
sum [Dye "x", Dye "y", Dye"z"]
and get "0 + x + y + z" as the result. (In reality you probably want to keep track of bracketing and so forth.) In this way, you can take functions that accept any Num instance and feed the "dye" through them to see what they're actually computing on a given run.
Has anybody ever put anything like this on Hackage? I'd prefer to not invent this stuff if somebody has already done it...
(The small problem with the approach above, of course, is that as soon as the function wants to do comparisons or take flow control decisions, you've got trouble. It's not impossible to solve, but it *is* a lot of work...)
Hi, Why not make some kinf of AST and pretty-print it ? Also you can use -XOverloadedStrings to write "x" + "y" instead of Dye "x" + Dye "y". If the goal is to see some common expressions as text, I believe there is such a package on Hackage but can't remember its name. Cheers, Thu

2010/7/14 Vo Minh Thu
2010/7/14 Andrew Coppin
: I'm trying to write a function that builds a series of results in a very complicated way. Eventually I ended up writing things like
newtype Dye = Dye String deriving (Eq, Show)
instance Num Dye where (Dye x) + (Dye y) = Dye (x ++ " + " ++ y) (Dye x) - (Dye y) = Dye (x ++ " - " ++ y) (Dye x) * (Dye y) = Dye (x ++ " * " ++ y) abs (Dye x) = Dye ("abs " ++ x)
and so on. In this way, you can do something like
sum [Dye "x", Dye "y", Dye"z"]
and get "0 + x + y + z" as the result. (In reality you probably want to keep track of bracketing and so forth.) In this way, you can take functions that accept any Num instance and feed the "dye" through them to see what they're actually computing on a given run.
Has anybody ever put anything like this on Hackage? I'd prefer to not invent this stuff if somebody has already done it...
(The small problem with the approach above, of course, is that as soon as the function wants to do comparisons or take flow control decisions, you've got trouble. It's not impossible to solve, but it *is* a lot of work...)
Hi,
Why not make some kinf of AST and pretty-print it ? Also you can use -XOverloadedStrings to write "x" + "y" instead of Dye "x" + Dye "y".
If the goal is to see some common expressions as text, I believe there is such a package on Hackage but can't remember its name.
Oh, maybe not on Hackage, I think what I had in mind was in fact a blog post: http://tom.lokhorst.eu/2009/09/deeply-embedded-dsls Cheers, Thu

http://hackage.haskell.org/package/simple-reflect This is what is used in lambdabot.

2010/7/15 Vo Minh Thu
2010/7/14 Vo Minh Thu
: 2010/7/14 Andrew Coppin
: I'm trying to write a function that builds a series of results in a very complicated way. Eventually I ended up writing things like
newtype Dye = Dye String deriving (Eq, Show)
instance Num Dye where (Dye x) + (Dye y) = Dye (x ++ " + " ++ y) (Dye x) - (Dye y) = Dye (x ++ " - " ++ y) (Dye x) * (Dye y) = Dye (x ++ " * " ++ y) abs (Dye x) = Dye ("abs " ++ x)
and so on. In this way, you can do something like
sum [Dye "x", Dye "y", Dye"z"]
and get "0 + x + y + z" as the result. (In reality you probably want to keep track of bracketing and so forth.) In this way, you can take functions that accept any Num instance and feed the "dye" through them to see what they're actually computing on a given run.
Has anybody ever put anything like this on Hackage? I'd prefer to not invent this stuff if somebody has already done it...
(The small problem with the approach above, of course, is that as soon as the function wants to do comparisons or take flow control decisions, you've got trouble. It's not impossible to solve, but it *is* a lot of work...)
Hi,
Why not make some kinf of AST and pretty-print it ? Also you can use -XOverloadedStrings to write "x" + "y" instead of Dye "x" + Dye "y".
If the goal is to see some common expressions as text, I believe there is such a package on Hackage but can't remember its name.
Oh, maybe not on Hackage, I think what I had in mind was in fact a blog post: http://tom.lokhorst.eu/2009/09/deeply-embedded-dsls
Found it: http://hackage.haskell.org/package/repr Cheers, Thu

Vo Minh Thu wrote:
Found it: http://hackage.haskell.org/package/repr
Between this and simple-reflect, it looks like Hackage has got it covered. Thanks guys.

Vo Minh Thu wrote:
Why not make some kinf of AST and pretty-print it ?
Yes, that's the logical next step. (And I've already coded it once. The example code was just to put across what I'm trying to do.)
Also you can use -XOverloadedStrings to write "x" + "y" instead of Dye "x" + Dye "y".
Good tip.

On 14 July 2010 22:37, Andrew Coppin
(The small problem with the approach above, of course, is that as soon as the function wants to do comparisons or take flow control decisions, you've got trouble. It's not impossible to solve, but it *is* a lot of work...)
Hi Andrew You could try pairing both the Dye representation and a genuinely numeric one in the same type. Kansas Lava - a embedded hardware description language - uses this technique to be able to both interpret signals and generate them. There is a new paper describing Kansas Lava here: http://www.ittc.ku.edu/csdl/fpg/biblio http://www.ittc.ku.edu/csdl/fpg/sites/default/files/kansas-lava-ifl09.pdf I think Antony Courtney was also using a dual representation in the graphics program Haven - shallow so it could use normal function mechanism to calculate "point in polygon" and deep so it could build interfaces across the JNI bridge in Java. Best wishes Stephen

Stephen Tetley wrote:
On 14 July 2010 22:37, Andrew Coppin
wrote: (The small problem with the approach above, of course, is that as soon as the function wants to do comparisons or take flow control decisions, you've got trouble. It's not impossible to solve, but it *is* a lot of work...)
Hi Andrew
You could try pairing both the Dye representation and a genuinely numeric one in the same type.
Yes. That's the obvious way to do this. But, like I said, " a lot of work". Especially if somebody else has already done it for me. ;-)
participants (4)
-
Andrew Coppin
-
Derek Elkins
-
Stephen Tetley
-
Vo Minh Thu