plt-redex:免费避免捕获替换?
plt-redex: capture-avoiding substitution for free?
每次在PLT redex中定义一种语言,我都需要手动定义一个(避免捕获)替换函数。例如,此模型未完成,因为 subst
未定义:
#lang racket/base
(require redex/reduction-semantics)
(define-language Λ
[V ::= x (λ x M)]
[M ::= (M M) V]
[C ::= hole (V C) (C M)]
[x ::= variable-not-otherwise-mentioned])
(define -->β
(reduction-relation Λ
[--> (in-hole C ((λ x M) V))
(in-hole C (subst M x V))]))
但是subst
的定义很明显。 PLT redex 可以自动处理替换吗?
是的!只需使用 #:binding-forms
声明描述您的语言的绑定结构。
这是一个类似的模型,通过 substitute
函数避免捕获替换:
#lang racket/base
(require redex/reduction-semantics)
(define-language Λ
[V ::= x (λ x M)]
[M ::= (M M) V]
[C ::= hole (V C) (C M)]
[x ::= variable-not-otherwise-mentioned]
#:binding-forms
(λ x M #:refers-to x)) ;; "term M refers to the variable x"
(define -->β
(reduction-relation Λ
[--> (in-hole C ((λ x M) V))
(in-hole C (substitute M x V))]))
(apply-reduction-relation -->β
(term ((λ x (λ y x)) y)))
;; '((λ y«2» y))
字母等值也是免费的,请参阅 alpha-equivalent?
(谢谢Paul Stansifer!)
每次在PLT redex中定义一种语言,我都需要手动定义一个(避免捕获)替换函数。例如,此模型未完成,因为 subst
未定义:
#lang racket/base
(require redex/reduction-semantics)
(define-language Λ
[V ::= x (λ x M)]
[M ::= (M M) V]
[C ::= hole (V C) (C M)]
[x ::= variable-not-otherwise-mentioned])
(define -->β
(reduction-relation Λ
[--> (in-hole C ((λ x M) V))
(in-hole C (subst M x V))]))
但是subst
的定义很明显。 PLT redex 可以自动处理替换吗?
是的!只需使用 #:binding-forms
声明描述您的语言的绑定结构。
这是一个类似的模型,通过 substitute
函数避免捕获替换:
#lang racket/base
(require redex/reduction-semantics)
(define-language Λ
[V ::= x (λ x M)]
[M ::= (M M) V]
[C ::= hole (V C) (C M)]
[x ::= variable-not-otherwise-mentioned]
#:binding-forms
(λ x M #:refers-to x)) ;; "term M refers to the variable x"
(define -->β
(reduction-relation Λ
[--> (in-hole C ((λ x M) V))
(in-hole C (substitute M x V))]))
(apply-reduction-relation -->β
(term ((λ x (λ y x)) y)))
;; '((λ y«2» y))
字母等值也是免费的,请参阅 alpha-equivalent?
(谢谢Paul Stansifer!)