Skip to content

Instantly share code, notes, and snippets.

@VictorTaelin
Created February 19, 2025 12:33
Show Gist options
  • Save VictorTaelin/ead226a3cf0ab5d7643a22ed60a18e09 to your computer and use it in GitHub Desktop.
Save VictorTaelin/ead226a3cf0ab5d7643a22ed60a18e09 to your computer and use it in GitHub Desktop.
Another "AGI moment" prompt

Prompt

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

Answer

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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment