Ticket #1201 (new bug)

Opened 6 years ago

Last modified 5 months ago

ghci runs Template Haskell splices 4 times

Reported by: igloo Owned by: simonpj
Priority: low Milestone: _|_
Component: Template Haskell Version: 6.6
Keywords: Cc: alfonso.acosta@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: ghci018 Blocked By:
Blocking: Related Tickets:

Description

ghci (both 6.6 and HEAD) appears to run Template Haskell splices 4 times, as shown here:

$ ghci -fth -v0
Prelude> :m + Language.Haskell.TH
Prelude Language.Haskell.TH> $(do runIO $ (Random.randomIO :: IO Int) >>= print; [| 1 |] )
7184082642420483628
-8222359774145102883
2625059211496099566
1462332877512834399
1

Change History

Changed 6 years ago by igloo

  • testcase changed from ghci016 to ghci018

Changed 6 years ago by simonpj

  • owner set to simonpj
  • priority changed from normal to low
  • milestone changed from 6.6.2 to _|_

GHCi tries to typecheck the fragment you type in several different ways. When you type an expression 'e' at the command prompt, GHC tries to typecheck the following monadic statements:

  • it <- e; print it
  • it <- e
  • let it = e; print it

The first one that typechecks wins. But typechecking runs the TH splices; hence the behaviour you see.

It's debatable whether this is a bug. Up to now I have not attempted to guarantee that splices are run only once. It would certainly be possible to change GHCi to guarantee this. (I.e. a couple of hours, not a couple of days.) But I don't think we'll do it for the 6.6 branch, unless you yell really loudly; and the more people that say "this would really help me" the more likely it is that we'll do it on the HEAD.

So, if you are a GHC user who cares about this point, add a comment to this bug report. Thanks.

Simon

PS: why four times, not three? It's a rather clunky riff on the third item, to improve error messages.

Changed 6 years ago by guest

  • cc alfonso.acosta@… added

Changed 5 years ago by simonmar

  • architecture changed from Unknown to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Unknown to Unknown/Multiple

Changed 12 months ago by phurst

  • failure set to None/Unknown

This bit me while I was playing around with some random TH stuff in GHC; is this still an easy fix?

Changed 5 months ago by morabbin

Confirmed still happens in GHCi 7.6.1, but with a minor change to the splice:

$(do runIO $ (System.Random.randomIO :: IO Int) >>= print; [| 1 |] )

i.e., use System.Random not Random.

Given SPJ's comment above, close as wontifx?

Changed 5 months ago by simonpj

I'm inclined to leave it open in case someone feels inclined to fix it. From my comment, it probably isn't hard.

Simon

Note: See TracTickets for help on using tickets.