Support for mutual recursion #111
-
Does Libretto somehow support mutual recursion? Is it possible, e.g., to define a linear function NB. I don't have any concrete example in mind, but this question just occurred to me while I was exploring Libretto. |
Beta Was this translation helpful? Give feedback.
Answered by
TomasMikula
Feb 21, 2025
Replies: 1 comment 2 replies
-
It is possible, although it can get hairy quickly. Here's an example: def isEven_(
isOdd: UInt31 -⚬ Bool,
): UInt31 -⚬ Bool =
λ { n =>
switch(UInt31.decrement(n))
.is { case InR(n0) => isOdd(n0) }
.is { case InL(done) => done |> Bool.constTrue }
.end
}
def isOdd_(
isEven: UInt31 -⚬ Bool,
): UInt31 -⚬ Bool =
λ { n =>
switch(UInt31.decrement(n))
.is { case InR(n0) => isEven(n0) }
.is { case InL(done) => done |> Bool.constFalse }
.end
}
val isEven: UInt31 -⚬ Bool =
rec { self => isEven_(isOdd_(self)) }
val isOdd: UInt31 -⚬ Bool =
rec { self => isOdd_(isEven_(self)) } |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
sergefdrv
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is possible, although it can get hairy quickly.
Here's an example: