Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:14:21+00:00 2026-06-13T03:14:21+00:00

Suppose for a minute that we think the following is a good idea: data

  • 0

Suppose for a minute that we think the following is a good idea:

data Fold x y = Fold {start :: y, step :: x -> y -> y}

fold :: Fold x y -> [x] -> y

Under this scheme, functions such as length or sum can be implemented by calling fold with the appropriate Fold object as argument.

Now, suppose you want to do clever optimisation tricks. In particular, suppose you want to write

unFold :: ([x] -> y) -> Fold x y

It should be relatively easy to rule a RULES pragma such that fold . unFold = id. But the interesting question is… can we actually implement unFold?

Obviously you can use RULES to apply arbitrary code transformations, whether or not they preserve the original meaning of the code. But can you really write an unFold implementation which actually does what its type signature suggests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:14:22+00:00Added an answer on June 13, 2026 at 3:14 am

    You can, but you need to make a slight modification to Fold in order to pull it off.

    All functions on lists can be expressed as a fold, but sometimes to accomplish this, extra bookkeeping is needed. Suppose we add an additional type parameter to your Fold type, which passes along this additional contextual information.

    data Fold a c r = Fold { _start :: (c, r), _step :: a -> (c,r) -> (c,r) }
    

    Now we can implement fold like so

    fold :: Fold a c r -> [a] -> r
    fold (Fold step start) = snd . foldr step start
    

    Now what happens when we try to go the other way?

    unFold :: ([a] -> r) -> Fold a c r
    

    Where does the c come from? Functions are opaque values, so it’s hard to know how to inspect a function and know which contextual information it relies on. So, let’s cheat a little. We’re going to have the “contextual information” be the entire list, so then when we get to the leftmost element, we can just apply the function to the original list, ignoring the prior cumulative results.

    unFold :: ([a] -> r) -> Fold a [a] r
    unFold f = Fold { _start = ([], f [])
                    , _step = \a (c, _r) -> let c' = a:c in (c', f c') }
    

    Now, sadly, this does not necessarily compose with fold, because it requires that c must be [a]. Let’s fix that by hiding c with existential quantification.

    {-# LANGUAGE ExistentialQuantification #-}
    data Fold a r = forall c. Fold
      { _start :: (c,r)
      , _step :: a -> (c,r) -> (c,r) }
    
    fold :: Fold a r -> [a] -> r
    fold (Fold start step) = snd . foldr step start
    
    unFold :: ([a] -> r) -> Fold a r
    unFold f = Fold start step where
      start = ([], f [])
      step a (c, _r) = let c' = a:c in (c', f c')
    

    Now, it should always be true that fold . unFold = id. And, given a relaxed notion of equality for the Fold data type, you could also say that unFold . fold = id. You can even provide a smart constructor that acts like the old Fold constructor:

    makeFold :: r -> (a -> r -> r) -> Fold a r
    makeFold start step = Fold start' step' where
      start' = ((), start)
      step' a ((), r) = ((), step a r)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service that updates some data every minute. When I change the
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a process that is updating a record and encounters a record
I'm running a loop in javascript that should take about a minute to complete.
Suppose I create a file for writing like this: std::ofstream my_file(filename, std::ios_base::out | std::ios_base::trunc);
We've got a reasonable sized C++ application that's pretty old at this stage, so
Please note that this is asking a question about constructors, not about classes which
Suppose I have the following code: List<SomeObject> someObjects = ReturnListWithThousandsOfObjects(); foreach(SomeObject someobject in someObjects)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.