Zulip Chat Archive

Stream: new members

Topic: Church-encoded free monad


Adrien Champion (May 20 2024 at 16:39):

I have a question regarding the free monad, or more precisely regarding a "Church-encoded version" of the free monad.

I'm interested in an efficient pause monad, i.e. a monad that can suspend computations. It lead me here

where the accepted answer references three great blog posts, though you need to change the background to be able to read them.

Adrien Champion (May 20 2024 at 16:40):

But first, thanks to

I managed to write a working Pause monad which looks like this.

free version

Adrien Champion (May 20 2024 at 16:40):

I was afraid because Free is u + 1 since it quantifies on β which is a Type u, but it works.

That's great, but if I understand the first link I provided it yields code that's asymptotically slower than necessary.

So I went for the Yoneda/Rec encoding discussed in the second blog post reference (beware the aggressive background), which goes like this unless I messed something up.

Church-encoded

Adrien Champion (May 20 2024 at 16:41):

But now I don't see how I can write the Fre.Pause.step function. My understanding is that I need to get code to produce the result I want, meaning the (hidden) β : Type u in Fre's definition must be σ × Option (Pause σ α) or something similar.

But that can't work since β : Type u and σ × Option (Pause σ α) : Type (u + 1).

Adrien Champion (May 20 2024 at 16:41):

At this point I assume I discovered something well-known about type constructors like Fre in constructive languages and/or languages with non-cumulative type universes. Probably that if allowed this would bring untyped lambda-calculus or something.

Is it the case, or did I mess something up?

If it's the former, is there a way around it? Like using Free but doing something tricky with SeqLeft/SeqRight?

If it's the latter, I would appreciate if anyone had time to elaborate.


Last updated: May 02 2025 at 03:31 UTC