[Haskell-cafe] Short Cut / Rewrite Rules Problem in GHC

Roberto Zunino roberto.zunino at sns.it
Tue Oct 4 00:30:57 EDT 2005

(moved to glasgow-haskell-users, hoping it's more appropriate)

Chris wrote:

> Hi All. I am a student and a noob to Haskell. I am having some  
> trouble with an example from the paper "Playing by the rules:  
> Rewriting as a practical optimisation technique in GHC" by Simon  
> Peyton Jones, Andrew Tolmach and Tony Hoare, specifically, the Short- 
> cut Deforestation example in section 3.1. I was trying to compile the  
> following using GHC version 6.4 on Mac OS X 10.4. The definition for  
> build and the rule are from the paper (the rule also appears in the  
> GHC online doc in section 7.10.1). 


>  build :: (forall b. (a->b->b) -> b -> b) -> [a]
>  build' g = g (:) []

(Unrelated: there is an extra ' above)

>  {-# RULES
>  "foldr/build"
>    forall k z (g::forall b. (a->b->b) -> b -> b) .
>    foldr k z (build g) = g k z
>  #-}

Beware of "#" at the beginning of line!
Try putting a space before the #.

My wild guess of what is causing the lexer error: as C compilers, GHC 
seems to read

# 12345 "foo"

as meaning "pretend that this is line 12345 in file foo". This is 
commonly used by automatic code generators (e.g. yacc) to force the 
compiler to emit error messages pointing to the human-written source 
(the file passed to yacc) and not to the generated code. This happens 
only if the # is the first character of the line.

( I do not dare to suggest modifying ghc/compiler/parser/Lexer.x as in

<bol> {
  \n					;
  "#-}"					{ token ITclose_prag }
  ^\# (line)?				{ begin line_prag1 }

... so I'm _not_ suggesting it! ;-) )

Roberto Zunino.

More information about the Glasgow-haskell-users mailing list