1

Well, suppose that I have a set of functional definitions (with a syntax tree) in church encoding :

true : λx -> λy -> x
false : λx -> λy -> y

Giving the definition λx -> λy -> y, it is very clear how to return the named definition, applying a matching with alpha-equivalence will be enough.

α true λx -> λy -> y = false
α false λx -> λy -> y = true

But consider the example below:

0 : λf λz -> x
succ : λn λf λx -> f (n f x)
3 : succ (succ (succ 0)))

So, when 3 suffers from beta-reduction it will unfold to some definition like :

3_unfolded : (λf -> (λx -> (f (f (f x))))) : (A -> A) -> A -> A

You can see the term can get bigger easily, of course, it is not a good way to represent pure data, because of the size of the term. So, I want to know if there is an algorithm able efficiently to rename again every definition after suffering evaluation. Them 3_unfolded will become (succ (succ (succ 0))) again by giving the set of definitions of natural church encoding (0, and succ only).

I know there are some side effects, like ambiguous representations, but let's ignore that (if you expand the same definition of succ and rename to succ_2, for example).

Tiago Campos
  • 503
  • 3
  • 14
  • 1
    "Giving the definition λx -> λy -> x, it is very clear how to return the named definition" could you please show it (including it in your question) so it is clear what you mean by that. do you mean, to detect that it is actually `true`? if so, the question becomes, by what Haskell term do you represent your "`λx -> λy -> x`" entity? – Will Ness Jul 12 '21 at 16:03
  • I fail to understand the goal. Can't you check if the normal form contains Church numerals and replace them with the wanted equivalent? Is the goal more general, instead? If so, you should start by precisely formalizing it. – chi Jul 12 '21 at 16:12
  • Beta equivalence is undecidable. – Daniel Wagner Jul 12 '21 at 16:23
  • ...also I don't think `3` evaluates to `3_unfolded` under the usual semantics for the lambda calculus. For it to do that you have to do evaluation under binders, and that's... usually not done. Nothing fundamentally bad about it, it just makes it harder for the programmer to write a program that reliably terminates. – Daniel Wagner Jul 12 '21 at 16:25
  • Also also, it's not at all clear to me why you think `succ (succ (succ 0))` is preferable to `3_unfolded`. What is a "leak of named definition"? – Daniel Wagner Jul 12 '21 at 16:41
  • The entity is a representation of lambda as a tree, and reverting the terms to named definition must work for all lambda terms. succ (succ (succ 0)) is preferable of (λf -> (λx -> (f (f (f x)))) just because human readability. – Tiago Campos Jul 12 '21 at 16:49
  • But my terms can suffer beta-reductions, the goal is to use them in the type checker (and friendly printing these terms to the programmer). It is undecidable? – Tiago Campos Jul 12 '21 at 16:52
  • Also, I always have a term and a set of definitions which is the constructors of the term. So, the example of Nat can work for list, bools, every possible church encoding. – Tiago Campos Jul 12 '21 at 16:57
  • 1
    This is like trying to recover a program by its output. Of course you can always shove the output into a "print" statement and declare victory, but of course there's an infinitude of programs that produce the same output. Do you want the shortest one? (Impossible) The most user-friendly one? (How would you define that?) – n. m. could be an AI Jul 12 '21 at 17:21
  • Have you considered simply adding data types to your language...? – Daniel Wagner Jul 12 '21 at 21:39
  • Datatypes are not compatible with the theory I was dealing with, anyway as you said beta equivalence is undecidable, I guess it's my "cruel" answer. Thanks all of you. – Tiago Campos Jul 12 '21 at 23:35

1 Answers1

2

This is essentially the problem of beta-equivalence, and it’s undecidable in general; it also won’t necessarily produce usable output even when it could produce something, e.g. with some restrictions including strong normalisation. Therefore I think your best strategies here will be heuristic, because by default, reductions destroy information. The solutions are to retain information that you care about, or avoid needing information that’s gone. For instance:

  1. Decouple the memory representation of terms from their LC representations, in particular cases where you care about efficiency and usability. For example, you can store and print a Church numeral as a Natural, while still allowing it to be converted to a function as needed. I think this is the most practical technical angle.

  2. Retain information about the provenance of each term, and use that as a hint to reconstruct named terms. For example, if you know that a term arose by a given shape of beta-reduction, you can beta-expand/alpha-match to potentially rediscover an application of a function like succ. This may help in simple cases but I expect it will fall down in nontrivial programs.

  3. Instead of considering this an algorithmic problem, consider it a usability design problem, and focus on methods of identifying useful information and presenting it clearly. For example, search for the largest matching function body that is also the most specific, e.g. a term might match both λx. x (identity) and λf. λx. f x (function application), but the latter is more specific, and even more specifically it can be a numeral (λs. λz. s z = 1); if there are multiple possibilities, present the most likely few.

Whenever you encounter a problem that’s undecidable for arbitrary programs, it’s worth remembering that humans write extremely non-arbitrary programs. So heuristic solutions can work remarkably well in practice.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
  • I think the first approach is the easiest to apply to me. A heuristic approach will be good for most of the cases, however, trusting in a local solution might be a problem for me in the future. Retaining information is also overcomplex in the case of an aggressive normalization, and probably a waste of memory. I will avoid the beta-equivalence problem. Thanks for your answer Jon, was very clear. – Tiago Campos Jul 13 '21 at 04:01