How can two continuations cancel each other out?What are Scala continuations and why use them?Why am I getting “Non-exhaustive patterns in function…” when I invoke my Haskell substring function?How can a time function exist in functional programming?how can I implement this monad transformer with a continuation?Scala continuations: many shifts in sequenceWhat optimizations can GHC be expected to perform reliably?How do continuations work in complex applications, like a web server or GUI app?Desugaring rule for case expression within a do block.Can switching in-and-out PyFrameObjects be a good implementation of continuations?Haskell foldl implementation with foldr

Biology of a Firestarter

How do I adjust encounters to challenge my lycanthrope players without negating their cool new abilities?

Is it safe to use two single-pole breakers for a 240 V circuit?

Single word that parallels "Recent" when discussing the near future

Would life always name the light from their sun "white"

How to add block near a product image in a product detail page in Magento 2

How to describe a building set which is like LEGO without using the "LEGO" word?

Can a tourist shoot a gun for recreational purpose in the USA?

Why is it harder to turn a motor/generator with shorted terminals?

How to cope with regret and shame about not fully utilizing opportunities during PhD?

Adding labels and comments to a matrix

When does the attacker choose the damage type dealt by a weapon with multiple damage options?

Extract the characters before last colon

How about space ziplines

How to rename a files in a directory

the grammar about `adv adv` as 'too quickly'

Will the volt, ampere, ohm or other electrical units change on May 20th, 2019?

Why does SSL Labs now consider CBC suites weak?

Problem in downloading videos using youtube-dl from unsupported sites

Is 95% of what you read in the financial press “either wrong or irrelevant?”

Why did the soldiers of the North disobey Jon?

Is this possible when it comes to the relations of P, NP, NP-Hard and NP-Complete?

Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?

What information exactly does an instruction cache store?



How can two continuations cancel each other out?


What are Scala continuations and why use them?Why am I getting “Non-exhaustive patterns in function…” when I invoke my Haskell substring function?How can a time function exist in functional programming?how can I implement this monad transformer with a continuation?Scala continuations: many shifts in sequenceWhat optimizations can GHC be expected to perform reliably?How do continuations work in complex applications, like a web server or GUI app?Desugaring rule for case expression within a do block.Can switching in-and-out PyFrameObjects be a good implementation of continuations?Haskell foldl implementation with foldr






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















I'm reading through Some Tricks for List Manipulation, and it contains the following:




zipRev xs ys = foldr f id xs snd (ys,[])
where
f x k c = k (((y:ys),r) -> c (ys,(x,y):r))


What we can see here is that we have two continuations stacked on top
of each other. When this happens, they can often “cancel out”, like
so:



zipRev xs ys = snd (foldr f (ys,[]) xs)
where
f x (y:ys,r) = (ys,(x,y):r)



I don't understand how you "cancel out" stacked continuations to get from the top code block to the bottom one. What pattern do you look for to make this transformation, and why does it work?










share|improve this question






















  • By the way: those versions of zipRev fail if the second list is shorter than the first. I believe the laziest possible version that works regardless of relative lengths is this one I just put together.

    – dfeuer
    1 hour ago

















7















I'm reading through Some Tricks for List Manipulation, and it contains the following:




zipRev xs ys = foldr f id xs snd (ys,[])
where
f x k c = k (((y:ys),r) -> c (ys,(x,y):r))


What we can see here is that we have two continuations stacked on top
of each other. When this happens, they can often “cancel out”, like
so:



zipRev xs ys = snd (foldr f (ys,[]) xs)
where
f x (y:ys,r) = (ys,(x,y):r)



I don't understand how you "cancel out" stacked continuations to get from the top code block to the bottom one. What pattern do you look for to make this transformation, and why does it work?










share|improve this question






















  • By the way: those versions of zipRev fail if the second list is shorter than the first. I believe the laziest possible version that works regardless of relative lengths is this one I just put together.

    – dfeuer
    1 hour ago













7












7








7








I'm reading through Some Tricks for List Manipulation, and it contains the following:




zipRev xs ys = foldr f id xs snd (ys,[])
where
f x k c = k (((y:ys),r) -> c (ys,(x,y):r))


What we can see here is that we have two continuations stacked on top
of each other. When this happens, they can often “cancel out”, like
so:



zipRev xs ys = snd (foldr f (ys,[]) xs)
where
f x (y:ys,r) = (ys,(x,y):r)



