Stream: beginners

Topic: ✔ Church Booleans


view this post on Zulip nat-418 (Mar 29 2024 at 23:56):

While attempting to implement Church Booleans in Roc, I encountered the following errors:

── CIRCULAR TYPE in lambda.roc ─────────────────────────────────────────────────

I'm inferring a weird self-referential type for and:

8│  and = \p, q -> p q p
    ^^^

Here is my best effort at writing down the type. You will see ∞ for
parts of the type that repeat something already printed out
infinitely.

    (a, ∞ -> b), a -> b


── CIRCULAR TYPE in lambda.roc ─────────────────────────────────────────────────

I'm inferring a weird self-referential type for or:

9│  or = \p, q -> p p q
    ^^

Here is my best effort at writing down the type. You will see ∞ for
parts of the type that repeat something already printed out
infinitely.

    (∞, a -> b), a -> b

────────────────────────────────────────────────────────────────────────────────

2 errors and 1 warning found in 43 ms
.

Perhaps I missed something obvious in the documentation or tutorials, but I don't understand why Roc is objecting to these functions.

view this post on Zulip Richard Feldman (Mar 30 2024 at 00:21):

so the basic problem is that the type system (apparently - TIL!) doesn't support functions which pass themselves to themselves and have only type variables for arguments. I think this is a limitation of Hindley-Milner type systems.

view this post on Zulip Richard Feldman (Mar 30 2024 at 00:21):

(Elm and Haskell give similar errors; this isn't specific to Roc)

view this post on Zulip Richard Feldman (Mar 30 2024 at 00:33):

for what it's worth, chatGPT suggests this implementation of Church And:

and = \p, q -> \x, y -> p (q x y) y

view this post on Zulip nat-418 (Mar 30 2024 at 14:34):

Thanks for the reply, and for the language. For posterity, I got my baby steps running by writing this code to a file and then loading it into the REPL ($ roc repl < bools.roc):

true = \x, _y -> x
false = \_x, y -> y
and = \p, q -> \x, y -> p (q x y) y
or = \p, q -> \x, y -> p x (q x y)

"Test 'true'"
(true true true) "ok" "err!"
(true true false) "ok" "err!"
(true false true) "err!" "ok"
(true false false) "err!" "ok"

"Test 'false'"
(false true true) "ok" "err!"
(false true false) "err!" "ok"
(false false true) "ok" "err!"
(false false false) "err!" "ok"

"Test 'and'"
(and true true) "ok" "err!"
(and true false) "err!" "ok"
(and false true) "err!" "ok"
(and false false) "err!" "ok"

"Test 'or'"
(or true true) "ok" "err!"
(or true false) "ok" "err!"
(or false true) "ok" "err!"
(or false false) "err!" "ok"

view this post on Zulip Notification Bot (Mar 30 2024 at 14:34):

nat-418 has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC