Create a λ-Term fold
that, when applied to a Church nat N, a function F, an initial value X, and a Church N-Tuple, performs a right-fold over the N-tuple. In other words, create a generic fold<N,F,X,t>
function for N-tuples. Example:
- (fold λf.λx.(f x) F X λt(t 1)) == (F 1 X)
- (fold λf.λx.(f (f x)) F X λt(t 1 2)) == (F 1 (F 2 X))
- (fold λf.λx.(f (f (f x))) F X λt(t 1 2 3)) == (F 1 (F 2 (F 3 X)))
Your final answer must be a λ-Term that implements fold
correctly, as follows:
fold = term_here
Below is a possible solution, in HVM3:
@nat(n) = ~ n {
0: λf λx x
n: λf λx !&{f0 f1}=f (@nat(n) f0 (f1 x))
}
@fold = λn λt λF λX
! fS = (λA λt λe0 (A t λB λx (e0 λe1 (B λC (F x (e1 C))))))
! fZ = (λt λe0 (t (e0 λe1 (e1 X))))
(n fS fZ t λx(x λx(x)))
@main = (@fold @nat(3) λt(t 1 2 3))