putting the result of a function in `infix` declaration

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
TP
Reply | Threaded
Open this post in threaded view
|

putting the result of a function in `infix` declaration

TP
Hi everyone,

Consider the following code:

--------------
import Prelude hiding ((+))

data Expr = Plus Expr Expr | Minus Expr Expr | Expr String deriving Show

f :: Expr -> Int
f (Plus _ _) = 1
f (Minus _ _) = 2

-- infix (f (Plus undefined undefined)) +  -- (*)
infix 6 +
(+) :: Expr -> Expr -> Expr
e1 + e2 = Plus e1 e2

main = do

let a = Expr "a"
let b = Expr "b"
print $ a + b
--------------

I would like to declare the infix precedence of (+) with a function
(commented line (*)) instead of directly as above.

Is there any means to do that?
Do we need some kind of preprocessing machinery? How to do that in Haskell?

In a more general way, let us suppose I have a Haskell library able to
perform some calculations: how to use it in a pre-processing step, before
compilation of the executable? It could be a function to compute
symbolically roots of a polynomial of second degree to be used at runtime.
We would put some placeholders in the code where the result of the pre-
processing calculation would enter.

Thanks in advance,

TP


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: putting the result of a function in `infix` declaration

Karl Voelker
On Sun, Jun 23, 2013 at 6:36 AM, TP <[hidden email]> wrote:
In a more general way, let us suppose I have a Haskell library able to
perform some calculations: how to use it in a pre-processing step, before
compilation of the executable?

You are looking for Template Haskell.


I'm not sure how up-to-date the wiki is, but it should give you the general idea.

-Karl V.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: putting the result of a function in `infix` declaration

Roel van Dijk-3
Related to Karl's Template Haskell suggestion you could also have a look at quasiquotation:


The GHC documentation has an example of a expression quoter:



On 23 June 2013 22:22, Karl Voelker <[hidden email]> wrote:
On Sun, Jun 23, 2013 at 6:36 AM, TP <[hidden email]> wrote:
In a more general way, let us suppose I have a Haskell library able to
perform some calculations: how to use it in a pre-processing step, before
compilation of the executable?

You are looking for Template Haskell.


I'm not sure how up-to-date the wiki is, but it should give you the general idea.

-Karl V.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe