Skip to content

Support for mutual recursion #111

Answered by TomasMikula
sergefdrv asked this question in Q&A
Discussion options

You must be logged in to vote

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)) }

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@TomasMikula
Comment options

@sergefdrv
Comment options

Answer selected by sergefdrv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants