简体   繁体   中英

What is a mapping between natural numbers and valid simply typed lambda calculus terms?

Is there any efficient algorithm that maps between well-typed, closed terms of the simply typed lambda calculus and natural numbers? For example, using bruijn indexes (and probably on incorrect order):

0 → (λ 0)
1 → (λ (λ (0 1)))
2 → (λ (λ (1 0)))
3 → (λ 0 (λ 0))
4 → (λ (λ 0) 0)
5 → (λ (λ 1) 0)
6 → ... so on

Related questions: is there an algorithm that maps between natural numbers and normalized terms of the simply typed lambda calculus? Also, the same questions applied to the untyped lambda calculus.

The Binary Lambda Calculus defines a binary encoding for any closed term in the untyped lambda calculus, and also suggests a bijection between natural numbers and binary strings, but the former is not surjective. Still, the paper http://arxiv.org/abs/1401.0379 "Counting Terms in the Binary Lambda Calculus" might yield efficient ranking/unranking mappings.

Due to the highly context-sensitive nature of typed lambda calculus I would be surprised if there were an efficient algorithm, or rather a "natural" efficient algorithm.

This paper has nice formulae for counting untyped lambda terms and from that they derive a fairly simple function for enumerating untyped terms. They also provide a counting function for normal forms which should be easy to adapt to a generating function too. Unfortunately, they only make a typed generator function by filtering that which is absurdly expensive (one of the results of the paper is just how absurd it is).

As for a more efficient way to generate typed terms, my advice would be to generate typing derivations instead of terms and then type checking them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM