How to encode a Deferred type with Church?

48 views Asked by At

With functions we can abstract from any type. Here is the Option type as an example:

const Some = x => y =>
  k => k(x);

const None = y =>
  k => y;

const sqr = n => n * n;

const run = f => t => t(f);

const x = Some(5) (0),
  y = None(0);

run(sqr) (x); // 25
run(sqr) (y); // 0

Now I want to encode a deferred type to obtain a lazy effect in Javascript. But I don't know the right approach and if such an endeavor even makes sense with Church Encoding. Here is my first shot:

const Deferred = thunk =>
  k => k(thunk());

const inc = n => n + 1;

const sqr = n => n * n;

const run = f => t => t(f);

const x = Deferred(() => inc(4));

run(sqr) (x); // 25

I am totally in the dark here. Does this approach lead to anything meaningful?

1

There are 1 answers

0
Bergi On BEST ANSWER

Church encoding (and more precisely, Scott encoding) provide a way to encode branching in a data type with different cases (e.g. instance constructors). Your Deferred type however is a (newtype) wrapper over a function that takes a continuation, there are no multiple cases to be encoded. I don't think you can apply the concept here.