Skip to content

Instantly share code, notes, and snippets.

@EduardoRFS
Last active July 29, 2023 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EduardoRFS/ab2e6fa3e6045d8c436b8b86ad9bd824 to your computer and use it in GitHub Desktop.
Save EduardoRFS/ab2e6fa3e6045d8c436b8b86ad9bd824 to your computer and use it in GitHub Desktop.

CPSify Sigma

CPSifying Sigma seems to suffer from the same issue as lambda encoding of any inductive type, which is self reference.

The lambda encodings of inductive types can be solved by using self types, instead of declaring unit using the traditional church encoding, by using self types we self encode the induction principle, allowing to be known that (u : Unit) -> u == unit.

// traditional church encoding of unit, no induction
Unit : Type;
unit : Unit;

Unit = (A : Type) -> (x : A) -> A;
unit = A => x => x;

// self dependent encoding of unit, inductive
Unit : Type;
unit : Unit;

Unit = %self(u). (P : Unit -> Type) -> P unit -> P u;
unit = P => x => x;

Instead of doing the traditional polymorphic CPS version, we can do a self referential CPS version

// original
x : A;
x = e;

// traditional CPS polymorphic
CPS : (A : Type) -> Type;
CPS = A => (K : Type) -> (k : (x : A) -> Type) -> K;

x : CPS A;
x = K => k => k e;

// self referential version
CPS : (A : Type) -> Type;
CPS = A => %self(x). (K : CPS A -> Type) ->
  (k : (y : T) -> K (K => k => k y)) -> K x;

x : CPS A;
x = K => k => k e;

This allows to transform any external callback.

example : B (fst x)
example = x (a => B (fst a)) (y => snd y);

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment