问题
I'm looking for a BetaEq
type indexed on a : Term
, b : Term
, that can be inhabited iff a
and b
are identical, or if they can be turned into identical terms after a series of beta-reductions. For example, suppose id = (lam (var 0))
, a = (app id (app id id))
and b = (app (app id id) id)
; then we should be able to construct a term of type BetaEq a b
, because both sides can be reduced to (app id id)
. I've attempted this:
data BetaEq : (a : Term) -> (b : Term) -> Set where
refl : (a : Term) -> BetaEq a a
redl : (a : Term) -> (b : Term) -> BetaEq (apply-redex a) b -> BetaEq a b
redr : (a : Term) -> (b : Term) -> BetaEq a (apply-redex b) -> BetaEq a b
Where apply-redex
performs a single reduction. But that looks a little invented, so I'm not sure if that's the right way to do it. Note that both terms could diverge, so we can't simply consider normal forms. What is the standard way of representing beta-equality?
回答1:
Assuming well-scoped untyped lambda terms:
open import Data.Fin
open import Data.Nat
data Tm (n : ℕ) : Set where
var : Fin n → Tm n
app : Tm n → Tm n → Tm n
lam : Tm (suc n) → Tm n
And also a definition of single substitution for the outermost variable (but note that it is always preferable to define single substitution in terms of parallel substitution):
sub : ∀ {n} → Tm n → Tm (suc n) → Tm n
Then beta equality is the congruence closure of beta reduction:
data _~_ {n} : Tm n → Tm n → Set where
β : ∀ {t u} → app (lam t) u ~ sub u t
app : ∀ {t t' u u'} → t ~ t' → u ~ u' → app t u ~ app t' u'
lam : ∀ {t t'} → t ~ t' → lam t ~ lam t'
~refl : ∀ {t} → t ~ t
~sym : ∀ {t t'} → t ~ t' → t' ~ t
~trans : ∀ {t t' t''} → t ~ t' → t' ~ t'' → t ~ t''
By congruence closure, we mean the least relation which is:
- An equivalence relation, i.e. one which is reflexive, symmetric and transitive.
- A congruence with respect to term constructors, i.e. reduction can take place inside any constructor.
- Implied by one-step beta reduction.
Alternatively, you can give a directed notion of reduction, and then define convertibility as reduction to a common term:
open import Data.Product
open import Relation.Binary.Closure.ReflexiveTransitive
-- one-step reduction
data _~>_ {n} : Tm n → Tm n → Set where
β : ∀ {t u} → app (lam t) u ~> sub u t
app₁ : ∀ {t t' u} → t ~> t' → app t u ~> app t' u
app₂ : ∀ {t u u'} → u ~> u' → app t u ~> app t u'
lam : ∀ {t t'} → t ~> t' → lam t ~> lam t'
-- multi-step reduction as reflexive-transitive closure
_~>*_ : ∀ {n} → Tm n → Tm n → Set
_~>*_ = Star _~>_
_~_ : ∀ {n} → Tm n → Tm n → Set
t ~ u = ∃ λ t' → (t ~>* t') × (u ~>* t')
It depends on the situation which version is more convenient. It is the case that the two definitions are equivalent, but AFAIK proving this equivalence is fairly hard, as it requires showing confluence of reduction.
来源:https://stackoverflow.com/questions/54933027/what-is-the-proper-way-to-represent-a-beta-equality-type-for-%ce%bb-terms