class ChurchNumerals {
    static void main(args) {

        def zero = { f -> { a -> a } }
        def succ = { n -> { f -> { a -> f(n(f)(a)) } } }
        def add = { n -> { k -> { n(succ)(k) } } }
        def mult = { f -> { g -> { a -> f(g(a)) } } }
        def pow = { f -> { g -> g(f) } }

        def toChurchNum
        toChurchNum = { n ->
            n == 0 ? zero: succ(toChurchNum(n - 1))
        }

        def toInt = { n ->
            n(x -> x + 1)(0)
        }

        def three = succ(succ(succ(zero)))
        println toInt(three)