conversion from callcc to continuation passing style cps
play

Conversion from callcc to Continuation Passing Style (CPS) Tom - PowerPoint PPT Presentation

Conversion from callcc to Continuation Passing Style (CPS) Tom Nikken Based on slides from Xavier Leroy, INRIA Type Theory and Coq May 8th 2018 Tom Nikken Conversion from callcc to CPS 1 / 16 Conversion to continuation-passing style (CPS)


  1. Conversion from callcc to Continuation Passing Style (CPS) Tom Nikken Based on slides from Xavier Leroy, INRIA Type Theory and Coq May 8th 2018 Tom Nikken Conversion from callcc to CPS 1 / 16

  2. Conversion to continuation-passing style (CPS) Goal: make explicit the handling of continuations. Input: a call-by-value functional language with callcc . Output: a pure functional language (no callcc ). Uses: compilation of callcc , semantics, programming with continuations in Scheme, Haskell, ... Tom Nikken Conversion from callcc to CPS 2 / 16

  3. Illustration of CPS in Javascript We need to save the continuation, in case we encounter a callcc Idea: turn everything into a function from its continuation to its value function id(x) { return x; } Tom Nikken Conversion from callcc to CPS 3 / 16

  4. Illustration of CPS in Javascript We need to save the continuation, in case we encounter a callcc Idea: turn everything into a function from its continuation to its value function id(x, cont) { cont(x); } Tom Nikken Conversion from callcc to CPS 3 / 16

  5. Illustration of CPS in Javascript function fact(n,cont) { if (n == 0) cont (1) ; else fact(n-1, function (t0) { cont(n * t0) }) ; } Tom Nikken Conversion from callcc to CPS 4 / 16

  6. Illustration of CPS in Javascript function fact(n,cont) { if (n == 0) cont (1) ; else fact(n-1, function (t0) { cont(n * t0) }) ; } fact (5, function (n) { console.log(n) ; }) Tom Nikken Conversion from callcc to CPS 4 / 16

  7. Now in the lambda calculus Tom Nikken Conversion from callcc to CPS 5 / 16

  8. Now in the lambda calculus � ... � : ( λ + callcc ) → λ If a : τ , then � a � : ( � τ � → answer ) → answer , where: � τ � = τ for base types and � τ 1 → τ 2 � = � τ 1 � → ( � τ 2 � → answer ) → answer � N � = λ k . k N � x � = λ k . k x � λ x . a � = λ k . k ( λ x . � a � ) � f a � = λ k . � f � ( λ v f . � a � ( λ v a . v f v a k )) � callcc f � = λ k . � f � ( λ v f . v f k k ) � throw a b � = λ k . � a � ( λ v a . � b � ( λ v b . v a v b )) Tom Nikken Conversion from callcc to CPS 5 / 16

  9. Now in the lambda calculus � ... � : ( λ + callcc ) → λ If a : τ , then � a � : ( � τ � → answer ) → answer , where: � τ � = τ for base types and � τ 1 → τ 2 � = � τ 1 � → ( � τ 2 � → answer ) → answer � N � = λ k . k N � x � = λ k . k x � λ x . a � = λ k . k ( λ x . � a � ) � f a � = λ k . � f � ( λ v f . � a � ( λ v a . v f v a k )) � callcc f � = λ k . � f � ( λ v f . v f k k ) � throw a b � = λ k . � a � ( λ v a . � b � ( λ v b . v a v b )) Tom Nikken Conversion from callcc to CPS 5 / 16

  10. Remarks callcc conversion duplicates k : one as argument to f and one to propagate the current continuation. Throw ignores k . Argument of application is always a value, so works for CBV and CBN. Tom Nikken Conversion from callcc to CPS 6 / 16

  11. Exercise � f x � = β ? Tom Nikken Conversion from callcc to CPS 7 / 16

  12. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) Tom Nikken Conversion from callcc to CPS 8 / 16

  13. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) = λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ k 2 . k 2 x ) ( λ v b . v a v b k )) Tom Nikken Conversion from callcc to CPS 8 / 16

  14. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) = λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ k 2 . k 2 x ) ( λ v b . v a v b k )) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ v b . v a v b k ) x ) Tom Nikken Conversion from callcc to CPS 8 / 16

  15. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) = λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ k 2 . k 2 x ) ( λ v b . v a v b k )) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ v b . v a v b k ) x ) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . v a x k ) Tom Nikken Conversion from callcc to CPS 8 / 16

  16. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) = λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ k 2 . k 2 x ) ( λ v b . v a v b k )) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ v b . v a v b k ) x ) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . v a x k ) → β λ k . ( λ v a . v a x k ) f Tom Nikken Conversion from callcc to CPS 8 / 16

  17. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) = λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ k 2 . k 2 x ) ( λ v b . v a v b k )) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ v b . v a v b k ) x ) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . v a x k ) → β λ k . ( λ v a . v a x k ) f → β λ k . f x k Tom Nikken Conversion from callcc to CPS 8 / 16

  18. Exercise � f x � = λ k . � f � ( λ v a . � x � ( λ v b . v a v b k )) = λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ k 2 . k 2 x ) ( λ v b . v a v b k )) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . ( λ v b . v a v b k ) x ) → β λ k . ( λ k 1 . k 1 f ) ( λ v a . v a x k ) → β λ k . ( λ v a . v a x k ) f → β λ k . f x k Perform β reductions at transformation time to eliminate the “administrative redexes” introduced by the translation. Tom Nikken Conversion from callcc to CPS 8 / 16

  19. Example To show the effect of the translation. � f ( f x ) � = β λ k . f x ( λ v . f v k ) Tom Nikken Conversion from callcc to CPS 9 / 16

  20. Example To show the effect of the translation. � f ( f x ) � = β λ k . f x ( λ v . f v k ) � µ fact . λ n . if n = 0 then 1 else n ∗ fact ( n − 1) � = β λ k 0 . k 0 ( µ fact . λ n . λ k . if n = 0 then k 1 else fact ( n − 1)( λ v . k ( n ∗ v ))) Tom Nikken Conversion from callcc to CPS 9 / 16

  21. Example To show the effect of the translation. � f ( f x ) � = β λ k . f x ( λ v . f v k ) � µ fact . λ n . if n = 0 then 1 else n ∗ fact ( n − 1) � = β λ k 0 . k 0 ( µ fact . λ n . λ k . if n = 0 then k 1 else fact ( n − 1)( λ v . k ( n ∗ v ))) � callcc ( λ k . a ) � = β λ k . � a � k Tom Nikken Conversion from callcc to CPS 9 / 16

  22. Example To show the effect of the translation. � f ( f x ) � = β λ k . f x ( λ v . f v k ) � µ fact . λ n . if n = 0 then 1 else n ∗ fact ( n − 1) � = β λ k 0 . k 0 ( µ fact . λ n . λ k . if n = 0 then k 1 else fact ( n − 1)( λ v . k ( n ∗ v ))) � callcc ( λ k . a ) � = β λ k . � a � k Tom Nikken Conversion from callcc to CPS 9 / 16

  23. Still confused? The types can help. � N � = λ k . k N � N � : Tom Nikken Conversion from callcc to CPS 10 / 16

  24. Still confused? The types can help. � N � = λ k . k N � N � : ( nat → answer ) → answer k : Tom Nikken Conversion from callcc to CPS 10 / 16

  25. Still confused? The types can help. � N � = λ k . k N � N � : ( nat → answer ) → answer k : nat → answer Tom Nikken Conversion from callcc to CPS 10 / 16

  26. Still confused? � λ x . a � = λ k . k ( λ x . � a � ) � λ x . a � : ( � x → a � → answer ) → answer � a � : ( � a � → answer ) → answer Tom Nikken Conversion from callcc to CPS 11 / 16

  27. Still confused? � λ x . a � = λ k . k ( λ x . � a � ) � λ x . a � : ( � x → a � → answer ) → answer � a � : ( � a � → answer ) → answer k : � x → a � → answer Tom Nikken Conversion from callcc to CPS 11 / 16

  28. Still confused? � λ x . a � = λ k . k ( λ x . � a � ) � λ x . a � : ( � x → a � → answer ) → answer � a � : ( � a � → answer ) → answer k : � x → a � → answer � x → a � = � x � → ( � a � → answer ) → answer Tom Nikken Conversion from callcc to CPS 11 / 16

  29. Still confused? � λ x . a � = λ k . k ( λ x . � a � ) � λ x . a � : ( � x → a � → answer ) → answer � a � : ( � a � → answer ) → answer k : � x → a � → answer � x → a � = � x � → ( � a � → answer ) → answer λ x . � a � : � x � → ( � a � → answer ) → answer Tom Nikken Conversion from callcc to CPS 11 / 16

  30. Still confused? � f a � = λ k . � f � ( λ v f . � a � ( λ v a . v f v a k )) with f : a → b and a : a � f a � : ( � b � → answer ) → answer � f � : ( � a → b � → answer ) → answer � a � : ( � a � → answer ) → answer Tom Nikken Conversion from callcc to CPS 12 / 16

  31. Still confused? � f a � = λ k . � f � ( λ v f . � a � ( λ v a . v f v a k )) with f : a → b and a : a � f a � : ( � b � → answer ) → answer � f � : ( � a → b � → answer ) → answer � a � : ( � a � → answer ) → answer k : � b � → answer Tom Nikken Conversion from callcc to CPS 12 / 16

  32. Still confused? � f a � = λ k . � f � ( λ v f . � a � ( λ v a . v f v a k )) with f : a → b and a : a � f a � : ( � b � → answer ) → answer � f � : ( � a → b � → answer ) → answer � a � : ( � a � → answer ) → answer k : � b � → answer v f : � a → b � = � a � → ( � b � → answer ) → answer Tom Nikken Conversion from callcc to CPS 12 / 16

Recommend


More recommend