I don't understand how you "cancel out" stacked continuations to get from the top code block to the bottom one. What pattern do you look for to make this transformation, and why does it work?










share|improve this question














I'm reading through Some Tricks for List Manipulation, and it contains the following:




zipRev xs ys = foldr f id xs snd (ys,[])
where
f x k c = k (((y:ys),r) -> c (ys,(x,y):r))


What we can see here is that we have two continuations stacked on top
of each other. When this happens, they can often “cancel out”, like
so:



zipRev xs ys = snd (foldr f (ys,[]) xs)
where
f x (y:ys,r) = (ys,(x,y):r)



I don't understand how you "cancel out" stacked continuations to get from the top code block to the bottom one. What pattern do you look for to make this transformation, and why does it work?







haskell continuations continuation-passing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









Joseph SibleJoseph Sible

8,23831439




8,23831439












  • By the way: those versions of zipRev fail if the second list is shorter than the first. I believe the laziest possible version that works regardless of relative lengths is this one I just put together.

    – dfeuer
    1 hour ago

















  • By the way: those versions of zipRev fail if the second list is shorter than the first. I believe the laziest possible version that works regardless of relative lengths is this one I just put together.

    – dfeuer
    1 hour ago
















By the way: those versions of zipRev fail if the second list is shorter than the first. I believe the laziest possible version that works regardless of relative lengths is this one I just put together.

– dfeuer
1 hour ago





By the way: those versions of zipRev fail if the second list is shorter than the first. I believe the laziest possible version that works regardless of relative lengths is this one I just put together.

– dfeuer
1 hour ago












2 Answers
2






active

oldest

votes


















6














A function f :: a -> b can be "disguised" inside double continuations as a function f' :: ((a -> r1) -> r2) -> ((b -> r1) -> r2).



obfuscate :: (a -> b) -> ((a -> r1) -> r2) -> (b -> r1) -> r2
obfuscate f k2 k1 = k2 (k1 . f)


obfuscate has the nice property that it respects function composition: you can prove that obfuscate f . obfuscate g === obfuscate (f . g) in a few steps. That means that you can frequently use this transformation to untangle double-continuation computations that compose obfuscated functions together by factoring the obfuscate out of the composition. This question is an example of such an untangling.



The f in the top code block is the obfuscated version of the f in the bottom block (more precisely, top f x is the obfuscated version of bottom f x). You can see this by noticing how top f applies the outer continuation to a function that transforms its input and then applies the whole thing to the inner continuation, just like in the body of obfuscate.



So we can start to untangle zipRev:



zipRev xs ys = foldr f id xs snd (ys,[])
where
f x = obfuscate ((y:ys,r) -> (ys,(x,y):r))


Since the action of foldr here is to compose a bunch of obfuscated functions with each other (and with an rightmost id, which we can leave on the right), we can factor the obfuscate to the outside of the whole fold:



zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[])
where
f x (y:ys,r) = (ys,(x,y):r)


Now apply the definition of obfuscate and simplify:



zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[]) 
zipRev xs ys = id (snd . (accum -> foldr f accum xs)) (ys,[])
zipRev xs ys = snd (foldr f (ys,[]) xs)


QED!






share|improve this answer


















  • 2





    A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

    – duplode
    1 hour ago


















3
















Given a function



g :: a₁ -> a₂


we can lift it to a function on continuations, switching the order:



lift g = (c a₁ -> c (g a₁))
:: (a₂ -> t) -> a₁ -> t


This transformation is a contravariant functor, which is to say that it interacts with function composition by switching its order:



g₁ :: a₁ -> a₂
g₂ :: a₂ -> a₃

lift g₁ . lift g₂
== (c₁ a₁ -> c₁ (g₁ a₁)) . (c₂ a₂ -> c₂ (g₂ a₂))
== c₂ a₁ -> (a₂ -> c₂ (g₂ a₂)) (g₁ a₁)
== c₂ a₁ -> c₂ (g₂ (g₁ a₁))
== lift (g₂ . g₁)
:: (a₃ -> t) -> a₁ -> t

lift id
== (c a₁ -> c a₁)
== id
:: (a₁ -> t) -> a₁ -> t


We can lift the lifted function again in the same way to a function on stacked continuations, with the order switched back:



lift (lift g)
== (k c -> k ((c a₁ -> c (g a₁)) c))
== (k c -> k (a₁ -> c (g a₁)))
:: ((a₁ -> t) -> u) -> (a₂ -> t) -> u


