GHC | 20 Aug 2012 15:52
Favicon

#7168: Loop

#7168: Loop
-----------------------------------------+----------------------------------
 Reporter:  bas                          |          Owner:                                  
     Type:  bug                          |         Status:  new                             
 Priority:  normal                       |      Component:  Compiler                        
  Version:  7.4.1                        |       Keywords:  Lazy evaluation, Loop, Eagerness
       Os:  Unknown/Multiple             |   Architecture:  x86                             
  Failure:  Incorrect result at runtime  |       Testcase:                                  
Blockedby:                               |       Blocking:                                  
  Related:                               |  
-----------------------------------------+----------------------------------
 When patternmatching on (a,b), the variables a and b are sometimes
 evaluated eagerly, though they should not be. To see where this occurs,
 please try to evaluate f :

 {{{
 f = (1,error "this will not be evaluated!") .++. carefully f
 (a,b) .++. (c,d) = (a+c,0)
 carefully (a,b) = (1, a + b)
 }}}

 Note that the expected result here is (2,0). My workaround: the program
 works correctly if the last line is replaced by:

 {{{
 carefully x = (1, fst x + snd x)
 }}}

--

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/7168>
(Continue reading)

GHC | 20 Aug 2012 16:11
Favicon

Re: #7168: Loop

#7168: Loop
-------------------------------------------------+--------------------------
    Reporter:  bas                               |        Owner:                             
        Type:  bug                               |       Status:  closed                     
    Priority:  normal                            |    Component:  Compiler                   
     Version:  7.4.1                             |   Resolution:  invalid                    
    Keywords:  Lazy evaluation, Loop, Eagerness  |           Os:  Unknown/Multiple           
Architecture:  x86                               |      Failure:  Incorrect result at runtime
    Testcase:                                    |    Blockedby:                             
    Blocking:                                    |      Related:                             
-------------------------------------------------+--------------------------
Changes (by ross):

  * status:  new => closed
  * resolution:  => invalid

Comment:

 It's not that a and b are evaluated, but the pairs are, precisely as
 specified in the semantics of pattern matching.  The following does the
 same:
 {{{
 f = (1,8) .++. carefully f
 (a,b) .++. (c,d) = (1,0)
 carefully (a,b) = (1,2)
 }}}

--

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/7168#comment:1>
(Continue reading)

GHC | 20 Aug 2012 16:25
Favicon

Re: #7168: Loop

#7168: Loop
------------------------------------------+---------------------------------
  Reporter:  bas                          |          Owner:                                  
      Type:  bug                          |         Status:  closed                          
  Priority:  normal                       |      Milestone:                                  
 Component:  Compiler                     |        Version:  7.4.1                           
Resolution:  invalid                      |       Keywords:  Lazy evaluation, Loop, Eagerness
        Os:  Unknown/Multiple             |   Architecture:  x86                             
   Failure:  Incorrect result at runtime  |     Difficulty:  Unknown                         
  Testcase:                               |      Blockedby:                                  
  Blocking:                               |        Related:                                  
------------------------------------------+---------------------------------
Changes (by pcapriotti):

  * difficulty:  => Unknown

Comment:

 To get the semantics you expect, use lazy pattern matching:

 {{{
 carefully ~(a,b) = (1, a + b)
 }}}

--

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/7168#comment:2>

Gmane