How to "Show" an Operation?

Dear all, If I have a problem where I have to select from a set of operations, how would I print the result? Example: If I can chose from (x+y), (x*y), (x^2+y)... and I feed them all into my problem solver and it finds that "(x*y) is right", how can I print that string? -- Martin

One way woul be to represent them usnig a data structure. If it's only
artihmetic operations, it should be pretty easy.
data Arith = Var String
| Plus Artih Arith
| Mult Arith Arith
| Pow Arith Arith
x + y would be Plus (Var "x") (Var "y"), so on so forth..
You can even use infix constructors like :+:, :*:, :^:
On 4 June 2010 16:37, Martin Drautzburg
Dear all,
If I have a problem where I have to select from a set of operations, how would I print the result?
Example: If I can chose from (x+y), (x*y), (x^2+y)... and I feed them all into my problem solver and it finds that "(x*y) is right", how can I print that string?
-- Martin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ozgur Akgun

On Friday 04 June 2010 17:37:16, Martin Drautzburg wrote:
Dear all,
If I have a problem where I have to select from a set of operations, how would I print the result?
Example: If I can chose from (x+y), (x*y), (x^2+y)... and I feed them all into my problem solver and it finds that "(x*y) is right", how can I print that string?
You'd have to pass the description string to the problem solver too. If it was previously solver :: Num a => Whatever -> [a -> a -> a] -> Result it would become for example solver :: Num a => Whatever -> [(a -> a -> a, String)] -> Result

On Friday, 4. June 2010 18:02:15 Daniel Fischer wrote:
On Friday 04 June 2010 17:37:16, Martin Drautzburg wrote:
Dear all,
If I have a problem where I have to select from a set of operations, how would I print the result?
Example: If I can chose from (x+y), (x*y), (x^2+y)... and I feed them all into my problem solver and it finds that "(x*y) is right", how can I print that string?
You'd have to pass the description string to the problem solver too. If it was previously
solver :: Num a => Whatever -> [a -> a -> a] -> Result
it would become for example
solver :: Num a => Whatever -> [(a -> a -> a, String)] -> Result
Thanks to Ozgur Akgun for his suggestion. However I really wanted a "human readable" name, but admittedly I haven't said so. About this one: The only thing I miss in this solution, is that the name is identified by its position in the Pair. In this case this is certainly not a problem. But anyways, I tried to give it a name, using record syntax. This worked well until I wanted to define a different set of operations, which should also be named. It turned out that the "name" function, which was implicitly created (using record syntax) cannot be used again. How should I work around that. I could use two different "name" function, but I don't like this. Would I have to define a typeclass "namedFunction" which all have a "name" function? I guess my mind is still a bit stuck in the OO world. -- Martin

How should I work around that. I could use two different "name" function, but I don't like this. Would I have to define a typeclass "namedFunction" which all have a "name" function?
How about a "named" type: data Named a = Named { val_of :: a, name_of :: String } You can put it in Functor and Applicative to get some nice syntax for modifying the value.

If I have a problem where I have to select from a set of operations, how would I print the result?
Example: If I can chose from (x+y), (x*y), (x^2+y)... and I feed them all into my problem solver and it finds that "(x*y) is right", how can I print that string?
As others have pointed out, you can't go from operation to representation, but you can pair operations and expressions with their representations. Unless your problem solver just tries out a list of known expressions, you'll probably end up with a proper expression representation as an algebraic datatype, and a way to bind the variables. However, if you really only want arithmetic expressions and their values, you could define your own instances of the classes defining those operations. A small example of this approach can be found here: http://community.haskell.org/~claus/misc/R.hs It pairs up values with String representations of the expressions that led to those values data R a = R { rep:: String, val:: a } and defines a Num instance for 'Num a => R a' Then you can do things like (more examples in the source): -- expressions at default type: show the value *R> foldl (+) 0 [1..4] 10 *R> foldr (+) 0 [1..4] 10 -- expressions at 'R Int' types: show the representation *R> foldl (+) 0 [1..4]::R Int ((((0 + 1) + 2) + 3) + 4) *R> foldr (+) 0 [1..4]::R Int (1 + (2 + (3 + (4 + 0)))) *R> flip (foldr (+)) [1..4]::R Int->R Int \x->(1 + (2 + (3 + (4 + x)))) *R> flip (foldl (+)) [1..4]::R Int->R Int \x->((((x + 1) + 2) + 3) + 4) This approach does not easily scale to more complex expressions, and the code was meant for small demonstrations only, but it might give you some ideas. Claus

On Mon, Jun 7, 2010 at 9:49 PM, Claus Reinke
As others have pointed out, you can't go from operation to representation, but you can pair operations and expressions with their representations.
This idea is also implemented in my little 'repr' package: http://hackage.haskell.org/package/repr Bas

As others have pointed out, you can't go from operation to representation, but you can pair operations and expressions with their representations.
This idea is also implemented in my little 'repr' package:
And probably more completely/comfortably!-) The version I pointed to, which I have occasionally mentioned here over the years, was deliberately simplified and reduced. I think Lennart also had a fairly complete version. Wasn't there also a version in one of the IRC bots? This kind of trick also comes up in embedded DSLs, especially if used for embedded compilers / code generators (eg, I used to generate VRML and Javascript from a Haskell DSEL, and by changing the expression representation to Javascript, running the Haskell-embedded expression would generate Javascript). I first encountered this idea when learning about type classes: I was trying to convince myself that overloading does not break referential transparency, even though this example clearly shows that the same expression can have different meanings, depending only on type context. Claus
participants (6)
-
Bas van Dijk
-
Claus Reinke
-
Daniel Fischer
-
Evan Laforge
-
Martin Drautzburg
-
Ozgur Akgun