Continuation-Passing Style Transforms Type Theory and Coq Vincent Koppen 15-05-2018
Paper Title: Polymorphic Type Assignment and CPS Conversion Authors: Robert Harpery and Mark Lillibridge Year: 1993 Journal: lisp and symbolic computation : An International Journal
Overview 1 Untyped Terms call-by-value call-by-value (modified) call-by-name 2 Simple Type Assignment
Overview 1 Untyped Terms call-by-value call-by-value (modified) call-by-name 2 Simple Type Assignment
Untyped Terms Terms e ::= x | λ x . e | e 1 e 2 | callcc | throw | let x be e 1 in e 2 callcc, throw introduced last week let x be e 1 in e 2 : needed for the polymorphic type assignment Values (call-by-value) v ::= x | λ x . e | callcc | throw
Untyped Terms Terms e ::= x | λ x . e | e 1 e 2 | callcc | throw | let x be e 1 in e 2 callcc, throw introduced last week let x be e 1 in e 2 : needed for the polymorphic type assignment Values (call-by-value) v ::= x | λ x . e | callcc | throw
Untyped Terms Terms e ::= x | λ x . e | e 1 e 2 | callcc | throw | let x be e 1 in e 2 callcc, throw introduced last week let x be e 1 in e 2 : needed for the polymorphic type assignment Values (call-by-value) v ::= x | λ x . e | callcc | throw
Untyped Terms Terms e ::= x | λ x . e | e 1 e 2 | callcc | throw | let x be e 1 in e 2 callcc, throw introduced last week let x be e 1 in e 2 : needed for the polymorphic type assignment Values (call-by-value) v ::= x | λ x . e | callcc | throw
CPS Transform (call-by-value) Definition | v | cbv = λ k . k || v || cbv | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) | let x be e 1 in e 2 | cbv = λ k . | e 1 | cbv ( λ x . | e 2 | cbv k ) || x || cbv = x || λ x . e || cbv = λ x . | e | cbv || callcc || cbv = λ f .λ k . fkk || throw || cbv = λ c .λ k . k ( λ x .λ l . cx ) Note: | | cbv for terms, || || cbv for values. callcc gets a function f which has k both as input and as continuation throw discard continuation l and continue with k
CPS Transform (call-by-value) Definition | v | cbv = λ k . k || v || cbv | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) | let x be e 1 in e 2 | cbv = λ k . | e 1 | cbv ( λ x . | e 2 | cbv k ) || x || cbv = x || λ x . e || cbv = λ x . | e | cbv || callcc || cbv = λ f .λ k . fkk || throw || cbv = λ c .λ k . k ( λ x .λ l . cx ) Note: | | cbv for terms, || || cbv for values. callcc gets a function f which has k both as input and as continuation throw discard continuation l and continue with k
Example Let | e 1 | cbv = λ k ′ . k ′ || v || cbv , | e 2 | cbv = λ k ′′ . k ′′ || w || cbv . We have: | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ k ′′ . k ′′ || w || cbv )( λ x 2 . x 1 x 2 k )) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ x 2 . x 1 x 2 k ) || w || cbv ) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . x 1 || w || cbv k ) → β λ k . ( λ x 1 . x 1 || w || cbv k ) || v || cbv → β λ k . || v || cbv || w || cbv k
Example Let | e 1 | cbv = λ k ′ . k ′ || v || cbv , | e 2 | cbv = λ k ′′ . k ′′ || w || cbv . We have: | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ k ′′ . k ′′ || w || cbv )( λ x 2 . x 1 x 2 k )) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ x 2 . x 1 x 2 k ) || w || cbv ) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . x 1 || w || cbv k ) → β λ k . ( λ x 1 . x 1 || w || cbv k ) || v || cbv → β λ k . || v || cbv || w || cbv k
Example Let | e 1 | cbv = λ k ′ . k ′ || v || cbv , | e 2 | cbv = λ k ′′ . k ′′ || w || cbv . We have: | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ k ′′ . k ′′ || w || cbv )( λ x 2 . x 1 x 2 k )) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ x 2 . x 1 x 2 k ) || w || cbv ) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . x 1 || w || cbv k ) → β λ k . ( λ x 1 . x 1 || w || cbv k ) || v || cbv → β λ k . || v || cbv || w || cbv k
Example Let | e 1 | cbv = λ k ′ . k ′ || v || cbv , | e 2 | cbv = λ k ′′ . k ′′ || w || cbv . We have: | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ k ′′ . k ′′ || w || cbv )( λ x 2 . x 1 x 2 k )) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ x 2 . x 1 x 2 k ) || w || cbv ) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . x 1 || w || cbv k ) → β λ k . ( λ x 1 . x 1 || w || cbv k ) || v || cbv → β λ k . || v || cbv || w || cbv k
Example Let | e 1 | cbv = λ k ′ . k ′ || v || cbv , | e 2 | cbv = λ k ′′ . k ′′ || w || cbv . We have: | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ k ′′ . k ′′ || w || cbv )( λ x 2 . x 1 x 2 k )) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ x 2 . x 1 x 2 k ) || w || cbv ) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . x 1 || w || cbv k ) → β λ k . ( λ x 1 . x 1 || w || cbv k ) || v || cbv → β λ k . || v || cbv || w || cbv k
Example Let | e 1 | cbv = λ k ′ . k ′ || v || cbv , | e 2 | cbv = λ k ′′ . k ′′ || w || cbv . We have: | e 1 e 2 | cbv = λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ k ′′ . k ′′ || w || cbv )( λ x 2 . x 1 x 2 k )) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . ( λ x 2 . x 1 x 2 k ) || w || cbv ) → β λ k . ( λ k ′ . k ′ || v || cbv )( λ x 1 . x 1 || w || cbv k ) → β λ k . ( λ x 1 . x 1 || w || cbv k ) || v || cbv → β λ k . || v || cbv || w || cbv k
CPS commutes with substitution Lemma || [ v / x ] v ′ || cbv = [ || v || cbv / x ] || v ′ || cbv | [ v / x ] e | cbv = [ || v || cbv / x ] | e | cbv Proof: simultaneous induction on the structure of v ′ , e . v ′ = x ′ x ′ = x : || [ v / x ] x || cbv = || v || cbv = [ || v || cbv / x ] || x || cbv x ′ � = x : || [ v / x ] x ′ || cbv = || x ′ || cbv = [ || v || cbv / x ] || x ′ || cbv e = e 1 e 2 | [ v / x ]( e 1 e 2 ) | cbv = | [ v / x ] e 1 [ v / x ] e 2 | cbv = λ k . | [ v / x ] e 1 | cbv ( λ x 1 . | [ v / x ] e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . [ || v || cbv / x ] | e 1 | cbv ( λ x 1 . [ || v || cbv / x ] | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] | e 1 e 2 | cbv
CPS commutes with substitution Lemma || [ v / x ] v ′ || cbv = [ || v || cbv / x ] || v ′ || cbv | [ v / x ] e | cbv = [ || v || cbv / x ] | e | cbv Proof: simultaneous induction on the structure of v ′ , e . v ′ = x ′ x ′ = x : || [ v / x ] x || cbv = || v || cbv = [ || v || cbv / x ] || x || cbv x ′ � = x : || [ v / x ] x ′ || cbv = || x ′ || cbv = [ || v || cbv / x ] || x ′ || cbv e = e 1 e 2 | [ v / x ]( e 1 e 2 ) | cbv = | [ v / x ] e 1 [ v / x ] e 2 | cbv = λ k . | [ v / x ] e 1 | cbv ( λ x 1 . | [ v / x ] e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . [ || v || cbv / x ] | e 1 | cbv ( λ x 1 . [ || v || cbv / x ] | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] | e 1 e 2 | cbv
CPS commutes with substitution Lemma || [ v / x ] v ′ || cbv = [ || v || cbv / x ] || v ′ || cbv | [ v / x ] e | cbv = [ || v || cbv / x ] | e | cbv Proof: simultaneous induction on the structure of v ′ , e . v ′ = x ′ x ′ = x : || [ v / x ] x || cbv = || v || cbv = [ || v || cbv / x ] || x || cbv x ′ � = x : || [ v / x ] x ′ || cbv = || x ′ || cbv = [ || v || cbv / x ] || x ′ || cbv e = e 1 e 2 | [ v / x ]( e 1 e 2 ) | cbv = | [ v / x ] e 1 [ v / x ] e 2 | cbv = λ k . | [ v / x ] e 1 | cbv ( λ x 1 . | [ v / x ] e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . [ || v || cbv / x ] | e 1 | cbv ( λ x 1 . [ || v || cbv / x ] | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] | e 1 e 2 | cbv
CPS commutes with substitution Lemma || [ v / x ] v ′ || cbv = [ || v || cbv / x ] || v ′ || cbv | [ v / x ] e | cbv = [ || v || cbv / x ] | e | cbv Proof: simultaneous induction on the structure of v ′ , e . v ′ = x ′ x ′ = x : || [ v / x ] x || cbv = || v || cbv = [ || v || cbv / x ] || x || cbv x ′ � = x : || [ v / x ] x ′ || cbv = || x ′ || cbv = [ || v || cbv / x ] || x ′ || cbv e = e 1 e 2 | [ v / x ]( e 1 e 2 ) | cbv = | [ v / x ] e 1 [ v / x ] e 2 | cbv = λ k . | [ v / x ] e 1 | cbv ( λ x 1 . | [ v / x ] e 2 | cbv ( λ x 2 . x 1 x 2 k )) = λ k . [ || v || cbv / x ] | e 1 | cbv ( λ x 1 . [ || v || cbv / x ] | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] λ k . | e 1 | cbv ( λ x 1 . | e 2 | cbv ( λ x 2 . x 1 x 2 k )) = [ || v || cbv / x ] | e 1 e 2 | cbv
Recommend
More recommend