Stacking two contravariant functors gives us a (covariant) functor:



lift (lift g₁) . lift (lift g₂)
== lift (lift g₂ . lift g₁)
== lift (lift (g₁ . g₂))
:: ((a₁ -> t) -> u) -> (a₃ -> t) -> u

lift (lift id)
== lift id
== id
:: ((a₁ -> t) -> u) -> (a₁ -> t) -> u


This is exactly the transformation being reversed in your example, with g = (y:ys, r) -> (ys, (x, y):r). This g is an endomorphism (a₁ = a₂), and the foldr is composing together a bunch of copies of it with various x. What we’re doing is replacing the composition of double-lifted functions with the double-lift of the composition of the functions, which is just an inductive application of the functor laws:



f :: x -> a₁ -> a₁
c :: (a₁ -> t) -> u
xs :: [x]

foldr (x -> lift (lift (f x))) c xs
== lift (lift (a₁ -> foldr f a₁ xs)) c
:: (a₁ -> t) -> u





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56122022%2fhow-can-two-continuations-cancel-each-other-out%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    A function f :: a -> b can be "disguised" inside double continuations as a function f' :: ((a -> r1) -> r2) -> ((b -> r1) -> r2).



    obfuscate :: (a -> b) -> ((a -> r1) -> r2) -> (b -> r1) -> r2
    obfuscate f k2 k1 = k2 (k1 . f)


    obfuscate has the nice property that it respects function composition: you can prove that obfuscate f . obfuscate g === obfuscate (f . g) in a few steps. That means that you can frequently use this transformation to untangle double-continuation computations that compose obfuscated functions together by factoring the obfuscate out of the composition. This question is an example of such an untangling.



    The f in the top code block is the obfuscated version of the f in the bottom block (more precisely, top f x is the obfuscated version of bottom f x). You can see this by noticing how top f applies the outer continuation to a function that transforms its input and then applies the whole thing to the inner continuation, just like in the body of obfuscate.



    So we can start to untangle zipRev:



    zipRev xs ys = foldr f id xs snd (ys,[])
    where
    f x = obfuscate ((y:ys,r) -> (ys,(x,y):r))


    Since the action of foldr here is to compose a bunch of obfuscated functions with each other (and with an rightmost id, which we can leave on the right), we can factor the obfuscate to the outside of the whole fold:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[])
    where
    f x (y:ys,r) = (ys,(x,y):r)


    Now apply the definition of obfuscate and simplify:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[]) 
    zipRev xs ys = id (snd . (accum -> foldr f accum xs)) (ys,[])
    zipRev xs ys = snd (foldr f (ys,[]) xs)


    QED!






    share|improve this answer


















    • 2





      A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

      – duplode
      1 hour ago















    6














    A function f :: a -> b can be "disguised" inside double continuations as a function f' :: ((a -> r1) -> r2) -> ((b -> r1) -> r2).



    obfuscate :: (a -> b) -> ((a -> r1) -> r2) -> (b -> r1) -> r2
    obfuscate f k2 k1 = k2 (k1 . f)


    obfuscate has the nice property that it respects function composition: you can prove that obfuscate f . obfuscate g === obfuscate (f . g) in a few steps. That means that you can frequently use this transformation to untangle double-continuation computations that compose obfuscated functions together by factoring the obfuscate out of the composition. This question is an example of such an untangling.



    The f in the top code block is the obfuscated version of the f in the bottom block (more precisely, top f x is the obfuscated version of bottom f x). You can see this by noticing how top f applies the outer continuation to a function that transforms its input and then applies the whole thing to the inner continuation, just like in the body of obfuscate.



    So we can start to untangle zipRev:



    zipRev xs ys = foldr f id xs snd (ys,[])
    where
    f x = obfuscate ((y:ys,r) -> (ys,(x,y):r))


    Since the action of foldr here is to compose a bunch of obfuscated functions with each other (and with an rightmost id, which we can leave on the right), we can factor the obfuscate to the outside of the whole fold:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[])
    where
    f x (y:ys,r) = (ys,(x,y):r)


    Now apply the definition of obfuscate and simplify:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[]) 
    zipRev xs ys = id (snd . (accum -> foldr f accum xs)) (ys,[])
    zipRev xs ys = snd (foldr f (ys,[]) xs)


    QED!






    share|improve this answer


















    • 2





      A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

      – duplode
      1 hour ago













    6












    6








    6







    A function f :: a -> b can be "disguised" inside double continuations as a function f' :: ((a -> r1) -> r2) -> ((b -> r1) -> r2).



    obfuscate :: (a -> b) -> ((a -> r1) -> r2) -> (b -> r1) -> r2
    obfuscate f k2 k1 = k2 (k1 . f)


    obfuscate has the nice property that it respects function composition: you can prove that obfuscate f . obfuscate g === obfuscate (f . g) in a few steps. That means that you can frequently use this transformation to untangle double-continuation computations that compose obfuscated functions together by factoring the obfuscate out of the composition. This question is an example of such an untangling.



    The f in the top code block is the obfuscated version of the f in the bottom block (more precisely, top f x is the obfuscated version of bottom f x). You can see this by noticing how top f applies the outer continuation to a function that transforms its input and then applies the whole thing to the inner continuation, just like in the body of obfuscate.



    So we can start to untangle zipRev:



    zipRev xs ys = foldr f id xs snd (ys,[])
    where
    f x = obfuscate ((y:ys,r) -> (ys,(x,y):r))


    Since the action of foldr here is to compose a bunch of obfuscated functions with each other (and with an rightmost id, which we can leave on the right), we can factor the obfuscate to the outside of the whole fold:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[])
    where
    f x (y:ys,r) = (ys,(x,y):r)


    Now apply the definition of obfuscate and simplify:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[]) 
    zipRev xs ys = id (snd . (accum -> foldr f accum xs)) (ys,[])
    zipRev xs ys = snd (foldr f (ys,[]) xs)


    QED!






    share|improve this answer













    A function f :: a -> b can be "disguised" inside double continuations as a function f' :: ((a -> r1) -> r2) -> ((b -> r1) -> r2).



    obfuscate :: (a -> b) -> ((a -> r1) -> r2) -> (b -> r1) -> r2
    obfuscate f k2 k1 = k2 (k1 . f)


    obfuscate has the nice property that it respects function composition: you can prove that obfuscate f . obfuscate g === obfuscate (f . g) in a few steps. That means that you can frequently use this transformation to untangle double-continuation computations that compose obfuscated functions together by factoring the obfuscate out of the composition. This question is an example of such an untangling.



    The f in the top code block is the obfuscated version of the f in the bottom block (more precisely, top f x is the obfuscated version of bottom f x). You can see this by noticing how top f applies the outer continuation to a function that transforms its input and then applies the whole thing to the inner continuation, just like in the body of obfuscate.



    So we can start to untangle zipRev:



    zipRev xs ys = foldr f id xs snd (ys,[])
    where
    f x = obfuscate ((y:ys,r) -> (ys,(x,y):r))


    Since the action of foldr here is to compose a bunch of obfuscated functions with each other (and with an rightmost id, which we can leave on the right), we can factor the obfuscate to the outside of the whole fold:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[])
    where
    f x (y:ys,r) = (ys,(x,y):r)


    Now apply the definition of obfuscate and simplify:



    zipRev xs ys = obfuscate (accum -> foldr f accum xs) id snd (ys,[]) 
    zipRev xs ys = id (snd . (accum -> foldr f accum xs)) (ys,[])
    zipRev xs ys = snd (foldr f (ys,[]) xs)


    QED!







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 1 hour ago









    user11228628user11228628

    76416




    76416







    • 2





      A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

      – duplode
      1 hour ago












    • 2





      A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

      – duplode
      1 hour ago







    2




    2





    A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

    – duplode
    1 hour ago





    A footnote: for an immediate proof that obfuscate respects function composition, we just have to notice it is fmap for the double continuation type.

    – duplode
    1 hour ago













    3
















    Given a function



    g :: a₁ -> a₂


    we can lift it to a function on continuations, switching the order:



    lift g = (c a₁ -> c (g a₁))
    :: (a₂ -> t) -> a₁ -> t


    This transformation is a contravariant functor, which is to say that it interacts with function composition by switching its order:



    g₁ :: a₁ -> a₂
    g₂ :: a₂ -> a₃

    lift g₁ . lift g₂
    == (c₁ a₁ -> c₁ (g₁ a₁)) . (c₂ a₂ -> c₂ (g₂ a₂))
    == c₂ a₁ -> (a₂ -> c₂ (g₂ a₂)) (g₁ a₁)
    == c₂ a₁ -> c₂ (g₂ (g₁ a₁))
    == lift (g₂ . g₁)
    :: (a₃ -> t) -> a₁ -> t

    lift id
    == (c a₁ -> c a₁)
    == id
    :: (a₁ -> t) -> a₁ -> t


    We can lift the lifted function again in the same way to a function on stacked continuations, with the order switched back:



    lift (lift g)
    == (k c -> k ((c a₁ -> c (g a₁)) c))
    == (k c -> k (a₁ -> c (g a₁)))
    :: ((a₁ -> t) -> u) -> (a₂ -> t) -> u


    Stacking two contravariant functors gives us a (covariant) functor:



    lift (lift g₁) . lift (lift g₂)
    == lift (lift g₂ . lift g₁)
    == lift (lift (g₁ . g₂))
    :: ((a₁ -> t) -> u) -> (a₃ -> t) -> u

    lift (lift id)
    == lift id
    == id
    :: ((a₁ -> t) -> u) -> (a₁ -> t) -> u


    This is exactly the transformation being reversed in your example, with g = (y:ys, r) -> (ys, (x, y):r). This g is an endomorphism (a₁ = a₂), and the foldr is composing together a bunch of copies of it with various x. What we’re doing is replacing the composition of double-lifted functions with the double-lift of the composition of the functions, which is just an inductive application of the functor laws:



    f :: x -> a₁ -> a₁
    c :: (a₁ -> t) -> u
    xs :: [x]

    foldr (x -> lift (lift (f x))) c xs
    == lift (lift (a₁ -> foldr f a₁ xs)) c
    :: (a₁ -> t) -> u





    share|improve this answer





























      3
















      Given a function



      g :: a₁ -> a₂


      we can lift it to a function on continuations, switching the order:



      lift g = (c a₁ -> c (g a₁))
      :: (a₂ -> t) -> a₁ -> t


      This transformation is a contravariant functor, which is to say that it interacts with function composition by switching its order:



      g₁ :: a₁ -> a₂
      g₂ :: a₂ -> a₃

      lift g₁ . lift g₂
      == (c₁ a₁ -> c₁ (g₁ a₁)) . (c₂ a₂ -> c₂ (g₂ a₂))
      == c₂ a₁ -> (a₂ -> c₂ (g₂ a₂)) (g₁ a₁)
      == c₂ a₁ -> c₂ (g₂ (g₁ a₁))
      == lift (g₂ . g₁)
      :: (a₃ -> t) -> a₁ -> t

      lift id
      == (c a₁ -> c a₁)
      == id
      :: (a₁ -> t) -> a₁ -> t


      We can lift the lifted function again in the same way to a function on stacked continuations, with the order switched back:



      lift (lift g)
      == (k c -> k ((c a₁ -> c (g a₁)) c))
      == (k c -> k (a₁ -> c (g a₁)))
      :: ((a₁ -> t) -> u) -> (a₂ -> t) -> u


      Stacking two contravariant functors gives us a (covariant) functor:



      lift (lift g₁) . lift (lift g₂)
      == lift (lift g₂ . lift g₁)
      == lift (lift (g₁ . g₂))
      :: ((a₁ -> t) -> u) -> (a₃ -> t) -> u

      lift (lift id)
      == lift id
      == id
      :: ((a₁ -> t) -> u) -> (a₁ -> t) -> u


      This is exactly the transformation being reversed in your example, with g = (y:ys, r) -> (ys, (x, y):r). This g is an endomorphism (a₁ = a₂), and the foldr is composing together a bunch of copies of it with various x. What we’re doing is replacing the composition of double-lifted functions with the double-lift of the composition of the functions, which is just an inductive application of the functor laws:



      f :: x -> a₁ -> a₁
      c :: (a₁ -> t) -> u
      xs :: [x]

      foldr (x -> lift (lift (f x))) c xs
      == lift (lift (a₁ -> foldr f a₁ xs)) c
      :: (a₁ -> t) -> u





      share|improve this answer



























        3












        3








        3









        Given a function



        g :: a₁ -> a₂


        we can lift it to a function on continuations, switching the order:



        lift g = (c a₁ -> c (g a₁))
        :: (a₂ -> t) -> a₁ -> t


        This transformation is a contravariant functor, which is to say that it interacts with function composition by switching its order:



        g₁ :: a₁ -> a₂
        g₂ :: a₂ -> a₃

        lift g₁ . lift g₂
        == (c₁ a₁ -> c₁ (g₁ a₁)) . (c₂ a₂ -> c₂ (g₂ a₂))
        == c₂ a₁ -> (a₂ -> c₂ (g₂ a₂)) (g₁ a₁)
        == c₂ a₁ -> c₂ (g₂ (g₁ a₁))
        == lift (g₂ . g₁)
        :: (a₃ -> t) -> a₁ -> t

        lift id
        == (c a₁ -> c a₁)
        == id
        :: (a₁ -> t) -> a₁ -> t


        We can lift the lifted function again in the same way to a function on stacked continuations, with the order switched back:



        lift (lift g)
        == (k c -> k ((c a₁ -> c (g a₁)) c))
        == (k c -> k (a₁ -> c (g a₁)))
        :: ((a₁ -> t) -> u) -> (a₂ -> t) -> u


        Stacking two contravariant functors gives us a (covariant) functor:



        lift (lift g₁) . lift (lift g₂)
        == lift (lift g₂ . lift g₁)
        == lift (lift (g₁ . g₂))
        :: ((a₁ -> t) -> u) -> (a₃ -> t) -> u

        lift (lift id)
        == lift id
        == id
        :: ((a₁ -> t) -> u) -> (a₁ -> t) -> u


        This is exactly the transformation being reversed in your example, with g = (y:ys, r) -> (ys, (x, y):r). This g is an endomorphism (a₁ = a₂), and the foldr is composing together a bunch of copies of it with various x. What we’re doing is replacing the composition of double-lifted functions with the double-lift of the composition of the functions, which is just an inductive application of the functor laws:



        f :: x -> a₁ -> a₁
        c :: (a₁ -> t) -> u
        xs :: [x]

        foldr (x -> lift (lift (f x))) c xs
        == lift (lift (a₁ -> foldr f a₁ xs)) c
        :: (a₁ -> t) -> u





        share|improve this answer

















        Given a function



        g :: a₁ -> a₂


        we can lift it to a function on continuations, switching the order:



        lift g = (c a₁ -> c (g a₁))
        :: (a₂ -> t) -> a₁ -> t


        This transformation is a contravariant functor, which is to say that it interacts with function composition by switching its order:



        g₁ :: a₁ -> a₂
        g₂ :: a₂ -> a₃

        lift g₁ . lift g₂
        == (c₁ a₁ -> c₁ (g₁ a₁)) . (c₂ a₂ -> c₂ (g₂ a₂))
        == c₂ a₁ -> (a₂ -> c₂ (g₂ a₂)) (g₁ a₁)
        == c₂ a₁ -> c₂ (g₂ (g₁ a₁))
        == lift (g₂ . g₁)
        :: (a₃ -> t) -> a₁ -> t

        lift id
        == (c a₁ -> c a₁)
        == id
        :: (a₁ -> t) -> a₁ -> t


        We can lift the lifted function again in the same way to a function on stacked continuations, with the order switched back:



        lift (lift g)
        == (k c -> k ((c a₁ -> c (g a₁)) c))
        == (k c -> k (a₁ -> c (g a₁)))
        :: ((a₁ -> t) -> u) -> (a₂ -> t) -> u


        Stacking two contravariant functors gives us a (covariant) functor:



        lift (lift g₁) . lift (lift g₂)
        == lift (lift g₂ . lift g₁)
        == lift (lift (g₁ . g₂))
        :: ((a₁ -> t) -> u) -> (a₃ -> t) -> u

        lift (lift id)
        == lift id
        == id
        :: ((a₁ -> t) -> u) -> (a₁ -> t) -> u


        This is exactly the transformation being reversed in your example, with g = (y:ys, r) -> (ys, (x, y):r). This g is an endomorphism (a₁ = a₂), and the foldr is composing together a bunch of copies of it with various x. What we’re doing is replacing the composition of double-lifted functions with the double-lift of the composition of the functions, which is just an inductive application of the functor laws:



        f :: x -> a₁ -> a₁
        c :: (a₁ -> t) -> u
        xs :: [x]

        foldr (x -> lift (lift (f x))) c xs
        == lift (lift (a₁ -> foldr f a₁ xs)) c
        :: (a₁ -> t) -> u






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 1 hour ago

























        answered 1 hour ago









        Anders KaseorgAnders Kaseorg

        1,8301018




        1,8301018



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56122022%2fhow-can-two-continuations-cancel-each-other-out%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її