Convert a function to a string of operators?

Hello! I was wondering if it was possible to "convert" a function (which may also call functions) to a plain list of operators on parameters. Example: foo a b = a + b bar a b = a * b baw a b c = bar (foo a b) c baw' a b c = (a + b) * c Any way to get `baw'' from `baw'? Preferrably as a String. -- Evgeny

Make your own expression type and make it an instance of the Num typeclass.
Then you can build your expression using the usual operators and then use
show to convert to a string.
For example:
https://github.com/jvranish/grhug/blob/master/SymbolicDifferentiation/Symbol...
It probably does more than you want, but you should be able to get the basic
idea.
The really slick thing about it is that you can use expression type on any
function that takes a Num and you'll get a representation of the computation
that took place to get the result.
For example:
show (baw a b c :: Int) -- will show you an int
and
show (baw a b c :: Expr) -- will give you "(a + b) * c" (well... a, b, c
will be replace by whatever you passed in, but you can make them variable
names just the same)
- Job
On Fri, Mar 4, 2011 at 3:32 PM, Evgeny Grablyk
Hello!
I was wondering if it was possible to "convert" a function (which may also call functions) to a plain list of operators on parameters. Example:
foo a b = a + b bar a b = a * b
baw a b c = bar (foo a b) c baw' a b c = (a + b) * c
Any way to get `baw'' from `baw'? Preferrably as a String.
-- Evgeny
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 4 March 2011 22:10, Job Vranish
Make your own expression type and make it an instance of the Num typeclass.
This is also the approach I took in repr: http://hackage.haskell.org/package/repr For example: $ cabal install repr $ ghci
import Text.Repr let r = 1.5 + 2 + (3 + (-4) * (5 - pi / sqrt 6)) :: Repr Double extract r 17.281195923884734 show r "fromRational (3 % 2) + 2 + (3 + negate 4 * (5 - pi / sqrt 6))"
Bas

From: Evgeny Grablyk
Hello!
I was wondering if it was possible to "convert" a function (which may also call functions) to a plain list of operators on parameters. Example:
If your "operators" are only things in Num (or maybe some other typeclasses), the suggestions you have gotten about making a special Num instance are excellent. If you are curious how things are actually compiled, you can use the -ddump-simpl option. This is way more detail that you probably want, showing things like how + is implemented in terms of more primitive operations, or explicit passing around of class method dictionaries. I hoped the ghci debugger might be useful, but it seems to evaluate expressions like (baw 1 10 100) to a final result in one step, and can't show you an expression corresponding to unevaluated values. Brandon

Many thanks for your help! Seems to be what I need. Two more related questions: Will methods explained here work for boolean expressions? Is there a way to extract parameter names from function definition to use them in Show instance? Or should I just use same names everywhere? In case that helps, here's the code I need to convert (the code in where part of solveScheme): http://npaste.de/aKY3cn0xZf/ -- Evgeny

Will methods explained here work for boolean expressions?
The convenience of defining using specialised datatypes for serialising numeric operations comes from Num being a typeclass. This is not the case for Bool: Prelude> :info Num class (Eq a, Show a) => Num a where (+) :: a -> a -> a ... -- Defined in GHC.Num Prelude> :info Bool data Bool = False | True -- Defined in GHC.Bool
Is there a way to extract parameter names from function definition to use them in Show instance? Or should I just use same names everywhere?
The only kind of introspection I know comes from Template Haskell, but
I'm sure there are other methods that I'm not aware of available.
On Sun, Mar 6, 2011 at 7:28 AM, Evgeny Grablyk
Many thanks for your help! Seems to be what I need. Two more related questions:
Will methods explained here work for boolean expressions? Is there a way to extract parameter names from function definition to use them in Show instance? Or should I just use same names everywhere?
In case that helps, here's the code I need to convert (the code in where part of solveScheme): http://npaste.de/aKY3cn0xZf/
-- Evgeny
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thu, Mar 10, 2011 at 13:34, Lyndon Maydwell
Will methods explained here work for boolean expressions?
The convenience of defining using specialised datatypes for serialising numeric operations comes from Num being a typeclass. This is not the case for Bool:
Prelude> :info Num class (Eq a, Show a) => Num a where (+) :: a -> a -> a ... -- Defined in GHC.Num
Prelude> :info Bool data Bool = False | True -- Defined in GHC.Bool
If you want something like this for Bool (and other standard data types) have a look at the Awesome Prelude [1]. It is an implementation of the prelude where each data type is a type class. Erik [1] http://tom.lokhorst.eu/2010/02/awesomeprelude-presentation-video
participants (6)
-
Bas van Dijk
-
Brandon Moore
-
Erik Hesselink
-
Evgeny Grablyk
-
Job Vranish
-
Lyndon Maydwell