Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
GHC
GHC
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,327
    • Issues 4,327
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge Requests 390
    • Merge Requests 390
  • Requirements
    • Requirements
    • List
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Security & Compliance
    • Security & Compliance
    • Dependency List
    • License Compliance
  • Operations
    • Operations
    • Incidents
    • Environments
  • Analytics
    • Analytics
    • CI / CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Collapse sidebar
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #4978

Closed
Open
Opened Feb 24, 2011 by tibbe@trac-tibbe

Continuation passing style loop doesn't compile into a loop

I was investigating some poor performance in Data.Binary.Builder from the binary package. I boiled it down to GHC not turning a loop, expressed in CPS, into tail recursive function.

Here's the test code:

-- Simplification of a problem spotted in Data.Binary.Builder
module Repro (test) where

-- A builder that carries around one 'Int' worth of state.
newtype Builder = Builder { runBuilder ::  (Int -> Int) -> Int -> Int }

empty = Builder id
append (Builder f) (Builder g) = Builder (f . g)
add i = Builder $ \ k n -> k (n + i)
run b = runBuilder b id 0    

loop :: [Int] -> Builder
loop [] = empty
loop (x:xs) = add 1 `append` loop xs
    
test :: Int
test = run (loop [1..100])

Here's the (cleaned up) core:

test4 :: [Int] -> (Int -> Int) -> Int -> Int
test4 =
  \ (ys :: [Int])
    (k :: Int -> Int) ->
    case ys of _ {
      [] -> k;
      : x xs ->
        let {
          k2 :: Int -> Int
          k2 = test4 xs k } in
        \ (n_abz :: Int) ->
          k2
            (case n_abz of _ { I# x# ->
             I# (+# x# 1)
             })
    }

test3 :: [Int]
test3 = eftInt 1 100

test2 :: Int -> Int
test2 = test4 test3 (id @ Int)

test1 :: Int
test1 = I# 0

test :: Int
test = test2 test1

Note how test4 allocates a continuation it uses to call itself. Perhaps it could instead SAT the original continuation.

Trac metadata
Trac field Value
Version 7.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
Assignee
Assign to
7.2.1
Milestone
7.2.1
Assign milestone
Time tracking
None
Due date
None
Reference: ghc/ghc#4978