7

In Agda, one can conveniently represent λ-terms using PHOAS:

data Term (V : Set) : Set where
  var : V → Term V
  abs : (V → Term V) → Term V
  app : Term V → Term V → Term V

That approach has several benefits over Bruijn indices, as explained in "Parametric Higher-Order Abstract Syntax for Mechanized Semantics". As far as I know, though, there can't be a eval : ∀ {V} -> Term V -> Term V function that, given a λ-term, returns its normal form - after all, Agda is total and the λ-calculus is not. But I wonder if it would be possible to write such eval function for affine λ-terms; i.e., those where bound variables occur at most once. That language is obviously total.

MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
  • 1
    Presumably yes. Translate your argument for why the language is "obviously total" into Agda and you will have your function. – luqui Aug 14 '18 at 16:25
  • @luqui but how? I honestly have no idea. I know how to write `subst` for that type from the paper; [here](https://lpaste.net/3842453789416095744) is an example of applying `id` to a constant. But writing a `eval : ∀ {V} → Term V → Term V` looks impossible. Note `apply : Term V -> V -> Term V`, so, `eval (app f x) = subst (apply f x)` would not be typeable because `x : V` instead of `Term V`. I guess I'd need a whole new way to write `eval`, but I'm not sure what that'd be like. – MaiaVictor Aug 14 '18 at 16:37
  • 1
    @MaiaVictor I would first just consider normalization for the first-order syntax, you can always go to and from PHOAS (although this may require to postulate some parametricity theorems). – András Kovács Aug 14 '18 at 16:40
  • @AndrásKovács normalization for the first-order syntax is itself something I didn't manage to do, but since I'm more interested in PHOAS I decided to keep the question smaller by just asking about it. Is there any obvious way to normalize the first-order syntax? My idea was to write `eval : Nat -> Term -> Term` where the `Nat` is the size of the term; like [this](https://lpaste.net/989790593641086976). But I'm not sure (haven't actually thought yet) how to prove that it actually returns the normal form. That's probably silly anyway so I wonder if you know / could link a better way to do it? – MaiaVictor Aug 14 '18 at 16:42
  • 5
    I'm not sure PHOAS is ideal for this job. I'd want a representation that more explicitly witnesses affinity. Nice problem. – pigworker Aug 14 '18 at 17:17
  • 1
    You might try first converting the lambda expression to a combinator expression with an “abstraction algorithm” using only Bxyz=x(yz), Cxyz=xzy, Kxy=x, Ix=x—the result is guaranteed by construction to be affine, so it might be easier to formulate the proof that evaluation is terminating – Jon Purdy Aug 15 '18 at 10:32
  • 1
    At least two strategies occur to me. One is to exploit the fact that every beta reduction guarantees a net loss of at least one lambda. The other (which I think I prefer) is to show that every affine lambda term has a simple type. – pigworker Aug 15 '18 at 18:46

0 Answers0