"stage restriction" when using Data.Heap

When I :load this into ghci import Data.Heap heap = empty :: MinHeap (Int,String) insert (1,"kjh") heap I get GHC stage restriction: `heap' is used in a top-level splice or annotation, and must be imported, not defined locally In the second argument of `insert', namely `heap' In the expression: insert (1, "kjh") heap But when I type in import Data.Heap let heap = empty :: MinHeap (Int,String) insert (1,"kjh") heap everything works and I get fromList [(1,"kjh")] Why is that so and what can I do to prevent the "stage restriction"? -- Martin

The GHC stage restriction is an error invoked when some Template Haskell
[1] construct requires the evaluation of run-time values at compile time. I
am going out on a limb and assuming that same code probably works in GHCI
because the interpreter does not have a discrete compile-time phase.
I assume that MinHeap is templated, but exactly how I do not know. Maybe
someone who is more familiar with Data.Heap will have an answer.
[1] http://www.haskell.org/haskellwiki/Template_Haskell
On Mon, Jan 7, 2013 at 10:03 PM, Martin Drautzburg wrote: When I :load this into ghci import Data.Heap heap = empty :: MinHeap (Int,String)
insert (1,"kjh") heap I get GHC stage restriction: `heap'
is used in a top-level splice or annotation,
and must be imported, not defined locally
In the second argument of `insert', namely `heap'
In the expression: insert (1, "kjh") heap But when I type in import Data.Heap let heap = empty :: MinHeap (Int,String)
insert (1,"kjh") heap everything works and I get fromList [(1,"kjh")] Why is that so and what can I do to prevent the "stage restriction"?
--
Martin _______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

On 08/01/13 07:03, Martin Drautzburg wrote:
import Data.Heap
heap = empty :: MinHeap (Int,String) insert (1,"kjh") heap
Your error is in the last line. `insert (1,"kjh") heap` is an expression, and in Haskell expressions can only occur inside (function) declarations. So you should instead write something like: heap = empty :: MinHeap (Int,String) heap2 = insert (1,"kjh") heap In Ghci, expressions are evaluated and the result is printed, which is why you are able to use it there. Twan

On Tue, Jan 08, 2013 at 07:03:20AM +0100, Martin Drautzburg wrote:
When I :load this into ghci
import Data.Heap
heap = empty :: MinHeap (Int,String) insert (1,"kjh") heap
I get
GHC stage restriction: `heap' is used in a top-level splice or annotation, and must be imported, not defined locally In the second argument of `insert', namely `heap' In the expression: insert (1, "kjh") heap
But when I type in
import Data.Heap
let heap = empty :: MinHeap (Int,String) insert (1,"kjh") heap
everything works and I get
fromList [(1,"kjh")]
Why is that so and what can I do to prevent the "stage restriction"?
Twan's analysis is correct. But wow, that's a horrible error message. Do you by any chance have the TemplateHaskell extension enabled (either with {-# LANGUAGE TemplateHaskell #-} at the top or your file, or with :set -XTemplateHaskell in a .ghci file, or anything like that)? If not this should really be filed as a bug, GHC has no business giving TH-related error messages without TH being turned on. -Brent

On Tuesday 08 January 2013, 12:02:45, Brent Yorgey wrote:
Twan's analysis is correct. But wow, that's a horrible error message. Do you by any chance have the TemplateHaskell extension enabled (either with {-# LANGUAGE TemplateHaskell #-} at the top or your file, or with :set -XTemplateHaskell in a .ghci file, or anything like that)?
Must be: module Naked where start :: Int start = 13 start + 5 ====== $ ghci Naked GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Naked ( Naked.hs, interpreted ) Naked.hs:6:1: Parse error: naked expression at top level Failed, modules loaded: none. ====== $ ghci -XTemplateHaskell Naked GHCi, version 7.6.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Naked ( Naked.hs, interpreted ) Naked.hs:6:1: GHC stage restriction: `start' is used in a top-level splice or annotation, and must be imported, not defined locally In the first argument of `(+)', namely `start' In the expression: start + 5 Failed, modules loaded: none. The message without TH is clear and good.
If not this should really be filed as a bug, GHC has no business giving TH-related error messages without TH being turned on.
-Brent
participants (5)
-
Brent Yorgey
-
Daniel Fischer
-
Darren Grant
-
Martin Drautzburg
-
Twan van Laarhoven