Re: [Haskell-cafe] Template Haskell: let statement in a splice put in the main = do part of a program?

2013-08-27 Thread TP
adam vogt wrote:

 TH quotes limited as you've noticed. One way to generate similar code
 is to note that:
 
 do
   let x = y
   z
 
 is the same as let x = y in do z. You can generate the latter with
 something like the following file, but the `a' isn't in scope for the
 second argument to makeLetStatement. The uglier $(dyn a) works,
 though I suppose it's more verbose than manually in-lining the
 variable a.
 
 {-# LANGUAGE TemplateHaskell #-}
 import Language.Haskell.TH
 
 main = $(let
 
 makeLetStatement :: String - ExpQ - ExpQ
 makeLetStatement s rest = letE [ valD (varP (mkName s))
 (normalB $ stringE s) []]
 rest
 
 in makeLetStatement a [| print $(dyn a) |] )

Thanks Adam.
Unfortunately, this solution is not satisfying because the goal is to put 
only one mention to a in the main part, putting all the repetitive code 
and ExpQ's in a separate module. Tonight, I've tried hard one more time 
without more success.
Maybe I have to stick to non-let expressions in the main part of a script, 
when it comes to TH. It should nevertheless allow me to call functions, make 
tests, etc.

Thanks,

TP


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Template Haskell: let statement in a splice put in the main = do part of a program?

2013-08-25 Thread adam vogt
On Sat, Aug 24, 2013 at 11:00 AM, TP paratribulati...@free.fr wrote:
 that has type Stmt, in an ExpQ that seems to be the only thing that we can
 put in a splice. I have found that it can only be done by doE (or DoE) and
 compE (or CompE) according to

 http://www.haskell.org/ghc/docs/latest/html/libraries/template-haskell-2.8.0.0/Language-Haskell-TH.html#v:doE

 But doE is not a solution as we have seen above, and compE is to construct
 list comprehensions, which is a different thing.

 So, is there any solution to my problem?

Hi TP,

TH quotes limited as you've noticed. One way to generate similar code
is to note that:

do
  let x = y
  z

is the same as let x = y in do z. You can generate the latter with
something like the following file, but the `a' isn't in scope for the
second argument to makeLetStatement. The uglier $(dyn a) works,
though I suppose it's more verbose than manually in-lining the
variable a.

{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH

main = $(let

makeLetStatement :: String - ExpQ - ExpQ
makeLetStatement s rest = letE [ valD (varP (mkName s))
(normalB $ stringE s) []]
rest

in makeLetStatement a [| print $(dyn a) |] )


--
Adam

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Template Haskell: let statement in a splice put in the main = do part of a program?

2013-08-24 Thread Brandon Allbery
On Sat, Aug 24, 2013 at 11:00 AM, TP paratribulati...@free.fr wrote:

 main = do

 $(makeLetStatement a)
 -- print a


Is that the actual indentation you used? Because it's wrong if so, and the
error you would get is the one you're reporting. Indentation matters in
Haskell.

In an equation for `main': main = do { $(makeLetStatement a) }


You cannot *end* a do with a let-statement; it requires something else
following it. You have nothing following it, as shown by the above fragment
from the error message.

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Template Haskell: let statement in a splice put in the main = do part of a program?

2013-08-24 Thread TP
Brandon Allbery wrote:

 main = do

 $(makeLetStatement a)
 -- print a

 
 Is that the actual indentation you used? Because it's wrong if so, and the
 error you would get is the one you're reporting. Indentation matters in
 Haskell.

Yes, it matters, but not after main = do: all the lines can start at the 
beginning of the line. Am I wrong? Or do I not understand what you say?

 In an equation for `main': main = do { $(makeLetStatement a) }

 
 You cannot *end* a do with a let-statement; it requires something else
 following it. You have nothing following it, as shown by the above
 fragment from the error message.

Yes, I have explained why: to be able to see the evaluation of the splice; 
otherwise I obtain Not in scope: `a' if I uncomment -- print a at the 
end of my code; I have explained everything in my initial post.

TP



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe